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)
-
- (Object) checks
readonly
Returns the value of attribute checks.
-
- (Object) collisions
readonly
Returns the value of attribute collisions.
Instance Method Summary (collapse)
- - (Object) circle_interval(axis, object)
- - (Boolean) collide?(object, other)
- - (Boolean) collide_circle_circle?(object, other)
- - (Boolean) collide_circle_polygon?(object, other) (also: #collide_circle_aabb?)
- - (Boolean) collide_polygon_circle?(object, other) (also: #collide_aabb_circle?)
- - (Boolean) collide_polygon_polygon?(object, other) (also: #collide_aabb_aabb?)
- - (Object) find_collisions
- - (Boolean) interested_in_collision_of?(type1, type2)
- - (Object) on_collision_of(first_objs, second_objs, &block)
- - (Object) polygon_interval(axis, object) (also: #aabb_interval)
-
- (Object) project_and_detect(axis, a, b)
returns true if the projections overlap.
- - (Object) register_collidable(actor)
- - (Object) run_callbacks(collisions)
- - (Object) unregister_collidable(actor)
Instance Attribute Details
- (Object) checks (readonly)
Returns the value of attribute checks
3 4 5 |
# File 'lib/gamebox/core/arbiter.rb', line 3 def checks @checks end |
- (Object) collisions (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
- (Object) circle_interval(axis, object)
179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/gamebox/core/arbiter.rb', line 179 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 |
- (Boolean) collide?(object, other)
97 98 99 100 101 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 |
# File 'lib/gamebox/core/arbiter.rb', line 97 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 |
- (Boolean) collide_circle_circle?(object, other)
129 130 131 132 133 134 135 136 137 |
# File 'lib/gamebox/core/arbiter.rb', line 129 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 |
- (Boolean) collide_circle_polygon?(object, other) Also known as: collide_circle_aabb?
199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/gamebox/core/arbiter.rb', line 199 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 |
- (Boolean) collide_polygon_circle?(object, other) Also known as: collide_aabb_circle?
194 195 196 |
# File 'lib/gamebox/core/arbiter.rb', line 194 def collide_polygon_circle?(object, other) collide_circle_polygon?(other, object) end |
- (Boolean) collide_polygon_polygon?(object, other) Also known as: collide_aabb_aabb?
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/gamebox/core/arbiter.rb', line 142 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 |
- (Object) find_collisions
61 62 63 64 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 |
# File 'lib/gamebox/core/arbiter.rb', line 61 def find_collisions aabb_tree = stagehand(:spatial_tree) 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 |
- (Boolean) interested_in_collision_of?(type1, type2)
55 56 57 58 59 |
# File 'lib/gamebox/core/arbiter.rb', line 55 def interested_in_collision_of?(type1, type2) @collision_handlers ||= {} (@collision_handlers[type1] && @collision_handlers[type1][type2]) || (@collision_handlers[type2] && @collision_handlers[type2][type1]) end |
- (Object) on_collision_of(first_objs, second_objs, &block)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/gamebox/core/arbiter.rb', line 13 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 |
- (Object) polygon_interval(axis, object) Also known as: aabb_interval
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/gamebox/core/arbiter.rb', line 165 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 |
- (Object) project_and_detect(axis, a, b)
returns true if the projections overlap
158 159 160 161 162 163 |
# File 'lib/gamebox/core/arbiter.rb', line 158 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 |
- (Object) register_collidable(actor)
5 6 7 |
# File 'lib/gamebox/core/arbiter.rb', line 5 def register_collidable(actor) stagehand(:spatial_tree).add(actor) end |
- (Object) run_callbacks(collisions)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gamebox/core/arbiter.rb', line 32 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 |
- (Object) unregister_collidable(actor)
9 10 11 |
# File 'lib/gamebox/core/arbiter.rb', line 9 def unregister_collidable(actor) stagehand(:spatial_tree).remove(actor) end |