class CyberarmEngine::BoundingBox

Attributes

max[RW]
min[RW]

Public Class Methods

new(*args) click to toggle source
# 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

Public Instance Methods

+(other) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/cyberarm_engine/bounding_box.rb, line 24
def ==(other)
  @min == other.min &&
    @max == other.max
end
clone() click to toggle source
# 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) click to toggle source

does this bounding box envelop other bounding box? (inclusive of border)

# 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
depth() click to toggle source
# File lib/cyberarm_engine/bounding_box.rb, line 96
def depth
  @max.z - @min.z
end
difference(other) click to toggle source

returns the difference between both bounding boxes

# 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
height() click to toggle source
# File lib/cyberarm_engine/bounding_box.rb, line 92
def height
  @max.y - @min.y
end
inside?(vector) click to toggle source

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

# 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) click to toggle source

returns whether bounding box intersects other

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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
sum() click to toggle source
# File lib/cyberarm_engine/bounding_box.rb, line 142
def sum
  @min.sum + @max.sum
end
union(other) click to toggle source

returns a new bounding box that includes both bounding boxes

# 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
volume() click to toggle source
# File lib/cyberarm_engine/bounding_box.rb, line 84
def volume
  width * height * depth
end
width() click to toggle source
# File lib/cyberarm_engine/bounding_box.rb, line 88
def width
  @max.x - @min.x
end