Class: BulldogPhysics::Particles::Collisions::ParticleWorld

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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
# File 'lib/Particles/particle_world.rb', line 31

def initialize(max_contacts = 100, iterations = 1)
  @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_generatorsObject

Returns the value of attribute contact_generators.



22
23
24
# File 'lib/Particles/particle_world.rb', line 22

def contact_generators
  @contact_generators
end

#contactsObject

Returns the value of attribute contacts.



23
24
25
# File 'lib/Particles/particle_world.rb', line 23

def contacts
  @contacts
end

#particlesObject

Returns the value of attribute particles.



24
25
26
# File 'lib/Particles/particle_world.rb', line 24

def particles
  @particles
end

#registryObject

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_gravityObject



106
107
108
109
# File 'lib/Particles/particle_world.rb', line 106

def add_gravity
  gravity = ParticleGravity.new(Vector3.new(0, -0.5, 0))
  @particles.each{|part| @registry.add(part, gravity)}
end

#generate_contactsObject

Calls each of the registered contact generators to report * their contacts. Returns the number of generated contacts.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/Particles/particle_world.rb', line 53

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.



78
79
80
# File 'lib/Particles/particle_world.rb', line 78

def integrate(duration)
  @particles.each{|p| p.integrate(duration)}
end

#run_physics(duration) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/Particles/particle_world.rb', line 82

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_frameObject

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.



43
44
45
46
47
48
49
# File 'lib/Particles/particle_world.rb', line 43

def start_frame()
  

  @particles.each do |particle|
    particle.clearAccumulator()
  end
end