# Car-surface alignment

By: a guest on Jun 30th, 2012  |  syntax: Lua  |  size: 1.11 KB  |  hits: 28  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. --vehicle-surface alignment script by CrystalMV
2.
3. function getEulerAnglesFromMatrix(x1,y1,z1,x2,y2,z2,x3,y3,z3)
4.         local nz1,nz2,nz3
5.         nz3 = math.sqrt(x2*x2+y2*y2)
6.         nz1 = -x2*z2/nz3
7.         nz2 = -y2*z2/nz3
8.         local vx = nz1*x1+nz2*y1+nz3*z1
9.         local vz = nz1*x3+nz2*y3+nz3*z3
10.         return math.deg(math.asin(z2)),-math.deg(math.atan2(vx,vz)),-math.deg(math.atan2(x2,y2))
11. end
12.
13. function getAnglesFromNormalAndAngle(zx,zy,zz,rz)
15.         local yx,yy,yz = -math.sin(rz),math.cos(rz),0
16.         local xx,xy,xz = yy*zz-yz*zy,yz*zx-yx*zz,yz*zy-yy*zx
17.         yx,yy,yz = zy*xz-zz*xy,zz*xx-xz*zx,zx*xy-zy*xx
18.         return getEulerAnglesFromMatrix(xx,xy,xz,yx,yy,yz,zx,zy,zz)
19. end
20.
21. function putCarOnSurface()
22.         local car = getPedOccupiedVehicle(localPlayer)
23.         if not car then return end
24.         local x,y,z = getElementPosition(car)
25.         local hit,sx,sy,sz,hitel,nx,ny,nz = processLineOfSight(x,y,z,x,y,z-1000,true,false,false,true)
26.         if not hit then return end
27.         local rx,ry,rz = getElementRotation(car)
28.         rx,ry,rz = getAnglesFromNormalAndAngle(nx,ny,nz,rz)
29.         setElementRotation(car,rx,ry,rz,"ZXY")
30. end