# 3d Wall sliding collision

I'm working on collision for walls in my game and the way I currently have it I get stuck walking into a wall. I'm trying to make my character slide on the walls but still collide.

My character moves off of a vector I create using the angle he is facing.

this is my collision function:

    private static bool CheckForCollisions(ref Crate c1, ref Player c2,bool direction)
{
for (int i = 0; i < c1.model.Meshes.Count; i++)
{
// Check whether the bounding boxes of the two cubes intersect.
BoundingSphere c1BoundingSphere = c1.model.Meshes[i].BoundingSphere;
c1BoundingSphere.Center += c1.position + new Vector3(2, 0, 2);

for (int j = 0; j < c2.model.Meshes.Count; j++)
{
BoundingSphere c2BoundingSphere = c2.model.Meshes[j].BoundingSphere;
if (direction)
c2BoundingSphere.Center += c2.position + new Vector3(c2.getPlannedDirection().X, 0, 0);
else if (!direction)
c2BoundingSphere.Center += c2.position + new Vector3(0, 0, c2.getPlannedDirection().Y);
//c2BoundingSphere.Center += c2.position;

if (c1BoundingSphere.Intersects(c2BoundingSphere))
{
return true;
}
}
}
return false;
}


This is my update:

for (int x = 0; x <= 29; x++)
{
for (int y = 0; y <= 29; y++)
{
if (crate[x, y].getType() == 11 && collisionEnabled)
{
if (CheckForCollisions(ref crate[x, y], ref player,true))
{
player.clearPlannedDirectionX();
//Console.Write(player.getPosition().X + "," + player.getPosition().Y + "," + player.getPosition().Z);
//movePlayer = false;
}
if (CheckForCollisions(ref crate[x, y], ref player,false))
{
player.clearPlannedDirectionZ();
//Console.Write(player.getPosition().X + "," + player.getPosition().Y + "," + player.getPosition().Z);
//movePlayer = false;
}
}
}
}

• why are you using bounding sphere for walls instead of a bounding box?? with bounding spheres, collision can happen much before the player reaches wall in case ur radius is large
– Ani
Aug 14 '13 at 8:52

This can happen because the position increment may take the character inside the bounding sphere where you have cleared the planned direction. So unless your player has a velocity component in y direction, it might not come out.

BTW, shouldn't this

else if (!direction)
c2BoundingSphere.Center += c2.position + new Vector3(0, 0, c2.getPlannedDirection().Y);


be (last Y --> Z)

else if (!direction)
c2BoundingSphere.Center += c2.position + new Vector3(0, 0, c2.getPlannedDirection().Z);