The CameraHelper utility is a powerful tool for RedM game developers who need to interact with the game camera system. This utility provides a comprehensive set of functions for camera manipulation, coordinate conversion, and entity detection through raycasting.
The utility includes several key functions that make camera-related operations more manageable:
Here's the complete implementation of the CameraHelper utility:
CameraHelper = {
GetPosition = function()
return GetGameplayCamCoord()
end,
GetRotation = function(unk)
return GetGameplayCamRot(unk)
end,
DegToRad = function (degs)
return degs * 3.141592653589793 / 180
end,
RotationToDirection = function(rotation)
local z = math.rad(rotation.z)
local x = math.rad(rotation.x)
local num = math.abs(math.cos(x))
return vector3(-math.sin(z) * num, math.cos(z) * num, math.sin(x))
end,
WorldToScreenRel = function(worldCoords)
local check, x, y = GetScreenCoordFromWorldCoord(worldCoords.x, worldCoords.y, worldCoords.z)
if not check then
return false
end
screenCoordsx = (x - 0.5) * 2.0
screenCoordsy = (y - 0.5) * 2.0
return true, screenCoordsx, screenCoordsy
end,
ScreenToWorld = function(giveX, giveY)
local camVec2, camVec21
camRot = CameraHelper.GetRotation(2) + vec3(0, 45.0, 0.0)
camPos = CameraHelper.GetPosition()
direction = CameraHelper.RotationToDirection(camRot)
camVec3 = camRot + vec3(10.0, 0.0, 0.0)
camVec31 = camRot + vec3(-10.0, 0.0, 0.0)
camVec32 = camRot + vec3(0.0, 0.0, -10.0)
direction1 = CameraHelper.RotationToDirection(camRot + vec3(0.0, 0.0, 10.0)) - CameraHelper.RotationToDirection(camVec32)
direction2 = CameraHelper.RotationToDirection(camVec3) - CameraHelper.RotationToDirection(camVec31)
local rad = CameraHelper.DegToRad(camRot.y)
camVec33 = (direction1 * math.cos(rad)) - (direction2 * math.sin(rad))
camVec34 = (direction1 * math.sin(rad)) + (direction2 * math.cos(rad))
local find, screenX, screenY = CameraHelper.WorldToScreenRel(((camPos + (direction * 10.0)) + camVec33) + camVec34)
if not find then
return camPos + (direction * 10.0)
else
camVec2 = vec2(screenX, screenY)
end
local find2, screenX2, screenY2 = CameraHelper.WorldToScreenRel(camPos + (direction * 10.0))
if not find2 then
return camPos + (direction * 10.0)
else
camVec21 = vec2(screenX2, screenY2)
end
if math.abs(camVec2.x - camVec21.x) < 0.001 or math.abs(camVec2.y - camVec21.y) < 0.001 then
return camPos + (direction * 10.0)
end
local x = (giveX - camVec21.x) / (camVec2.x - camVec21.x)
local y = (giveY - camVec21.y) / (camVec2.y - camVec21.y)
return ((camPos + (direction * 10.0)) + (camVec33 * x) + (camVec34 * y))
end,
RaycastForEntity = function(screenCoord, ignoreEntity, maxDistance)
local world = CameraHelper.ScreenToWorld(screenCoord.x, screenCoord.y)
local camVec3 = CameraHelper.GetPosition()
local camVec4 = world - camVec3
camVec4 = norm(camVec4)
local ray = StartShapeTestRay(camVec3 + (camVec4 * 1.0), camVec3 + (camVec4 * maxDistance), -1, ignoreEntity)
local _, _, _, _, entity = GetShapeTestResult(ray)
return entity
end
}GetPosition()Returns the current position of the gameplay camera as a vector3.
local cameraPosition = CameraHelper.GetPosition()GetRotation(unk)Returns the current rotation of the gameplay camera. The unk parameter is typically set to 2.
local cameraRotation = CameraHelper.GetRotation(2)WorldToScreenRel(worldCoords)Converts world coordinates to screen relative coordinates.
local success, screenX, screenY = CameraHelper.WorldToScreenRel(worldCoords)ScreenToWorld(giveX, giveY)Converts screen coordinates to world coordinates.
local worldCoords = CameraHelper.ScreenToWorld(0.5, 0.5)RaycastForEntity(screenCoord, ignoreEntity, maxDistance)Performs a raycast from the screen position to detect entities.
local entity = CameraHelper.RaycastForEntity(
vec2(0.5, 0.5), -- Screen position vector2
PlayerPedId(), -- Ignore self
10.0 -- Max distance
)DegToRad(degs)Converts degrees to radians.
local radians = CameraHelper.DegToRad(90)RotationToDirection(rotation)Converts a rotation vector to a direction vector.
local direction = CameraHelper.RotationToDirection(rotation)Here's a practical example of using the CameraHelper to detect an entity in the middle of the screen:
local entity = CameraHelper.RaycastForEntity(
vec2(0.5, 0.5), -- Screen position vector2
PlayerPedId(), -- Ignore self
10.0 -- Max distance
)
print(entity) -- Prints the detected entity IDOriginal implementation by kasuganosoras.
The CameraHelper utility provides a robust foundation for camera-related operations in RedM development. Whether you're working on a complex camera system or simply need to detect entities in the player's view, this utility offers the essential functions you need.