Sha256: 5a9a3826ca9c116611fe02f4585af247712c792b01282e5d56ba236c0a33e7b5

Contents?: true

Size: 1.73 KB

Versions: 9

Compression:

Stored size: 1.73 KB

Contents

require 'ruby-processing'

#
# Ported from http://processing.org/learning/topics/chain.html
#
# One mass is attached to the mouse position and the other is attached the position of the other mass. 
# The gravity in the environment pulls down on both.
#
class Chain < Processing::App
  
  attr_reader :gravity
  
  load_library :control_panel

  def setup
    smooth
    fill 0
    
    # Inputs: spring1, spring2, mass, gravity
    @gravity = 6.0
    @mass    = 2.0
    @s1      = Spring2d.new(width/2, height/2, @mass)
    @s2      = Spring2d.new(width/2, height/2, @mass)
    
    # Control panel for changing gravity
    control_panel do |c|
      c.slider :gravity, 0..30
    end
  end
  
  def draw
    background 204
    @s1.update(mouse_x, mouse_y)
    display(@s1, mouse_x, mouse_y)

    @s2.update(@s1.x, @s1.y)
    display(@s2, @s1.x, @s1.y)
  end
  
  def display(spring, nx, ny)
    no_stroke
    ellipse(spring.x, spring.y, spring.diameter, spring.diameter)
    stroke 255
    line(spring.x, spring.y, nx, ny)
  end

end

class Spring2d
  
  attr_reader :x, :y
  
  def initialize(xpos, ypos, mass)
    @x         = xpos # The x-coordinate
    @y         = ypos # The y-coordinate
    @mass      = mass
    @vx, @vy   = 0, 0 # The x- and y-axis velocities
    @radius    = 20
    @stiffness = 0.2
    @damping   = 0.7
  end

  def update(target_x, target_y)
    force_x = (target_x - self.x) * @stiffness
    ax = force_x / @mass
    @vx = @damping * (@vx + ax)
    @x += @vx
    
    force_y = (target_y - self.y) * @stiffness
    force_y += $app.gravity
    ay = force_y / @mass
    @vy = @damping * (@vy + ay)
    @y += @vy
  end
  
  def diameter
    @radius * 2
  end
  
end

Chain.new(:width => 200, :height => 200, :title => "Chain", :full_screen => false)

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ruby-processing-1.0.11 samples/processing_app/topics/simulate/chain.rb
ruby-processing-1.0.10.1 samples/processing_app/topics/simulate/chain.rb
ruby-processing-1.0.9 samples/processing_app/topics/simulate/chain.rb
ruby-processing-1.0.4 samples/processing_app/topics/simulate/chain.rb
ruby-processing-1.0.3 samples/processing_app/topics/simulate/chain.rb
ruby-processing-1.0.5 samples/processing_app/topics/simulate/chain.rb
ruby-processing-1.0.6 samples/processing_app/topics/simulate/chain.rb
ruby-processing-1.0.7 samples/processing_app/topics/simulate/chain.rb
ruby-processing-1.0.8 samples/processing_app/topics/simulate/chain.rb