Scripts/resources/[standalone]/ox_lib/imports/raycast/client.lua

72 lines
2.2 KiB
Lua
Raw Normal View History

2024-12-29 20:02:43 +00:00
lib.raycast = {}
local StartShapeTestLosProbe = StartShapeTestLosProbe
local GetShapeTestResultIncludingMaterial = GetShapeTestResultIncludingMaterial
2024-12-30 10:15:34 +00:00
local glm_sincos = require 'glm'.sincos
local glm_rad = require 'glm'.rad
local math_abs = math.abs
local GetFinalRenderedCamCoord = GetFinalRenderedCamCoord
local GetFinalRenderedCamRot = GetFinalRenderedCamRot
2024-12-29 20:02:43 +00:00
---@alias ShapetestIgnore
---| 1 GLASS
---| 2 SEE_THROUGH
---| 3 GLASS | SEE_THROUGH
---| 4 NO_COLLISION
---| 7 GLASS | SEE_THROUGH | NO_COLLISION
2024-12-30 10:15:34 +00:00
---@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.
2024-12-29 20:02:43 +00:00
---@param ignore ShapetestIgnore? Defaults to 4.
---@return boolean hit
---@return number entityHit
---@return vector3 endCoords
---@return vector3 surfaceNormal
---@return number materialHash
2024-12-30 10:15:34 +00:00
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, endCoords, surfaceNormal, material, entityHit = GetShapeTestResultIncludingMaterial(handle)
if retval ~= 1 then
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)
2024-12-29 20:02:43 +00:00
end
2024-12-30 10:15:34 +00:00
---@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
2024-12-29 20:02:43 +00:00
return lib.raycast