I wanted to ask for some help with the math on Yaw, Roll, Pitch and Space Rotation. Unfortunately, my math skills are not the best. I am building a map on 1.26 and , therefore, I use MemoryHack for these functions. However, calculations in this memory hack are not correct. Therefore, I thought that maybe someone knowledgeable about 3D rotation math can help me.
JASS:
// Effect Rotation API Radians
function GetObjectYawRad takes integer pObject returns real // Z | Yaw | returns Degrees!
local integer pMatrix = 0
local real r11 = 0.
local real r21 = 0.
local real r31 = 0.
local real yaw = 0.
local real pitch = 0.
if pObject > 0 then
set pMatrix = ReadRealMemory( pObject + 0x28 )
if pMatrix > 0 then
set r11 = ReadRealFloat( pMatrix + 0x108 )
set r21 = ReadRealFloat( pMatrix + 0x114 )
set r31 = ReadRealFloat( pMatrix + 0x120 )
set pitch = -Asin( r31 ) // Atan2( -r31, SquareRoot( Pow( r32, 2 ) + Pow( r33, 2 ) ) )
set yaw = -Atan2( r21, r11 )
if yaw < 0 then
set yaw = 6.28319 + yaw
endif
endif
endif
return yaw
endfunction
function GetObjectPitchRad takes integer pObject returns real // Y | returns Degrees!
local integer pMatrix = 0
local real r31 = 0.
local real r32 = 0.
local real r33 = 0.
local real pitch = 0.
if pObject > 0 then
set pMatrix = ReadRealMemory( pObject + 0x28 )
if pMatrix > 0 then
set r31 = ReadRealFloat( pMatrix + 0x120 )
set r32 = ReadRealFloat( pMatrix + 0x124 )
set r33 = ReadRealFloat( pMatrix + 0x128 )
set pitch = -Asin( r31 ) // Atan2( -r31, SquareRoot( Pow( r32, 2 ) + Pow( r33, 2 ) ) )
if r31 < 0. and r33 > 0. then
set pitch = pitch
elseif r31 < 0. and r33 < 0. then
set pitch = 3.14159 - pitch
elseif r31 > 0. and r33 < 0. then
set pitch = 3.14159 - pitch
elseif r31 > 0. and r33 > 0. then
set pitch = 6.28319 + pitch
endif
endif
endif
return pitch
endfunction
function GetObjectRollRad takes integer pObject returns real // X | returns Degrees!
local integer pMatrix = 0
local real r31 = 0.
local real r32 = 0.
local real r33 = 0.
local real pitch = 0.
local real roll = 0.
if pObject > 0 then
set pMatrix = ReadRealMemory( pObject + 0x28 )
if pMatrix > 0 then
set r31 = ReadRealFloat( pMatrix + 0x120 )
set r32 = ReadRealFloat( pMatrix + 0x124 )
set r33 = ReadRealFloat( pMatrix + 0x128 )
set pitch = -Asin( r31 )
set roll = Atan2( r32 / Cos( pitch ), r33 / Cos( pitch ) )
if roll < 0. then
set roll = 6.28319 + roll
endif
endif
endif
return roll
endfunction
function GetObjectFacingRad takes integer pObject returns real // Z | Yaw | returns Degrees!
return GetObjectYawRad( pObject )
endfunction
function SetObjectSpaceRotationRad takes integer pObject, real yaw, real pitch, real roll returns nothing
local integer pMatrix = 0
local real Sx = Sin( roll )
local real Sy = Sin( pitch )
local real Sz = Sin( -yaw )
local real Cx = Cos( roll )
local real Cy = Cos( pitch )
local real Cz = Cos( -yaw )
if pObject > 0 then
set pMatrix = ReadRealMemory( pObject + 0x28 )
if pMatrix > 0 then
call WriteRealFloat( pMatrix + 0x108, Cy * Cz )
call WriteRealFloat( pMatrix + 0x10C, -Cy * Sz )
call WriteRealFloat( pMatrix + 0x110, Sy )
call WriteRealFloat( pMatrix + 0x114, Cz * Sx * Sy + Cx * Sz )
call WriteRealFloat( pMatrix + 0x118, Cx * Cz - Sx * Sy * Sz )
call WriteRealFloat( pMatrix + 0x11C, -Cy * Sx )
call WriteRealFloat( pMatrix + 0x120, -Cx * Cz * Sy + Sx * Sz )
call WriteRealFloat( pMatrix + 0x124, Cz * Sx + Cx * Sy * Sz )
call WriteRealFloat( pMatrix + 0x128, Cx * Cy )
endif
endif
endfunction
function SetObjectYawRad takes integer pObject, real angle returns nothing // Z Yaw | In Degrees!
local integer pMatrix = 0
local real r31 = 0.
local real r32 = 0.
local real r33 = 0.
local real pitch = 0.
local real roll = 0.
if pObject > 0 then
set pMatrix = ReadRealMemory( pObject + 0x28 )
if pMatrix > 0 then
set r31 = ReadRealFloat( pMatrix + 0x120 )
set r32 = ReadRealFloat( pMatrix + 0x124 )
set r33 = ReadRealFloat( pMatrix + 0x128 )
set pitch = -Asin( r31 ) //Atan2( -r31, SquareRoot( Pow( r32, 2 ) + Pow( r33, 2 ) ) )
set roll = Atan2( r32 / Cos( pitch ), r33 / Cos( pitch ) )
call SetObjectSpaceRotationRad( pObject, angle, pitch, roll )
endif
endif
endfunction
function SetObjectPitchRad takes integer pObject, real angle returns nothing // Y | In Degrees!
local integer pMatrix = 0
local real r11 = 0.
local real r21 = 0.
local real r32 = 0.
local real r33 = 0.
local real yaw = 0.
local real roll = 0.
if pObject > 0 then
set pMatrix = ReadRealMemory( pObject + 0x28 )
if pMatrix > 0 then
set r11 = ReadRealFloat( pMatrix + 0x108 )
set r21 = ReadRealFloat( pMatrix + 0x114 )
set r32 = ReadRealFloat( pMatrix + 0x124 )
set r33 = ReadRealFloat( pMatrix + 0x128 )
set yaw = Atan2( r21 / Cos( angle ), r11 / Cos( angle ) )
set roll = Atan2( r32 / Cos( angle ), r33 / Cos( angle ) )
call SetObjectSpaceRotationRad( pObject, yaw, angle, roll )
endif
endif
endfunction
function SetObjectRollRad takes integer pObject, real angle returns nothing // X | In Degrees!
local integer pMatrix = 0
local real r11 = 0.
local real r21 = 0.
local real r31 = 0.
local real yaw = 0.
local real pitch = 0.
if pObject > 0 then
set pMatrix = ReadRealMemory( pObject + 0x28 )
if pMatrix > 0 then
set r11 = ReadRealFloat( pMatrix + 0x108 )
set r21 = ReadRealFloat( pMatrix + 0x114 )
set r31 = ReadRealFloat( pMatrix + 0x120 )
set pitch = -Asin( r31 ) // Atan2( -r31, SquareRoot( Pow( r32, 2 ) + Pow( r33, 2 ) ) )
set yaw = Atan2( r21 / Cos( pitch ), r11 / Cos( pitch ) )
call SetObjectSpaceRotationRad( pObject, yaw, pitch, angle )
endif
endif
endfunction
function SetObjectOrientationRad takes integer pObject, real yaw, real pitch, real roll returns nothing
if pObject > 0 then
call SetObjectSpaceRotationRad( pObject, yaw, pitch, roll )
//call SetObjectRollRad( pObject, roll )
//call SetObjectPitchRad( pObject, pitch )
//call SetObjectYawRad( pObject, yaw )
endif
endfunction
//===================================================================
// Effect Rotation API Degrees
function SetObjectSpaceRotation takes integer pObject, real yaw, real pitch, real roll returns nothing
call SetObjectSpaceRotationRad( pObject, Deg2Rad( yaw ), Deg2Rad( pitch ), Deg2Rad( roll ) )
endfunction
function GetObjectYaw takes integer pObject returns real // Z | Yaw | returns Degrees!
return Rad2Deg( GetObjectYawRad( pObject ) )
endfunction
function GetObjectFacing takes integer pObject returns real
return Rad2Deg( GetObjectFacingRad( pObject ) )
endfunction
function GetObjectPitch takes integer pObject returns real
return Rad2Deg( GetObjectPitchRad( pObject ) )
endfunction
function GetObjectRoll takes integer pObject returns real
return Rad2Deg( GetObjectRollRad( pObject ) )
endfunction
function SetObjectYaw takes integer pObject, real angle returns nothing
call SetObjectYawRad( pObject, Deg2Rad( angle ) )
endfunction
function SetObjectFacing takes integer pObject, real angle returns nothing
call SetObjectYawRad( pObject, Deg2Rad( angle ) )
endfunction
function SetObjectPitch takes integer pObject, real angle returns nothing
call SetObjectPitchRad( pObject, Deg2Rad( angle ) )
endfunction
function SetObjectRoll takes integer pObject, real angle returns nothing
call SetObjectRollRad( pObject, Deg2Rad( angle ) )
endfunction
function SetObjectOrientation takes integer pObject, real yaw, real pitch, real roll returns nothing
call SetObjectOrientationRad( pObject, Deg2Rad( yaw ), Deg2Rad( pitch ), Deg2Rad( roll ) )
endfunction