Server side implementation of the native : Get Offset From Entity In World Coords

Hello to everyone , last days we were trying to implement the native GetOffsetFromEntityInWorldCoords on the server side.

This is our results so far , it seems to work fine , in case you find any bugs we would be happy to resolve them and update the post.

Our implementation:


GetRotationToDirection = function(rot) 
    rotZ = rot.z
    rotX = rot.x
    multXY = math.abs(math.cos(rotX))
    v = {x = 0.0 , y =  0.0 , z = 0.0}
    v.x = -math.sin(rotZ) * multXY
    v.y = math.cos(rotZ) * multXY
    v.z = math.sin(rotX)

    v = vector3(v.x , v.y , v.z)

    return v
end

-- Forward Relative Vector
GetRelativeForwardVector = function(rot)
	return GetRotationToDirection(rot)
end

-- Right Relative Vector
GetRelativeRightVector = function(rot) 
    num = math.cos(rot.y)
    v = {x = 0.0 , y =  0.0 , z = 0.0}
    v.x = math.cos(-rot.z) * num
    v.y = math.sin(rot.z) * num
    v.z = math.sin(-rot.y)

    v = vector3(v.x , v.y , v.z)
    return v
end

-- Up Relative Vector
GetRelativeUpVector = function(forwardV, rightV)
	v = {x = 0.0 , y =  0.0 , z = 0.0}
	v.x = forwardV.y*rightV.z - forwardV.z*rightV.y
	v.y = forwardV.z*rightV.x - forwardV.x*rightV.z
	v.z = forwardV.x*rightV.y - forwardV.y*rightV.x

	v = vector3(v.x , v.y , v.z)
	return v
end	

GetOffsetFromEntityInWorldCoords = function(coords,offset,rot) 
    rightV	= GetRelativeRightVector(rot)
    forwardV= GetRelativeForwardVector(rot)
	upV		= GetRelativeUpVector(forwardV , rightV)

	newV    = vector3(
		( forwardV.x * offset.x ) + ( rightV.x * offset.y ) + ( upV.x * offset.z),
		( forwardV.y * offset.x ) + ( rightV.y * offset.y ) + ( upV.y * offset.z),
		( forwardV.z * offset.x ) + ( rightV.z * offset.y ) + ( upV.z * offset.z)
	)

	finalV	= coords + newV

    return finalV;
end

How to use :

local rot 	        = GetEntityRotation(Entity)
local coords        = GetEntityCoords(Entity)
local offset        = vector3(1.0 , 0.0 , 0.0)
local new_coords    = GetOffsetFromEntityInWorldCoords(coords , offset , rot)

Credits for some of the math : Get Offset From Entity Given World Coords (general calculation) - Documentation - GTAForums

1 Like

Top Nice Work!!!

can someone tell me how does this works