Module: Arbiter
- Included in:
- Stage
- Defined in:
- lib/gamebox/core/arbiter.rb
Overview
this module gets mixed into a stage to allow it to handle collision detection
Instance Attribute Summary collapse
-
#checks ⇒ Object
readonly
Returns the value of attribute checks.
-
#collisions ⇒ Object
readonly
Returns the value of attribute collisions.
Instance Method Summary collapse
- #aabb_tree ⇒ Object
- #circle_interval(axis, object) ⇒ Object
-
#collide?(object, other) ⇒ Boolean
This reads terribly and violates open-closed principle, but does perform better than any dynamic lookup based on shape_types.
- #collide_circle_circle?(object, other) ⇒ Boolean
- #collide_circle_polygon?(object, other) ⇒ Boolean (also: #collide_circle_aabb?)
- #collide_polygon_circle?(object, other) ⇒ Boolean (also: #collide_aabb_circle?)
- #collide_polygon_polygon?(object, other) ⇒ Boolean (also: #collide_aabb_aabb?)
- #find_collisions ⇒ Object
- #interested_in_collision_of?(type1, type2) ⇒ Boolean
- #on_collision_of(first_objs, second_objs, &block) ⇒ Object
- #polygon_interval(axis, object) ⇒ Object (also: #aabb_interval)
-
#project_and_detect(axis, a, b) ⇒ Object
returns true if the projections overlap.
- #register_collidable(actor) ⇒ Object
- #run_callbacks(collisions) ⇒ Object
- #unregister_collidable(actor) ⇒ Object
Instance Attribute Details
#checks ⇒ Object (readonly)
Returns the value of attribute checks.
3 4 5 |
# File 'lib/gamebox/core/arbiter.rb', line 3 def checks @checks end |
#collisions ⇒ Object (readonly)
Returns the value of attribute collisions.
3 4 5 |
# File 'lib/gamebox/core/arbiter.rb', line 3 def collisions @collisions end |
Instance Method Details
#aabb_tree ⇒ Object
13 14 15 |
# File 'lib/gamebox/core/arbiter.rb', line 13 def aabb_tree @aabb_tree ||= SpatialTree.new(self) end |
#circle_interval(axis, object) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/gamebox/core/arbiter.rb', line 184 def circle_interval(axis, object) axis_x = axis[0] axis_y = axis[1] obj_x = object.center_x obj_y = object.center_y length = Math.sqrt(axis_x * axis_x + axis_y * axis_y) cn = axis_x*obj_x + axis_y*obj_y rlength = object.radius*length min = cn - rlength max = cn + rlength [min,max] end |
#collide?(object, other) ⇒ Boolean
This reads terribly and violates open-closed principle, but does perform better than any dynamic lookup based on shape_types
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/gamebox/core/arbiter.rb', line 102 def collide?(object, other) case object.shape_type when :circle case other.shape_type when :circle collide_circle_circle? object, other when :aabb collide_circle_aabb? object, other when :polygon collide_circle_polygon? object, other end when :aabb case other.shape_type when :circle collide_aabb_circle? object, other when :aabb collide_aabb_aabb? object, other when :polygon collide_aabb_polygon? object, other end when :polygon case other.shape_type when :circle collide_polygon_circle? object, other when :aabb collide_polygon_aabb? object, other when :polygon collide_polygon_polygon? object, other end end end |
#collide_circle_circle?(object, other) ⇒ Boolean
134 135 136 137 138 139 140 141 142 |
# File 'lib/gamebox/core/arbiter.rb', line 134 def collide_circle_circle?(object, other) x_delta = other.center_x - object.center_x x_dist = x_delta * x_delta y_delta = other.center_y - object.center_y y_dist = y_delta * y_delta total_radius = object.radius + other.radius (x_dist + y_dist) <= (total_radius * total_radius) end |
#collide_circle_polygon?(object, other) ⇒ Boolean Also known as: collide_circle_aabb?
204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/gamebox/core/arbiter.rb', line 204 def collide_circle_polygon?(object, other) if collide_circle_circle? object, other potential_sep_axis = other.cw_world_edge_normals potential_sep_axis.each do |axis| return false unless project_and_detect(axis, object, other) end true else false end end |
#collide_polygon_circle?(object, other) ⇒ Boolean Also known as: collide_aabb_circle?
199 200 201 |
# File 'lib/gamebox/core/arbiter.rb', line 199 def collide_polygon_circle?(object, other) collide_circle_polygon?(other, object) end |
#collide_polygon_polygon?(object, other) ⇒ Boolean Also known as: collide_aabb_aabb?
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/gamebox/core/arbiter.rb', line 147 def collide_polygon_polygon?(object, other) if collide_circle_circle? object, other # collect vector's perp potential_sep_axis = (object.cw_world_edge_normals | other.cw_world_edge_normals).uniq potential_sep_axis.each do |axis| return false unless project_and_detect(axis, object, other) end else return false end true end |
#find_collisions ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/gamebox/core/arbiter.rb', line 65 def find_collisions collidable_actors = aabb_tree.moved_items.values collisions = {} collidable_actors.each do |first| # if first.is? :collidable # HUH? it appears that querying modifies the tree somehow? aabb_tree.collisions(first) do |second| # if second.is? :collidable if first != second && interested_in_collision_of?(first.actor_type, second.actor_type) && collide?(first, second) if !collisions[second] || (collisions[second] && !collisions[second].include?(first)) collisions[first] ||= [] collisions[first] << second end # end # end end end end unique_collisions = [] collisions.each do |first,seconds| seconds.each do |second| unique_collisions << [first,second] end end run_callbacks unique_collisions aabb_tree.reset end |
#interested_in_collision_of?(type1, type2) ⇒ Boolean
59 60 61 62 63 |
# File 'lib/gamebox/core/arbiter.rb', line 59 def interested_in_collision_of?(type1, type2) @collision_handlers ||= {} (@collision_handlers[type1] && @collision_handlers[type1][type2]) || (@collision_handlers[type2] && @collision_handlers[type2][type1]) end |
#on_collision_of(first_objs, second_objs, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/gamebox/core/arbiter.rb', line 17 def on_collision_of(first_objs, second_objs, &block) first_objs = [first_objs].flatten second_objs = [second_objs].flatten @collision_handlers ||= {} first_objs.each do |fobj| second_objs.each do |sobj| if fobj < sobj @collision_handlers[fobj] ||= {} @collision_handlers[fobj][sobj] = [false,block] else @collision_handlers[sobj] ||= {} @collision_handlers[sobj][fobj] = [true,block] end end end end |
#polygon_interval(axis, object) ⇒ Object Also known as: aabb_interval
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/gamebox/core/arbiter.rb', line 170 def polygon_interval(axis, object) min = max = nil object.cw_world_points.each do |edge| # vector dot product d = edge[0] * axis[0] + edge[1] * axis[1] min ||= d max ||= d min = d if d < min max = d if d > max end [min,max] end |
#project_and_detect(axis, a, b) ⇒ Object
returns true if the projections overlap
163 164 165 166 167 168 |
# File 'lib/gamebox/core/arbiter.rb', line 163 def project_and_detect(axis, a, b) a_min, a_max = send("#{a.shape_type}_interval", axis, a) b_min, b_max = send("#{b.shape_type}_interval", axis, b) a_min <= b_max && b_min <= a_max end |
#register_collidable(actor) ⇒ Object
5 6 7 |
# File 'lib/gamebox/core/arbiter.rb', line 5 def register_collidable(actor) aabb_tree.add(actor) end |
#run_callbacks(collisions) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/gamebox/core/arbiter.rb', line 36 def run_callbacks(collisions) @collision_handlers ||= {} collisions.each do |collision| first = collision.first second = collision.last unless first.actor_type < second.actor_type tmp = first first = second second = tmp end colliders = @collision_handlers[first.actor_type] swapped, callback = colliders[second.actor_type] unless colliders.nil? unless callback.nil? if swapped callback.call second, first else callback.call first, second end end end end |
#unregister_collidable(actor) ⇒ Object
9 10 11 |
# File 'lib/gamebox/core/arbiter.rb', line 9 def unregister_collidable(actor) aabb_tree.remove(actor) end |