I've recently implemented slope collision detection for my project. The issue I'm having is that the character occasionally passes through the line.
This usually occurs when the characters MoveAcceleration is increased from 5000 to 8000 via a game mechanic. I was wondering if there is a way to make the line collision more reliable without reducing the MoveAcceleration variable ?
This is how the characters position is calculated
velocity.X += movement * MoveAcceleration * elapsed;
Position += velocity * elapsed;
This is how the slopeCollision is handled
private void HandleSlopeCollisions(GameTime gameTime)
{
isOnSlope = false;
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (velocity.Y >= 0)
{
if (attachedPath != null)
{
isOnSlope = true;
position.Y = attachedPath.InterpolateY(position.X);
velocity.Y = 0;
if (position.X < attachedPath.MinimumX || position.X > attachedPath.MaximumX)
{
attachedPath = null;
}
}
else
{
Vector2 footPosition = position;
Vector2 expectedFootPosition = footPosition + velocity * elapsed;
CollisionPath landablePath = null;
float landablePosition = float.MaxValue;
foreach (CollisionPath path in collisionPaths)
{
if (expectedFootPosition.X >= path.MinimumX && expectedFootPosition.X <= path.MaximumX)
{
float pathOldY = path.InterpolateY(footPosition.X);
float pathNewY = path.InterpolateY(expectedFootPosition.X);
if (footPosition.Y <= pathOldY && expectedFootPosition.Y >= pathNewY && pathNewY < landablePosition)
{
isOnSlope = true;
landablePath = path;
landablePosition = pathNewY;
}
}
}
if (landablePath != null)
{
isOnSlope = true;
velocity.Y = 0;
footPosition.Y = landablePosition;
attachedPath = landablePath;
position.Y = footPosition.Y;
}
}
}
else
{
attachedPath = null;
}
}