feat(raycast): add fromCoords method

This commit is contained in:
Linden
2024-05-08 05:00:33 +10:00
parent 24cedb07ae
commit b1cb2142f0

View File

@@ -8,11 +8,6 @@ local math_abs = math.abs
local GetFinalRenderedCamCoord = GetFinalRenderedCamCoord local GetFinalRenderedCamCoord = GetFinalRenderedCamCoord
local GetFinalRenderedCamRot = GetFinalRenderedCamRot local GetFinalRenderedCamRot = GetFinalRenderedCamRot
local function getForwardVector()
local sin, cos = glm_sincos(glm_rad(GetFinalRenderedCamRot(2)))
return vec3(-sin.z * math_abs(cos.x), cos.z * math_abs(cos.x), sin.x)
end
---@alias ShapetestIgnore ---@alias ShapetestIgnore
---| 1 GLASS ---| 1 GLASS
---| 2 SEE_THROUGH ---| 2 SEE_THROUGH
@@ -20,28 +15,57 @@ end
---| 4 NO_COLLISION ---| 4 NO_COLLISION
---| 7 GLASS | SEE_THROUGH | NO_COLLISION ---| 7 GLASS | SEE_THROUGH | NO_COLLISION
---@param flags number? Line of sight flags, defaults to 511 (see: https://docs.fivem.net/natives/?_0x377906D8A31E5586). ---@alias ShapetestFlags integer
---| 1 INCLUDE_MOVER
---| 2 INCLUDE_VEHICLE
---| 4 INCLUDE_PED
---| 8 INCLUDE_RAGDOLL
---| 16 INCLUDE_OBJECT
---| 32 INCLUDE_PICKUP
---| 64 INCLUDE_GLASS
---| 128 INCLUDE_RIVER
---| 256 INCLUDE_FOLIAGE
---| 511 INCLUDE_ALL
---@param coords vector3
---@param destination vector3
---@param flags ShapetestFlags? Defaults to 511.
---@param ignore ShapetestIgnore? Defaults to 4. ---@param ignore ShapetestIgnore? Defaults to 4.
---@param distance number? Defaults to 10.
---@return boolean hit ---@return boolean hit
---@return number entityHit ---@return number entityHit
---@return vector3 endCoords ---@return vector3 endCoords
---@return vector3 surfaceNormal ---@return vector3 surfaceNormal
---@return number materialHash ---@return number materialHash
function lib.raycast.cam(flags, ignore, distance) function lib.raycast.fromCoords(coords, destination, flags, ignore)
local camCoords = GetFinalRenderedCamCoord() local handle = StartShapeTestLosProbe(coords.x, coords.y, coords.z, destination.x, destination.y,
local destination = camCoords + getForwardVector() * (distance or 10)
local handle = StartShapeTestLosProbe(camCoords.x, camCoords.y, camCoords.z, destination.x, destination.y,
destination.z, flags or 511, cache.ped, ignore or 4) destination.z, flags or 511, cache.ped, ignore or 4)
while true do while true do
Wait(0) Wait(0)
local retval, hit, coords, surfaceNormal, materialHash, entityHit = GetShapeTestResultIncludingMaterial(handle) local retval, hit, endCoords, surfaceNormal, material, entityHit = GetShapeTestResultIncludingMaterial(handle)
if retval ~= 1 then if retval ~= 1 then
return hit, entityHit, coords, surfaceNormal, materialHash return hit, entityHit, endCoords, surfaceNormal, material
end end
end end
end end
local function getForwardVector()
local sin, cos = glm_sincos(glm_rad(GetFinalRenderedCamRot(2)))
return vec3(-sin.z * math_abs(cos.x), cos.z * math_abs(cos.x), sin.x)
end
---@param flags ShapetestFlags? Defaults to 511.
---@param ignore ShapetestIgnore? Defaults to 4.
---@param distance number? Defaults to 10.
function lib.raycast.fromCamera(flags, ignore, distance)
local coords = GetFinalRenderedCamCoord()
local destination = coords + getForwardVector() * (distance or 10)
return lib.raycast.fromCoords(GetFinalRenderedCamCoord(), destination, flags, ignore)
end
---@deprecated
lib.raycast.cam = lib.raycast.fromCamera
return lib.raycast return lib.raycast