Skip to content

Commit

Permalink
more toxiclibs
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed Mar 31, 2015
1 parent 6af4ddb commit 82cd535
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'toxiclibs'
require_relative 'attractor'
require_relative 'particle'

attr_reader :particles, :attractor, :physics

def setup
size 640, 360
@physics = Physics::VerletPhysics2D.new
physics.setDrag(0.01)
@particles = (0..50).map { Particle.new(TVec2D.new(rand(width), rand(height)), physics) }
@attractor = Attractor.new(TVec2D.new(width / 2, height / 2), physics)
end

def draw
background(255)
physics.update
attractor.display
particles.each(&:display)
if mouse_pressed?
attractor.lock
attractor.set(mouse_x, mouse_y)
else
attractor.unlock
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
class Attractor < Physics::VerletParticle2D
include Processing::Proxy
attr_accessor :r

def initialize(loc, physics)
super(loc)
@r = 24
physics.add_particle(self)
physics.add_behavior(Physics::AttractionBehavior2D.new(self, $app.width, 0.1))
end

def display
fill(0)
ellipse(x, y, r * 2, r * 2)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com

# class Spore extends the class "VerletParticle2D"
class Particle < Physics::VerletParticle2D
include Processing::Proxy
attr_reader :r

def initialize(loc, physics)
super(loc)
@r = 8
physics.add_particle(self)
physics.add_behavior(Physics::AttractionBehavior2D.new(self, r * 4, -1))
end

def display
fill 127
stroke 0
stroke_weight 2
ellipse x, y, r * 2, r * 2
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
class Blanket
attr_reader :particles, :springs

def initialize(physics)
@particles = []
@springs = []
w = 20
h = 20
len = 10
strength = 0.125
h.times do |y|
w.times do |x|
p = Particle.new(TVec2D.new($app.width / 2 + x * len - w * len / 2, y * len))
physics.addParticle(p)
particles << p
if x > 0
previous = particles[particles.size - 2]
c = Connection.new(p, previous, len, strength)
physics.addSpring(c)
springs << c
end
if y > 0
above = particles[particles.size - w - 1]
c = Connection.new(p, above, len, strength)
physics.addSpring(c)
springs << c
end
end
end
topleft = particles[0]
topleft.lock
topright = particles[w - 1]
topright.lock
end

def display
springs.each(&:display)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
class Connection < Physics::VerletSpring2D
include Processing::Proxy
def initialize(p1, p2, len, strength)
super(p1, p2, len, strength)
end

def display
stroke(0)
line(a.x, a.y, b.x, b.y)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com

# Notice how we are using inheritance here!
# We could have just stored a reference to a VerletParticle object
# inside the Particle class, but inheritance is a nice alternative
class Particle < Physics::VerletParticle2D
include Processing::Proxy
def initialize(loc)
super(loc)
end

# All we're doing really is adding a display function to a VerletParticle
def display
fill(175)
stroke(0)
ellipse(x, y, 16, 16)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
#
# This example is adapted from Karsten Schmidt's SoftBodySquare example
#
# <p>Softbody square demo is showing how to create a 2D square mesh out of
# verlet particles and make it stable enough to adef total structural
# deformation by including an inner skeleton.</p>
#
# <p>Usage: move mouse to drag/deform the square</p>
#
#
# Copyright (c) 2008-2009 Karsten Schmidt
#
# This demo & library is free software you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation either
# version 2.1 of the License, or (at your option) any later version.
#
# http://creativecommons.org/licenses/LGPL/2.1/
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
require 'toxiclibs'
require_relative 'blanket'
require_relative 'connection'
require_relative 'particle'

attr_reader :b, :physics

def setup
size(640, 360)
@physics = Physics::VerletPhysics2D.new
physics.addBehavior(Physics::GravityBehavior2D.new(TVec2D.new(0, 0.1)))
@b = Blanket.new(physics)
end

def draw
background(255)
physics.update
b.display
end

0 comments on commit 82cd535

Please sign in to comment.