Skip to content

Commit 35bc006

Browse files
committed
more toxi examples
1 parent f0883e1 commit 35bc006

7 files changed

Lines changed: 272 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'forwardable'
2+
3+
# The Nature of Code
4+
# <http://www.shiffman.net/teaching/nature>
5+
# Spring 2010
6+
# Toxiclibs example: http://toxiclibs.org/
7+
8+
# Force directed graph
9+
# Heavily based on: http://code.google.com/p/fidgen/
10+
# A cluster is a grouping of Nodes
11+
class Cluster
12+
extend Forwardable
13+
def_delegators(:@app, :stroke, :stroke_weight, :line, :physics)
14+
attr_reader :nodes, :diameter
15+
16+
# We initialize a Cluster with a number of nodes, a diameter, and centerpoint
17+
def initialize(n, d, center)
18+
@app = $app
19+
# Set the diameter
20+
@diameter = d
21+
# Create the nodes
22+
@nodes = (0..n).map { Node.new(center.add(TVec2D.randomVector)) }
23+
# Connect all the nodes with a Spring
24+
nodes[0..nodes.size - 2].each_with_index do |ni, i|
25+
nodes[i + 1..nodes.size - 1].each do |nj|
26+
# A Spring needs two particles, a resting length, and a strength
27+
physics.add_spring(Physics::VerletSpring2D.new(ni, nj, diameter, 0.01))
28+
end
29+
end
30+
end
31+
32+
def display
33+
# Show all the nodes
34+
nodes.each(&:display)
35+
end
36+
37+
# Draw all the internal connections
38+
def show_connections
39+
stroke(0, 150)
40+
stroke_weight(2)
41+
nodes[0..nodes.size - 2].each_with_index do |pi, i|
42+
nodes[i + 1..nodes.size - 1].each do |pj|
43+
line(pi.x, pi.y, pj.x, pj.y)
44+
end
45+
end
46+
end
47+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# The Nature of Code
2+
# <http://www.shiffman.net/teaching/nature>
3+
# Spring 2010
4+
# Toxiclibs example: http://toxiclibs.org/
5+
6+
# Force directed graph
7+
# Heavily based on: http://code.google.com/p/fidgen/
8+
9+
# Notice how we are using inheritance here!
10+
# We could have just stored a reference to a VerletParticle object
11+
# inside the Node class, but inheritance is a nice alternative
12+
class Node < Physics::VerletParticle2D
13+
extend Forwardable
14+
def_delegators(:@app, :fill, :stroke, :stroke_weight, :ellipse)
15+
def initialize(pos)
16+
super(pos)
17+
@app = $app
18+
end
19+
20+
# All we're doing really is adding a :display function to a VerletParticle
21+
def display
22+
fill(0, 150)
23+
stroke(0)
24+
stroke_weight(2)
25+
ellipse(x, y, 16, 16)
26+
end
27+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# The Nature of Code
2+
# Daniel Shiffman
3+
# http://natureofcode.com
4+
# bare sketch run with k9 run simple_cluster.rb
5+
# Force directed graph,
6+
# heavily based on: http://code.google.com/p/fidgen/
7+
8+
require 'toxiclibs'
9+
require_relative 'cluster'
10+
require_relative 'node'
11+
require 'forwardable'
12+
13+
attr_reader :physics, :cluster, :f, :show_physics, :show_particles
14+
15+
def settings
16+
size(640, 360)
17+
end
18+
19+
def setup
20+
sketch_title 'Simple Cluster'
21+
@f = createFont('Georgia', 12, true)
22+
@show_physics = true
23+
@show_particles = true
24+
@show_physics = true
25+
@show_particles = true
26+
# Initialize the physics
27+
@physics = Physics::VerletPhysics2D.new
28+
@physics.set_world_bounds(Toxi::Rect.new(10, 10, width - 20, height - 20))
29+
# Spawn a new random graph
30+
@cluster = Cluster.new(8, 100, TVec2D.new(width / 2, height / 2))
31+
end
32+
33+
def draw
34+
# Update the physics world
35+
physics.update
36+
background(255)
37+
# Display all points
38+
cluster.display if show_particles
39+
# If we want to see the physics
40+
cluster.show_connections if show_physics
41+
# Instructions
42+
fill(0)
43+
text_font(f)
44+
text("'p' to display or hide particles\n'c' to display or hide connections\n'n' for new graph", 10, 20)
45+
end
46+
47+
# Key press commands
48+
def key_pressed
49+
case key
50+
when 'c'
51+
@show_physics = !show_physics
52+
@show_particles = true if show_physics
53+
when 'p'
54+
@show_particles = !show_particles
55+
@show_particles = true unless show_physics
56+
when 'n'
57+
physics.clear
58+
@cluster = Cluster.new(rand(3..20), rand(10..width / 2), TVec2D.new(width / 2, height / 2))
59+
end
60+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# The Nature of Code
2+
# Daniel Shiffman
3+
# http://natureofcode.com
4+
class Blanket
5+
extend Forwardable
6+
def_delegators(:@app, :width)
7+
attr_reader :particles, :springs, :physics
8+
9+
def initialize(physics)
10+
@app = $app
11+
@particles = []
12+
@springs = []
13+
w = 20
14+
h = 20
15+
len = 10
16+
strength = 0.125
17+
h.times do |y|
18+
w.times do |x|
19+
p = Particle.new(TVec2D.new(width / 2 + x * len - w * len / 2, y * len))
20+
physics.add_particle(p)
21+
particles << p
22+
if x > 0
23+
previous = particles[particles.size - 2]
24+
c = Connection.new(p, previous, len, strength)
25+
physics.add_spring(c)
26+
springs << c
27+
end
28+
if y > 0
29+
above = particles[particles.size - w - 1]
30+
c = Connection.new(p, above, len, strength)
31+
physics.add_spring(c)
32+
springs << c
33+
end
34+
end
35+
end
36+
topleft = particles[0]
37+
topleft.lock
38+
topright = particles[w - 1]
39+
topright.lock
40+
end
41+
42+
def display
43+
springs.each(&:display)
44+
end
45+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# The Nature of Code
2+
# Daniel Shiffman
3+
# http://natureofcode.com
4+
class Connection < Physics::VerletSpring2D
5+
extend Forwardable
6+
def_delegators(:@app, :stroke, :line)
7+
def initialize(p1, p2, len, strength)
8+
super(p1, p2, len, strength)
9+
@app = $app
10+
end
11+
12+
def display
13+
stroke(0)
14+
line(a.x, a.y, b.x, b.y)
15+
end
16+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
extend Forwardable
10+
def_delegators(:@app, :fill, :stroke, :ellipse)
11+
def initialize(loc)
12+
super(loc)
13+
@app = $app
14+
end
15+
16+
# All we're doing really is adding a display function to a VerletParticle
17+
def display
18+
fill(175)
19+
stroke(0)
20+
ellipse(x, y, 16, 16)
21+
end
22+
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 'forwardable'
34+
require_relative 'blanket'
35+
require_relative 'connection'
36+
require_relative 'particle'
37+
38+
attr_reader :b, :physics
39+
40+
def settings
41+
size 640, 360
42+
end
43+
44+
def setup
45+
sketch_title 'Soft Body Square'
46+
@physics = Physics::VerletPhysics2D.new
47+
physics.add_behavior(Physics::GravityBehavior2D.new(TVec2D.new(0, 0.1)))
48+
@b = Blanket.new physics
49+
end
50+
51+
def draw
52+
background(255)
53+
physics.update
54+
b.display
55+
end

0 commit comments

Comments
 (0)