Skip to content

Commit cb51183

Browse files
committed
Fixed GetAABB
Uses all 8 entity corners instead of GetPos
1 parent 41e6a93 commit cb51183

1 file changed

Lines changed: 38 additions & 17 deletions

File tree

lua/cfw/extensions/position_sv.lua

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local CLASS = CFW.Classes.Contraption
22
local VEC_0 = Vector(0, 0, 0)
3-
local HUGE = math.huge
43

54
function CLASS:GetPos()
65
-- TODO: Optimize this
@@ -11,29 +10,51 @@ function CLASS:GetPos()
1110
pos = pos + ent:GetPos()
1211
end
1312

14-
return pos / table.Count(self.ents)
13+
return pos / self.count
1514
end
1615

17-
function CLASS:GetAABB()
18-
local mins, maxs = Vector(HUGE, HUGE, HUGE), -Vector(HUGE, HUGE, HUGE)
16+
do -- AABB
17+
local HUGE = math.huge
18+
local corner = Vector()
1919

20-
for ent in pairs(self.ents) do
21-
local pos = ent:GetPos()
20+
local function expandAABB(ent, x, y, z, mins, maxs)
21+
corner:SetUnpacked(x, y, z)
2222

23-
if pos.x < mins.x then mins.x = pos.x end
24-
if pos.y < mins.y then mins.y = pos.y end
25-
if pos.z < mins.z then mins.z = pos.z end
23+
local worldCorner = ent:LocalToWorld(corner)
2624

27-
if pos.x > maxs.x then maxs.x = pos.x end
28-
if pos.y > maxs.y then maxs.y = pos.y end
29-
if pos.z > maxs.z then maxs.z = pos.z end
25+
if worldCorner.x < mins.x then mins.x = worldCorner.x end
26+
if worldCorner.y < mins.y then mins.y = worldCorner.y end
27+
if worldCorner.z < mins.z then mins.z = worldCorner.z end
28+
if worldCorner.x > maxs.x then maxs.x = worldCorner.x end
29+
if worldCorner.y > maxs.y then maxs.y = worldCorner.y end
30+
if worldCorner.z > maxs.z then maxs.z = worldCorner.z end
3031
end
3132

32-
local center = (mins + maxs) * 0.5
33+
function CLASS:GetAABB()
34+
local mins, maxs = Vector(HUGE, HUGE, HUGE), -Vector(HUGE, HUGE, HUGE)
35+
36+
for ent in pairs(self.ents) do
37+
local obbMins, obbMaxs = ent:GetCollisionBounds()
38+
local minX, minY, minZ = obbMins.x, obbMins.y, obbMins.z
39+
local maxX, maxY, maxZ = obbMaxs.x, obbMaxs.y, obbMaxs.z
40+
41+
-- Calculate all 8 corners of the entity's OBB in world space
42+
expandAABB(ent, maxX, minY, minZ, mins, maxs) -- Top Left Front
43+
expandAABB(ent, maxX, minY, maxZ, mins, maxs) -- Top Left Back
44+
expandAABB(ent, maxX, maxY, minZ, mins, maxs) -- Top Right Front
45+
expandAABB(ent, maxX, maxY, maxZ, mins, maxs) -- Top Right Back
46+
expandAABB(ent, minX, minY, minZ, mins, maxs) -- Bottom Left Front
47+
expandAABB(ent, minX, minY, maxZ, mins, maxs) -- Bottom Left Back
48+
expandAABB(ent, minX, maxY, minZ, mins, maxs) -- Bottom Right Front
49+
expandAABB(ent, minX, maxY, maxZ, mins, maxs) -- Bottom Right Back
50+
end
3351

34-
debugoverlay.Cross(mins, 12, 0.03, Color(255, 0, 0), true)
35-
debugoverlay.Cross(maxs, 12, 0.03, Color(0, 255, 0), true)
36-
debugoverlay.Box(center, mins - center, maxs - center, 0.03, self.color)
52+
local center = (mins + maxs) * 0.5
3753

38-
return mins, maxs, center
54+
debugoverlay.Cross(mins, 12, 0.03, Color(255, 0, 0), true)
55+
debugoverlay.Cross(maxs, 12, 0.03, Color(0, 255, 0), true)
56+
debugoverlay.Box(center, mins - center, maxs - center, 0.03, self.color)
57+
58+
return mins, maxs, center
59+
end
3960
end

0 commit comments

Comments
 (0)