Thread: collision not taking affect

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    collision not taking affect

    When the bullet comes in contact with the enemy nothing happens, why isn't it?

    this is my code.

    Code:
    if(collision_x(shoot.coll, enemy.coll) == 2){
                enemy.alive = false;
    }
    the is the collision_x function
    Code:
    int collision_x(SDL_Rect A, SDL_Rect B){
        if(A.x <= B.w){
            return 1;
        }
        if(A.w >= B.x){
            return 2;
        }
        return 0;
    }
    thank you

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You are only checking if A's x position is lesser than the width of B and vice versa. For proper colission checking in one axis you need to check some other parameters. Try it out with a pen and paper first.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Try to test what isn't true as opposed to what is for collision.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Comparing absolute and relative values makes no sense.
    Simply substituting distance with time, those tests are like comparing if 4/06/2010 is less than or equal to 13 days.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Bounding box collision detection
    Code:
    bool CCollisionDetection::DetectBoundingBox(const CSprite &spriteOne, const CSprite &spriteTwo)
    {
        //
        //Grab the bounds of the sprites
        //
        const BoundingBox &boundOne = spriteOne.Bounds();
        const BoundingBox &boundTwo = spriteTwo.Bounds();
    
        //if the left is greater than the right no collison
        if(boundOne.left > boundTwo.right){
            return false;
        }//if
    
        //if the right is less than the left no collision
        if(boundOne.right < boundTwo.left){
            return false;
        }//if
    
        //if the bottom is less than the top no collision
        if(boundOne.bottom < boundTwo.top){
            return false;
        }//if
    
        //if the top is greater than the bottom no collision
        if(boundOne.top > boundTwo.bottom){
            return false;
        }//if
    
        //if all the checks failed that means they are colliding
        return true;
    }
    BoundingBox is just a simple struct defined as:
    Code:
    struct BoundingBox
    {
        int top;
        int bottom;
        int left;
        int right;
    };
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some collision handling fun
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 04-13-2008, 08:45 AM
  2. Collision Detection Problems
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 12-17-2006, 03:25 PM
  3. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 03:21 PM
  4. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM
  5. Replies: 4
    Last Post: 05-03-2002, 09:40 PM