Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local objects = {}
- function spawn(cmd, id)
- id = tonumber(id) or 931
- local obj = createObject(id, localPlayer.matrix.position + localPlayer.matrix.forward * 2 + localPlayer.matrix.up * 1)
- if (not obj) then
- return
- end
- local minX, minY, minZ, maxX, maxY, maxZ = obj:getBoundingBox()
- local objMatrix = obj:getMatrix()
- local frontSide = objMatrix:transformPosition(Vector3((minX+maxX)/2, minY, minZ))
- local backSide = objMatrix:transformPosition(Vector3((minX+maxX)/2, maxY, minZ))
- local backSideGround = Vector3(backSide.x, backSide.y, getGroundPosition(backSide.x, backSide.y, backSide.z))
- local frontSideGround = Vector3(frontSide.x, frontSide.y, getGroundPosition(frontSide.x, frontSide.y, frontSide.z))
- if (backSideGround.z == frontSideGround.z) then
- return
- end
- local orientation = (frontSideGround.z >= backSideGround.z)
- local neutralPosition = orientation and Vector3(backSideGround.x, backSideGround.y, frontSideGround.z) or Vector3(frontSideGround.x, frontSideGround.y, backSideGround.z)
- objects[#objects+1] = {
- object = obj,
- orientation = orientation,
- sides = {
- front = frontSide,
- back = backSide
- },
- ground = {
- front = frontSideGround,
- back = backSideGround,
- neutral = neutralPosition
- }
- }
- local angle = getAngle(frontSideGround, backSideGround, neutralPosition)
- local degAngle = math.deg(angle) + (orientation and -90 or 0)
- local rotx, roty, rotz = getElementRotation(obj)
- print("back/front angle", orientation, rotx, angle, degAngle)
- setElementRotation(obj, degAngle, roty, rotz)
- end
- addCommandHandler("spawn", spawn)
- function clear()
- for _, container in ipairs(objects) do
- destroyElement(container.object)
- end
- objects = {}
- end
- addCommandHandler("clear", clear)
- function render()
- for _, container in ipairs(objects) do
- local front, back = container.sides.front, container.sides.back
- dxDrawLine3D(front.x, front.y, front.z, back.x, back.y, back.z, tocolor(255, 0, 0), 2)
- front, back, neutral = container.ground.front, container.ground.back, container.ground.neutral
- dxDrawLine3D(front.x, front.y, front.z, back.x, back.y, back.z, tocolor(0, 255, 0), 3)
- if (container.orientation) then
- dxDrawLine3D(front.x, front.y, front.z + 0.01, neutral.x, neutral.y, neutral.z + 0.01, tocolor(0, 0, 255), 3)
- else
- dxDrawLine3D(back.x, back.y, back.z + 0.01, neutral.x, neutral.y, neutral.z + 0.01, tocolor(0, 0, 255), 3)
- end
- end
- end
- addEventHandler("onClientRender", root, render)
- function distance(vec1, vec2)
- return math.sqrt(
- (vec1.x - vec2.x) ^ 2 +
- (vec1.y - vec2.y) ^ 2 +
- (vec1.z - vec2.z) ^ 2
- )
- end
- function getAngle(vec1, vec2, vec3)
- local ab = distance(vec1, vec2)
- local bc = distance(vec2, vec3)
- local ac = distance(vec1, vec3)
- return math.acos(
- ((ab ^ 2) + (bc ^ 2) - (ac ^ 2)) / (2 * ab * bc)
- )
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement