Skip to content

Instantly share code, notes, and snippets.

@ymurase
Created October 21, 2014 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymurase/aec532a82f5862170356 to your computer and use it in GitHub Desktop.
Save ymurase/aec532a82f5862170356 to your computer and use it in GitHub Desktop.
A sample of ruby processing
require 'pp'
class Pendulum
def initialize(x, y, r)
@origin = PVector.new(x, y)
@r = r
@angle = Math::PI / 4
@a_velocity = 0.0
@a_acceleration = 0.0
@damping = 0.995
end
def update
gravity = 0.4
@a_acceleration = (-1.0 * gravity / @r) * Math.sin(@angle)
@a_velocity += @a_acceleration
@angle += @a_velocity
@a_velocity *= @damping
end
def display
x = @r * Math.sin(@angle) + @origin.x
y = @r * Math.cos(@angle) + @origin.y
stroke(0)
line(@origin.x, @origin.y, x, y)
fill(175)
ellipse(x, y, 16, 16)
end
end
def setup
size(640,360)
@pendulum = Pendulum.new(width/2, 10, 125)
end
def draw
background(255)
@pendulum.update
@pendulum.display
end
@monkstone
Copy link

# try this 
require 'pp'
load_library :vecmath

class Pendulum

  def initialize(x, y, r)
    @origin = Vec2D.new(x, y)
    @r = r
    @angle = Math::PI / 4
    @a_velocity = 0.0
    @a_acceleration = 0.0
    @damping = 0.995
  end

  def update
    gravity = 0.4
    @a_acceleration = (-1.0 * gravity / @r) * Math.sin(@angle)
    @a_velocity += @a_acceleration
    @angle += @a_velocity
    @a_velocity *= @damping
  end

  def display
    x = @r * Math.sin(@angle) + @origin.x
    y = @r * Math.cos(@angle) + @origin.y
    stroke(0)
    line(@origin.x, @origin.y, x, y)
    fill(175)
    ellipse(x, y, 16, 16)
  end
end

def setup
  size(640,360)
  @pendulum = Pendulum.new(width/2, 10, 125)
end

def draw
  background(255)

  @pendulum.update
  @pendulum.display
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment