Jump to content
  • Advertisement
Sign in to follow this  
racumin

2d rectangle collision detection

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

Hi, I'm new to game development and I need to know helpful tips for collision detection. I am trying to create a Super Mario game. My objects are just plain rectangles and I already know when my objects will collide. I did this by checking every time if a given rectangle intersects any of the rectangles in the list of objects in the "world". Now, my problem is, I do not know where did it collide. I can detect some of them in some scenarios but not all scenarios. This is my algorithm. 1. if r1 and r2 intersects 2. check the location of the center of r2 with respect to r1 3. the only possible answers in 2 are TOP, BOTTOM, LEFT, RIGHT, TOP-LEFT, TOP-RIGHT, BOTTOM-LEFT and BOTTOM-RIGHT. In my program, if the result of the checking is TOP, BOTTOM, LEFT or RIGHT, I have no problem. But if it is TOP-LEFT, TOP-RIGHT, BOTTOM-LEFT or BOTTOM-RIGHT, I am having problems determining where did they really collide. Is it on th TOP, BOTTOM, LEFT or RIGHT? Please give me easy-to-implement solutions and links to study. Also, if you have better solution in determining where the objects collide, please tell me. Thank you very much!

Share this post


Link to post
Share on other sites
Advertisement
How do you check whether two rectangles intersect? Probably you can get some more information directly from your collision detection method.

Do your rectangles rotate? If not then two corners must have collided in events of TOP-LEFT, TOP-RIGHT, ...
Maybe that is the cause of your problem and you assume, that the collision point is on one of the rectangles edges.

For what do you need the information where it happened? Maybe it is sufficient to know that it happened, update the states of the two collided entities and go on.

Cheers,
fng

P.S.: A jump and run game is a very good idea to start off!

Share this post


Link to post
Share on other sites
image placing your first rectangle into the centre of a 3x3 grid.


-------------
| | | |
| | | |
| | | |
-------------
| | | |
| |r1 | |
| | | |
-------------
| | | |
| | | |
| | | |
-------------



Now place the second rectangle anywhere on the same grid, doesn't have to line up with the grid lines though.

You can figure out where the rectangles are wrt each other by looking at their the centre location wrt the bounding edges of rectangle 1. For example, if r2 is in the top left corner, the centre will be above the top boundry of r1 and to the left of the left edge of r1. You have a collision if r2's bottom right vertex is inside r1.


Share this post


Link to post
Share on other sites
Thanks for the replies. I check collision by checking if the distance from the center of r1 and r2 is less than the sum of their lengths divided by two. I do this for both x and y coordinates.

My rectangles do not rotate.

I need to know where the rectangles collide so that I can put logic that depends where they collide.

Thanks!

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!