Module: Chingu::Traits::CollisionDetection
- Defined in:
- lib/chingu/traits/collision_detection.rb
Overview
Research: 1) QuadTrees: lab.polygonal.de/2007/09/09/quadtree-demonstration/ 2) Sweep and Prune
SEE: www.shmup-dev.com/forum/index.php?board=65.0
Makes use of 2 attributes
bounding_box - a Rect-instance, uses in bounding_box collisions
radius - a number
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#bounding_box_bounding_circle_collision?(object2) ⇒ Boolean
BoundingBox vs Radius collision.
-
#bounding_box_collision?(object2) ⇒ Boolean
Collide self with a given game object by checking both objects bounding_box’es Returns true if colliding.
-
#bounding_circle_collision?(object2) ⇒ Boolean
Collide self using distance between 2 objects and their radius.
-
#collides?(object2) ⇒ Boolean
(also: #collision?)
The standard method called when self needs to be checked for a collision with another object By default it calls bounding_box_collision? which will check for intersectons between the two objects “bounding_box” attributs (a Chingu::Rect instance).
-
#each_bounding_box_collision(*klasses) ⇒ Object
Explicit bounding_box-collision Works like each_collision but with inline-code for speedups.
-
#each_bounding_circle_collision(*klasses) ⇒ Object
Explicit radius-collision Works like each_collsion but with inline-code for speedups.
-
#each_collision(*klasses) ⇒ Object
Collides self with all objects of given classes Yields self and the objects it collides with.
Instance Method Details
#bounding_box_bounding_circle_collision?(object2) ⇒ Boolean
BoundingBox vs Radius collision
stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/chingu/traits/collision_detection.rb', line 80 def bounding_box_bounding_circle_collision?(object2) rect = self.respond_to?(:bounding_box) ? self.bounding_box : object2.bounding_box circle = self.respond_to?(:radius) ? self : object2 radius = circle.radius.to_i distance_x = (circle.x - rect.x - rect.width/2).abs distance_y = (circle.y - rect.y - rect.height/2).abs return false if distance_x > (rect.width/2 + circle.radius) return false if distance_y > (rect.height/2 + circle.radius) return true if distance_x <= (rect.width/2) return true if distance_y <= (rect.height/2) cornerDistance_sq = (distance_x - rect.width/2) ** 2 + (distance_y - rect.height/2) ** 2 return (cornerDistance_sq <= (circle.radius ** 2)) end |
#bounding_box_collision?(object2) ⇒ Boolean
Collide self with a given game object by checking both objects bounding_box’es Returns true if colliding.
63 64 65 |
# File 'lib/chingu/traits/collision_detection.rb', line 63 def bounding_box_collision?(object2) self.bounding_box.collide_rect?(object2.bounding_box) end |
#bounding_circle_collision?(object2) ⇒ Boolean
Collide self using distance between 2 objects and their radius. Returns true if colliding.
71 72 73 |
# File 'lib/chingu/traits/collision_detection.rb', line 71 def bounding_circle_collision?(object2) Gosu.distance(self.x, self.y, object2.x, object2.y) < self.radius + object2.radius end |
#collides?(object2) ⇒ Boolean Also known as: collision?
The standard method called when self needs to be checked for a collision with another object By default it calls bounding_box_collision? which will check for intersectons between the two objects “bounding_box” attributs (a Chingu::Rect instance)
48 49 50 51 52 53 54 55 56 |
# File 'lib/chingu/traits/collision_detection.rb', line 48 def collides?(object2) if self.respond_to?(:bounding_box) && object2.respond_to?(:bounding_box) bounding_box_collision?(object2) elsif self.respond_to?(:radius) && object2.respond_to?(:radius) bounding_circle_collision?(object2) else bounding_box_bounding_circle_collision?(object2) end end |
#each_bounding_box_collision(*klasses) ⇒ Object
Explicit bounding_box-collision Works like each_collision but with inline-code for speedups
126 127 128 129 130 131 132 |
# File 'lib/chingu/traits/collision_detection.rb', line 126 def each_bounding_box_collision(*klasses) Array(klasses).each do |klass| klass.all.each do |object| yield(self, object) if self.bounding_box.collide_rect?(object.bounding_box) end end end |
#each_bounding_circle_collision(*klasses) ⇒ Object
Explicit radius-collision Works like each_collsion but with inline-code for speedups
114 115 116 117 118 119 120 |
# File 'lib/chingu/traits/collision_detection.rb', line 114 def each_bounding_circle_collision(*klasses) Array(klasses).each do |klass| klass.all.each do |object| yield(self, object) if Gosu.distance(self.x, self.y, object.x, object.y) < self.radius + object.radius end end end |
#each_collision(*klasses) ⇒ Object
Collides self with all objects of given classes Yields self and the objects it collides with
102 103 104 105 106 107 108 |
# File 'lib/chingu/traits/collision_detection.rb', line 102 def each_collision(*klasses) Array(klasses).each do |klass| klass.all.each do |object| yield(self, object) if collides?(object) end end end |