-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
monkstone
committed
Mar 31, 2015
1 parent
6af4ddb
commit 82cd535
Showing
7 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
samples/external_library/ruby_gem/toxiclibs/physics/attract_repel/attract_repel.rb
This file contains 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
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 |
19 changes: 19 additions & 0 deletions
19
samples/external_library/ruby_gem/toxiclibs/physics/attract_repel/attractor.rb
This file contains 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
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 |
23 changes: 23 additions & 0 deletions
23
samples/external_library/ruby_gem/toxiclibs/physics/attract_repel/particle.rb
This file contains 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
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 |
42 changes: 42 additions & 0 deletions
42
samples/external_library/ruby_gem/toxiclibs/physics/soft_body/blanket.rb
This file contains 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
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 |
14 changes: 14 additions & 0 deletions
14
samples/external_library/ruby_gem/toxiclibs/physics/soft_body/connection.rb
This file contains 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
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 |
20 changes: 20 additions & 0 deletions
20
samples/external_library/ruby_gem/toxiclibs/physics/soft_body/particle.rb
This file contains 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
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 |
50 changes: 50 additions & 0 deletions
50
samples/external_library/ruby_gem/toxiclibs/physics/soft_body/soft_body_square_adapted.rb
This file contains 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
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 |