Sign in to follow this  
Delfi

How do i rotate a vector around a normal?

Recommended Posts

Hi, i want to make a (3D) character controller where a character can go up hills just like on flat land, so i have a normal of surface below the character and the new force that would be given to the character as if he was on level land - if i use this force when he is on hill he is pushed into the hill which makes it unable to climb on hills properly, my math skills aren't elite, so i am asking, how could i "transform" or the velocity vector of character by the floor normal?

Share this post


Link to post
Share on other sites
You find the axis of rotation by taking the cross-product of the terrain's normal and the object "up" vector -- axis = cross( normal, up ). You find the angle of rotation by computing the arccosine of the dot product of the terrain's normal and the objects "up" vector -- angle = acos( dot( normal, up) ). Apply that to the direction of travel.

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
You find the axis of rotation by taking the cross-product of the terrain's normal and the object "up" vector -- axis = cross( normal, up ). You find the angle of rotation by computing the arccosine of the dot product of the terrain's normal and the objects "up" vector -- angle = acos( dot( normal, up) ). Apply that to the direction of travel.


woah, let's say i understood the first 20 words, but then it lost tracking of things to do, and what do you mean with the direction of travel - is this the velocity vector i was talking about?

Share this post


Link to post
Share on other sites
Couple of questions. Do you want the character to be aligned with the ground normal, or remain upright when going up hills? (I'd think the latter would be more natural as it's what people tend to do to maintain their balance. Also, John's solution applies to both cases, but it'll be easier to explain how to use it if we know what behavior you want.)

Also, do you already have collision detection working between the character and the terrain, or is that also part of the problem to be solved?

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Couple of questions. Do you want the character to be aligned with the ground normal, or remain upright when going up hills? (I'd think the latter would be more natural as it's what people tend to do to maintain their balance. Also, John's solution applies to both cases, but it'll be easier to explain how to use it if we know what behavior you want.)

Also, do you already have collision detection working between the character and the terrain, or is that also part of the problem to be solved?


i'd have the character to remain upright, i use newton physics engine for collision and dynamics, but it is still on me to implement a character controller, it is for this game: http://www.gamedev.net/community/forums/topic.asp?topic_id=355685

if you go with the character uphill you will see you can't do that very easily as you try to push actor into the floor, and similar glitch applies when going downhill - you practically fly because of the lack of contact maintaince with floor.

Share this post


Link to post
Share on other sites
So all you want to do is align the movement direction to the terrain surface? This shouldn't be too hard if you have the normal. Assume you have your current acceleration/force vector V from player input. You'll want to remove the component that's in the direction of the surface normal N, and then rescale the vector so that your speed (or acceleration or whatever) stays constant.

V = V - (V dot N)*N
V = normalize(V) * original_force_magnitude

Share this post


Link to post
Share on other sites
Thanks Fingers, i materialized the pseudocode into this code, please confirm this is correct:

<code>
dp:= DotProduct(V, N);

V.X:= V.X - dp * N.X;
V.Y:= V.Y - dp * N.Y;
V.Z:= V.Z - dp * N.Z;

V:= Scalevector( normalize(V), original_force_magnitude);
</code>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this