Quaternion and Euler convertion

Started by ysc3839, Feb 28, 2016, 07:53 AM

Previous topic - Next topic

ysc3839

//http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm
function Vector::toQuaternion()
{
 // Assuming the angles are in radians.
 local c1 = ::cos(this.x / 2),
 s1 = ::sin(this.x / 2),
 c2 = ::cos(this.z / 2),
 s2 = ::sin(this.z / 2),
 c3 = ::cos(this.y / 2),
 s3 = ::sin(this.y / 2),
 c1c2 = c1 * c2,
 s1s2 = s1 * s2,
 x = c1c2 * s3 + s1s2 * c3,
 y = s1 * c2 * c3 + c1 * s2 * s3,
 z = c1 * s2 * c3 - s1 * c2 * s3,
 w = c1c2 * c3 - s1s2 * s3;
 return ::Quaternion(x, y, z, w);
}
EntityVector.rawset("toQuaternion", Vector.toQuaternion);

// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
function Quaternion::toEuler()
{
 local heading, attitude, bank;
 local test = this.x * this.y + this.z * this.w;
 if (test > 0.499) { // singularity at north pole
  heading = 2 * ::atan2(this.x, this.w);
  attitude = PI / 2;
 } else if (test < -0.499) { // singularity at south pole
  heading = -2 * ::atan2(this.x, this.w);
  attitude = -PI / 2;
 } else {
  local sqx = this.x * this.x, sqy = this.y * this.y, sqz = this.z * this.z;
  heading = ::atan2(2 * this.y * this.w - 2 * this.x * this.z , 1 - 2 * sqy - 2 * sqz); // X
  attitude = ::asin(2 * test); // Z
  bank = ::atan2(2 * this.x * this.w - 2 * this.y * this.z , 1 - 2 * sqx - 2 * sqz); // Y
 }
 return ::Vector(heading, bank, attitude);
}
EntityQuaternon.rawset("toEuler", Quaternion.toEuler);

KAKAN

Well, I have one question. Try this code:
print( EntityVector );
print( EntityQuaternion );
You'll receive that the index x doesn't exist.( x is the value )
And also, if it works, try printing typeof EntityVector
It's a class. So are you sure that those 2 lines( Vex and Quaternion .rawset ) are going to work?
That's my question. Further more, you don't need to use rawset :D
oh no

ysc3839

Quote from: KAKAN on Feb 28, 2016, 07:59 AMWell, I have one question. Try this code:
print( EntityVector );
print( EntityQuaternion );
You'll receive that the index x doesn't exist.( x is the value )
And also, if it works, try printing typeof EntityVector
It's a class. So are you sure that those 2 lines( Vex and Quaternion .rawset ) are going to work?
That's my question. Further more, you don't need to use rawset :D
First, there is a typo about "EntityQuaternion". In old version it's "EntityQuaternon". It has been fixed in new version. But the binaries in download page haven't update.
So your code won't work in old version, but works in new version.
And I tested this code, that's nothing wrong about rawset. Also I want to know why not use rawset?

ysc3839

Quote from: vito on Feb 28, 2016, 03:35 PMThat thing is what I want for http://forum.vc-mp.org/?topic=2207.0
Thanks.
btw great link http://www.euclideanspace.com/maths/geometry/rotations/conversions/
I wrote this code bacause I need to test object rotation. I'm happy to hear that it can help you! :)