Class: CyberarmEngine::BoundingBox
- Inherits:
-
Object
- Object
- CyberarmEngine::BoundingBox
- Defined in:
- lib/cyberarm_engine/lib/bounding_box.rb
Instance Attribute Summary collapse
-
#max ⇒ Object
Returns the value of attribute max.
-
#min ⇒ Object
Returns the value of attribute min.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #==(other) ⇒ Object
- #clone ⇒ Object
-
#contains?(other) ⇒ Boolean
does this bounding box envelop other bounding box? (inclusive of border).
- #depth ⇒ Object
-
#difference(other) ⇒ Object
returns the difference between both bounding boxes.
- #height ⇒ Object
-
#initialize(*args) ⇒ BoundingBox
constructor
A new instance of BoundingBox.
-
#intersect?(other) ⇒ Boolean
returns whether bounding box intersects other.
- #normalize(entity) ⇒ Object
- #normalize_with_offset(entity) ⇒ Object
-
#point?(vector) ⇒ Boolean
returns whether the vector is inside of the bounding box.
- #sum ⇒ Object
-
#union(other) ⇒ Object
returns a new bounding box that includes both bounding boxes.
- #volume ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(*args) ⇒ BoundingBox
Returns a new instance of BoundingBox.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 5 def initialize(*args) case args.size when 0 @min = Vector.new(0, 0, 0) @max = Vector.new(0, 0, 0) when 2 @min = args.first.clone @max = args.last.clone when 4 @min = Vector.new(args[0], args[1], 0) @max = Vector.new(args[2], args[3], 0) when 6 @min = Vector.new(args[0], args[1], args[2]) @max = Vector.new(args[3], args[4], args[5]) else raise "Invalid number of arguments! Got: #{args.size}, expected: 0, 2, 4, or 6." end end |
Instance Attribute Details
#max ⇒ Object
Returns the value of attribute max.
3 4 5 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 3 def max @max end |
#min ⇒ Object
Returns the value of attribute min.
3 4 5 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 3 def min @min end |
Instance Method Details
#+(other) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 120 def +(other) box = BoundingBox.new box.min = self.min + other.min box.min = self.max + other.max return box end |
#-(other) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 128 def -(other) box = BoundingBox.new box.min = self.min - other.min box.min = self.max - other.max return box end |
#==(other) ⇒ Object
24 25 26 27 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 24 def ==(other) @min == other.min && @max == other.max end |
#clone ⇒ Object
140 141 142 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 140 def clone BoundingBox.new(@min.x, @min.y, @min.z, @max.x, @max.y, @max.z) end |
#contains?(other) ⇒ Boolean
does this bounding box envelop other bounding box? (inclusive of border)
66 67 68 69 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 66 def contains?(other) other.min.x >= min.x && other.min.y >= min.y && other.min.z >= min.z && other.max.x <= max.x && other.max.y <= max.y && other.max.z <= max.z end |
#depth ⇒ Object
90 91 92 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 90 def depth @max.z - @min.z end |
#difference(other) ⇒ Object
returns the difference between both bounding boxes
44 45 46 47 48 49 50 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 44 def difference(other) temp = BoundingBox.new temp.min = @min - other.min temp.max = @max - other.max return temp end |
#height ⇒ Object
86 87 88 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 86 def height @max.y - @min.y end |
#intersect?(other) ⇒ Boolean
returns whether bounding box intersects other
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 53 def intersect?(other) if other.is_a?(Ray) other.intersect?(self) elsif other.is_a?(BoundingBox) (@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) else raise "Unknown collider: #{other.class}" end end |
#normalize(entity) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 94 def normalize(entity) temp = BoundingBox.new temp.min.x = @min.x.to_f * entity.scale.x temp.min.y = @min.y.to_f * entity.scale.y temp.min.z = @min.z.to_f * entity.scale.z temp.max.x = @max.x.to_f * entity.scale.x temp.max.y = @max.y.to_f * entity.scale.y temp.max.z = @max.z.to_f * entity.scale.z return temp end |
#normalize_with_offset(entity) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 107 def normalize_with_offset(entity) temp = BoundingBox.new temp.min.x = @min.x.to_f * entity.scale.x + entity.position.x temp.min.y = @min.y.to_f * entity.scale.y + entity.position.y temp.min.z = @min.z.to_f * entity.scale.z + entity.position.z temp.max.x = @max.x.to_f * entity.scale.x + entity.position.x temp.max.y = @max.y.to_f * entity.scale.y + entity.position.y temp.max.z = @max.z.to_f * entity.scale.z + entity.position.z return temp end |
#point?(vector) ⇒ Boolean
returns whether the vector is inside of the bounding box
72 73 74 75 76 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 72 def point?(vector) vector.x.between?(@min.x, @max.x) && vector.y.between?(@min.y, @max.y) && vector.z.between?(@min.z, @max.z) end |
#sum ⇒ Object
136 137 138 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 136 def sum @min.sum + @max.sum end |
#union(other) ⇒ Object
returns a new bounding box that includes both bounding boxes
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 30 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 |
#volume ⇒ Object
78 79 80 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 78 def volume width * height * depth end |
#width ⇒ Object
82 83 84 |
# File 'lib/cyberarm_engine/lib/bounding_box.rb', line 82 def width @max.x - @min.x end |