Class: CyberarmEngine::BoundingBox

Inherits:
Object
  • Object
show all
Defined in:
lib/cyberarm_engine/bounding_box.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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/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

#maxObject

Returns the value of attribute max.



3
4
5
# File 'lib/cyberarm_engine/bounding_box.rb', line 3

def max
  @max
end

#minObject

Returns the value of attribute min.



3
4
5
# File 'lib/cyberarm_engine/bounding_box.rb', line 3

def min
  @min
end

Instance Method Details

#+(other) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/cyberarm_engine/bounding_box.rb', line 126

def +(other)
  box = BoundingBox.new
  box.min = min + other.min
  box.min = max + other.max

  box
end

#-(other) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/cyberarm_engine/bounding_box.rb', line 134

def -(other)
  box = BoundingBox.new
  box.min = min - other.min
  box.min = max - other.max

  box
end

#==(other) ⇒ Object



24
25
26
27
# File 'lib/cyberarm_engine/bounding_box.rb', line 24

def ==(other)
  @min == other.min &&
    @max == other.max
end

#cloneObject



146
147
148
# File 'lib/cyberarm_engine/bounding_box.rb', line 146

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)

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/cyberarm_engine/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

#depthObject



96
97
98
# File 'lib/cyberarm_engine/bounding_box.rb', line 96

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/bounding_box.rb', line 44

def difference(other)
  temp = BoundingBox.new
  temp.min = @min - other.min
  temp.max = @max - other.max

  temp
end

#heightObject



92
93
94
# File 'lib/cyberarm_engine/bounding_box.rb', line 92

def height
  @max.y - @min.y
end

#inside?(vector) ⇒ Boolean

returns whether the 3D vector is inside of the bounding box

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/cyberarm_engine/bounding_box.rb', line 72

def inside?(vector)
  (vector.x.between?(@min.x, @max.x) || vector.x.between?(@max.x, @min.x)) &&
    (vector.y.between?(@min.y, @max.y) || vector.y.between?(@max.y, @min.y)) &&
    (vector.z.between?(@min.z, @max.z) || vector.z.between?(@max.z, @min.z))
end

#intersect?(other) ⇒ Boolean

returns whether bounding box intersects other

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cyberarm_engine/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



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cyberarm_engine/bounding_box.rb', line 100

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

  temp
end

#normalize_with_offset(entity) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cyberarm_engine/bounding_box.rb', line 113

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

  temp
end

#point?(vector) ⇒ Boolean

returns whether the 2D vector is inside of the bounding box

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/cyberarm_engine/bounding_box.rb', line 79

def point?(vector)
  (vector.x.between?(@min.x, @max.x) || vector.x.between?(@max.x, @min.x)) &&
    (vector.y.between?(@min.y, @max.y) || vector.y.between?(@max.y, @min.y))
end

#sumObject



142
143
144
# File 'lib/cyberarm_engine/bounding_box.rb', line 142

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/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

  temp
end

#volumeObject



84
85
86
# File 'lib/cyberarm_engine/bounding_box.rb', line 84

def volume
  width * height * depth
end

#widthObject



88
89
90
# File 'lib/cyberarm_engine/bounding_box.rb', line 88

def width
  @max.x - @min.x
end