Exemple #1
0
Quaternion Quaternion::Slerp( const Quaternion& begin,const Quaternion& end,float t )
{
	Quaternion tempEnd=end;
	//用点乘计算两四元数夹角的cos值
	float cosa=begin.DotMultiply(tempEnd);

	if (cosa<0.f)
	{
		//如果点乘为负,则反转一个四元数以取得短的4D "弧"
		tempEnd=-tempEnd;
		cosa=-cosa;
	}

	//检查他们是否过于接近以避免除0
	float k0,k1;
	if (cosa>0.9999f)
	{
		//非常接近,用线性插值
		k0=1.f-t;
		k1=t;
	}
	else
	{
		//用sin^2+cos^2=1计算sin值
		float sina=Math::Sqrt(1.f-cosa*cosa);
		//通过sin和cos计算角度
		float a=Math::Atan2(sina,cosa);
		float invSina=1.f/sina;
		k0=Math::Sin((1.f-t)*a)*invSina;
		k1=Math::Sin(t*a)*invSina;
	}
	Quaternion result=begin*k0+tempEnd*k1;
	result.Normalize();
	return result;
}