Jump to content

Get rotation with 2 3D points


Swimer

Recommended Posts

Posted

I have a point of my object that needs to be rotated towards another, considering all 3 directions (3D). This, I understand, needs to be solved mathematically.

Posted (edited)
--// Quick Example

local ped = createPed(0, 0, 0, 10)
local vehicle = createVehicle(411, 10, 0, 10)

function lookAtVehicle()
   local px, py, pz = getElementPosition(ped)
   local vx, vy, vz = getElementPosition(vehicle)
   local rx, ry, rz = findRotation3D(px, py, pz, vx, vy, vz)
   setElementRotation(ped, rx, ry, rz)
end
addCommandHandler("trot", lookAtVehicle)


function findRotation3D( x1, y1, z1, x2, y2, z2 ) 
	local rotx = math.atan2 ( z2 - z1, getDistanceBetweenPoints2D ( x2,y2, x1,y1 ) )
	rotx = math.deg(rotx)
	local rotz = -math.deg( math.atan2( x2 - x1, y2 - y1 ) )
	rotz = rotz < 0 and rotz + 360 or rotz
	return rotx, 0,rotz
end

this will make the ped to rotate and look at vehicle every time

Edited by Hydra

Social Media:

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...