# Quick 2D Collision Detection Problem

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

## Recommended Posts

Hi, guys. I have a fair bit of programming experience, but I'm new to both C# and XNA. I'm trying to make a 2D platform game, but I'm having trouble getting my collision detection to work properly. I'll show you the code then explain the issue:

Player's Movement Code
 public void UpdateMovement(KeyboardState k, List<Sprite> sList) { // gravity if (state == State.Falling) { velocity.Y += gravity; if (velocity.Y > yVelocityMax) velocity.Y = yVelocityMax; } // movement if (k.IsKeyDown(Keys.Space) && !kprev.IsKeyDown(Keys.Space) && !(state == State.Falling)) { velocity.Y += -jumpStrength; } if (k.IsKeyDown(Keys.D)) { velocity.X += speed; } if (k.IsKeyDown(Keys.A)) { velocity.X -= speed; } position += velocity; // collision detection bool falling = true; foreach (Sprite s in sList) { if (s != this && s.Collides(get_boundingBox())) { if (position.Y + box.Height >= s.get_boundingBox().Top && position.Y <= s.get_boundingBox().Bottom) { velocity.Y = 0; position.Y = s.get_boundingBox().Top - box.Height; state = State.Standing; falling = false; } } } if (falling) { state = State.Falling; } velocity.X = 0; // prevent sliding kprev = k; } 

Collides Function
 public bool Collides(Rectangle r) { return r.Intersects(get_boundingBox()); } 

Bounding Box Code
 public Rectangle get_boundingBox() { return new Rectangle((int)position.X, (int)position.Y, box.Width, box.Height); } 

So, the way the collision detection works is that it updates the player's position with all the effects of gravity, keyboard movement, and such, and then runs through a list of all the other Sprites looking for collisions. If it finds a collision, it readjusts the player's position so that the player, and tells the player it isn't falling anymore.

The problem is that when the player lands, the collision detection seems to make the player switch rapidly between falling and standing, changing it's Y position by + 5 and then back. This makes the player unable to jump consistently, because jumping is disabled when falling. Now, I believe the issue is with the Collides function. When the position it readjusted, it no longer thinks it is colliding with the platform and therefore changes the player to falling and applys gravity... until the player collides again. I am just not sure how to fix this without making a mess or patchwork code. When the player collides with the platform, it needs to be placed back to a position which is as far away from the platform as possible while still touching the platform. Or I need a way to handle situations where the player is neither colliding nor falling. Any ideas?

• ### 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!

• 11
• 23
• 36
• 16
• 75