Sha256: 58aaf79c48a59595384782648edefbcdc8aa3e6300324ef60e76f0dcd85ed8cf

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

# The Nature of Code
# <http:#www.shiffman.net/teaching/nature>
# Spring 2010
# PBox2D example

# An uneven surface

load_library :jbox2d
load_library :surface

include SB

attr_reader :surface, :box2d, :particles

def setup
  size(500,300)
  smooth
  
  # Initialize box2d physics and create the world
  @box2d = Processing::Box2d::Box2d.new(self)
  box2d.create_world
  # We are setting a custom gravity
  box2d.set_gravity(0, -20)
  
  # Create the empty list
  @particles = []
  # Create the surface
  @surface = Surface.new(box2d)
end

def draw
  # If the mouse is pressed, we make new particles
  
  
  # We must always step through time!
  box2d.step
  
  background(138, 66, 54)  
  # Draw the surface
  surface.display
  # NB question mark is reqd to call mouse_pressed value, else method gets called.
  particles << Particle.new(box2d, mouse_x, mouse_y, rand(2.0 .. 6)) if mouse_pressed?    
  # Draw all particles
  particles.each do |p|
    p.display
  end  
  # Particles that leave the screen, we delete them
  # (note they have to be deleted from both the box2d world and our list
  particles.each_with_index do |p, i|
    if (p.done)
      particles.delete_at(i)
    end
  end  
  # Just drawing the framerate to see how many particles it can handle
  fill(0)
  text("framerate: #{frame_rate.to_i}", 12, 16)
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby-processing-2.6.2 samples/external_library/java_processing/pbox2d/bumpy_surface_noise.rb
ruby-processing-2.6.1 samples/external_library/java_processing/pbox2d/bumpy_surface_noise.rb
ruby-processing-2.6.0 samples/external_library/java_processing/pbox2d/bumpy_surface_noise.rb
ruby-processing-2.5.1 samples/external_library/java_processing/pbox2d/bumpy_surface_noise.rb
ruby-processing-2.5.0 samples/external_library/java_processing/pbox2d/bumpy_surface_noise.rb