6
\$\begingroup\$

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

\$\endgroup\$
6
  • \$\begingroup\$ Use Box2D and set the body's restitution to 0. \$\endgroup\$ Sep 4, 2012 at 20:19
  • \$\begingroup\$ Using 3D :P but I drew this diagram to make it more easy to understand. Thanks though \$\endgroup\$
    – Nick
    Sep 4, 2012 at 21:02
  • \$\begingroup\$ How about use bulletphysics and set the restitution to 0? PS. I like physics engines. \$\endgroup\$ Sep 4, 2012 at 21:08
  • \$\begingroup\$ @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. \$\endgroup\$
    – msell
    Sep 5, 2012 at 4:31
  • \$\begingroup\$ @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. \$\endgroup\$ Sep 5, 2012 at 7:01

2 Answers 2

5
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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;
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .