Skip to content

Instantly share code, notes, and snippets.

@dodo
Created October 28, 2009 14:19
Show Gist options
  • Save dodo/220509 to your computer and use it in GitHub Desktop.
Save dodo/220509 to your computer and use it in GitHub Desktop.
require 'java'
require 'core'
include_class Java::processing.core.PApplet
def run!(acls)
runswing! acls
end
def runswing!(acls)
jFrame = Java::javax.swing.JFrame
frame = jFrame.new acls.name
applet = acls.new
frame.content_pane.add applet
frame.default_close_operation = jFrame::EXIT_ON_CLOSE
applet.init
frame.pack
frame.visible = true
end
class Closer < Java::java.awt.event.WindowAdapter
def windowClosing(e)
exit 0
end
end
def runawk!(acls)
frame = Java::java.awt.Frame::new
applet = acls.new
frame.setResizable false
applet.frame = frame
frame.setLayout nil
frame.add applet
frame.pack
applet.init
insets = frame.getInsets
winW = [applet.width , PApplet::MIN_WINDOW_WIDTH ].max + insets.left + insets.right
winH = [applet.height, PApplet::MIN_WINDOW_HEIGHT].max + insets.top + insets.bottom
frame.setSize winW, winH
usableWinH = winH - insets.top - insets.bottom
applet.setBounds((winW - applet.width)/2 , insets.top + (usableWinH - applet.height)/2 , applet.width, applet.height)
frame.addWindowListener Closer.new
applet.setupFrameResizeListener
frame.setVisible true
applet.requestFocus
end
$gravity = 200.0
$fps = 24
require 'processing'
class Planet
attr_accessor :mass, :r, :pos
def initialize(parent)
@p = parent
@mass = rand(504) + 8
@r = @mass**(1.0/3.0) * 12.5
@pos = rand(760) + 20, rand(560) + 20
end
def draw(alpha = 11)
@p.fill 99, alpha+11
@p.stroke 66, alpha
x, y = @pos
@p.ellipse x, y, @r, @r
end
end
class Particle
attr_accessor :pos, :speed, :angle, :v, :last, :color
def initialize(parent, pos, angle, color = 111, speed = 100)
@p = parent
@last = [pos]*6
@pos = pos
@speed = speed
@angle = angle
@color = color
@v = 0.1 * speed * Math.sin(angle), -0.1 * speed * Math.cos(angle)
end
def update(planets)
@last.shift
@last += [@pos]
pos = @pos
planets.each { |planet|
d = (pos[0] - planet.pos[0])**2 + (pos[1] - planet.pos[1])**2
d *= d if Math.sqrt(d) < planet.r
a = $gravity * planet.mass * (pos[0] - planet.pos[0]) / (d * Math.sqrt(d)),
$gravity * planet.mass * (pos[1] - planet.pos[1]) / (d * Math.sqrt(d))
@v = (@v[0] - a[0])*0.99,
(@v[1] - a[1])*0.99
}
r = Math.sqrt(@v[0]**2+@v[1]**2)
@v = @v[0]*0.3, @v[1]*0.3 if r > 100
@v = @v[0]*3.0, @v[1]*3.0 if r < 10
@pos = [ pos[0]+@v[0], pos[1]+@v[1] ]
end
def draw
@p.noFill
#@p.fill 222, 5
@p.stroke @color
(0..(rand(4)+2)).each {
@p.curveTightness rand(70)/10.0-3.5
@p.beginShape
@last.each { |x, y| @p.curveVertex x, y }
@p.curveVertex *@pos
@p.endShape
}
end
end
class Slingshot < PApplet
def setup
size 800, 600
smooth
frameRate $fps
@planets, @particles, @i, @n = [], [], 0, 0
(0..(rand(7)+1)).each { @planets += [Planet.new self]}
(0..30).each { @particles += [Particle.new(self, [10,300],rand(360),color(111,11))]}
(0..30).each { @particles += [Particle.new(self, [790,300],rand(360),color(111,11))]}
background 0
@planets.each { |planet| planet.draw }
end
def draw
@i += 1
filter BLUR if 0 == @i % ($fps/2)
filter DILATE if 0 == @i % $fps
if 0 == @i % ($fps*2)
filter ERODE
@n += 1
@i = 0
end
if 5 == @n
@planets, @particles, @n = [], [], 0
(0..(rand(7)+1)).each { @planets += [Planet.new self]}
(0..30).each { @particles += [Particle.new(self, [10,300],rand(360),color(111,11))]}
(0..30).each { @particles += [Particle.new(self, [790,300],rand(360),color(111,11))]}
#background 11, 1
filter ERODE
filter ERODE
@planets.each { |planet| planet.draw 0 }
end
#background 11, 1
#@planets.each { |planet| planet.draw }
@particles.each { |plarticle| plarticle.update @planets }
@particles.each { |plarticle| plarticle.draw }
end
end
run! Slingshot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment