# What do I need to do to my slope collision detection to prevent the player from passing through my collision?

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;
}
}
``````
-
I haven't done it myself but it sounds like you need to use Ray-Intersection testing ghoshehsoft.wordpress.com/2010/11/25/… It's how bullets are calculated since they move too quickly to actually "collide" with the target. – Mike C Nov 7 '11 at 15:29
You should essentially never do your physics using lines. It's too easy to end up crossing the line due to floating-point precision issues. Always use full 2D shapes. – Andrew Russell Nov 8 '11 at 2:41
I was initially going to use a triangle box collision, but I couldn't find much material on the subject – dbomb101 Nov 8 '11 at 12:08

## 1 Answer

I see three things that bother me here:

First, the first logic path seems to rely on a variable that doesn't seem to be set up. I'm confused. Perhaps there's a bigger picture not shown here.

Second, I think the second logic path is vulnerable to floating point issues.

Note that you do NOT check if the second point is in an unacceptable location, but rather if the movement vector crosses the path line. If this were done in integer precision I would consider it solid. The problem is the case of when you are exactly on the line, though--you are relying on a floating point equality test to keep the next move from taking you through the line. That's scary.

Third, it seems to me that you can freely pass though anything if your y velocity is negative. I assume those are screen coordinates instead of world coordinates so -y means you're going up. The ground is sloping up more steeply than your path is, you go right through it.

-