collision not taking affect

This is a discussion on collision not taking affect within the C++ Programming forums, part of the General Programming Boards category; 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, ...

  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,209
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    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,308
    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,812
    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, 09: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, 04:21 PM
  4. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-07-2002, 12:28 AM
  5. Replies: 4
    Last Post: 05-03-2002, 10:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21