Advertisement
RidwanRF

Object creation

Aug 30th, 2022 (edited)
1,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.13 KB | None | 0 0
  1. local objects = {}
  2.  
  3. function spawn(cmd, id)
  4.     id = tonumber(id) or 931
  5.  
  6.     local obj = createObject(id, localPlayer.matrix.position + localPlayer.matrix.forward * 2 + localPlayer.matrix.up * 1)
  7.  
  8.     if (not obj) then
  9.         return
  10.     end
  11.  
  12.     local minX, minY, minZ, maxX, maxY, maxZ = obj:getBoundingBox()
  13.     local objMatrix = obj:getMatrix()
  14.     local frontSide = objMatrix:transformPosition(Vector3((minX+maxX)/2, minY, minZ))
  15.     local backSide = objMatrix:transformPosition(Vector3((minX+maxX)/2, maxY, minZ))    
  16.  
  17.     local backSideGround = Vector3(backSide.x, backSide.y, getGroundPosition(backSide.x, backSide.y, backSide.z))
  18.     local frontSideGround = Vector3(frontSide.x, frontSide.y, getGroundPosition(frontSide.x, frontSide.y, frontSide.z))
  19.  
  20.     if (backSideGround.z == frontSideGround.z) then
  21.         return
  22.     end
  23.  
  24.     local orientation = (frontSideGround.z >= backSideGround.z)
  25.     local neutralPosition = orientation and Vector3(backSideGround.x, backSideGround.y, frontSideGround.z) or Vector3(frontSideGround.x, frontSideGround.y, backSideGround.z)
  26.  
  27.     objects[#objects+1] = {
  28.         object = obj,
  29.         orientation = orientation,
  30.         sides = {
  31.             front = frontSide,
  32.             back = backSide
  33.         },
  34.         ground = {
  35.             front = frontSideGround,
  36.             back = backSideGround,
  37.             neutral = neutralPosition
  38.         }
  39.     }
  40.  
  41.     local angle = getAngle(frontSideGround, backSideGround, neutralPosition)
  42.     local degAngle = math.deg(angle) + (orientation and -90 or 0)
  43.     local rotx, roty, rotz = getElementRotation(obj)
  44.  
  45.     print("back/front angle", orientation, rotx, angle, degAngle)
  46.     setElementRotation(obj, degAngle, roty, rotz)
  47. end
  48. addCommandHandler("spawn", spawn)
  49.  
  50. function clear()
  51.     for _, container in ipairs(objects) do
  52.         destroyElement(container.object)
  53.     end
  54.     objects = {}
  55. end
  56. addCommandHandler("clear", clear)
  57.  
  58. function render()
  59.     for _, container in ipairs(objects) do
  60.         local front, back = container.sides.front, container.sides.back
  61.         dxDrawLine3D(front.x, front.y, front.z, back.x, back.y, back.z, tocolor(255, 0, 0), 2)
  62.  
  63.         front, back, neutral = container.ground.front, container.ground.back, container.ground.neutral
  64.         dxDrawLine3D(front.x, front.y, front.z, back.x, back.y, back.z, tocolor(0, 255, 0), 3)
  65.  
  66.         if (container.orientation) then
  67.             dxDrawLine3D(front.x, front.y, front.z + 0.01, neutral.x, neutral.y, neutral.z + 0.01, tocolor(0, 0, 255), 3)
  68.         else
  69.             dxDrawLine3D(back.x, back.y, back.z + 0.01, neutral.x, neutral.y, neutral.z + 0.01, tocolor(0, 0, 255), 3)
  70.         end
  71.     end
  72. end
  73. addEventHandler("onClientRender", root, render)
  74.  
  75. function distance(vec1, vec2)
  76.     return math.sqrt(
  77.         (vec1.x - vec2.x) ^ 2 +
  78.         (vec1.y - vec2.y) ^ 2 +
  79.         (vec1.z - vec2.z) ^ 2
  80.     )
  81. end
  82.  
  83. function getAngle(vec1, vec2, vec3)
  84.     local ab = distance(vec1, vec2)
  85.     local bc = distance(vec2, vec3)
  86.     local ac = distance(vec1, vec3)
  87.    
  88.     return math.acos(
  89.         ((ab ^ 2) + (bc ^ 2) - (ac ^ 2)) / (2 * ab * bc)
  90.     )
  91. end
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement