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

## 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 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 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 on other sites
Ill be sure and save this posting, those are very simple and useful methods, thanks

##### Share on other sites
it will be alot easier to figure out if you use a piece of paper and a pen / pencil, just draw 2 boxes intersecting and you can visualize how it would work.

1. 1
2. 2
3. 3
Rutin
17
4. 4
5. 5
JoeJ
13

• 9
• 14
• 10
• 25
• 9
• ### Forum Statistics

• Total Topics
632645
• Total Posts
3007629
• ### Who's Online (See full list)

There are no registered users currently online

×