Class: BulldogPhysics::Particles::Collisions::ParticleWorld
- Inherits:
-
Object
- Object
- BulldogPhysics::Particles::Collisions::ParticleWorld
- Defined in:
- lib/Particles/particle_world.rb
Overview
Keeps track of a set of particles, and provides the means to update them all.
Instance Attribute Summary collapse
-
#contact_generators ⇒ Object
Returns the value of attribute contact_generators.
-
#contacts ⇒ Object
Returns the value of attribute contacts.
-
#particles ⇒ Object
Returns the value of attribute particles.
-
#registry ⇒ Object
Returns the value of attribute registry.
Instance Method Summary collapse
- #add_gravity ⇒ Object
-
#generate_contacts ⇒ Object
Calls each of the registered contact generators to report * their contacts.
-
#initialize(max_contacts = 100, iterations = 1) ⇒ ParticleWorld
constructor
Creates a new particle simulator that can handle up to the given number of contacts per frame.
-
#integrate(duration) ⇒ Object
Integrates all the particles in this world forward in time by the given duration.
- #run_physics(duration) ⇒ Object
-
#start_frame ⇒ Object
Initializes the world for a simulation frame.
Constructor Details
#initialize(max_contacts = 100, iterations = 1) ⇒ ParticleWorld
Creates a new particle simulator that can handle up to the given number of contacts per frame. You can also optionally give a number of contact-resolution iterations to use. If you * don’t give a number of iterations, then twice the number of contacts will be used.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/Particles/particle_world.rb', line 31 def initialize(max_contacts = 100, iterations = 1) puts "INIT WORLD" @max_contacts = max_contacts @calculate_iterations = iterations @particles = Array.new @contacts = Array.new @contact_generators = Array.new @registry = ParticleForceRegistry.new @resolver = ParticleContactResolver.new end |
Instance Attribute Details
#contact_generators ⇒ Object
Returns the value of attribute contact_generators.
22 23 24 |
# File 'lib/Particles/particle_world.rb', line 22 def contact_generators @contact_generators end |
#contacts ⇒ Object
Returns the value of attribute contacts.
23 24 25 |
# File 'lib/Particles/particle_world.rb', line 23 def contacts @contacts end |
#particles ⇒ Object
Returns the value of attribute particles.
24 25 26 |
# File 'lib/Particles/particle_world.rb', line 24 def particles @particles end |
#registry ⇒ Object
Returns the value of attribute registry.
25 26 27 |
# File 'lib/Particles/particle_world.rb', line 25 def registry @registry end |
Instance Method Details
#add_gravity ⇒ Object
107 108 109 110 |
# File 'lib/Particles/particle_world.rb', line 107 def add_gravity gravity = ParticleGravity.new(Vector3.new(0, -0.5, 0)) @particles.each{|part| @registry.add(part, gravity)} end |
#generate_contacts ⇒ Object
Calls each of the registered contact generators to report * their contacts. Returns the number of generated contacts.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/Particles/particle_world.rb', line 54 def generate_contacts() limit = @max_contacts #if @contacts.size == 0 # return 0 #end for g in @contact_generators used = g.add_contact(@contacts, limit) limit -= used # We’ve run out of contacts to fill. This means we’re missing // contacts. if limit <= 0 break end end #return number of contacts used return @max_contacts - limit end |
#integrate(duration) ⇒ Object
Integrates all the particles in this world forward in time by the given duration.
79 80 81 |
# File 'lib/Particles/particle_world.rb', line 79 def integrate(duration) @particles.each{|p| p.integrate(duration)} end |
#run_physics(duration) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/Particles/particle_world.rb', line 83 def run_physics(duration) # check for colliding and hitting floor @contacts.clear @registry.update_forces(duration) integrate(duration) used_contacts = generate_contacts() if(used_contacts > 0) if(@calculate_iterations > 0) @resolver.iterations = used_contacts * 2 @resolver.resolve_contacts(@contacts, used_contacts, duration) end end #@resolver.resolve_contacts(@contacts, @contacts.size, duration) end |
#start_frame ⇒ Object
Initializes the world for a simulation frame. This clears * the force accumulators for particles in the world. After calling this, the particles can have their forces for this * frame added.
44 45 46 47 48 49 50 |
# File 'lib/Particles/particle_world.rb', line 44 def start_frame() @particles.each do |particle| particle.clearAccumulator() end end |