Skip to content

Instantly share code, notes, and snippets.

@kasuganosoras
Created May 8, 2023 22:40
Show Gist options
  • Save kasuganosoras/006a2441ba35981697ba65914547ca4d to your computer and use it in GitHub Desktop.
Save kasuganosoras/006a2441ba35981697ba65914547ca4d to your computer and use it in GitHub Desktop.
RedM Camera Helper
--[[
Copyright 2022 ZeroDream
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]--
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
}
-- Example for getting entity in 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 id
print(entity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment