Jump to content
  • Advertisement
Sign in to follow this  
zChrisse

2D collision

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

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


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!