Game Development Reference
In-Depth Information
Axis aligned bounding box with center point and half dimension
To define axis aligned bounding box, we need it's center point and half dimension vector,
which points from center point to one of the corners of the box, and two methods, one that
tells if AABB contains a point, and one that tells if AABB intersects with another AABB.
This is how our implementation looks like:
10-partitioning/misc/axis_aligned_bounding_box.rb
1 class AxisAlignedBoundingBox
2 attr_reader :center , :half_dimension
3 def initialize (center, half_dimension)
4 @center = center
5 @half_dimension = half_dimension
6 @dhx = ( @half_dimension [0] - @center [0] ) . abs
7 @dhy = ( @half_dimension [1] - @center [1] ) . abs
8 end
9
10 def contains? (point)
11 return false unless ( @center [0] + @dhx ) >= point [0]
12 return false unless ( @center [0] - @dhx ) <= point [0]
13 return false unless ( @center [1] + @dhy ) >= point [1]
14 return false unless ( @center [1] - @dhy ) <= point [1]
15 true
16 end
17
Search WWH ::




Custom Search