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