Class: PhysicsManager
- Inherits:
-
Object
- Object
- PhysicsManager
- Extended by:
- Forwardable
- Defined in:
- lib/gamebox/core/physics_manager.rb
Overview
PhysicsManager creates and manages chipmunks space.
Instance Attribute Summary (collapse)
-
- (Object) space
Returns the value of attribute space.
-
- (Object) step_size
Time per physics step, in milliseconds (default 15).
Instance Method Summary (collapse)
-
- (Object) add_collision_func(first_objs, second_objs, &block)
allows for passing arrays of collision types not just single ones add_collision_func(, [:baz,:yar]) becomes: add_collision_func(:foo, :baz) add_collision_func(:foo, :yar) add_collision_func(:bar, :baz) add_collision_func(:bar, :yar).
- - (Object) configure
- - (Object) pause
- - (Object) pause_physics
- - (Object) register_physical_constraint(constraint)
- - (Object) register_physical_object(obj, static = false)
- - (Object) restart_physics
- - (Object) unpause
- - (Object) unregister_physical_constraint(constraint)
- - (Object) unregister_physical_object(obj, static = false)
- - (Object) update(time)
- - (Object) update_physics(time)
Instance Attribute Details
- (Object) space
Returns the value of attribute space
11 12 13 |
# File 'lib/gamebox/core/physics_manager.rb', line 11 def space @space end |
- (Object) step_size
Time per physics step, in milliseconds (default 15). Small steps make the simulation more stable than large steps, but if the step is too small, it may negatively affect performance, i.e. cause very high CPU use and/or low framerate.
19 20 21 |
# File 'lib/gamebox/core/physics_manager.rb', line 19 def step_size @step_size end |
Instance Method Details
- (Object) add_collision_func(first_objs, second_objs, &block)
allows for passing arrays of collision types not just single ones add_collision_func(, [:baz,:yar]) becomes: add_collision_func(:foo, :baz) add_collision_func(:foo, :yar) add_collision_func(:bar, :baz) add_collision_func(:bar, :yar)
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/gamebox/core/physics_manager.rb', line 73 def add_collision_func(first_objs, second_objs, &block) firsts = [first_objs].flatten seconds = [second_objs].flatten firsts.each do |f| seconds.each do |s| @space.add_collision_func(f,s) do |a, b| block.call a.actor, b.actor end end end end |
- (Object) configure
26 27 28 29 30 31 |
# File 'lib/gamebox/core/physics_manager.rb', line 26 def configure @space = CP::Space.new @space.iterations = 10 self.step_size = 15 @leftover_step_time = 0 end |
- (Object) pause
50 51 52 |
# File 'lib/gamebox/core/physics_manager.rb', line 50 def pause pause_physics end |
- (Object) pause_physics
58 59 60 |
# File 'lib/gamebox/core/physics_manager.rb', line 58 def pause_physics @physics_paused = true end |
- (Object) register_physical_constraint(constraint)
105 106 107 |
# File 'lib/gamebox/core/physics_manager.rb', line 105 def register_physical_constraint(constraint) @space.add_constraint constraint end |
- (Object) register_physical_object(obj, static = false)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/gamebox/core/physics_manager.rb', line 86 def register_physical_object(obj,static=false) obj.when :remove_me do unregister_physical_object obj end if static obj.shapes.each do |shape| @space.add_static_shape shape end else @space.add_body(obj.body) obj.shapes.each do |shape| @space.add_shape shape end end end |
- (Object) restart_physics
62 63 64 |
# File 'lib/gamebox/core/physics_manager.rb', line 62 def restart_physics @physics_paused = false end |
- (Object) unpause
54 55 56 |
# File 'lib/gamebox/core/physics_manager.rb', line 54 def unpause restart_physics end |
- (Object) unregister_physical_constraint(constraint)
109 110 111 |
# File 'lib/gamebox/core/physics_manager.rb', line 109 def unregister_physical_constraint(constraint) @space.remove_constraint constraint end |
- (Object) unregister_physical_object(obj, static = false)
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/gamebox/core/physics_manager.rb', line 113 def unregister_physical_object(obj,static=false) if static obj.shapes.each do |shape| @space.remove_static_shape shape end else @space.remove_body(obj.body) obj.shapes.each do |shape| @space.remove_shape shape end end end |
- (Object) update(time)
34 35 36 |
# File 'lib/gamebox/core/physics_manager.rb', line 34 def update(time) update_physics time end |
- (Object) update_physics(time)
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gamebox/core/physics_manager.rb', line 38 def update_physics(time) unless @physics_paused @leftover_step_time += time steps = (@leftover_step_time / @step_size).ceil steps.times do @space.step @step_size_seconds end @leftover_step_time -= (steps * @step_size).round end end |