Sign in to follow this  
SelethD

Collision Detection (Again :)

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this