mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-15 04:32:35 +00:00
Refactored mesh handling, imported AABB tree implementation from I-MIC FPS
This commit is contained in:
24
Gemfile.lock
Normal file
24
Gemfile.lock
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
PATH
|
||||||
|
remote: .
|
||||||
|
specs:
|
||||||
|
cyberarm_engine (0.24.4)
|
||||||
|
gosu (~> 1.1)
|
||||||
|
|
||||||
|
GEM
|
||||||
|
remote: https://rubygems.org/
|
||||||
|
specs:
|
||||||
|
gosu (1.4.6)
|
||||||
|
minitest (5.25.5)
|
||||||
|
rake (13.2.1)
|
||||||
|
|
||||||
|
PLATFORMS
|
||||||
|
x64-mingw-ucrt
|
||||||
|
|
||||||
|
DEPENDENCIES
|
||||||
|
bundler (~> 2.2)
|
||||||
|
cyberarm_engine!
|
||||||
|
minitest (~> 5.0)
|
||||||
|
rake (~> 13.0)
|
||||||
|
|
||||||
|
BUNDLED WITH
|
||||||
|
2.5.3
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
|
@include "light_struct"
|
||||||
|
|
||||||
out vec4 frag_color;
|
out vec4 frag_color;
|
||||||
|
|
||||||
@include "light_struct"
|
|
||||||
const int DIRECTIONAL = 0;
|
const int DIRECTIONAL = 0;
|
||||||
const int POINT = 1;
|
const int POINT = 1;
|
||||||
const int SPOT = 2;
|
const int SPOT = 2;
|
||||||
|
|
||||||
flat in Light out_lights[7];
|
|
||||||
in vec2 out_tex_coords;
|
in vec2 out_tex_coords;
|
||||||
flat in int out_light_count;
|
flat in int out_light_count;
|
||||||
|
flat in Light out_lights[7];
|
||||||
|
|
||||||
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||||
|
|
||||||
@@ -27,43 +28,88 @@ vec4 directionalLight(Light light) {
|
|||||||
return vec4(_diffuse + _ambient + _specular, 1.0);
|
return vec4(_diffuse + _ambient + _specular, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
vec4 pointLight(Light light) {
|
|
||||||
return vec4(0.25, 0.25, 0.25, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
vec4 spotLight(Light light) {
|
|
||||||
return vec4(0.5, 0.5, 0.5, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
vec4 calculateLighting(Light light) {
|
|
||||||
vec4 result;
|
|
||||||
|
|
||||||
// switch(light.type) {
|
|
||||||
// case DIRECTIONAL: {
|
|
||||||
// result = directionalLight(light);
|
|
||||||
// }
|
|
||||||
// case SPOT: {
|
|
||||||
// result = spotLight(light);
|
|
||||||
// }
|
|
||||||
// default: {
|
|
||||||
// result = pointLight(light);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (light.type == DIRECTIONAL) {
|
|
||||||
result = directionalLight(light);
|
|
||||||
} else {
|
|
||||||
result = pointLight(light);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
frag_color = vec4(0.0);
|
Light light;
|
||||||
|
light.type = DIRECTIONAL;
|
||||||
|
|
||||||
for(int i = 0; i < out_light_count; i++)
|
light.position = vec3(100, 100, 100);
|
||||||
{
|
|
||||||
frag_color += texture(diffuse, out_tex_coords) * calculateLighting(out_lights[i]);
|
light.diffuse = vec3(0.5, 0.5, 0.5);
|
||||||
}
|
light.ambient = vec3(0.8, 0.8, 0.8);
|
||||||
|
light.specular = vec3(0.2, 0.2, 0.2);
|
||||||
|
|
||||||
|
light.intensity = 1.0;
|
||||||
|
|
||||||
|
frag_color = texture(diffuse, out_tex_coords) * directionalLight(light);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #version 330 core
|
||||||
|
// @include "light_struct"
|
||||||
|
|
||||||
|
// out vec4 frag_color;
|
||||||
|
|
||||||
|
// const int DIRECTIONAL = 0;
|
||||||
|
// const int POINT = 1;
|
||||||
|
// const int SPOT = 2;
|
||||||
|
|
||||||
|
// in vec2 out_tex_coords;
|
||||||
|
// flat in int out_light_count;
|
||||||
|
// flat in Light out_lights[7];
|
||||||
|
|
||||||
|
// uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||||
|
|
||||||
|
// vec4 directionalLight(Light light) {
|
||||||
|
// vec3 norm = normalize(texture(normal, out_tex_coords).rgb);
|
||||||
|
// vec3 diffuse_color = texture(diffuse, out_tex_coords).rgb;
|
||||||
|
// vec3 frag_pos = texture(position, out_tex_coords).rgb;
|
||||||
|
|
||||||
|
// vec3 lightDir = normalize(light.position - frag_pos);
|
||||||
|
// float diff = max(dot(norm, lightDir), 0);
|
||||||
|
|
||||||
|
// vec3 _ambient = light.ambient;
|
||||||
|
// vec3 _diffuse = light.diffuse * diff;
|
||||||
|
// vec3 _specular = light.specular;
|
||||||
|
|
||||||
|
// return vec4(_diffuse + _ambient + _specular, 1.0);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// vec4 pointLight(Light light) {
|
||||||
|
// return vec4(0.25, 0.25, 0.25, 1);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// vec4 spotLight(Light light) {
|
||||||
|
// return vec4(0.5, 0.5, 0.5, 1);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// vec4 calculateLighting(Light light) {
|
||||||
|
// vec4 result;
|
||||||
|
|
||||||
|
// // switch(light.type) {
|
||||||
|
// // case DIRECTIONAL: {
|
||||||
|
// // result = directionalLight(light);
|
||||||
|
// // }
|
||||||
|
// // case SPOT: {
|
||||||
|
// // result = spotLight(light);
|
||||||
|
// // }
|
||||||
|
// // default: {
|
||||||
|
// // result = pointLight(light);
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// if (light.type == DIRECTIONAL) {
|
||||||
|
// result = directionalLight(light);
|
||||||
|
// } else {
|
||||||
|
// result = pointLight(light);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// void main() {
|
||||||
|
// frag_color = vec4(0.0);
|
||||||
|
|
||||||
|
// for(int i = 0; i < out_light_count; i++)
|
||||||
|
// {
|
||||||
|
// frag_color += texture(diffuse, out_tex_coords) * calculateLighting(out_lights[i]);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# version 330 core
|
#version 330 core
|
||||||
|
|
||||||
layout(location = 0) in vec3 in_position;
|
layout(location = 0) in vec3 in_position;
|
||||||
layout(location = 1) in vec3 in_color;
|
layout(location = 1) in vec3 in_color;
|
||||||
|
|||||||
@@ -67,12 +67,4 @@ require_relative "cyberarm_engine/ui/elements/menu_item"
|
|||||||
require_relative "cyberarm_engine/game_state"
|
require_relative "cyberarm_engine/game_state"
|
||||||
require_relative "cyberarm_engine/ui/gui_state"
|
require_relative "cyberarm_engine/ui/gui_state"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/model"
|
|
||||||
require_relative "cyberarm_engine/model_cache"
|
|
||||||
require_relative "cyberarm_engine/model/material"
|
|
||||||
require_relative "cyberarm_engine/model/model_object"
|
|
||||||
require_relative "cyberarm_engine/model/parser"
|
|
||||||
require_relative "cyberarm_engine/model/parsers/wavefront_parser"
|
|
||||||
require_relative "cyberarm_engine/model/parsers/collada_parser" if RUBY_ENGINE != "mruby" && defined?(Nokogiri)
|
|
||||||
|
|
||||||
require_relative "cyberarm_engine/builtin/intro_state"
|
require_relative "cyberarm_engine/builtin/intro_state"
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Model
|
class Model
|
||||||
|
include OpenGL
|
||||||
|
include CyberarmEngine
|
||||||
|
|
||||||
attr_accessor :objects, :materials, :vertices, :uvs, :texures, :normals, :faces, :colors, :bones, :material_file,
|
attr_accessor :objects, :materials, :vertices, :uvs, :texures, :normals, :faces, :colors, :bones, :material_file,
|
||||||
:current_material, :current_object, :vertex_count, :smoothing
|
:current_material, :current_object, :vertex_count, :smoothing
|
||||||
attr_reader :position, :bounding_box, :textured_material, :file_path, :positions_buffer_id, :colors_buffer_id,
|
attr_reader :position, :bounding_box, :textured_material, :file_path, :positions_buffer_id, :colors_buffer_id,
|
||||||
@@ -49,9 +52,9 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@objects.each { |o| @vertex_count += o.vertices.size }
|
@objects.each { |o| @vertex_count += o.vertices.size }
|
||||||
|
|
||||||
# start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
||||||
# build_collision_tree
|
build_collision_tree
|
||||||
# puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start_time) / 1000.0).round(2)} seconds"
|
puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start_time) / 1000.0).round(2)} seconds"
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(parser)
|
def parse(parser)
|
||||||
@@ -178,7 +181,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def build_collision_tree
|
def build_collision_tree
|
||||||
@aabb_tree = IMICFPS::AABBTree.new
|
@aabb_tree = AABBTree.new
|
||||||
|
|
||||||
@faces.each do |face|
|
@faces.each do |face|
|
||||||
box = BoundingBox.new
|
box = BoundingBox.new
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Model
|
class Model
|
||||||
class ModelObject
|
class Mesh
|
||||||
attr_reader :id, :name, :vertices, :uvs, :normals, :materials, :bounding_box, :debug_color
|
attr_reader :id, :name, :vertices, :uvs, :normals, :materials, :bounding_box, :debug_color
|
||||||
attr_accessor :faces, :scale
|
attr_accessor :faces, :scale
|
||||||
|
|
||||||
@@ -48,12 +48,12 @@ module CyberarmEngine
|
|||||||
if _model
|
if _model
|
||||||
@model.current_object = _model
|
@model.current_object = _model
|
||||||
else
|
else
|
||||||
raise "Couldn't find ModelObject!"
|
raise "Couldn't find Mesh!"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def change_object(id, name)
|
def change_object(id, name)
|
||||||
@model.objects << Model::ModelObject.new(id, name)
|
@model.objects << Model::Mesh.new(id, name)
|
||||||
@model.current_object = @model.objects.last
|
@model.current_object = @model.objects.last
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
begin
|
begin
|
||||||
require "opengl"
|
require "opengl"
|
||||||
|
require "glu"
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
puts "Required gem is not installed, please install 'opengl-bindings' and try again."
|
puts "Required gem is not installed, please install 'opengl-bindings' and try again."
|
||||||
exit(1)
|
exit(1)
|
||||||
@@ -8,7 +9,7 @@ end
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
def gl_error?
|
def gl_error?
|
||||||
e = glGetError
|
e = glGetError
|
||||||
if e != GL_NO_ERROR
|
if e != OpenGL::GL_NO_ERROR
|
||||||
warn "OpenGL error detected by handler at: #{caller[0]}"
|
warn "OpenGL error detected by handler at: #{caller[0]}"
|
||||||
warn " #{gluErrorString(e)} (#{e})\n"
|
warn " #{gluErrorString(e)} (#{e})\n"
|
||||||
exit if Window.instance&.exit_on_opengl_error?
|
exit if Window.instance&.exit_on_opengl_error?
|
||||||
@@ -38,3 +39,15 @@ require_relative "opengl/renderer/g_buffer"
|
|||||||
require_relative "opengl/renderer/bounding_box_renderer"
|
require_relative "opengl/renderer/bounding_box_renderer"
|
||||||
require_relative "opengl/renderer/opengl_renderer"
|
require_relative "opengl/renderer/opengl_renderer"
|
||||||
require_relative "opengl/renderer/renderer"
|
require_relative "opengl/renderer/renderer"
|
||||||
|
|
||||||
|
require_relative "trees/aabb_tree_debug"
|
||||||
|
require_relative "trees/aabb_node"
|
||||||
|
require_relative "trees/aabb_tree"
|
||||||
|
|
||||||
|
require_relative "model"
|
||||||
|
require_relative "model_cache"
|
||||||
|
require_relative "model/material"
|
||||||
|
require_relative "model/mesh"
|
||||||
|
require_relative "model/parser"
|
||||||
|
require_relative "model/parsers/wavefront_parser"
|
||||||
|
require_relative "model/parsers/collada_parser" if RUBY_ENGINE != "mruby" && defined?(Nokogiri)
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Light
|
class Light
|
||||||
|
include OpenGL
|
||||||
|
|
||||||
DIRECTIONAL = 0
|
DIRECTIONAL = 0
|
||||||
POINT = 1
|
POINT = 1
|
||||||
SPOT = 2
|
SPOT = 2
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class PerspectiveCamera
|
class PerspectiveCamera
|
||||||
|
include OpenGL
|
||||||
|
include GLU
|
||||||
|
|
||||||
attr_accessor :position, :orientation, :aspect_ratio, :field_of_view,
|
attr_accessor :position, :orientation, :aspect_ratio, :field_of_view,
|
||||||
:min_view_distance, :max_view_distance
|
:min_view_distance, :max_view_distance
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class GBuffer
|
class GBuffer
|
||||||
|
include OpenGL
|
||||||
|
|
||||||
attr_reader :screen_vbo, :vertices, :uvs
|
attr_reader :screen_vbo, :vertices, :uvs
|
||||||
attr_reader :width, :height
|
attr_reader :width, :height
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class OpenGLRenderer
|
class OpenGLRenderer
|
||||||
|
include OpenGL
|
||||||
|
include CyberarmEngine
|
||||||
|
|
||||||
@@immediate_mode_warning = false
|
@@immediate_mode_warning = false
|
||||||
|
|
||||||
attr_accessor :show_wireframe
|
attr_accessor :show_wireframe
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ module CyberarmEngine
|
|||||||
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
|
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
|
||||||
class Shader
|
class Shader
|
||||||
include OpenGL
|
include OpenGL
|
||||||
|
|
||||||
@@shaders = {} # Cache for {Shader} instances
|
@@shaders = {} # Cache for {Shader} instances
|
||||||
PREPROCESSOR_CHARACTER = "@".freeze # magic character for preprocessor phase of {Shader} compilation
|
PREPROCESSOR_CHARACTER = "@".freeze # magic character for preprocessor phase of {Shader} compilation
|
||||||
|
|
||||||
@@ -298,6 +299,7 @@ module CyberarmEngine
|
|||||||
# @see Shader.use Shader.use
|
# @see Shader.use Shader.use
|
||||||
def use(&block)
|
def use(&block)
|
||||||
return unless compiled?
|
return unless compiled?
|
||||||
|
|
||||||
raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader
|
raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader
|
||||||
|
|
||||||
Shader.active_shader = self
|
Shader.active_shader = self
|
||||||
|
|||||||
126
lib/cyberarm_engine/trees/aabb_node.rb
Normal file
126
lib/cyberarm_engine/trees/aabb_node.rb
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module CyberarmEngine
|
||||||
|
class AABBTree
|
||||||
|
class AABBNode
|
||||||
|
attr_accessor :bounding_box, :parent, :object
|
||||||
|
attr_reader :a, :b
|
||||||
|
|
||||||
|
def initialize(parent:, object:, bounding_box:)
|
||||||
|
@parent = parent
|
||||||
|
@object = object
|
||||||
|
@bounding_box = bounding_box
|
||||||
|
|
||||||
|
@a = nil
|
||||||
|
@b = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def a=(leaf)
|
||||||
|
@a = leaf
|
||||||
|
@a.parent = self
|
||||||
|
end
|
||||||
|
|
||||||
|
def b=(leaf)
|
||||||
|
@b = leaf
|
||||||
|
@b.parent = self
|
||||||
|
end
|
||||||
|
|
||||||
|
def leaf?
|
||||||
|
@object
|
||||||
|
end
|
||||||
|
|
||||||
|
def insert_subtree(leaf)
|
||||||
|
if leaf?
|
||||||
|
new_node = AABBNode.new(parent: nil, object: nil, bounding_box: @bounding_box.union(leaf.bounding_box))
|
||||||
|
|
||||||
|
new_node.a = self
|
||||||
|
new_node.b = leaf
|
||||||
|
|
||||||
|
new_node
|
||||||
|
else
|
||||||
|
cost_a = @a.bounding_box.volume + @b.bounding_box.union(leaf.bounding_box).volume
|
||||||
|
cost_b = @b.bounding_box.volume + @a.bounding_box.union(leaf.bounding_box).volume
|
||||||
|
|
||||||
|
if cost_a == cost_b
|
||||||
|
cost_a = @a.proximity(leaf)
|
||||||
|
cost_b = @b.proximity(leaf)
|
||||||
|
end
|
||||||
|
|
||||||
|
if cost_b < cost_a
|
||||||
|
self.b = @b.insert_subtree(leaf)
|
||||||
|
else
|
||||||
|
self.a = @a.insert_subtree(leaf)
|
||||||
|
end
|
||||||
|
|
||||||
|
@bounding_box = @bounding_box.union(leaf.bounding_box)
|
||||||
|
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def search_subtree(collider, items = [])
|
||||||
|
if @bounding_box.intersect?(collider)
|
||||||
|
if leaf?
|
||||||
|
items << self
|
||||||
|
else
|
||||||
|
@a.search_subtree(collider, items)
|
||||||
|
@b.search_subtree(collider, items)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
items
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_subtree(leaf)
|
||||||
|
if leaf
|
||||||
|
self
|
||||||
|
elsif leaf.parent == self
|
||||||
|
other_child = other(leaf)
|
||||||
|
other_child.parent = @parent
|
||||||
|
other_child
|
||||||
|
else
|
||||||
|
leaf.parent.disown_child(leaf)
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def other(leaf)
|
||||||
|
@a == leaf ? @b : @a
|
||||||
|
end
|
||||||
|
|
||||||
|
def disown_child(leaf)
|
||||||
|
value = other(leaf)
|
||||||
|
raise "Can not replace child of a leaf!" if @parent.leaf?
|
||||||
|
raise "Node is not a child of parent!" unless leaf.child_of?(@parent)
|
||||||
|
|
||||||
|
if @parent.a == self
|
||||||
|
@parent.a = value
|
||||||
|
else
|
||||||
|
@parent.b = value
|
||||||
|
end
|
||||||
|
|
||||||
|
@parent.update_bounding_box
|
||||||
|
end
|
||||||
|
|
||||||
|
def child_of?(leaf)
|
||||||
|
self == leaf.a || self == leaf.b
|
||||||
|
end
|
||||||
|
|
||||||
|
def proximity(leaf)
|
||||||
|
(@bounding_box - leaf.bounding_box).sum.abs
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_bounding_box
|
||||||
|
node = self
|
||||||
|
|
||||||
|
unless node.leaf?
|
||||||
|
node.bounding_box = node.a.bounding_box.union(node.b.bounding_box)
|
||||||
|
|
||||||
|
while (node = node.parent)
|
||||||
|
node.bounding_box = node.a.bounding_box.union(node.b.bounding_box)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
55
lib/cyberarm_engine/trees/aabb_tree.rb
Normal file
55
lib/cyberarm_engine/trees/aabb_tree.rb
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module CyberarmEngine
|
||||||
|
class AABBTree
|
||||||
|
include AABBTreeDebug
|
||||||
|
|
||||||
|
attr_reader :root, :objects, :branches, :leaves
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@objects = {}
|
||||||
|
@root = nil
|
||||||
|
@branches = 0
|
||||||
|
@leaves = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
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] # FIXME
|
||||||
|
|
||||||
|
leaf = AABBNode.new(parent: nil, object: object, bounding_box: bounding_box.dup)
|
||||||
|
@objects[object] = leaf
|
||||||
|
|
||||||
|
insert_leaf(leaf)
|
||||||
|
end
|
||||||
|
|
||||||
|
def insert_leaf(leaf)
|
||||||
|
@root = @root ? @root.insert_subtree(leaf) : leaf
|
||||||
|
end
|
||||||
|
|
||||||
|
def update(object, bounding_box)
|
||||||
|
leaf = remove(object)
|
||||||
|
leaf.bounding_box = bounding_box
|
||||||
|
insert_leaf(leaf)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a list of all objects that collided with collider
|
||||||
|
def search(collider, return_nodes = false)
|
||||||
|
items = []
|
||||||
|
if @root
|
||||||
|
items = @root.search_subtree(collider)
|
||||||
|
items.map!(&:object) unless return_nodes
|
||||||
|
end
|
||||||
|
|
||||||
|
items
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove(object)
|
||||||
|
leaf = @objects.delete(object)
|
||||||
|
@root = @root.remove_subtree(leaf) if leaf
|
||||||
|
|
||||||
|
leaf
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
29
lib/cyberarm_engine/trees/aabb_tree_debug.rb
Normal file
29
lib/cyberarm_engine/trees/aabb_tree_debug.rb
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module CyberarmEngine
|
||||||
|
# Gets included into AABBTree
|
||||||
|
module AABBTreeDebug
|
||||||
|
def inspect
|
||||||
|
@branches = 0
|
||||||
|
@leaves = 0
|
||||||
|
if @root
|
||||||
|
node = @root
|
||||||
|
|
||||||
|
debug_search(node.a)
|
||||||
|
debug_search(node.b)
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "<#{self.class}:#{object_id}> has #{@branches} branches and #{@leaves} leaves"
|
||||||
|
end
|
||||||
|
|
||||||
|
def debug_search(node)
|
||||||
|
if node.leaf?
|
||||||
|
@leaves += 1
|
||||||
|
else
|
||||||
|
@branches += 1
|
||||||
|
debug_search(node.a)
|
||||||
|
debug_search(node.b)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user