I'm currently trying to make a 2D platformer game and I'm using a similar method of detecting collision as the ones discussed in these threads where define a newPosition and check the tile at the newPosition and if the tile is passable I move to the newPosition, and if not, I stay where I am.
However, this is causing problems with jumping involved and also moving left and right because I stick to walls and the floor since a collision is detected and my Position stays the same. I'm trying to separate the detection between horizontal collision and vertical collision but am having some trouble, any help would be appreciated.
I used the platformer starter kit for physics just for testing.
Here is my collision method
However, this is causing problems with jumping involved and also moving left and right because I stick to walls and the floor since a collision is detected and my Position stays the same. I'm trying to separate the detection between horizontal collision and vertical collision but am having some trouble, any help would be appreciated.
I used the platformer starter kit for physics just for testing.
Here is my collision method
private bool HandleCollision(float newPositionX, float newPositionY)
{
Tile tile;
Location tileLocation;
isOnGround = false;
//top left
tileLocation = new Location((int)newPositionX / collision.TileWidth, (int)newPositionY / collision.TileHeight);
tile = collision.Tiles[tileLocation];
if (tile != null && tile.TileIndex == 263)
{
return true;
}
//top right
tileLocation = new Location(((int)newPositionX + collisionBox.Width) / collision.TileWidth, (int)newPositionY / collision.TileHeight);
tile = collision.Tiles[tileLocation];
if (tile != null && tile.TileIndex == 263)
{
return true;
}
//bottom left
tileLocation = new Location((int)newPositionX / collision.TileWidth, ((int)newPositionY + collisionBox.Height) / collision.TileHeight);
tile = collision.Tiles[tileLocation];
if (tile != null && tile.TileIndex == 263)
{
isOnGround = true;
return true;
}
//bottom right
tileLocation = new Location(((int)newPositionX + collisionBox.Width) / collision.TileWidth, ((int)newPositionY + collisionBox.Height) / collision.TileHeight);
tile = collision.Tiles[tileLocation];
if (tile != null && tile.TileIndex == 263)
{
isOnGround = true;
return true;
}
return false;
}
And my Physics methodpublic void ApplyPhysics(GameTime gameTime)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
Vector2 newPosition = Position;
//x velocity
velocity.X += movement * MoveAcceleration * elapsed;
velocity.X = MathHelper.Clamp(velocity.X, -MaxMoveSpeed, MaxMoveSpeed);
//y velocity
velocity.Y = Jump(velocity.Y, gameTime);
velocity.Y = MathHelper.Clamp(velocity.Y + GravityAcceleration * elapsed, -MaxFallSpeed, MaxFallSpeed);
// Apply pseudo-drag horizontally.
if (isOnGround)
velocity.X *= GroundDragFactor;
else
velocity.X *= AirDragFactor;
//Apply velocity
newPosition += velocity * elapsed;
newPosition = new Vector2 ((float)Math.Round(newPosition.X), (float)Math.Round(newPosition.Y));
if (!HandleCollision(newPosition.X, newPosition.Y))
{
Position = newPosition;
}
else
{
newPosition = Position;
velocity = Vector2.Zero;
}


.jpg)




