Files
i-mic-fps/lib/math/bounding_box.rb

86 lines
2.0 KiB
Ruby

class IMICFPS
class BoundingBox
attr_accessor :min, :max
def initialize
@min = Vector.new
@max = Vector.new
end
def ==(other)
@min == other.min &&
@max == other.max
end
# returns a new bounding box that includes both bounding boxes
def union(other)
temp = BoundingBox.new
temp.min.x = [@min.x, other.min.x].min
temp.min.y = [@min.y, other.min.y].min
temp.min.z = [@min.z, other.min.z].min
temp.max.x = [@max.x, other.max.x].max
temp.max.y = [@max.y, other.max.y].max
temp.max.z = [@max.z, other.max.z].max
return temp
end
# returns boolean
def intersect(other)
(@min.x <= other.max.x && @max.x >= other.min.x) &&
(@min.y <= other.max.y && @max.y >= other.min.y) &&
(@min.z <= other.max.z && @max.z >= other.min.z)
end
def difference(other)
temp = BoundingBox.new
temp.min = @min - other.min
temp.max = @max - other.max
return temp
end
def volume
width * height * depth
end
def width
@max.x - @min.x
end
def height
@max.y - @min.y
end
def depth
@max.z - @min.z
end
def normalize(entity)
temp = BoundingBox.new
temp.min.x = @min.x.to_f * entity.scale
temp.min.y = @min.y.to_f * entity.scale
temp.min.z = @min.z.to_f * entity.scale
temp.max.x = @max.x.to_f * entity.scale
temp.max.y = @max.y.to_f * entity.scale
temp.max.z = @max.z.to_f * entity.scale
return temp
end
def normalize_with_offset(entity)
temp = BoundingBox.new
temp.min.x = @min.x.to_f * entity.scale + entity.position.x
temp.min.y = @min.y.to_f * entity.scale + entity.position.y
temp.min.z = @min.z.to_f * entity.scale + entity.position.z
temp.max.x = @max.x.to_f * entity.scale + entity.position.x
temp.max.y = @max.y.to_f * entity.scale + entity.position.y
temp.max.z = @max.z.to_f * entity.scale + entity.position.z
return temp
end
end
end