Translate quaternions to euler rotations for correct rotations

This commit is contained in:
Fierelier 2021-09-10 14:20:33 +02:00
parent 9b237b265c
commit 3f3fc673a8
3 changed files with 59 additions and 4 deletions

View File

@ -75,13 +75,14 @@ function registerObjectNow(id,model,texture,drawDistance)
displayStatus("Loaded model: " ..model.. ", " ..tostring(#objectsToRegister).. " remaining...")
end
function placeObject(...) -- id,interior,x,y,z,rx,ry,rz,lod
function placeObject(...) -- id,interior,x,y,z,rx,ry,rz,rw,lod
table.insert(objectsToPlace,arg)
end
function placeObjectNow(id,interior,x,y,z,rx,ry,rz,lod)
function placeObjectNow(id,interior,x,y,z,rx,ry,rz,rw,lod)
if objectRegister[id] == nil then return end
obj = createObject(objectRegister[id],x,y + 6000,z,rx,ry,rz)
erx,ery,erz = fromQuaternion(rx,ry,rz,rw)
obj = createObject(objectRegister[id],x,y + 6000,z,erx,ery,erz)
setElementInterior(obj,interior)
setElementCollisionsEnabled(obj,false)
displayStatus("Placed model: " ..tostring(id).. ", " ..tostring(#objectsToPlace).. " remaining...")

View File

@ -0,0 +1,53 @@
-- Author: KawaNoII (http://www.chinesemta.com) alexaxel705 (alexaxel705@gmail.com)
-- Source: https://wiki.multitheftauto.com/wiki/Quaternion
local identityMatrix = {
[1] = {1, 0, 0},
[2] = {0, 1, 0},
[3] = {0, 0, 1}
}
function QuaternionTo3x3(x,y,z,w)
local matrix3x3 = {[1] = {}, [2] = {}, [3] = {}}
local symetricalMatrix = {
[1] = {(-(y*y)-(z*z)), x*y, x*z},
[2] = {x*y, (-(x*x)-(z*z)), y*z},
[3] = {x*z, y*z, (-(x*x)-(y*y))}
}
local antiSymetricalMatrix = {
[1] = {0, -z, y},
[2] = {z, 0, -x},
[3] = {-y, x, 0}
}
for i = 1, 3 do
for j = 1, 3 do
matrix3x3[i][j] = identityMatrix[i][j]+(2*symetricalMatrix[i][j])+(2*w*antiSymetricalMatrix[i][j])
end
end
return matrix3x3
end
function getEulerAnglesFromMatrix(x1,y1,z1,x2,y2,z2,x3,y3,z3)
local nz1,nz2,nz3
nz3 = math.sqrt(x2*x2+y2*y2)
nz1 = -x2*z2/nz3
nz2 = -y2*z2/nz3
local vx = nz1*x1+nz2*y1+nz3*z1
local vz = nz1*x3+nz2*y3+nz3*z3
return math.deg(math.asin(z2)),-math.deg(math.atan2(vx,vz)),-math.deg(math.atan2(x2,y2))
end
function fromQuaternion(x,y,z,w)
local matrix = QuaternionTo3x3(x,y,z,w)
local ox,oy,oz = getEulerAnglesFromMatrix(
matrix[1][1], matrix[1][2], matrix[1][3],
matrix[2][1], matrix[2][2], matrix[2][3],
matrix[3][1], matrix[3][2], matrix[3][3]
)
return ox,oy,oz
end

View File

@ -185,6 +185,7 @@ def main():
fileMeta.write("<meta>\n")
fileMeta.write('<info author="unknown" version="1.0.0" name="' +pathOut.rsplit(os.path.sep,1)[-1]+ '"/>\n')
fileMeta.write('<download_priority_group>-1</download_priority_group>\n')
fileMeta.write('<script src="quaternion.lua" type="client"/>\n')
fileMeta.write('<script src="client.lua" type="client"/>\n')
for ide in data["ide"]:
fileMeta.write('<script src="gta/' +ide.replace("\\","/")+ '.lua" type="client"/>\n')
@ -205,7 +206,7 @@ def main():
for element in data["ipl"][ipl][section]:
if not element["id"] in objectIDs:
objectIDs.append(element["id"])
fileIpl.write('placeObject(' +element["id"]+ ',' +element["interior"]+ ',' +element["position"][0]+ ',' +element["position"][1]+ ',' +element["position"][2]+ ',' +element["rotation"][0]+ ',' +element["rotation"][1]+ ',' +element["rotation"][2]+ ',' +element["lod"]+ ')\n')
fileIpl.write('placeObject(' +element["id"]+ ',' +element["interior"]+ ',' +element["position"][0]+ ',' +element["position"][1]+ ',' +element["position"][2]+ ',' +element["rotation"][0]+ ',' +element["rotation"][1]+ ',' +element["rotation"][2]+ ',' +element["rotation"][3]+ ',' +element["lod"]+ ')\n')
modelNames = []
textureNames = []