LScript versions of EulerToQuaternion and QuaternionToEuler

vncnt

Well-known member
My LScript version of these functions seem to work fine (until now) in Legato.
I needed them in the Lock-2-World variation of the HoldPose function in Legato.

Procedure: select a frame range or a frame, use Lock-2-World and presto: the item will stop moving/rotating in that frame range or until after the current frame.
A Lock-2-Item is on its way: temporary parenting an item to another item without rebuilding the rig!

You might need these functions if you want to add/subtract rotations on multiple axis at once without gimbal lock.

Code:
EulerToQuaternion: heading,pitch,bank
{
	// SOURCE: http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm 
	
	// ANGLES ARE IN RADIANS
	c1 = cos(heading/2);
	s1 = sin(heading/2);
	c2 = cos(pitch/2);
	s2 = sin(pitch/2);
	c3 = cos(bank/2);
	s3 = sin(bank/2);
	
	c1c2 = c1*c2;
	s1s2 = s1*s2;
	
	Qw = c1c2*c3 - s1s2*s3;
	Qx = c1c2*s3 + s1s2*c3;
	Qy = s1*c2*c3 + c1*s2*s3;
	Qz = c1*s2*c3 - s1*c2*s3;
	
	return(Qw,Qx,Qy,Qz);
}



QuaternionToEuler: Qw,Qx,Qy,Qz
{
	// SOURCE: http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
	
	test = Qx*Qy + Qz*Qw;
	
	if(test > 0.499) {
		
		// FOUND SINGULARITY AT NORTH POLE
		heading = 2 * atan2(Qx,Qw);
		pitch = PI/2;
		bank = 0;
		return(heading,pitch,bank);
		}
	
	if(test < -0.499) {
		
		// FOUND SINGULARITY AT SOUTH POLE
		heading = -2 * atan2(Qx,Qw);
		pitch = - PI/2;
		bank = 0;
		return(heading,pitch,bank);
		}
	
    sqx = Qx*Qx;
    sqy = Qy*Qy;
    sqz = Qz*Qz;
    
    heading = atan2((2*Qy*Qw - 2*Qx*Qz),(1 - 2*sqy - 2*sqz));
	pitch = asin(2*test);
	bank = atan2((2*Qx*Qw - 2*Qy*Qz),(1 - 2*sqx - 2*sqz));
	
	return(heading,pitch,bank);
}
 
Top Bottom