class Universe

Attributes

bodies[R]
dimensions[R]

Public Class Methods

big?() click to toggle source
# File lib/universe.rb, line 19
def self.big?
  true
end
new(dimensions:, bodies:) click to toggle source
# File lib/universe.rb, line 4
def initialize(dimensions:, bodies:)
  @dimensions = dimensions
  @bodies = bodies
end

Public Instance Methods

evolve(dt) click to toggle source
# File lib/universe.rb, line 9
def evolve(dt)
  each_pair do |(body_i, body_j)|
    force_ij = body_i.force_from(body_j)
    # dv = (F/m)dt
    f_over_m = force_ij * (1.0/body_i.mass)
    body_i.velocity += f_over_m * dt
    body_i.position += body_i.velocity * dt
  end
end

Private Instance Methods

each_pair() { |body_i, body_j| ... } click to toggle source
# File lib/universe.rb, line 25
def each_pair
  # get all pairs of distinct bodies
  #
  #    1  2  3  4
  #  1    .  .  .
  #  2 .     .  .
  #  3 .  .     .
  #  4 .  .  .
  #
  bodies.each do |body_i|
    bodies.each do |body_j|
      next if body_i == body_j
      yield [body_i, body_j]
    end
  end
end