0
我試圖移植戈麥斯的掃AABB與AABB碰撞檢測算法,如on this page Lua,我不能得到它的工作。它將檢測「碰撞」,其輸出時間幾乎等於零,以及其他不準確之處。難道我做錯了什麼?掃AABB與AABB碰撞測試不起作用
local axis = {"x","y","z"}
-- box1 is the moving box, disp is the box's displacement, box2 is stationary
function Collision.swept_aabb_vs_aabb(box1, disp, box2)
local a = box2
local b = box1
local amin = a:minCorner()
local amax = a:maxCorner()
local bmin = b:minCorner()
local bmax = b:maxCorner()
local u0d, u1d = vector(0,0,0), vector(1,1,1)
for i=1,3 do
local ax = axis[i]
if amax[ax] < bmin[ax] and disp[ax] < 0 then
u0d[ax] = (amax[ax] - bmin[ax])/disp[ax]
elseif bmax[ax] < amin[ax] and disp[ax] > 0 then
u0d[ax] = (amin[ax] - bmax[ax])/disp[ax]
end
if bmax[ax] > amin[ax] and disp[ax] < 0 then
u1d[ax] = (amin[ax] - bmax[ax])/disp[ax]
elseif amax[ax] > bmin[ax] and disp[ax] > 0 then
u1d[ax] = (amax[ax] - bmin[ax])/disp[ax]
end
end
local u0 = max(u0d.x,u0d.y,u0d.z)
local u1 = min(u1d.x,u1d.y,u1d.z)
if u0 <= u1 then return u0 else return nil end
end
編輯:看來,對於所有3軸是沒有的,如果一個值分配給u0d條件被觸發,會做一些更多的測試。