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 3 attributes
@bounding_box - a Rect-instance, uses in bounding_box collisions
@radius -
@detect_collisions - [true|false], should object be checked for collisions with Object.each_collision
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#bounding_box ⇒ Object
Returns the value of attribute bounding_box.
-
#radius ⇒ Object
Returns the value of attribute radius.
Class Method Summary collapse
-
.included(base) ⇒ Object
attr_accessor :detect_collisions # slowed down example9 with 3 fps.
Instance Method Summary collapse
-
#bounding_box_collision?(object2) ⇒ Boolean
Collide self with a given game object by checking both objects bounding_box’es Returns true if colliding.
-
#collides?(object2) ⇒ Boolean
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_collision(klasses = []) ⇒ Object
Collides self with all objects of given classes Yields self and the objects it collides with.
-
#each_radius_collision(klasses = []) ⇒ Object
Explicit radius-collision Works like each_collsion but with inline-code for speedups.
-
#radius_collision?(object2) ⇒ Boolean
Collide self using distance between 2 objects and their radius.
-
#setup_trait(options) ⇒ Object
Automaticly try to set a bounding_box and radius.
-
#update_trait ⇒ Object
Have bounding box follow game objects x/y.
Instance Attribute Details
#bounding_box ⇒ Object
Returns the value of attribute bounding_box.
38 39 40 |
# File 'lib/chingu/traits/collision_detection.rb', line 38 def bounding_box @bounding_box end |
#radius ⇒ Object
Returns the value of attribute radius.
38 39 40 |
# File 'lib/chingu/traits/collision_detection.rb', line 38 def radius @radius end |
Class Method Details
.included(base) ⇒ Object
attr_accessor :detect_collisions # slowed down example9 with 3 fps
41 42 43 |
# File 'lib/chingu/traits/collision_detection.rb', line 41 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#bounding_box_collision?(object2) ⇒ Boolean
Collide self with a given game object by checking both objects bounding_box’es Returns true if colliding.
75 76 77 |
# File 'lib/chingu/traits/collision_detection.rb', line 75 def bounding_box_collision?(object2) self.bounding_box.collide_rect?(object2.bounding_box) end |
#collides?(object2) ⇒ Boolean
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)
66 67 68 69 |
# File 'lib/chingu/traits/collision_detection.rb', line 66 def collides?(object2) bounding_box_collision?(object2) #radius_collision?(object2) end |
#each_bounding_box_collision(klasses = []) ⇒ Object
Explicit bounding_box-collision Works like each_collision but with inline-code for speedups
128 129 130 131 132 133 134 |
# File 'lib/chingu/traits/collision_detection.rb', line 128 def each_bounding_box_collision(klasses = []) Array(klasses).each do |klass| klass.all.each do |object| yield(self, object) if @bounding_box.collide_rect?(object.bounding_box) end end end |
#each_collision(klasses = []) ⇒ Object
Collides self with all objects of given classes Yields self and the objects it collides with
103 104 105 106 107 108 109 |
# File 'lib/chingu/traits/collision_detection.rb', line 103 def each_collision(klasses = []) Array(klasses).each do |klass| klass.all.each do |object| yield(self, object) if collides?(object) end end end |
#each_radius_collision(klasses = []) ⇒ Object
Explicit radius-collision Works like each_collsion but with inline-code for speedups
115 116 117 118 119 120 121 |
# File 'lib/chingu/traits/collision_detection.rb', line 115 def each_radius_collision(klasses = []) Array(klasses).each do |klass| klass.all.each do |object| yield(self, object) if distance(@x, @y, object.x, object.y) < @radius + object.radius end end end |
#radius_collision?(object2) ⇒ Boolean
Collide self using distance between 2 objects and their radius. Returns true if colliding.
83 84 85 |
# File 'lib/chingu/traits/collision_detection.rb', line 83 def radius_collision?(object2) distance(self.x, self.y, object2.x, object2.y) < self.radius + object2.radius end |
#setup_trait(options) ⇒ Object
Automaticly try to set a bounding_box and radius. Don’t overwrite if they already exists.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/chingu/traits/collision_detection.rb', line 48 def setup_trait() if @x and @y and @image @bounding_box ||= Rect.new(@x, @y, @image.width, @image.height) end if @image @radius ||= (@image.height + @image.width) / 2 * 0.80 end ## @detect_collisions = true super end |
#update_trait ⇒ Object
Have bounding box follow game objects x/y
90 91 92 93 94 95 96 97 |
# File 'lib/chingu/traits/collision_detection.rb', line 90 def update_trait if defined?(@bounding_box) && @bounding_box.is_a?(Rect) @bounding_box.x = self.x @bounding_box.y = self.y end super end |