I'm creating a 2D platformer in XNA and I'm having some problems with gravity. My gravity causes the player to sink into a tile, rather than land on top. Here is my gravity implementation:
if (willPlayerCollide(State.Decending) == false)
{
changeState(State.Decending);
onGround = false;
velocity.Y += 0.1f;
position.Y += velocity.Y;
positionRect = new Rectangle((int)position.X, (int)position.Y, spriteWidth, spriteHeight);
futurePositionRec = new Rectangle((int)position.X, (int)position.Y + 1, spriteWidth, spriteHeight);
debug = velocity.Y.ToString();
}
else
{
changeState(State.Idle);
onGround = true;
velocity.Y = 0.0f;
}
The relevant portion of the collision detection code for willPlayerCollide is:
else if (potentialState == State.Decending)
{
futurePositionRec = new Rectangle((int)position.X, (int)position.Y + 1, spriteWidth, spriteHeight);
for (int i = 0; i < Level.impassableTileRecs.Count(); i++)
{
if (futurePositionRec.Intersects(Level.impassableTileRecs[i]))
{
collided = true;
tileCollRect = Level.impassableTileRecs[i];
}
}
}
With this code, as the player is falling, it will sink partially into the tile. Depending on how large the number that velocity.Y is incremented by is how much the player will sink into the ground. A large acceleration will cause the player to sink further into the tile. Any help is appreciated.