Doing the wall slide thing
I'm trying to setup 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?
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.
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.
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.
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.
Quote:Originally posted by Zoldar256only 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.
Assuming both vectors are normalized this is V (dot) N times N.
hth
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: Our methods are essentially the same. Minus the fix for my poor math ;)
Quote:Jesse: Our methods are essentially the same.
Yup...I think we posted our replies at the same time :)
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.
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!!
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!!
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.
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.
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.
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Weird OpenGL thing...  jfFlash  9  6,378 
Sep 13, 2006 08:11 PM Last Post: akb825 

lighting a big wall  Najdorf  4  3,648 
Feb 9, 2006 11:59 AM Last Post: Leisure Suit Lurie 

This thing = way too slow  LongJumper  9  7,486 
Dec 20, 2004 05:14 AM Last Post: TomorrowPlusX 

Projection matrix : Strange thing ?  Ole  4  4,217 
Sep 13, 2003 02:38 AM Last Post: Ole 

weird thing  fatt  5  4,554 
Jul 7, 2003 04:59 PM Last Post: fatt 