## Doing the wall slide thing

Member
Posts: 353
Joined: 2002.04
Post: #1
I'm trying to set-up the movement code in my game so that the player slides along an impassable wall as happens in most 3D games. If I have the code working to test the player's movement vector's collision with a triangle (which is the impassable wall), how do I then determine what would be the right vector to have the player slide along the wall?
Zoldar256
Unregistered

Post: #2
Let the current direction of movement be V.
First get the normal vector for your triangle. Call this N
Then get the projection of V onto N. Assuming both vectors are normalized this is V (dot) N times N. Call this projection.. I dunno.. P
The perpindicular movement vector is then V - P.
Jesse
Unregistered

Post: #3
Here's the way it's usually done. Let's say you've moved and your new position is 'inside' a wall. What you do is push yourself (the player, the camera, or whatever) along the normal of the polygon you've intersected by the distance that you are behind it (assuming you're a point - if you're a sphere, it would be the distance plus your radius). As an example, let's say you find yourself behind a wall by a distance of 20 units. Your new position is:

myNewPos = myCurrentPos - (wall.normal * wall.DistanceTo(myCurrentPos));

Or something like that (assuming being behind the wall gives you a negative distance).

This essentially negates the portion of your velocity (i.e. speed plus direction) that 'collides' with the wall, and leaves you with whatever is left. If your velocity is primarily 'into' the wall, there will only be a little left after you negate it, and you'll slide slowly along the wall. If your velocity is primarily along the wall, only a small portion will be negated and you will slide quickly. This is what you intuitively expect, and what happens in Quake, etc.

'Hope that was clear enough. If not, ask and I'm sure I or someone else will be happy to clarify.
Member
Posts: 304
Joined: 2002.04
Post: #4
Quote:Originally posted by Zoldar256
Assuming both vectors are normalized this is V (dot) N times N.
only N should be normalized - not V. But otherwise correct. Incidentally V - 2.0*P will give you a 100% bounce velocity. So you can mix if you want, i.e., V - 1.2*P will mostly slide - but still bounce you off the wall a bit.

hth
Zoldar256
Unregistered

Post: #5
Whoops, yea, was just pulling those outa my head at the time.
Jesse: Our methods are essentially the same. Minus the fix for my poor math ;-)
Jesse
Unregistered

Post: #6
Quote:Jesse: Our methods are essentially the same.

Yup...I think we posted our replies at the same time :-)
Member
Posts: 353
Joined: 2002.04
Post: #7
Haven't had the chance to get any work done on it yet, but thanks for all your help guys, will let you know how it goes.
Member
Posts: 304
Joined: 2002.04
Post: #8
Of course nothing is ever as easy as it seems at first.

After the collision and the slide - you have to test for collisions *again* b/c during the 'slide' you might hit another wall. You also have to test if you hit an edge or a corner - you have to test if you are touching 2 (or more) walls at once. IMHO its really hard to get right - Good Luck!!
Member
Posts: 353
Joined: 2002.04
Post: #9
I found a good article on this sort of thing here:

http://www.fluidstudios.com/publications.html

I'm going to try to implement something a bit simpler than what they have, but if it doesn't work out I will have a closer look.
Member
Posts: 353
Joined: 2002.04
Post: #10
Got the initial wall slide working well, thanks for all the help. Now I've got to move onto all those other tests codemattic mentioned.