Jump to content
  • Advertisement
Sign in to follow this  

collision detection example

This topic is 4953 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

well ive been puting this section of my project off but now i have to bite the bullet. COLLISION DETECTION ive tried before but was only secsesful with getting a sigle point to do collision detection. Could any of you point me toward a very simple bound box or maybe image based collision detection C++ Dev-Cpp API:SDL

Share this post

Link to post
Share on other sites
Have a Look at this

// Object-to-object reduced bounding-box collision detector:
// http://www.gamedev.net/reference/articles/article735.asp
// We check whether 2 sprite objects overlap each other,
// using the so-called reduced rectangles that are 'offset'(scale of 0-1) of
// the original ones.
int Sprite_Collide(CSprite &object1, CSprite &object2, float offset)
// We store the coordinates of our reduced rectangles here
double left1, left2;
double right1, right2;
double top1, top2;
double bottom1, bottom2;

// (this is our sprite)
// |---sprite width---|
// A******************* <-|-height*0.1
// **B...............** <-\
// **................** <-\
// **................** <-\
// **................** <-\
// **|---width*0.8--|** <-\
// **................** <-\
// **................** <-\
// **...............C** <---height*0.9
// *******************D
// ^^ width*0.1
// ^^^^^^^^^^^^^^^^^^ width*0.9

// What we do now is store the coordinates of our
// shrunken rectangle inside some variables.
// We make 'left' and 'top' equal the position B on our sprite
// (look at the "drawing") and 'right' and 'bottom' equal the
// position C. (Remember, left1, top1, etc contain the coordinates
// of the first sprite's reduced rectangle and left2, top2 contain
// the coordinated of the second sprite's reduced rectangle)
left1 = object1.getx()+object1.getw()*offset;
left2 = object2.getx()+object2.getw()*offset;
top1 = object1.gety()+object1.geth()*offset;
top2 = object2.gety()+object2.geth()*offset;

right1 = object1.getx()+object1.getw()*(1.0-offset);
right2 = object2.getx()+object2.getw()*(1.0-offset);
bottom1 = object1.gety()+object1.geth()*(1.0-offset);
bottom2 = object2.gety()+object2.geth()*(1.0-offset);

// We now do some little comparing and return 0 if the rectangles
// aren't colliding and 1 if they are.
if (bottom1 < top2) return 0;
if (top1 > bottom2) return 0;
if (right1 < left2) return 0;
if (left1 > right2) return 0;

return 1;
}//end Sprite Collide


Share this post

Link to post
Share on other sites
ooooooo [looksaround] ive tryed that example before but i inverted the results (return true if they dont collide) and thought it didnt work lol thanks alot it works now i am now VERY near to completing the game all thats left is to make some cool looking enemys (not the blobs ive been working with.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!