Implemented a first class BoundingBox, refactored to use bounding box.

This commit is contained in:
2019-02-23 18:28:47 -06:00
parent 7aa99a70a7
commit fc72f2bdc8
6 changed files with 132 additions and 65 deletions

View File

@@ -6,10 +6,9 @@ class IMICFPS
include OpenGL
include GLU
include CommonMethods
attr_accessor :scale
attr_accessor :visible, :renderable, :backface_culling
attr_accessor :scale, :visible, :renderable, :backface_culling
attr_reader :position, :rotation, :velocity
attr_reader :model, :name, :debug_color, :width, :height, :depth, :last_x, :last_y, :last_z, :normalized_bounding_box
attr_reader :model, :name, :debug_color, :normalized_bounding_box
def initialize(x: 0, y: 0, z: 0, bound_model: nil, scale: MODEL_METER_SCALE, backface_culling: true, auto_manage: true, manifest_file: nil)
@position = Vector.new(x, y, z)
@scale = scale
@@ -27,7 +26,6 @@ class IMICFPS
@physics = false
@mass = 100 # kg
@width, @height, @depth = 0,0,0
@delta_time = Gosu.milliseconds
@last_position = Vector.new(@position.x, @position.y, @position.z)
@@ -39,9 +37,6 @@ class IMICFPS
@normalized_bounding_box = normalize_bounding_box_with_offset
box = normalize_bounding_box
@width = box.max_x-box.min_x
@height = box.max_y-box.min_y
@depth = box.max_z-box.min_z
end
return self
@@ -61,9 +56,6 @@ class IMICFPS
@normalized_bounding_box = normalize_bounding_box_with_offset
box = normalize_bounding_box
@width = box.max_x-box.min_x
@height = box.max_y-box.min_y
@depth = box.max_z-box.min_z
end
def model
@@ -89,8 +81,6 @@ class IMICFPS
unless at_same_position?
@normalized_bounding_box = normalize_bounding_box_with_offset if model
end
@last_x, @last_y, @last_z = @x, @y, @z
end
def debug_color=(color)
@@ -101,53 +91,16 @@ class IMICFPS
@position == @last_position
end
# Do two Axis Aligned Bounding Boxes intersect?
def intersect(other)
me = normalized_bounding_box
other = other.normalized_bounding_box
# puts "bounding boxes match!" if a == b
if (me.min_x <= other.max_x && me.max_x >= other.min_x) &&
(me.min_y <= other.max_y && me.max_y >= other.min_y) &&
(me.min_z <= other.max_z && me.max_z >= other.min_z)
return true
else
return false
end
end
def distance(vertex, other)
return Math.sqrt((vertex.x-other.x)**2 + (vertex.y-other.y)**2 + (vertex.z-other.z)**2)
end
def normalize_bounding_box
box = @bound_model.model.bounding_box
temp = BoundingBox.new
temp.min_x = box.min_x.to_f*scale
temp.min_y = box.min_y.to_f*scale
temp.min_z = box.min_z.to_f*scale
temp.max_x = box.max_x.to_f*scale
temp.max_y = box.max_y.to_f*scale
temp.max_z = box.max_z.to_f*scale
return temp
def normalize_bounding_box_with_offset
@bound_model.model.bounding_box.normalize_with_offset(self)
end
def normalize_bounding_box_with_offset
box = @bound_model.model.bounding_box
temp = BoundingBox.new
temp.min_x = box.min_x.to_f*scale+@position.x
temp.min_y = box.min_y.to_f*scale+@position.y
temp.min_z = box.min_z.to_f*scale+@position.z
temp.max_x = box.max_x.to_f*scale+@position.x
temp.max_y = box.max_y.to_f*scale+@position.y
temp.max_z = box.max_z.to_f*scale+@position.z
return temp
def normalize_bounding_box
@bound_model.model.bounding_box.normalize(self)
end
def handleGlError