Sign in to follow this  
Nameless Stranger

Car on terrain - Interpolation

Recommended Posts

Hi, Following google and gamedev forums, I can successfuly move the car on the terrain, and it adjusts its orientation according to the triangle under it (Right now I'm using only the CenterPoint of the body as contact point). My problem now is how do I linearly interpolate between the current orientation and the destination orientation. Exactly, I'm having trouble finding the T of the equation (some people call it S). It's the value in the range [0..1] that's giving me trouble. Notice that I have vectors(X, Y, Z) that describe the Up, Direction and Position of the car. Currently, I'm doing this: [Inside game loop] LastUp = Car->Up; // Last Up vector the car had NewUp = FindNormalOfTriangleUnderCar(); // This is the destination up vector T = AngleBetweenVectors(NewUp, LastUp); // Returns [0..1] value. 0 = LastUp. 1 = NewUp. 0.5 = Half NewUp, halp LastUp Car->Up = LerpVectors(NewUp, LastUp, T); // Does LERP on the two vectors based on T. My problem is: While this works as expected, as the LastUp reaches closer to NewUp the interpolation takes longer to execute. For example: As soon as the car finds a new Up vector, T starts increasing faster (Because the difference of the angles is bigger). And when the angle between the vectors decrease, T is increased by smaller fractions, making the interpolation take longer to finish. In short: The smaller the angle between the vectors, the longer the interpolation takes to finish. I just want a fixed timestep of interpolation. How could I solve this problem? Thank you.

Share this post

Link to post
Share on other sites
What you want is spherical linear interpolation not just plain linear interpolation.
Spherical interpolation makes an initial vector coincide with another one (of same length), by moving it on the arc of a circle on their plane, with constant -unit- speed. This way, the interpolated vector always has the proper length. This is not the case with simple LERP.
To examplify: what you want to do is turn the hand of a clock, e.g. from 12hours to 4. This will be properly done with SLERP. LERP will simply move the head of the interpolated vector along the straight line from 12 to 4 (!) which -obviously- is not what you want. (It won't even preserve its length)

The formula for the SLERP is:
v(t) = (sin(a*(1-t))/sin(a))*v1 + (sin(a*t)/sin(a))*v2
where v1,v2 are the initial and final vectors, t is the interpolation parameter in [0,1] and "a" = acos(v1 dot v2)/(|v1|*|v2|)

The problem is however, that you cannot control the direction in which the interpolation will take place (CCW or CW). It will always choose the shortest arc!

Share this post

Link to post
Share on other sites
Thank you for your time and help.
This gave me a new insight of the problem, and a new path to follow.
However, I'm still stuck with the "finding T" problem. (Excuse me - but I'm not a strong math programmer).
I still can't figure out a way to find the T based on the current Up vector and the final Up vector - with a constant rate (Because the difference of the angles obviously decrease as the initial vector reaches the final vector) - and I'm using the angle of the vectors as T.

Does anyone knows how could I do this?
Should I force a fixed step for the interpolation take place (like in animation)? How do I do that?

I'm also looking into this:

To find a way to solve this problem.

Thank you for your time.

Share this post

Link to post
Share on other sites
Oops, sorry for replying late Nameless Stranger, I had totally missed your reply.
And of course you're right; you were talking about another problem in your original post... I guess I spotted another one first, and missed the point.

My guess is that -in this case- there is no correct way to find the time it would take to interpolate from one direction to another. And I'm saying this, because normally, this would be self-driven by the dynamics simulation;
E.g. the car moving under the effect of the total force exerted upon it, and rotating under the effect of total torque, it would adjust its orientation on its own.
You wouldn't have to deal with it yourself, all you'd have to do, is just apply the right forces at the right point.

The fact that you adjust the orientation simply by reading the normal to the ground under the car, makes me consider that you don't animate the scene with a dynamics simulation; With that in mind, anything I could suggest would be a hack, and probably a bad one, since I can't think of any good approximation to this.

If you want to consider switching to actual physics simulation, I'm sure you'll find a lot of material around; and you can always ask for other opinions here.
I can only suggest a very good series of tutorials, that seemed to help me a lot with this subject back ago.

I'm sorry though, I don't seem to have a solution to your current problem.

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