Jump to content
  • Advertisement
Sign in to follow this  

Slerp and divisions by zero

This topic is 4776 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

Some code from my quaternion class
	SQuaternion SQuaternion::slerp(const SQuaternion& q,f32 t)
		{
			float l_d,l_t,l_it,l_ac,l_s;
			SQuaternion l_q;

			l_d = s*q.s + v.x*q.v.x + v.y*q.v.y + v.z*q.v.z;

			l_ac = acos(abs(l_d));
			l_s = sin(l_ac);

			l_it = sin((1.0f-t)*l_ac)/l_s;
			l_t = sin(t*l_ac)/l_s;

			if(l_d<0.0f)
			{
				l_q.s = l_it*s - l_t*q.s;
				l_q.v.x = l_it*v.x - l_t*q.v.x;
				l_q.v.y = l_it*v.y - l_t*q.v.y;
				l_q.v.z = l_it*v.z - l_t*q.v.z;
			}
			else
			{
				l_q.s = l_it*s + l_t*q.s;
				l_q.v.x = l_it*v.x + l_t*q.v.x;
				l_q.v.y = l_it*v.y + l_t*q.v.y;
				l_q.v.z = l_it*v.z + l_t*q.v.z;
			}

			return l_q;
		}




My question is what if l_s is zero? Im getting divisions by zero. Some graphics code is calling this and crashing my GPU. How can I deal with the division by zero?

Share this post


Link to post
Share on other sites
Advertisement
l_s will only be zero if the two quaternions are the same or really close to the same. In that case you can just do regular linear interpolation: (1-t)*q1 + t*q2.

One thing to watch out for is that sometimes when taking the dot product of two quaternions, the result might be slightly less than -1 or greater than 1 due to numerical precision problems, even when using doubles. You should clamp the result to [-1, 1] before taking the acos() of it, otherwise you'll get junk back when the result is slightly outside of that range.

Also, the abs(l_d) in the line with the acos() looks fishy. Perhaps related to the problem I mentioned above?

Share this post


Link to post
Share on other sites
Quote:
Original post by sthomas
l_s will only be zero if the two quaternions are the same or really close to the same. In that case you can just do regular linear interpolation: (1-t)*q1 + t*q2.

One thing to watch out for is that sometimes when taking the dot product of two quaternions, the result might be slightly less than -1 or greater than 1 due to numerical precision problems, even when using doubles. You should clamp the result to [-1, 1] before taking the acos() of it, otherwise you'll get junk back when the result is slightly outside of that range.

Also, the abs(l_d) in the line with the acos() looks fishy. Perhaps related to the problem I mentioned above?
All of those problems seem to have been taken care of in the source I posted a link to...

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!