Nlerp/Slerp with 2 vectors. [Solved]

Started by
2 comments, last by Zakwayda 14 years, 8 months ago
I have 2 3D vectors. One being the original position, the 2nd being the target position. I'm trying to simply interpolate between the two. I should note I'm using the Configurable Math Library (love it), but I figure this is just a basic math issue that I'm probably doing incorrectly than anything wrong with the library so I didn't feel the need to post this on their specific forum. Code:

// ms_OriginalPos = Vector3
// ms_targetPos = Vector3
// ms_interpValue = float between 0.0 - 1.0
				
vector3 nlerpVec = cml::nlerp(ms_originalPos, ms_targetPos, ms_interpValue);
				
// Returned vector is normalized, so multiply it by the original length/magnitude to get the correct position.
float magnitude = (ms_targetPos - ms_originalPos).length();
vector3 newPosition = (nlerpVec * magnitude);
				
setPosition(newPosition);


I'm probably missing something very simple since my brain is a bit fried currently and this is my first attempt at something like this. Thanks for the help. [Edited by - ChaosPhoenix on August 2, 2009 4:42:22 PM]
Advertisement
Hi ChaosPheonix,

I *think* what you want here is actually lerp(), e.g.:
vector2 newPosition = cml::lerp(ms_originalPos, ms_targetPos, ms_interpValue);setPosition(newPosition);
lerp() interpolatoes linearly between the two input values, which is what you'd usually want when moving from one position to another. nlerp(), on the other hand, interpolates linearly and then normalizes the result; it's typically used with (unit-length) vectors or quaternions as a lower-cost substitute for spherical linear interpolation.
Ah, you're correct. Works fine.

I remember initially thinking I needed to use LERP but for some reason decided against it. Haha, like I said, a bit brain fried today.

Doesn't LERP run into problems if you use matrix rotation? Luckily I'm not dealing with rotation currently (and can use Quaternions if need be when the time comes), but just something I should probably just keep myself aware of.

Congrats on CML. I love it. I use it for all my OpenGL projects.

Quote:Doesn't LERP run into problems if you use matrix rotation? Luckily I'm not dealing with rotation currently (and can use Quaternions if need be when the time comes), but just something I should probably just keep myself aware of.
For rotation matrices or quaternions, you would typically use NLERP or SLERP.

NLERP basically reduces to interpolating the input values linearly, and then normalizing the result (or orthogonalizing, in the case of matrices).

NLERP is often used as a cheaper alternative to SLERP for interpolating between two orientations. Aside from efficiency, the main difference between NLERP and SLERP is that SLERP maintains a constant angular speed over the interpolation, while NLERP does not. Note also that with both operations (NLERP and SLERP), the quaternion version is considerably less costly than the matrix version.
Quote:Congrats on CML. I love it. I use it for all my OpenGL projects.
Thanks! I'm glad you've found it useful :)

This topic is closed to new replies.

Advertisement