Public Group

# 2D collision

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

## Recommended Posts

Im a beginner in 2d game making so bare with me.

My idea for detecting which way a object collide with another object is to save to previous coordinates of the colliding objject and then subtract the old ones from the new ones. If x is below zero and y is zero the object came from right.

What do you thing of this solution? Is it viable. Am i on the right track?

And also my math skilla are very weak

Using xna 4

##### Share on other sites
While your method may work for basic collisions, a more sophisticated (but still easily implemented with AABB's (Axis Aligned Bounding Boxes) exists.
This problem is usually approached in this way: When two objects are overlapping each other, the penetration vector is taken (the distance that one object is inside the other), and the two objects are separated by this amount. Then one can tell which side of the objects are colliding.

This is one of the best tutorials on the web for this:
http://www.metanetsoftware.com/technique/tutorialA.html

And if you haven't already, do some research on bounding volumes to reduce collision complexity:
http://en.wikipedia.org/wiki/Bounding_volume

##### Share on other sites
I also very much like this primer to physics engines from Wildbunny. While the math is more complex it helps you transition to using it, and gets you to understand how you would build a 2D collision system. It gets you from coding collision checks in a naive manner to producing code that is more versatile and flexible.

##### Share on other sites
If you want, take a look at my blog (in my sig). I built a game that used a 2d physics engine (chipmunk-physics). Go through the posts from the start, and you can see how I integrate it into the game.

Let me just say, using a physics engine makes writing 2d games a snap.

##### Share on other sites
Collision detection works in two phase - Broad phase and Narrow phase.

Since you are working on a 2D game and i assume a platformer , you should use QuadTree for BroadPhase or just store your tiles in a 2D array grid and create a mapping function that would take a coordinate as input and generate the row and column index (i and j) that contains the input coordinates. If the grid[i,j] contains a tile , you can directly check collision with that tile.

For Narrow phase , depending on the accuracy you need for your game , you can just create a rectangle(Bounding Box) surrounding your character and tiles and gems etc and check collision between Bounding box of two objects . For example - check if the rectangle enclosing your character intersects with the rectangle enclosing your gem. If they intersect , there is a collision .

If you need more accuracy you will need to create a convex hull for the character (Quick hull algorithm can be used to create Hull) and check collision between the hulls instead.

XNA also supports pixel level collision detection for Sprites . There is a sample on App hub on this . You shall consider looking at it.

However , Rectangle bounding box shall do just fine for a 2D side scroller game.

The Rectangle Structure in Microsoft.XNA.Framework contains two methods Rectangle.Contains(Rectangle) and Rectangle.Intersects(Rectangle) that can help you with checking collision between them. There are overloads for vectors,etc as well to Contains() and Intersects() methods .

This is a platformer game i had programmed : Platformer game . I had used 2D array grid to store map and Rectangle as bounding box for all entities in game . I used an offset on Rectangles while checking collision to get more accurate results .

##### Share on other sites

The Rectangle Structure in Microsoft.XNA.Framework contains two methods Rectangle.Contains(Rectangle) and Rectangle.Intersects(Rectangle) that can help you with checking collision between them. There are overloads for vectors,etc as well to Contains() and Intersects() methods .

Just use rectangle intersections to start. Don't worry about phases and trees and all of that stuff. It's just going to complicate things.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 30
• 9
• 16
• 12