mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 07:32:35 +00:00
Refactored bounding box to use 2 vectors, stubbed PhysicsManager.
This commit is contained in:
@@ -70,6 +70,7 @@ require_relative "lib/managers/entity_manager"
|
||||
require_relative "lib/managers/light_manager"
|
||||
require_relative "lib/managers/network_manager"
|
||||
require_relative "lib/managers/collision_manager"
|
||||
require_relative "lib/managers/physics_manager"
|
||||
|
||||
require_relative "lib/renderer/renderer"
|
||||
require_relative "lib/renderer/shader"
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
class IMICFPS
|
||||
class CollisionManager
|
||||
attr_reader :game_state, :collisions
|
||||
def initialize(game_state:)
|
||||
@game_state = game_state
|
||||
@aabb_tree = AABBTree.new
|
||||
@collisions = {}
|
||||
|
||||
@aabb_tree = AABBTree.new
|
||||
@physics_manager = PhysicsManager.new(collision_manager: self)
|
||||
end
|
||||
|
||||
def add(entity)
|
||||
@@ -12,6 +16,7 @@ class IMICFPS
|
||||
def update
|
||||
lazy_check_collisions
|
||||
@aabb_tree.update
|
||||
@physics_manager.update
|
||||
end
|
||||
|
||||
def remove(entity)
|
||||
|
||||
@@ -1,4 +1,18 @@
|
||||
class IMICFPS
|
||||
class PhysicsManager
|
||||
def initialize(collision_manager:)
|
||||
@collision_manager = collision_manager
|
||||
end
|
||||
|
||||
def update
|
||||
@collision_manager.collisions.each do |entity, versus|
|
||||
versus.each do |versus|
|
||||
resolve(entity, other)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def resolve(entity, other)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,56 +1,42 @@
|
||||
class IMICFPS
|
||||
class BoundingBox
|
||||
attr_accessor :min_x, :min_y, :min_z, :max_x, :max_y, :max_z
|
||||
attr_accessor :min, :max
|
||||
|
||||
def initialize(min_x = 0.0, min_y = 0.0, min_z = 0.0, max_x = 0.0, max_y = 0.0, max_z = 0.0)
|
||||
@min_x = min_x
|
||||
@min_y = min_y
|
||||
@min_z = min_z
|
||||
|
||||
@max_x = max_x
|
||||
@max_y = max_y
|
||||
@max_z = max_z
|
||||
def initialize
|
||||
@min = Vector.new
|
||||
@max = Vector.new
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
min_x == other.min_x &&
|
||||
min_y == other.min_y &&
|
||||
min_z == other.min_z &&
|
||||
max_x == other.max_x &&
|
||||
max_y == other.max_y &&
|
||||
max_z == other.max_z
|
||||
@min == other.min &&
|
||||
@max == other.max
|
||||
end
|
||||
|
||||
# returns a new bounding box that includes both bounding boxes
|
||||
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.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.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
|
||||
|
||||
# returns boolean
|
||||
def intersect(other)
|
||||
(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)
|
||||
(@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)
|
||||
end
|
||||
|
||||
def difference(other)
|
||||
temp = BoundingBox.new
|
||||
temp.min_x = min_x - other.min_x
|
||||
temp.min_y = min_y - other.min_y
|
||||
temp.min_z = min_z - other.min_z
|
||||
|
||||
temp.max_x = max_x - other.max_x
|
||||
temp.max_y = max_y - other.max_y
|
||||
temp.max_z = max_z - other.max_z
|
||||
temp.min = @min - other.min
|
||||
temp.max = @max - other.max
|
||||
|
||||
return temp
|
||||
end
|
||||
@@ -60,39 +46,39 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def width
|
||||
@max_x - @min_x
|
||||
@max.x - @min.x
|
||||
end
|
||||
|
||||
def height
|
||||
@max_y - @min_y
|
||||
@max.y - @min.y
|
||||
end
|
||||
|
||||
def depth
|
||||
@max_z - @min_z
|
||||
@max.z - @min.z
|
||||
end
|
||||
|
||||
def normalize(entity)
|
||||
temp = BoundingBox.new
|
||||
temp.min_x = min_x.to_f * entity.scale
|
||||
temp.min_y = min_y.to_f * entity.scale
|
||||
temp.min_z = min_z.to_f * entity.scale
|
||||
temp.min.x = @min.x.to_f * entity.scale
|
||||
temp.min.y = @min.y.to_f * entity.scale
|
||||
temp.min.z = @min.z.to_f * entity.scale
|
||||
|
||||
temp.max_x = max_x.to_f * entity.scale
|
||||
temp.max_y = max_y.to_f * entity.scale
|
||||
temp.max_z = max_z.to_f * entity.scale
|
||||
temp.max.x = @max.x.to_f * entity.scale
|
||||
temp.max.y = @max.y.to_f * entity.scale
|
||||
temp.max.z = @max.z.to_f * entity.scale
|
||||
|
||||
return temp
|
||||
end
|
||||
|
||||
def normalize_with_offset(entity)
|
||||
temp = BoundingBox.new
|
||||
temp.min_x = min_x.to_f * entity.scale + entity.position.x
|
||||
temp.min_y = min_y.to_f * entity.scale + entity.position.y
|
||||
temp.min_z = min_z.to_f * entity.scale + entity.position.z
|
||||
temp.min.x = @min.x.to_f * entity.scale + entity.position.x
|
||||
temp.min.y = @min.y.to_f * entity.scale + entity.position.y
|
||||
temp.min.z = @min.z.to_f * entity.scale + entity.position.z
|
||||
|
||||
temp.max_x = max_x.to_f * entity.scale + entity.position.x
|
||||
temp.max_y = max_y.to_f * entity.scale + entity.position.y
|
||||
temp.max_z = max_z.to_f * entity.scale + entity.position.z
|
||||
temp.max.x = @max.x.to_f * entity.scale + entity.position.x
|
||||
temp.max.y = @max.y.to_f * entity.scale + entity.position.y
|
||||
temp.max.z = @max.z.to_f * entity.scale + entity.position.z
|
||||
|
||||
return temp
|
||||
end
|
||||
|
||||
@@ -157,53 +157,53 @@ class IMICFPS
|
||||
|
||||
def mesh_vertices(box)
|
||||
[
|
||||
box.min_x, box.max_y, box.max_z,
|
||||
box.min_x, box.max_y, box.min_z,
|
||||
box.max_x, box.max_y, box.min_z,
|
||||
box.min.x, box.max.y, box.max.z,
|
||||
box.min.x, box.max.y, box.min.z,
|
||||
box.max.x, box.max.y, box.min.z,
|
||||
|
||||
box.min_x, box.max_y, box.max_z,
|
||||
box.max_x, box.max_y, box.max_z,
|
||||
box.max_x, box.max_y, box.min_z,
|
||||
box.min.x, box.max.y, box.max.z,
|
||||
box.max.x, box.max.y, box.max.z,
|
||||
box.max.x, box.max.y, box.min.z,
|
||||
|
||||
box.max_x, box.min_y, box.min_z,
|
||||
box.max_x, box.min_y, box.max_z,
|
||||
box.min_x, box.min_y, box.max_z,
|
||||
box.max.x, box.min.y, box.min.z,
|
||||
box.max.x, box.min.y, box.max.z,
|
||||
box.min.x, box.min.y, box.max.z,
|
||||
|
||||
box.max_x, box.min_y, box.min_z,
|
||||
box.min_x, box.min_y, box.min_z,
|
||||
box.min_x, box.min_y, box.max_z,
|
||||
box.max.x, box.min.y, box.min.z,
|
||||
box.min.x, box.min.y, box.min.z,
|
||||
box.min.x, box.min.y, box.max.z,
|
||||
|
||||
box.min_x, box.max_y, box.max_z,
|
||||
box.min_x, box.max_y, box.min_z,
|
||||
box.min_x, box.min_y, box.min_z,
|
||||
box.min.x, box.max.y, box.max.z,
|
||||
box.min.x, box.max.y, box.min.z,
|
||||
box.min.x, box.min.y, box.min.z,
|
||||
|
||||
box.min_x, box.min_y, box.max_z,
|
||||
box.min_x, box.min_y, box.min_z,
|
||||
box.min_x, box.max_y, box.max_z,
|
||||
box.min.x, box.min.y, box.max.z,
|
||||
box.min.x, box.min.y, box.min.z,
|
||||
box.min.x, box.max.y, box.max.z,
|
||||
|
||||
box.max_x, box.max_y, box.max_z,
|
||||
box.max_x, box.max_y, box.min_z,
|
||||
box.max_x, box.min_y, box.min_z,
|
||||
box.max.x, box.max.y, box.max.z,
|
||||
box.max.x, box.max.y, box.min.z,
|
||||
box.max.x, box.min.y, box.min.z,
|
||||
|
||||
box.max_x, box.min_y, box.max_z,
|
||||
box.max_x, box.min_y, box.min_z,
|
||||
box.max_x, box.max_y, box.max_z,
|
||||
box.max.x, box.min.y, box.max.z,
|
||||
box.max.x, box.min.y, box.min.z,
|
||||
box.max.x, box.max.y, box.max.z,
|
||||
|
||||
box.min_x, box.max_y, box.max_z,
|
||||
box.max_x, box.max_y, box.max_z,
|
||||
box.max_x, box.min_y, box.max_z,
|
||||
box.min.x, box.max.y, box.max.z,
|
||||
box.max.x, box.max.y, box.max.z,
|
||||
box.max.x, box.min.y, box.max.z,
|
||||
|
||||
box.min_x, box.max_y, box.max_z,
|
||||
box.max_x, box.min_y, box.max_z,
|
||||
box.min_x, box.min_y, box.max_z,
|
||||
box.min.x, box.max.y, box.max.z,
|
||||
box.max.x, box.min.y, box.max.z,
|
||||
box.min.x, box.min.y, box.max.z,
|
||||
|
||||
box.max_x, box.min_y, box.min_z,
|
||||
box.min_x, box.min_y, box.min_z,
|
||||
box.min_x, box.max_y, box.min_z,
|
||||
box.max.x, box.min.y, box.min.z,
|
||||
box.min.x, box.min.y, box.min.z,
|
||||
box.min.x, box.max.y, box.min.z,
|
||||
|
||||
box.max_x, box.min_y, box.min_z,
|
||||
box.min_x, box.max_y, box.min_z,
|
||||
box.max_x, box.max_y, box.min_z
|
||||
box.max.x, box.min.y, box.min.z,
|
||||
box.min.x, box.max.y, box.min.z,
|
||||
box.max.x, box.max.y, box.min.z
|
||||
]
|
||||
end
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ class IMICFPS
|
||||
if @root
|
||||
@root.insert_subtree(bounding_box.dup, object)
|
||||
else
|
||||
@root = AABBNode.new(parent: nil, object: object, bounding_box: BoundingBox.new(0,0,0, 0,0,0))
|
||||
@root = AABBNode.new(parent: nil, object: object, bounding_box: BoundingBox.new)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -48,6 +48,15 @@ class IMICFPS
|
||||
@b = nil
|
||||
end
|
||||
|
||||
def make_leaf
|
||||
@a = nil
|
||||
@b = nil
|
||||
end
|
||||
|
||||
def make_branch(node_a, node_b)
|
||||
|
||||
end
|
||||
|
||||
def insert_subtree(bounding_box, object)
|
||||
# p "#{bounding_box} -> #{object.class}"
|
||||
end
|
||||
|
||||
@@ -35,7 +35,7 @@ class IMICFPS
|
||||
@faces = []
|
||||
@smoothing= 0
|
||||
|
||||
@bounding_box = BoundingBox.new(0,0,0, 0,0,0)
|
||||
@bounding_box = BoundingBox.new
|
||||
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
||||
|
||||
parse
|
||||
|
||||
@@ -10,7 +10,7 @@ class IMICFPS
|
||||
@textures = []
|
||||
@normals = []
|
||||
@faces = []
|
||||
@bounding_box = BoundingBox.new(0,0,0, 0,0,0)
|
||||
@bounding_box = BoundingBox.new
|
||||
@debug_color = Color.new(1.0,1.0,1.0)
|
||||
|
||||
@scale = 1.0
|
||||
|
||||
@@ -151,25 +151,25 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def calculate_bounding_box(vertices, bounding_box)
|
||||
unless bounding_box.min_x.is_a?(Float)
|
||||
unless bounding_box.min.x.is_a?(Float)
|
||||
vertex = vertices.last
|
||||
bounding_box.min_x = vertex.x
|
||||
bounding_box.min_y = vertex.y
|
||||
bounding_box.min_z = vertex.z
|
||||
bounding_box.min.x = vertex.x
|
||||
bounding_box.min.y = vertex.y
|
||||
bounding_box.min.z = vertex.z
|
||||
|
||||
bounding_box.max_x = vertex.x
|
||||
bounding_box.max_y = vertex.y
|
||||
bounding_box.max_z = vertex.z
|
||||
bounding_box.max.x = vertex.x
|
||||
bounding_box.max.y = vertex.y
|
||||
bounding_box.max.z = vertex.z
|
||||
end
|
||||
|
||||
vertices.each do |vertex|
|
||||
bounding_box.min_x = vertex.x if vertex.x <= bounding_box.min_x
|
||||
bounding_box.min_y = vertex.y if vertex.y <= bounding_box.min_y
|
||||
bounding_box.min_z = vertex.z if vertex.z <= bounding_box.min_z
|
||||
bounding_box.min.x = vertex.x if vertex.x <= bounding_box.min.x
|
||||
bounding_box.min.y = vertex.y if vertex.y <= bounding_box.min.y
|
||||
bounding_box.min.z = vertex.z if vertex.z <= bounding_box.min.z
|
||||
|
||||
bounding_box.max_x = vertex.x if vertex.x >= bounding_box.max_x
|
||||
bounding_box.max_y = vertex.y if vertex.y >= bounding_box.max_y
|
||||
bounding_box.max_z = vertex.z if vertex.z >= bounding_box.max_z
|
||||
bounding_box.max.x = vertex.x if vertex.x >= bounding_box.max.x
|
||||
bounding_box.max.y = vertex.y if vertex.y >= bounding_box.max.y
|
||||
bounding_box.max.z = vertex.z if vertex.z >= bounding_box.max.z
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user