Converting World Coordinates to Yaw and Pitch in Lua

546 views Asked by At

Okay I have calculated (in LUA) the position of the player's bones.

local TargetCoords = GetPedBoneCoords("PED", 31086, 0, 0, 0)

then I get my camera position (vec3)

local cam = GetFinalRenderedCamCoord()

and these are the calculations I made to get Yaw and pitch from that coords.

local dX = TargetCoords.x - cam.x;
local dY = TargetCoords.y - cam.y;
local dZ = TargetCoords.z - cam.z;

local pitch = math.atan2(math.sqrt(dZ * dZ + dX * dX), dY) + math.pi
local yaw = math.atan2(dZ, dX)
local roll = 0.0

The numbers I get are from 0 to 5 but these are not valid yaw and pitch.

The Yaw goes from 60.0 to -60.0 and the pitch goes from -180 to 180 (or 0 to 360 both valid).

EDIT

Tried also different formulas

local dX = TargetCoords.x - cam.x;
local dY = TargetCoords.y - cam.y;
local dZ = TargetCoords.z - cam.z;

local dVL = math.sqrt(dX * dX + dY * dY + dZ * dZ)

local pitch = math.asin(dZ / dVL) 
local yaw   = math.atan2(dY, dZ) 
local roll  = 0.0

pitch = (math.deg(pitch) + 180) % 360 - 180
yaw = (math.deg(yaw) + 180) % 360 - 180

I get different numbers but it's not the target position on screen. Is it the right triagle?

P.S. I'm new in lua so I tried to check some formulas but it's not clear to me.

EXAMPLE

These are the target coords -> vector3(-1694.083, -218.2781, 57.62257)

This is the CamCoord (coordinates of camera position) -> vector3(1690.352,-213.4403,58.86302)

This is where the Yaw and pitch should go using the relative yaw and pitch position

SetGameplayCamRelativePitch(pitch, 2.0)
SetGameplayCamRelativeHeading(yaw)

These two functions do set the position of the crosshair based on player heading (So what direction the player is facing 360°).

The heading of the picture is around 137.53

0

There are 0 answers