Skip to content

Commit 82cd535

Browse files
author
monkstone
committed
more toxiclibs
1 parent 6af4ddb commit 82cd535

File tree

7 files changed

+194
-0
lines changed

7 files changed

+194
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'toxiclibs'
2+
require_relative 'attractor'
3+
require_relative 'particle'
4+
5+
attr_reader :particles, :attractor, :physics
6+
7+
def setup
8+
size 640, 360
9+
@physics = Physics::VerletPhysics2D.new
10+
physics.setDrag(0.01)
11+
@particles = (0..50).map { Particle.new(TVec2D.new(rand(width), rand(height)), physics) }
12+
@attractor = Attractor.new(TVec2D.new(width / 2, height / 2), physics)
13+
end
14+
15+
def draw
16+
background(255)
17+
physics.update
18+
attractor.display
19+
particles.each(&:display)
20+
if mouse_pressed?
21+
attractor.lock
22+
attractor.set(mouse_x, mouse_y)
23+
else
24+
attractor.unlock
25+
end
26+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# The Nature of Code
2+
# Daniel Shiffman
3+
# http://natureofcode.com
4+
class Attractor < Physics::VerletParticle2D
5+
include Processing::Proxy
6+
attr_accessor :r
7+
8+
def initialize(loc, physics)
9+
super(loc)
10+
@r = 24
11+
physics.add_particle(self)
12+
physics.add_behavior(Physics::AttractionBehavior2D.new(self, $app.width, 0.1))
13+
end
14+
15+
def display
16+
fill(0)
17+
ellipse(x, y, r * 2, r * 2)
18+
end
19+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# The Nature of Code
2+
# Daniel Shiffman
3+
# http://natureofcode.com
4+
5+
# class Spore extends the class "VerletParticle2D"
6+
class Particle < Physics::VerletParticle2D
7+
include Processing::Proxy
8+
attr_reader :r
9+
10+
def initialize(loc, physics)
11+
super(loc)
12+
@r = 8
13+
physics.add_particle(self)
14+
physics.add_behavior(Physics::AttractionBehavior2D.new(self, r * 4, -1))
15+
end
16+
17+
def display
18+
fill 127
19+
stroke 0
20+
stroke_weight 2
21+
ellipse x, y, r * 2, r * 2
22+
end
23+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# The Nature of Code
2+
# Daniel Shiffman
3+
# http://natureofcode.com
4+
class Blanket
5+
attr_reader :particles, :springs
6+
7+
def initialize(physics)
8+
@particles = []
9+
@springs = []
10+
w = 20
11+
h = 20
12+
len = 10
13+
strength = 0.125
14+
h.times do |y|
15+
w.times do |x|
16+
p = Particle.new(TVec2D.new($app.width / 2 + x * len - w * len / 2, y * len))
17+
physics.addParticle(p)
18+
particles << p
19+
if x > 0
20+
previous = particles[particles.size - 2]
21+
c = Connection.new(p, previous, len, strength)
22+
physics.addSpring(c)
23+
springs << c
24+
end
25+
if y > 0
26+
above = particles[particles.size - w - 1]
27+
c = Connection.new(p, above, len, strength)
28+
physics.addSpring(c)
29+
springs << c
30+
end
31+
end
32+
end
33+
topleft = particles[0]
34+
topleft.lock
35+
topright = particles[w - 1]
36+
topright.lock
37+
end
38+
39+
def display
40+
springs.each(&:display)
41+
end
42+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# The Nature of Code
2+
# Daniel Shiffman
3+
# http://natureofcode.com
4+
class Connection < Physics::VerletSpring2D
5+
include Processing::Proxy
6+
def initialize(p1, p2, len, strength)
7+
super(p1, p2, len, strength)
8+
end
9+
10+
def display
11+
stroke(0)
12+
line(a.x, a.y, b.x, b.y)
13+
end
14+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# The Nature of Code
2+
# Daniel Shiffman
3+
# http://natureofcode.com
4+
5+
# Notice how we are using inheritance here!
6+
# We could have just stored a reference to a VerletParticle object
7+
# inside the Particle class, but inheritance is a nice alternative
8+
class Particle < Physics::VerletParticle2D
9+
include Processing::Proxy
10+
def initialize(loc)
11+
super(loc)
12+
end
13+
14+
# All we're doing really is adding a display function to a VerletParticle
15+
def display
16+
fill(175)
17+
stroke(0)
18+
ellipse(x, y, 16, 16)
19+
end
20+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# The Nature of Code
2+
# Daniel Shiffman
3+
# http://natureofcode.com
4+
#
5+
# This example is adapted from Karsten Schmidt's SoftBodySquare example
6+
#
7+
# <p>Softbody square demo is showing how to create a 2D square mesh out of
8+
# verlet particles and make it stable enough to adef total structural
9+
# deformation by including an inner skeleton.</p>
10+
#
11+
# <p>Usage: move mouse to drag/deform the square</p>
12+
#
13+
#
14+
# Copyright (c) 2008-2009 Karsten Schmidt
15+
#
16+
# This demo & library is free software you can redistribute it and/or
17+
# modify it under the terms of the GNU Lesser General Public
18+
# License as published by the Free Software Foundation either
19+
# version 2.1 of the License, or (at your option) any later version.
20+
#
21+
# http://creativecommons.org/licenses/LGPL/2.1/
22+
#
23+
# This library is distributed in the hope that it will be useful,
24+
# but WITHOUT ANY WARRANTY without even the implied warranty of
25+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26+
# Lesser General Public License for more details.
27+
#
28+
# You should have received a copy of the GNU Lesser General Public
29+
# License along with this library if not, write to the Free Software
30+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31+
#
32+
require 'toxiclibs'
33+
require_relative 'blanket'
34+
require_relative 'connection'
35+
require_relative 'particle'
36+
37+
attr_reader :b, :physics
38+
39+
def setup
40+
size(640, 360)
41+
@physics = Physics::VerletPhysics2D.new
42+
physics.addBehavior(Physics::GravityBehavior2D.new(TVec2D.new(0, 0.1)))
43+
@b = Blanket.new(physics)
44+
end
45+
46+
def draw
47+
background(255)
48+
physics.update
49+
b.display
50+
end

0 commit comments

Comments
 (0)