# Calculating position from rotation and offset

## Recommended Posts

Hello again,

I'm trying to spawn the player onto an element. This element has a non-fixed rotation.

I'm just wondering if the function getPointFromDistanceRotation isn't enough to do this?

Player details can be found here and the ships details are here

I'm currently using this snippet (to try) to get the explained result:

```addCommandHandler("boat", function(player, cmd)
local x, y, z = getElementPosition(player)
local _, _, r = getElementRotation(player)
outputChatBox("player pos: "..x..","..y..","..z..","..r, player)

local ship = createShip(x, y, z, r) -- returns model of pirateship

local x, y, z = getElementPosition(ship)
local _, _, r = getElementRotation(ship)
outputChatBox("ship pos: "..x..","..y..","..z..","..r, player)

local x, y = getPointFromDistanceRotation(x - 0.1, y - 2.7, 5, r) -- offset included
outputChatBox("new player pos: "..x..","..y..","..z..","..r, player)
setElementPosition(player, x, y, 43.7)
end)```

What am I'm doing wrong? Do I need to calculate the offset coordinates extra?

Greetings,

Maurize

Edited by Maurize

I don't know if I understood you well, anyway: Firstly, try using this function to create the marker at a specific position of the object:

```function getPositionFromElementOffset(element,offX,offY,offZ)
local m = getElementMatrix ( element )  -- Get the matrix
local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  -- Apply transform
local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2]
local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3]
return x, y, z                               -- Return the transformed point
end```

Done that, you can set the player in front the mark that way:

```local playerRot = 0 -- you'll need to adjust player rotation
local playerX = markerX + math.sin( math.rad( playerRot ) ) * .6
local playerY = markerY + math.cos( math.rad( playerRot ) ) * .6
setElementPosition( player, playerX, playerY, markerZ+1 )
setElementRotation( player, 0, 0, ( 360-playerRot ) )```

If you change the object rotation, you'll also need to calculate the corresponding player's rotation in the pirate ship.

Edited by DNL291
• 1

Thank you very much!

The function is exactly what I needed!

The positions are now exactly like I want them.

## Create an account

Register a new account