Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

My character needs to slide and not bounce off a slope.

The solutions I found here use a Reflection vector, but they make my character bouncy when they run downwards a slope.

var reflectVector = normal.clone().multiplyScalar(-this.velocity.clone().dot(normal));
this.velocity.addSelf(reflectVector);

How can I make my character simply stick to the terrain when they walk? How to calculate the desired velocity

share|improve this question
    
Use Box2D and set the body's restitution to 0. –  ClassicThunder Sep 4 '12 at 20:19
    
Using 3D :P but I drew this diagram to make it more easy to understand. Thanks though –  Nick Sep 4 '12 at 21:02
    
How about use bulletphysics and set the restitution to 0? PS. I like physics engines. –  ClassicThunder Sep 4 '12 at 21:08
    
@ClassicThunder Suggesting a physics engine for a simple vector math problem is an overkill. It's good to learn the math first. Of course it's good to learn physics engines as well, but I wouldn't go that far yet. –  msell Sep 5 '12 at 4:31
    
@msell "a simple vector math problem" Its never just 1 simple vector math problem. Anyways it was just a reminder that he is reinventing the wheel. –  ClassicThunder Sep 5 '12 at 7:01
show 1 more comment

2 Answers

up vote 5 down vote accepted

Eliminate the normal component from the old velocity using vector projection to get the desired velocity. Assuming normal is a unit vector, the formula is:

desiredVelocity = oldVelocity - dot(normal, oldVelocity) * normal;

Applying gravity should accelerate the desired velocity, or if you just want constant speed, normalize it and scale as you wish.

You can also think this as a case of reflection. Reflection is oldVelocity - 2 * dot(normal, oldVelocity) * normal;. Here 2 can be replaced with (1 + e), where e is elasticy in the range [0, 1]. With 1 you get the perfect reflection and with 0 you get the formula above with your desired result. With a value between 0 and 1 you get small bounce.

share|improve this answer
add comment

here is pseudo code of one way it can be accomplished.

float speed = oldVelocity.Length();
tempVector = cross(oldVelocity, normal);
desiredVelocity = cross(normal, tempVector);
desiredVelocity.normalize();
desiredVelocity *= speed;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.