Jump to content
  • Advertisement
Sign in to follow this  
ChaosPhoenix

Nlerp/Slerp with 2 vectors. [Solved]

This topic is 3487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!