Jump to content
  • Advertisement
Sign in to follow this  
SelethD

Collision Detection (Again :)

This topic is 5135 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

I know this has been posted probably a thousand times, and thats the problem, there are too many examples and references and theories for collision detection. So what I am asking is what is the ... 1. Simplest 2. Less costly of processing 3. most commonly used for collision detection between A. two Rectangles B. a ray (2d vector) intersecting another ray, or line C. two elipses, or circles whichever one is easier It would be helpful is people would reply with either... 1. Pseudo code, or actual code, I work in c++ but can probably convert from somthing else. or 2. Links to any tutorial or document regarding the subject. Thanks in advance :)

Share this post


Link to post
Share on other sites
Advertisement
for two hitboxes:

if (
A.x1 > B.x2 ||
A.x2 < B.x1 ||
A.y1 > B.y2 ||
A.y2 < B.y1
) { NO COLLISION }


For two circles (circles are easier than elipses):

if ( distance_between_centerpoints <= A.radius + B.radius ) { 
COLLISION HAPPENS
}


Rays and lines a slightly more complicated but is a very common problem with common solutions. Please seach the forum or google for "line intersection". There is a lot out there, even on this very forum.

Share this post


Link to post
Share on other sites
for bounding box, its pretty easy. collision detection is done by checking is the 2 boxes overlap. leavoia's code showed how to do this.

collision reaction is a little bit trickier, but still pretty easy. the way i do it is this:


if(object1 (o1) AND object2 (o2) collide)
{
find which one is above / below the other, and which one is to the left/ right of each other. call them left,right,top,bottom.

find if they intersect more on the X/Y axis

if(they intersect more on the Y axis)
{
if(the top guy is moving)
move top guy to edge of bottom guy
else if(the bottom guy is moving)
move bottom guy to edge of top guy
else if (both are moving)
{
calculate the middle, and move them both to this pooint
}
}
else if(they intersect more on the X axis)
{
if(the left guy is moving)
move left guy to edge of right guy
else if(the right guy is moving)
move right guy to edge of left guy
else if(both are moving)
{
find middle point and move them there
}
}
}


and thats pretty much the gist of it. it works pretty good for me, too.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!