• FEATURED

View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Quick 2D Collision Detection Problem

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

No replies to this topic

#1RAMailley  Members

Posted 18 March 2012 - 06:22 PM

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?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.