Class: Pongo::PhysicsEngine
- Inherits:
-
Object
- Object
- Pongo::PhysicsEngine
- Defined in:
- lib/pongo/apengine.rb
Overview
The main engine class.
Instance Attribute Summary collapse
-
#constraint_collision_cycles ⇒ Object
Returns the value of attribute constraint_collision_cycles.
-
#constraint_cycles ⇒ Object
Returns the value of attribute constraint_cycles.
-
#container ⇒ Object
Returns the value of attribute container.
-
#damping ⇒ Object
Returns the value of attribute damping.
-
#forces ⇒ Object
Returns the value of attribute forces.
-
#groups ⇒ Object
Returns the value of attribute groups.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#renderer ⇒ Object
Returns the value of attribute renderer.
-
#tile_step ⇒ Object
Returns the value of attribute tile_step.
Instance Method Summary collapse
- #<<(item) ⇒ Object
-
#add_force(force) ⇒ Object
Adds a force to all particles in the system.
-
#add_group(group) ⇒ Object
Adds a Group to the engine.
- #build_group(collide_internal = false) ⇒ Object
- #check_collisions ⇒ Object
- #create_group(collide_internal = true, &block) ⇒ Object
-
#draw ⇒ Object
(also: #paint)
Calling this method will in turn call each Group’s paint() method.
- #gravity=(val) ⇒ Object
- #integrate ⇒ Object
- #log(message, level = :info) ⇒ Object
- #next_frame(options = {}) ⇒ Object
-
#remove_all_forces ⇒ Object
Removes all forces from the engine.
-
#remove_force(force) ⇒ Object
Removes a force from the engine.
-
#remove_group(group) ⇒ Object
Removes a Group from the engine.
- #satisfy_constraints ⇒ Object
-
#setup(options = {}) ⇒ Object
(also: #init)
Initializes the engine.
-
#step ⇒ Object
The main step function of the engine.
Instance Attribute Details
#constraint_collision_cycles ⇒ Object
Returns the value of attribute constraint_collision_cycles.
5 6 7 |
# File 'lib/pongo/apengine.rb', line 5 def constraint_collision_cycles @constraint_collision_cycles end |
#constraint_cycles ⇒ Object
Returns the value of attribute constraint_cycles.
5 6 7 |
# File 'lib/pongo/apengine.rb', line 5 def constraint_cycles @constraint_cycles end |
#container ⇒ Object
Returns the value of attribute container.
5 6 7 |
# File 'lib/pongo/apengine.rb', line 5 def container @container end |
#damping ⇒ Object
Returns the value of attribute damping.
5 6 7 |
# File 'lib/pongo/apengine.rb', line 5 def damping @damping end |
#forces ⇒ Object
Returns the value of attribute forces.
5 6 7 |
# File 'lib/pongo/apengine.rb', line 5 def forces @forces end |
#groups ⇒ Object
Returns the value of attribute groups.
5 6 7 |
# File 'lib/pongo/apengine.rb', line 5 def groups @groups end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/pongo/apengine.rb', line 5 def logger @logger end |
#renderer ⇒ Object
Returns the value of attribute renderer.
5 6 7 |
# File 'lib/pongo/apengine.rb', line 5 def renderer @renderer end |
#tile_step ⇒ Object
Returns the value of attribute tile_step.
5 6 7 |
# File 'lib/pongo/apengine.rb', line 5 def tile_step @tile_step end |
Instance Method Details
#<<(item) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/pongo/apengine.rb', line 122 def <<(item) if item.is_a?(Group) add_group(item) else if @groups.empty? @groups << Group.new(true) @groups.first.is_parented = true @groups.first.init end @groups.last << item end end |
#add_force(force) ⇒ Object
Adds a force to all particles in the system. The forces added to the APEngine class are persistent - once a force is added it is continually applied each APEngine.step() cycle.
39 40 41 |
# File 'lib/pongo/apengine.rb', line 39 def add_force(force) @forces << force end |
#add_group(group) ⇒ Object
Adds a Group to the engine.
73 74 75 76 77 |
# File 'lib/pongo/apengine.rb', line 73 def add_group(group) @groups << group group.is_parented = true group.init end |
#build_group(collide_internal = false) ⇒ Object
66 67 68 69 70 |
# File 'lib/pongo/apengine.rb', line 66 def build_group(collide_internal=false) group = Group.new(collide_internal) @groups << group group end |
#check_collisions ⇒ Object
118 119 120 |
# File 'lib/pongo/apengine.rb', line 118 def check_collisions @groups.each {|g| g.check_collisions} end |
#create_group(collide_internal = true, &block) ⇒ Object
135 136 137 138 139 |
# File 'lib/pongo/apengine.rb', line 135 def create_group(collide_internal=true, &block) @groups << Group.new(collide_internal) block.call(@groups.last) if block @groups.last end |
#draw ⇒ Object Also known as: paint
Calling this method will in turn call each Group’s paint() method. Generally you would call this method after stepping the engine in the main program cycle.
105 106 107 |
# File 'lib/pongo/apengine.rb', line 105 def draw @groups.each {|g| g.draw} end |
#gravity=(val) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/pongo/apengine.rb', line 43 def gravity=(val) case val when Numeric add_force(VectorForce.new(false, 0, val)) when Array add_force(VectorForce.new(false, *val)) when VectorForce add_force(val) else raise ArgumentError end end |
#integrate ⇒ Object
110 111 112 |
# File 'lib/pongo/apengine.rb', line 110 def integrate @groups.each {|g| g.integrate(@time_step)} end |
#log(message, level = :info) ⇒ Object
151 152 153 154 |
# File 'lib/pongo/apengine.rb', line 151 def log(, level=:info) = . + "\n" + .backtrace.join("\n") if .is_a?(Exception) @logger.send(level, ) if @logger end |
#next_frame(options = {}) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/pongo/apengine.rb', line 141 def next_frame(={}) [:before].call if [:before] step draw [:after].call if [:after] rescue log($!) raise $! end |
#remove_all_forces ⇒ Object
Removes all forces from the engine.
62 63 64 |
# File 'lib/pongo/apengine.rb', line 62 def remove_all_forces @forces.clear end |
#remove_force(force) ⇒ Object
Removes a force from the engine.
57 58 59 |
# File 'lib/pongo/apengine.rb', line 57 def remove_force(force) @forces.remove(force) end |
#remove_group(group) ⇒ Object
Removes a Group from the engine.
80 81 82 83 84 85 |
# File 'lib/pongo/apengine.rb', line 80 def remove_group(group) if @groups.delete(group) group.is_parented = false group.cleanup end end |
#satisfy_constraints ⇒ Object
114 115 116 |
# File 'lib/pongo/apengine.rb', line 114 def satisfy_constraints @groups.each {|g| g.satisfy_constraints} end |
#setup(options = {}) ⇒ Object Also known as: init
Initializes the engine. You must call this method prior to adding any particles or constraints.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/pongo/apengine.rb', line 16 def setup(={}) #dt=0.25) = { :dt => 0.25, :damping => 1, :constraint_cycles => 0, :constraint_collision_cycles => 1 }.update(.is_a?(Numeric) ? {:dt => } : ) @time_step = [:dt] * [:dt] @groups = [] @forces = [] self.damping = [:damping] self.constraint_cycles = [:constraint_cycles] self.constraint_collision_cycles = [:constraint_collision_cycles] self.container = [:container] if [:container] self.renderer = [:renderer] if [:renderer] self.logger = [:logger] if [:logger] self.gravity = [:gravity] if [:gravity] end |
#step ⇒ Object
The main step function of the engine. This method should be called continously to advance the simulation. The faster this method is called, the faster the simulation will run. Usually you would call this in your main program loop.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/pongo/apengine.rb', line 91 def step integrate constraint_cycles.times do satisfy_constraints end constraint_collision_cycles.times do satisfy_constraints check_collisions end end |