mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
Collision stuff
This commit is contained in:
@@ -29,6 +29,9 @@ class IMICFPS
|
|||||||
|
|
||||||
# binding.irb
|
# binding.irb
|
||||||
p @aabb_tree
|
p @aabb_tree
|
||||||
|
collisions.each do |ent, list|
|
||||||
|
puts "#{ent.class} -> [#{list.map{|e| e.class}.join(', ')}]"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove(entity)
|
def remove(entity)
|
||||||
@@ -40,9 +43,12 @@ class IMICFPS
|
|||||||
broadphase = {}
|
broadphase = {}
|
||||||
|
|
||||||
@game_state.entities.each do |entity|
|
@game_state.entities.each do |entity|
|
||||||
|
next unless entity.collidable?
|
||||||
|
next if entity.collision == :static # Only dynamic entities can be resolved
|
||||||
|
|
||||||
search = @aabb_tree.search(entity.bounding_box)
|
search = @aabb_tree.search(entity.bounding_box)
|
||||||
if search.size > 0
|
if search.size > 0
|
||||||
search.reject! {|ent| ent == entity}
|
search.reject! {|ent| ent == entity || !ent.collidable?}
|
||||||
broadphase[entity] = search
|
broadphase[entity] = search
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ class IMICFPS
|
|||||||
|
|
||||||
module EntityManager # Get included into GameState context
|
module EntityManager # Get included into GameState context
|
||||||
def add_entity(entity)
|
def add_entity(entity)
|
||||||
@collision_manager.add(entity) if entity.collidable?
|
@collision_manager.add(entity)# Add every entity to collision manager
|
||||||
@entities << entity
|
@entities << entity
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -42,8 +42,14 @@ class IMICFPS
|
|||||||
return temp
|
return temp
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# does this bounding box envelop other bounding box? (inclusive of border)
|
||||||
|
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
|
||||||
|
|
||||||
# returns whether the vector is inside of the bounding box
|
# returns whether the vector is inside of the bounding box
|
||||||
def contains(vector)
|
def point?(vector)
|
||||||
vector.x.between?(@min.x, @max.x) &&
|
vector.x.between?(@min.x, @max.x) &&
|
||||||
vector.y.between?(@min.y, @max.y) &&
|
vector.y.between?(@min.y, @max.y) &&
|
||||||
vector.z.between?(@min.z, @max.z)
|
vector.z.between?(@min.z, @max.z)
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ class IMICFPS
|
|||||||
|
|
||||||
def setup
|
def setup
|
||||||
bind_model("base", "biped")
|
bind_model("base", "biped")
|
||||||
|
@collision = :dynamic
|
||||||
|
|
||||||
@speed = 2.5 # meter's per second
|
@speed = 2.5 # meter's per second
|
||||||
@running_speed = 6.8 # meter's per second
|
@running_speed = 6.8 # meter's per second
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ class IMICFPS
|
|||||||
class Skydome < Entity
|
class Skydome < Entity
|
||||||
def setup
|
def setup
|
||||||
bind_model("base", "skydome")
|
bind_model("base", "skydome")
|
||||||
|
@collision = :none
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class IMICFPS
|
|||||||
include GLU
|
include GLU
|
||||||
include CommonMethods
|
include CommonMethods
|
||||||
attr_accessor :scale, :visible, :renderable, :backface_culling
|
attr_accessor :scale, :visible, :renderable, :backface_culling
|
||||||
attr_reader :position, :rotation, :velocity
|
attr_reader :position, :rotation, :velocity, :collision
|
||||||
attr_reader :model, :name, :debug_color, :bounding_box
|
attr_reader :model, :name, :debug_color, :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)
|
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)
|
@position = Vector.new(x, y, z)
|
||||||
@@ -22,8 +22,8 @@ class IMICFPS
|
|||||||
@debug_color = Color.new(0.0, 1.0, 0.0)
|
@debug_color = Color.new(0.0, 1.0, 0.0)
|
||||||
|
|
||||||
@collidable = [:static, :dynamic]
|
@collidable = [:static, :dynamic]
|
||||||
@collision = :static # :dynamic, moves in response, :static, does not move ever, :none, entities can pass through
|
@collision = :static # :dynamic => moves in response, :static => does not move ever, :none => no collision check, entities can pass through
|
||||||
@physics = false
|
@physics = false # Entity affected by gravity and what not
|
||||||
@mass = 100 # kg
|
@mass = 100 # kg
|
||||||
|
|
||||||
@delta_time = Gosu.milliseconds
|
@delta_time = Gosu.milliseconds
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ class IMICFPS
|
|||||||
other_child.parent = @parent
|
other_child.parent = @parent
|
||||||
return other_child
|
return other_child
|
||||||
else
|
else
|
||||||
leaf.disown_child(leaf)
|
leaf.parent.disown_child(leaf)
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,6 +18,10 @@ class IMICFPS
|
|||||||
leaf = AABBNode.new(parent: nil, object: object, bounding_box: bounding_box.dup)
|
leaf = AABBNode.new(parent: nil, object: object, bounding_box: bounding_box.dup)
|
||||||
@objects[object] = leaf
|
@objects[object] = leaf
|
||||||
|
|
||||||
|
insert_leaf(leaf)
|
||||||
|
end
|
||||||
|
|
||||||
|
def insert_leaf(leaf)
|
||||||
if @root
|
if @root
|
||||||
@root = @root.insert_subtree(leaf)
|
@root = @root.insert_subtree(leaf)
|
||||||
else
|
else
|
||||||
@@ -26,8 +30,10 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update(object, bounding_box)
|
def update(object, bounding_box)
|
||||||
remove(object)
|
leaf = remove(object)
|
||||||
insert(object, bounding_box)
|
leaf.bounding_box = bounding_box
|
||||||
|
@objects[object] = leaf
|
||||||
|
insert_leaf(leaf)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a list of all collided objects inside Bounding Box
|
# Returns a list of all collided objects inside Bounding Box
|
||||||
@@ -44,6 +50,9 @@ class IMICFPS
|
|||||||
def remove(object)
|
def remove(object)
|
||||||
leaf = @objects.delete(object)
|
leaf = @objects.delete(object)
|
||||||
@root = @root.remove_subtree(leaf) if leaf
|
@root = @root.remove_subtree(leaf) if leaf
|
||||||
|
@root.parent = nil
|
||||||
|
|
||||||
|
return leaf
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user