Skip to content

Commit

Permalink
feat(raycast): add fromCoords method
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed May 7, 2024
1 parent 24cedb0 commit b1cb214
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions imports/raycast/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,64 @@ local math_abs = math.abs
local GetFinalRenderedCamCoord = GetFinalRenderedCamCoord
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
---| 1 GLASS
---| 2 SEE_THROUGH
---| 3 GLASS | SEE_THROUGH
---| 4 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 distance number? Defaults to 10.
---@return boolean hit
---@return number entityHit
---@return vector3 endCoords
---@return vector3 surfaceNormal
---@return number materialHash
function lib.raycast.cam(flags, ignore, distance)
local camCoords = GetFinalRenderedCamCoord()
local destination = camCoords + getForwardVector() * (distance or 10)
local handle = StartShapeTestLosProbe(camCoords.x, camCoords.y, camCoords.z, destination.x, destination.y,
function lib.raycast.fromCoords(coords, destination, flags, ignore)
local handle = StartShapeTestLosProbe(coords.x, coords.y, coords.z, destination.x, destination.y,
destination.z, flags or 511, cache.ped, ignore or 4)

while true do
Wait(0)
local retval, hit, coords, surfaceNormal, materialHash, entityHit = GetShapeTestResultIncludingMaterial(handle)
local retval, hit, endCoords, surfaceNormal, material, entityHit = GetShapeTestResultIncludingMaterial(handle)

if retval ~= 1 then
return hit, entityHit, coords, surfaceNormal, materialHash
return hit, entityHit, endCoords, surfaceNormal, material
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

0 comments on commit b1cb214

Please sign in to comment.