Skip to content

Commit

Permalink
refactor(raycast): smoother raycast during camera movement (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
masnana authored May 7, 2024
1 parent 851df5b commit a3952a0
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions imports/raycast/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ lib.raycast = {}

local StartShapeTestLosProbe = StartShapeTestLosProbe
local GetShapeTestResultIncludingMaterial = GetShapeTestResultIncludingMaterial
local GetWorldCoordFromScreenCoord = GetWorldCoordFromScreenCoord
local glm_sincos = require 'glm'.sincos
local glm_rad = require 'glm'.rad
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
Expand All @@ -20,20 +29,19 @@ local GetWorldCoordFromScreenCoord = GetWorldCoordFromScreenCoord
---@return vector3 surfaceNormal
---@return number materialHash
function lib.raycast.cam(flags, ignore, distance)
local coords, normal = GetWorldCoordFromScreenCoord(0.5, 0.5)
local destination = coords + normal * (distance or 10)
local handle = StartShapeTestLosProbe(coords.x, coords.y, coords.z, destination.x, destination.y, destination.z,
flags or 511, cache.ped, ignore or 4)
local camCoords = GetFinalRenderedCamCoord()
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)

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

if retval ~= 1 then
---@diagnostic disable-next-line: return-type-mismatch
return hit, entityHit, endCoords, surfaceNormal, materialHash
end
end
if retval ~= 1 then
return hit, entityHit, coords, surfaceNormal, materialHash
end
end
end

return lib.raycast

0 comments on commit a3952a0

Please sign in to comment.