Say I am moving (0.5,0.5) into a wall whose surface normal is (-1,0). I want the end result to be (0,0.5), so I cannot continue to move into the wall but slide along the surface.
How can I achieve this? I have
comp_u = n * (maths::dot(dir,n) / maths::dot(n,n));
which gives me the component of the direction perpendicular to the surface of collision, where ‘n’ is a vector normal to the surface of collision, and I can cancel this out. However, if I try to move away from the wall, it will cancel that component out to, meaning I can never move away from the wall.
You should only do the operation if the direction of motion opposes the direction of the normal… That is,
dot(dir,n)is negative.If the result is positive, you are moving away from the wall and you do not modify
dir.