Class: Pongo::PhysicsEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/pongo/apengine.rb

Overview

The main engine class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#constraint_collision_cyclesObject

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_cyclesObject

Returns the value of attribute constraint_cycles.



5
6
7
# File 'lib/pongo/apengine.rb', line 5

def constraint_cycles
  @constraint_cycles
end

#containerObject

Returns the value of attribute container.



5
6
7
# File 'lib/pongo/apengine.rb', line 5

def container
  @container
end

#dampingObject

Returns the value of attribute damping.



5
6
7
# File 'lib/pongo/apengine.rb', line 5

def damping
  @damping
end

#forcesObject

Returns the value of attribute forces.



5
6
7
# File 'lib/pongo/apengine.rb', line 5

def forces
  @forces
end

#groupsObject

Returns the value of attribute groups.



5
6
7
# File 'lib/pongo/apengine.rb', line 5

def groups
  @groups
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/pongo/apengine.rb', line 5

def logger
  @logger
end

#rendererObject

Returns the value of attribute renderer.



5
6
7
# File 'lib/pongo/apengine.rb', line 5

def renderer
  @renderer
end

#tile_stepObject

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_collisionsObject



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

#drawObject 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

#integrateObject



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(message, level=:info)
  message = message.message + "\n" + message.backtrace.join("\n") if message.is_a?(Exception)
  @logger.send(level, message) 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(options={})
  options[:before].call if options[:before]
  step
  draw
  options[:after].call if options[:after]
rescue
  log($!)
  raise $!
end

#remove_all_forcesObject

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_constraintsObject



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(options={}) #dt=0.25)
  options = {
    :dt => 0.25, :damping => 1, :constraint_cycles => 0, 
    :constraint_collision_cycles => 1
  }.update(options.is_a?(Numeric) ? {:dt => options} : options)

  @time_step = options[:dt] * options[:dt]
  @groups = []
  @forces = []
  self.damping = options[:damping]
  self.constraint_cycles = options[:constraint_cycles]
  self.constraint_collision_cycles = options[:constraint_collision_cycles] 

  self.container = options[:container] if options[:container]
  self.renderer = options[:renderer] if options[:renderer]
  self.logger = options[:logger] if options[:logger]
  self.gravity = options[:gravity] if options[:gravity]
end

#stepObject

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