# question [EN/BR] How to calculate coordinates in front of an element

## Recommended Posts

ENGLISH

Let's say I have a player and I want to spawn a car in front of it with a specific distance away from the player. It must considers the player's rotation. It involves some math calculations, but I can't formulate it.

BRASIL

Vamos supor que eu tenha um player e eu queira spawnar um carro na frente dele com uma distância específica. Para fazer isso, deve levar em consideração a rotação atual do player. Eu sei que isso envolve cálculos matemáticos, mas não consegui formulá-los.

Thanks all

```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```

• 1
1 hour ago, Space_Unicorn said:

```
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
```

Uuuuh wee! It works like a charm! Thank you!

• 1
```function getFrontPosition(element,dist)
local x, y, z = getElementPosition(element)
local _,_,r = getElementRotation(element)
local tx = x + - ( dist ) * math.sin( math.rad( r ) )
local ty = y + dist * math.cos( math.rad( r ) )
local tz = z
return tx,ty,tz
end```

Edited by Avival
• 2

## Create an account

Register a new account