Jump to content

[HELP] Quaternions to Angles IPL files


Recommended Posts

Hi, im working on a resource that loads ipl and ide files in mta, usefull for map mods and such,

my problem is converting quaternions from ipl objets, here is my converting code

  
  
function QuaternionsToAngles(rX,rY,rZ,rW) 
    if rW > 1 then 
    rX = math.sqrt(x*x) 
    rY = math.sqrt(y*y) 
    rZ = math.sqrt(z*z) 
    rW = math.sqrt(w*w) 
   end 
    sqx = rX^2 
    sqy = rY^2 
    sqz = rZ^2 
    sqw = rW^2 
    unit = sqx + sqy + sqz + sqw 
    test = rX*rY+rZ*rW; 
    if(test > 0.4999999999999 * unit) then 
        RX = 2 * math.atan2(rX,rW); 
        RZ = PI/2; 
        RY = 0; 
    elseif(test < -0.4999999999999 * unit) then 
        RX = -2 * math.atan2(rX,rW); 
        RZ = -PI/2; 
        RY = 0; 
    else 
        tmp = (2*rY*rW)-(2*rX*rZ); 
        RX = math.atan2(tmp,sqx-sqy-sqz+sqw); 
        RZ = math.asin(2 * test/unit); 
        tmp = (2*rX*rW)-(2*rY*rZ); 
        RY = math.atan2(tmp,-sqx+sqy-sqy+sqw); 
    end 
    RX = math.deg(RX) 
    RY = math.deg(RY) 
    RZ = math.deg(RZ) 
    return RX,RY,RZ 
end 

It works for some objets only, another objets shows wrong angles, if someone got quaternions knowledges thanks :/

This is the only thing that i need to fix, i made possible to read ipl files and these things.

Link to comment
  • 4 months later...

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 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...