Last active
February 12, 2016 07:18
-
-
Save monkstone/67364f324d3c2ff04ce6 to your computer and use it in GitHub Desktop.
Euler Integration sketch in JRubyArt after Ira Greenberg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: UTF-8 | |
# module encapsulating run | |
module Runner | |
def run | |
move | |
render | |
bounds_collision | |
end | |
end | |
# Euler Ball Class, we include Processing::Proxy module to access PApplet methods | |
class EulerBall | |
include Processing::Proxy, Runner | |
attr_reader :pos, :spd, :radius, :bounds_x, :bounds_y | |
def initialize(position:, speed:, radius:) | |
@pos = position | |
@spd = speed | |
@radius = radius | |
@bounds_x = Bounds.new(lower: radius, upper: width - radius) | |
@bounds_y = Bounds.new(lower: radius, upper: height - radius) | |
end | |
def move | |
@pos += spd | |
end | |
def render | |
ellipse(pos.x, pos.y, radius * 2, radius * 2) | |
end | |
def bounds_collision | |
pos.x = bounds_x.position pos.x | |
spd.x *= -1 unless bounds_x.inside | |
pos.y = bounds_y.position pos.y | |
spd.y *= -1 unless bounds_y.inside | |
end | |
end | |
# Convenient boundary class, we only include MathTool module for constrain | |
class Bounds | |
include Processing::MathTool | |
attr_reader :low, :high, :inside | |
def initialize(lower:, upper:) | |
@low = lower | |
@high = upper | |
@inside = true | |
end | |
# Returns the current position or the limit, sets the `inside` flag | |
def position(val) | |
@inside = (low..high).cover? val | |
constrain(val, low, high) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Euler Integration (v01) | |
# Pos += spd | |
# | |
require_relative 'euler_ball' | |
attr_reader :ball | |
def setup | |
sketch_title 'Euler Integration' | |
@ball = EulerBall.new( | |
position: Vec2D.new(width / 2, height / 2), | |
# create a random direction vector, scaled by 3 | |
speed: Vec2D.random * 3, | |
radius: 10 | |
) | |
end | |
def draw | |
background(255) | |
# fill(255, 2) | |
# rect(-1, -1, width + 1, height + 1) | |
fill(0) | |
ball.run | |
end | |
def settings | |
size(400, 400) | |
smooth 4 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment