Collision stuff

This commit is contained in:
2019-02-25 11:33:18 -06:00
parent d5a5ced955
commit 97818c8a33
8 changed files with 32 additions and 9 deletions

View File

@@ -29,6 +29,9 @@ class IMICFPS
# binding.irb
p @aabb_tree
collisions.each do |ent, list|
puts "#{ent.class} -> [#{list.map{|e| e.class}.join(', ')}]"
end
end
def remove(entity)
@@ -40,9 +43,12 @@ class IMICFPS
broadphase = {}
@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)
if search.size > 0
search.reject! {|ent| ent == entity}
search.reject! {|ent| ent == entity || !ent.collidable?}
broadphase[entity] = search
end
end

View File

@@ -5,7 +5,7 @@ class IMICFPS
module EntityManager # Get included into GameState context
def add_entity(entity)
@collision_manager.add(entity) if entity.collidable?
@collision_manager.add(entity)# Add every entity to collision manager
@entities << entity
end

View File

@@ -42,8 +42,14 @@ class IMICFPS
return temp
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
def contains(vector)
def point?(vector)
vector.x.between?(@min.x, @max.x) &&
vector.y.between?(@min.y, @max.y) &&
vector.z.between?(@min.z, @max.z)

View File

@@ -8,6 +8,7 @@ class IMICFPS
def setup
bind_model("base", "biped")
@collision = :dynamic
@speed = 2.5 # meter's per second
@running_speed = 6.8 # meter's per second

View File

@@ -2,6 +2,7 @@ class IMICFPS
class Skydome < Entity
def setup
bind_model("base", "skydome")
@collision = :none
end
def draw

View File

@@ -7,7 +7,7 @@ class IMICFPS
include GLU
include CommonMethods
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
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)
@@ -22,8 +22,8 @@ class IMICFPS
@debug_color = Color.new(0.0, 1.0, 0.0)
@collidable = [:static, :dynamic]
@collision = :static # :dynamic, moves in response, :static, does not move ever, :none, entities can pass through
@physics = false
@collision = :static # :dynamic => moves in response, :static => does not move ever, :none => no collision check, entities can pass through
@physics = false # Entity affected by gravity and what not
@mass = 100 # kg
@delta_time = Gosu.milliseconds

View File

@@ -66,7 +66,7 @@ class IMICFPS
other_child.parent = @parent
return other_child
else
leaf.disown_child(leaf)
leaf.parent.disown_child(leaf)
return self
end
end

View File

@@ -18,6 +18,10 @@ class IMICFPS
leaf = AABBNode.new(parent: nil, object: object, bounding_box: bounding_box.dup)
@objects[object] = leaf
insert_leaf(leaf)
end
def insert_leaf(leaf)
if @root
@root = @root.insert_subtree(leaf)
else
@@ -26,8 +30,10 @@ class IMICFPS
end
def update(object, bounding_box)
remove(object)
insert(object, bounding_box)
leaf = remove(object)
leaf.bounding_box = bounding_box
@objects[object] = leaf
insert_leaf(leaf)
end
# Returns a list of all collided objects inside Bounding Box
@@ -44,6 +50,9 @@ class IMICFPS
def remove(object)
leaf = @objects.delete(object)
@root = @root.remove_subtree(leaf) if leaf
@root.parent = nil
return leaf
end
end
end