GE Lua Documentation

Press F to search!

intersectsRay_Capsule

Definition


-- @/lua/common/mathlib.lua:1480

-- https://iquilezles.org/articles/intersectors , returns first hit, untested
function intersectsRay_Capsule(rpos, rdir, cposa, cposb, cR )
  local ba, oa = cposb - cposa, rpos - cposa
  local baba, bard, baoa = ba:squaredLength(), ba:dot(rdir), ba:dot(oa)
  local a, b = baba - bard*bard, rdir:dot(oa) * baba - baoa*bard
  local h = b*b - a*((oa:squaredLength()-cR*cR) * baba - baoa*baoa)
  if h >= 0 then
    local t = (-b-sqrt(h))/a
    local y = baoa + t*bard
    if y > 0 and y < baba then return t end -- body
    oa:set(y <= 0 and oa or rpos - cposb) -- caps
    b = rdir:dot(oa)
    h = b*b - oa:squaredLength() - cR*cR
    if h > 0 then return -b-sqrt(h) end
  end
  return math.huge
end

Callers