132 lines
3.8 KiB
Plaintext
132 lines
3.8 KiB
Plaintext
|
local function pr(...)
|
||
|
local debug = false
|
||
|
if debug then
|
||
|
printf("aol_bbox_collision: " .. ...)
|
||
|
end
|
||
|
end
|
||
|
-- helper functions
|
||
|
local function diagonal_length(a,b)
|
||
|
return math.sqrt((a*a) + (b*b))
|
||
|
end
|
||
|
|
||
|
-- used to generate coordinates in local space of bounding box from dimensions
|
||
|
-- each index corresponds to a vertex
|
||
|
-- local space origin is at center of box
|
||
|
local box_local_matrix = {
|
||
|
vector():set(-0.5, -0.5, -0.5), -- bottom, top right
|
||
|
vector():set(0.5, -0.5, -0.5), -- bottom, top left
|
||
|
vector():set(-0.5, -0.5, 0.5), -- bottom, bottom right
|
||
|
vector():set(0.5, -0.5, 0.5), -- bottom, bottom left
|
||
|
vector():set(-0.5, 0.5, -0.5), -- top, top right
|
||
|
vector():set(0.5, 0.5, -0.5), -- top, top left
|
||
|
vector():set(-0.5, 0.5, 0.5), -- top, bottom right
|
||
|
vector():set(0.5, 0.5, 0.5), -- top, bottom left
|
||
|
}
|
||
|
|
||
|
local ray_pairs = {
|
||
|
-- Orthogonals
|
||
|
-- bottom plane
|
||
|
{1,2},
|
||
|
{1,3},
|
||
|
{2,4},
|
||
|
{3,4},
|
||
|
-- top plane
|
||
|
{5,6},
|
||
|
{5,7},
|
||
|
{6,8},
|
||
|
{7,8},
|
||
|
-- upward edges
|
||
|
{1,5},
|
||
|
{3,7},
|
||
|
{4,8},
|
||
|
{2,6},
|
||
|
|
||
|
-- Diagonals
|
||
|
-- bottom plane
|
||
|
{1,4},
|
||
|
{2,3},
|
||
|
-- top plane
|
||
|
{6,7},
|
||
|
{5,8},
|
||
|
-- front plane
|
||
|
{3,8},
|
||
|
{4,7},
|
||
|
-- back plane
|
||
|
{1,6},
|
||
|
{2,5},
|
||
|
-- left plane
|
||
|
{2,8},
|
||
|
{4,6},
|
||
|
-- right plane
|
||
|
{1,7},
|
||
|
{3,5}
|
||
|
}
|
||
|
|
||
|
-- Class for handling collisions with a bounding box, defined by its dimensions and origin
|
||
|
class "bbox_collider" (aol_bshape.bshape_collider)
|
||
|
|
||
|
function bbox_collider:__init(width, length, height, world_origin, local_origin, rotation)
|
||
|
super(world_origin, local_origin, rotation)
|
||
|
|
||
|
-- Stores dimensions of the bounding box
|
||
|
self.bbox_dims = {
|
||
|
width = width,
|
||
|
length = length,
|
||
|
height = height,
|
||
|
}
|
||
|
|
||
|
self.ray_pairs = ray_pairs
|
||
|
|
||
|
self.ray_pair_i2length = {
|
||
|
-- Orthogonals
|
||
|
-- bottom plane
|
||
|
[1] = self.bbox_dims.width,
|
||
|
[2] = self.bbox_dims.length,
|
||
|
[3] = self.bbox_dims.length,
|
||
|
[4] = self.bbox_dims.width,
|
||
|
-- top plane
|
||
|
[5] = self.bbox_dims.width,
|
||
|
[6] = self.bbox_dims.length,
|
||
|
[7] = self.bbox_dims.length,
|
||
|
[8] = self.bbox_dims.width,
|
||
|
-- upward edges
|
||
|
[9] = self.bbox_dims.height,
|
||
|
[10] = self.bbox_dims.height,
|
||
|
[11] = self.bbox_dims.height,
|
||
|
[12] = self.bbox_dims.height,
|
||
|
|
||
|
-- Diagonals
|
||
|
-- bottom plane
|
||
|
[13] = diagonal_length(self.bbox_dims.width,self.bbox_dims.length),
|
||
|
[14] = diagonal_length(self.bbox_dims.width,self.bbox_dims.length),
|
||
|
-- top plane
|
||
|
[15] = diagonal_length(self.bbox_dims.width,self.bbox_dims.length),
|
||
|
[16] = diagonal_length(self.bbox_dims.width,self.bbox_dims.length),
|
||
|
-- front plane
|
||
|
[17] = diagonal_length(self.bbox_dims.width,self.bbox_dims.height),
|
||
|
[18] = diagonal_length(self.bbox_dims.width,self.bbox_dims.height),
|
||
|
-- back plane
|
||
|
[19] = diagonal_length(self.bbox_dims.width,self.bbox_dims.height),
|
||
|
[20] = diagonal_length(self.bbox_dims.width,self.bbox_dims.height),
|
||
|
-- left plane
|
||
|
[21] = diagonal_length(self.bbox_dims.length,self.bbox_dims.height),
|
||
|
[22] = diagonal_length(self.bbox_dims.length,self.bbox_dims.height),
|
||
|
-- right plane
|
||
|
[23] = diagonal_length(self.bbox_dims.length,self.bbox_dims.height),
|
||
|
[24] = diagonal_length(self.bbox_dims.length,self.bbox_dims.height)
|
||
|
}
|
||
|
|
||
|
self:UpdateVertices()
|
||
|
self:UpdateBBox()
|
||
|
end
|
||
|
|
||
|
function bbox_collider:UpdateVertices()
|
||
|
for i, pos in ipairs(box_local_matrix) do
|
||
|
local vertex_pos = vec_add(pos, self.local_origin)
|
||
|
vertex_pos.x = vertex_pos.x*self.bbox_dims.width
|
||
|
vertex_pos.y = vertex_pos.y*self.bbox_dims.height
|
||
|
vertex_pos.z = vertex_pos.z*self.bbox_dims.length
|
||
|
|
||
|
self.vertices[i] = vertex_pos
|
||
|
end
|
||
|
end
|