Hello,
I think I have some progress on this... My function return the same Euler angles as in Med 0.32. I strongly believe that they are correct angles and that MTA's angles/rotation order (used in createObject, setObjectRotation) are wrong.
The problem is that I can't find how to convert from Med 0.32's angles to MTA's angles, even by looking and modifying MTA's source code, which is what I am currently trying to do.
Here is my test code:
function quaternionToEuler( q )
q.x = q[1]
q.y = q[2]
q.z = q[3]
q.w = q[4]
local sqx = q.x * q.x
local sqy = q.y * q.y
local sqz = q.z * q.z
local sqw = q.w * q.w
local unit = sqx + sqy + sqz + sqw
local test = ( q.x * q.y ) + ( q.z * q.w )
if ( test > 0.499999999 * unit ) then
print( "1" )
return
{
0.0,
( 2 * math.atan2( q.x, q.w ) * 57.295779513082320876798154814105 ) % 360,
90.0
}
elseif ( test < -0.499999999 * unit ) then
print( "2" )
return
{
0.0,
( -2 * math.atan2( q.x, q.w ) * 57.295779513082320876798154814105 ) % 360,
270.0
}
end
print( "3" )
return
{
( math.atan2( (2 * q.x * q.w) - (2 * q.y * q.z) , 1 - (2 * sqx) - (2 * sqz) ) * 57.295779513082320876798154814105 ) % 360,
( math.atan2( (2 * q.y * q.w) - (2 * q.x * q.z) , 1 - (2 * sqy) - (2 * sqz) ) * 57.295779513082320876798154814105 ) % 360,
( math.asin( 2 * test ) * 57.295779513082320876798154814105 ) % 360
}
end
local quaternions_test =
{
{ 0.298475, 0.252500, -0.558842, 0.731336 },
{ -0.106357, 0.076511, -0.667086, 0.733370 },
{ 0.000000, 0.000000, -0.703435, 0.710760 },
{ 0.000000, 0.000000, 0.117537, 0.993068 },
{ -0.008890, -0.009568, -0.682913, 0.730383 },
{ -0.003541, 0.024422, -0.801192, 0.597898 },
{ -0.003802, -0.043453, -0.087073, 0.995247 },
{ 0.000000, 0.000000, 0.000000, 1.000000 }
}
addEventHandler ( "onResourceStart", getRootElement( ),
function( res )
if res == getThisResource( ) then
local e = 0
for _, q in pairs( quaternions_test ) do
e = quaternionToEuler( q )
print( "pitch=" .. tostring( e[1] ) .. ", roll=" .. tostring( e[2] ) .. ", yaw=" .. tostring( e[3] ) )
end
end
end
)
Here are the IPL lines I used for test:
3244, ModelName, 0, -581.000000, 1918.835938, 84.578125, 0.298475, 0.252500, -0.558842, 0.731336, 133
3276, ModelName, 0, -454.210938, 2258.023438, 45.273438, -0.106357, 0.076511, -0.667086, 0.733370, -1
16054, ModelName, 0, -427.773438, 2238.257813, 44.796875, 0.000000, 0.000000, -0.703435, 0.710760, -1
16132, ModelName, 0, -600.632813, 1926.328125, 21.921875, 0.000000, 0.000000, 0.117537, 0.993068, -1
3276, ModelName, 0, -728.750000, 928.164063, 12.101563, -0.008890, -0.009568, -0.682913, 0.730383, -1
713, ModelName, 0, -806.656250, 981.812500, 13.000000, -0.003541, 0.024422, -0.801192, 0.597898, -1
16479, ModelName, 0, 86.242188, 1214.039063, 17.742188, -0.003802, -0.043453, -0.087073, 0.995247, -1
11557, ModelName, 0, -802.531250, 1070.781250, 25.359375, 0.000000, 0.000000, 0.000000, 1.000000, 126