# Slerp and divisions by zero

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

## 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 on other sites
The real question is: What happens when l_s is 0?

##### Share on other sites
Quote:
 Original post by smart_idiotThe real question is: What happens when l_s is 0?
Opps. Thats what I ment.

##### Share on other sites
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 on other sites
Quote:
 Original post by sthomasl_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...

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 10
• 9
• 35
• 16
• ### Forum Statistics

• Total Topics
634125
• Total Posts
3015678
×