Sign in to follow this  
programmermattc

XNA 2D Collision

Recommended Posts

I've been struggling with collision in my engine for some time now and will post this in the hopes someone could help me. I've got a simple 2D scene with a player character (sidescroller, platformer, Mario-style) and a bunch of blocks (the ground). The character hovers currently because if he fell he would go through the blocks (no collision). There are several blocks in front of him I'd like him to collide with when I move him forward. I've tried a few methods so far. When the player and blocks are generated they are given a bounding box and each time the player is moved I check to see if it collides with all blocks in the level with Rectangle.Intersects() (incredibly inefficient but I'm tying to get the basic functionality down first). If he collides with one, he's supposed to stop. The problem is he seems to stop way too early and is apparently colliding with nothing. Maybe my bounding boxes are wrong but I can't say that's definitely whats wrong. Does anyone have any better ideas on collision? (I'm at work so I can't post code until later tonight)

Share this post


Link to post
Share on other sites
It sounds as though you move your character for example 10 pixels each frame, and if it collides after those 10 pixels you simply stop at the previous position, 10 pixels earlier, while the real stop is somewhere in between. You can correct this either by moving 1 pixel at a time in a loop, until either a collision occurs or the correct number of pixels has been moved, or by calculating the actual collision point and move the character to that position.
You could first check collisions against the target position, and then do the loop moving a little bit every iteration and check collisions only against those blocks that you know will intersect at the end of the movement. This would avoid a lot unnecessary checks, if it turns out to be slow.
If you don't use actual pixel positions, but floating point values, then you should probably calculate the exact point of collision and move the character there. Try searching for swept box or rectangle test. I found this for example with google: http://supertux.lethargik.org/wiki/Sweep_collision_algorithms

Share this post


Link to post
Share on other sites
I am currently storing the movement as a float.

This made me think of another problem with this I had. If I collide I don't want to move back the full movement amount, I only want to move to the edge of the box (Player.X -= PlayerRightEdge.X - BoxLeftEdge.X). How do I know that I'm colliding on the left, right, top, or bottom without doing specific side checks for each object?

I'll check out that link when I get home.

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