mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 23:52:35 +00:00
Partially working AABB Tree! (needs much more work)
This commit is contained in:
@@ -5,67 +5,51 @@ class IMICFPS
|
||||
@root = nil
|
||||
end
|
||||
|
||||
def add(bounding_box, object)
|
||||
def insert(object, bounding_box)
|
||||
raise "BoundingBox can't be nil!" unless bounding_box
|
||||
raise "Object can't be nil!" unless object
|
||||
raise "Object already in tree!" if @objects[object]
|
||||
|
||||
leaf = AABBNode.new(parent: nil, object: object, bounding_box: bounding_box.dup)
|
||||
@objects[object] = leaf
|
||||
|
||||
if @root
|
||||
@root.insert_subtree(bounding_box.dup, object)
|
||||
@root.insert_subtree(leaf)
|
||||
else
|
||||
@root = AABBNode.new(parent: nil, object: object, bounding_box: BoundingBox.new)
|
||||
@root = leaf
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
needs_update = []
|
||||
|
||||
@objects.each do |object, node|
|
||||
unless object.bounding_box == node.bounding_box
|
||||
puts "#{object.class} mutated!"
|
||||
remove(node)
|
||||
add(object)
|
||||
next unless object.is_a?(Entity)
|
||||
unless object.normalized_bounding_box == node.bounding_box
|
||||
needs_update << object
|
||||
end
|
||||
end
|
||||
|
||||
needs_update.each do |object|
|
||||
remove(object)
|
||||
insert(object, object.normalized_bounding_box)
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a list of all collided objects inside Bounding Box
|
||||
def search(bounding_box)
|
||||
items = []
|
||||
@root.search_subtree(bounding_box)
|
||||
if @root
|
||||
items = @root.search_subtree(bounding_box)
|
||||
end
|
||||
|
||||
items.map! {|e| e.object}
|
||||
return items
|
||||
end
|
||||
|
||||
def remove(object)
|
||||
@root.remove_subtree(@objects[object])
|
||||
@objects[object] = nil
|
||||
end
|
||||
|
||||
class AABBNode
|
||||
attr_accessor :bounding_box, :parent, :object, :a, :b
|
||||
def initialize(parent:, object:, bounding_box:)
|
||||
@parent = parent
|
||||
@object = object
|
||||
@bounding_box = bounding_box
|
||||
|
||||
@a = nil
|
||||
@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
|
||||
|
||||
def remove_subtree(node)
|
||||
end
|
||||
|
||||
def search_subtree(bounding_box)
|
||||
end
|
||||
leaf = @objects.delete(object)
|
||||
@root.remove_subtree(leaf) if leaf
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user