need help with objects

Discussion in 'C' started by dhulli, Aug 26, 2007.

  1. dhulli

    dhulli New Member

    Joined:
    Aug 26, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    ok I am using a basic 2d game programming lib with C++, namely hge but the question is not specific to that.

    The problem I am having is using collision detection. hge uses a function for an object to check for a collision to another object (both of the same class) and the other object is given as an argument to the function.

    I have a character object and different wall objects. I want to make it so in the one collision function, i could pass some argument so it applies to all the wall objects (of the same class).Also note that the different wall objects have different data members like coordinates. Is it even possible? Because otherwise that'd make it very long to have a collision function for every wall object although they'd do the same thing.

    The nearest to that is using a function template for the collision but that'd still make it long considering the amount of wall objects i have. :eek:

    Also is there any difference between objects and instances?
    Thanks in advance!
    ~dhulli
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    An object is a thing. Like a carburetor. Or a wall. An object definition is not a thing (well, the fact of the definition is a thing, but it doesn't make one of the thing described). An instance IS the thing, realized by carrying out an act of production as described in the definition.

    If an object has a non-static definition of a collision function, that function will not be produced for each instance. It is an action whose process applies to every instance of the kind of object for which it designed. When it is invoked, it will be applied to a specific instance. That's what the this pointer is for.
     
  3. dhulli

    dhulli New Member

    Joined:
    Aug 26, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Okay, I understood the difference between object and instances.
    Actually the book i learn C++ from doesn't cover the this pointer so i looked it up from another and this is what i get:

    The way the collision is checked is similar to this
    Code:
    if (pChar->Collision_Check(pWall)) { }
    
    and the only way i can think of using the this pointer here is by using a function instead of pWall which would return "this" pointer.
    Now I suppose that there is a derived class for the wall and all the wall objects are made from that. There is a static function that returns the this pointer for use by pWall. So i call the static function by using the derived class followed by the scope resolution operator?

    Hopefully it's correct, actually I'll post when i get to my other pc and check it. Thanks a lot for help though it took some time to figure it out.
    :rolleyes:
    ~dhulli
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Your posts are too unclear to make definitive statements. Suppose however, that you have to "ball" instances, either of which may collide with a wall. The ball objects would have a collision detection method that pertained to other objects (walls, say). To see if ballOne collided with wallOne, one would invoke ballOne.collide (wallOne). One might also invoke ballTwo.collide (WallOne).

    The this pointer is implicitly passed as an argument. The collide method would then check wallOne coordinates against this->ballCoordinate, where this either pointed to ballOne's coordinates or ballTwo's coordinates.
     
  5. dhulli

    dhulli New Member

    Joined:
    Aug 26, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Ok what i tried didn't work, let me explain the situation clearly:

    The game library has a helper class called hgeRect for bounding boxes. This class has got data members for coordinates and member functions like Intersect() to check for intersection with another object of the same hgeRect class.
    Now simplifying, these are pointers to objects i have got for this class
    pBall, pWall1, pWall2, pWall3, pWall4, pWall5

    Like i said before, the intersection function is similar to this:
    if (pBall->Intersect(pWall1 || pWall2 || pWall3 || pWall4 || pWall5)) { ... }

    In order not to repeat the function or use too many or's since the number of wall objects could go on to become very large, i want it to generalize to one thing so as to cover all wall objects.

    Now here is where I get confused. From your earlier reply i made a derived class from hgeRect and made a static function in that. So I could use the static function to return the argument to pBall->Intersect() but apparently I can't use the this pointer in a static function :(
     
  6. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Why would you want a static function? Besides, there's about a 99.99 percent probability that you can't OR walls like that.

    If you need to check for intersection with five objects, you need to check for intersection with five objects. Wishing you could check for an intersection once and cover five things just isn't how it works.
     
  7. dhulli

    dhulli New Member

    Joined:
    Aug 26, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    oops sorry, i meant it like
    if (pChar->Intersect(pWall1) || pChar->Intersect(pWall2) ||....)

    I wanted to use a static function so it would be same for all of Wall objects. But oh well, i guess C++ is just the long way around, quite literally naming every wall object that i'll have an intersection with.
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    It isn't C++. It's programming. It's reality. If you want to test 12 independent conditions, you have to test 12 conditions. You can jink around and test one ball against 12 walls or you can test 12 walls against one ball. It's going to require actions. You would have to do it if you were using your own brain and pencil and paper. There is no magic.

    When you define a non-static method for an object, that method IS the same for all objects. The target of the action is specific to the object referenced. If you make the method static, then you'll have to find another way to couple the action to a specific instance.

    Excuse me for saying so, but you seem to be undertaking a task that is more complex then your current level of understanding is capable of.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice