Search code examples
math3dcoordinatescoordinate-transformation

Get position by having start position, turn and distance


How to get end point (3d) given start position as 3d point, direction as 3d rotation (0-360 in x, y and z) and distance? Values:

local x, y, z = 0,0,0 -- starting position
local rx, ry, rz = 0, 180, 360 -- rotation
local distance = 10 -- distance

local destinationX, destinationY, destinationZ = ... -- endpoint

I know about https://wiki.multitheftauto.com/wiki/GetPointFromDistanceRotation but that won't help me as all 3 endpoint values are important to me

Yes, I saw this topic, but I have Z - the vertical axis (GTA San Andreas), no matter how I tried the first answer on this topic, I could not do what I need.


Solution

  • You have a starting point P = (x, y, z). Then you have a direction vector v = (1, 0, 0), which you rotate using rx, ry, rz.

    With rx, ry, rz you can calculate the rotation matrix R using these equations: https://en.wikipedia.org/wiki/Rotation_matrix#General_rotations

    Then, the final point would be:

    equation