# getOffsetFromXYZ

## Recommended Posts

Hello.

I wan't to determine which side of the vehicle was shooted (front or rear). My problem is that the front and the rear will be the opposite when the vehicle is rotated 180 deegres. I know, i have to use getOffsetFromXYZ but i dont really understand the logic.

```function onFire(weapon,ammo,ammoInClip,hitX,hitY,hitZ,hitElement)
if getElementType(hitElement) == "vehicle" then

local x,y,z = getOffsetFromXYZ( getElementMatrix(hitElement), getElementPosition(hitElement) )

local offsetX = hitX - x
local offsetY = hitY - y
local offsetZ = hitZ - z

if offsetX > 0 then
outputChatBox("rear")
elseif offsetX < 0 then
outputChatBox("front")
end
end
end

function getOffsetFromXYZ( mat, vec )
-- make sure our matrix is setup correctly 'cos MTA used to set all of these to 1.
mat[1][4] = 0
mat[2][4] = 0
mat[3][4] = 0
mat[4][4] = 1
mat = matrix.invert( mat )
local offX = vec[1] * mat[1][1] + vec[2] * mat[2][1] + vec[3] * mat[3][1] + mat[4][1]
local offY = vec[1] * mat[1][2] + vec[2] * mat[2][2] + vec[3] * mat[3][2] + mat[4][2]
local offZ = vec[1] * mat[1][3] + vec[2] * mat[2][3] + vec[3] * mat[3][3] + mat[4][3]
return {offX, offY, offZ}
end```

Posted (edited)
```local posX, posY, posZ = getElementPosition(hitElement)

local offsetX, offsetY, offsetZ = getOffsetFromXYZ(getElementMatrix(hitElement), posX - hitX, posY - hitY, posZ - hitZ)```

The matrix is already containing the position of the vehicle.

Untested and not even sure if this is the right approach.

P.s this function can return the surface position:https://wiki.multitheftauto.com/wiki/ProcessLineOfSight

normalX, normalY, normalZ

Edited by IIYAMA

nvm that is probably not going to work.

I'd just get the angle between the hit pos and vehicle's pos + vehicle.rotation.z, thats it.

And which function should i use for getting the angle?