update of Nature of Code examples, need to do one more before 2.0

This commit is contained in:
Daniel Shiffman
2013-03-06 22:59:57 -05:00
parent 82c677ef33
commit 6b5521af3c
151 changed files with 15068 additions and 449 deletions
@@ -4,10 +4,10 @@
// The "Vehicle" class
class Vehicle {
// Vehicle now has a brain!
Perceptron brain;
PVector location;
PVector velocity;
PVector acceleration;
@@ -34,7 +34,7 @@ class Vehicle {
location.add(velocity);
// Reset accelerationelertion to 0 each cycle
acceleration.mult(0);
location.x = constrain(location.x,0,width);
location.y = constrain(location.y,0,height);
}
@@ -43,48 +43,48 @@ class Vehicle {
// We could add mass here if we want A = F / M
acceleration.add(force);
}
// Here is where the brain processes everything
void steer(ArrayList<PVector> targets) {
// Make an array of forces
PVector[] forces = new PVector[targets.size()];
// Steer towards all targets
for (int i = 0; i < forces.length; i++) {
forces[i] = seek(targets.get(i));
}
// That array of forces is the input to the brain
PVector result = brain.feedforward(forces);
// Use the result to steer the vehicle
applyForce(result);
// Train the brain according to the error
PVector error = PVector.sub(desired, location);
brain.train(forces,error);
}
// A method that calculates a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
PVector seek(PVector target) {
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Steering = Desired minus velocity
PVector steer = PVector.sub(desired,velocity);
steer.limit(maxforce); // Limit to maximum steering force
return steer;
}
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading() + PI/2;
float theta = velocity.heading2D() + PI/2;
fill(175);
stroke(0);
strokeWeight(1);
@@ -0,0 +1,57 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
Mover m;
float t = 0.0;
void setup() {
size(640, 360);
m = new Mover();
}
void draw() {
background(255);
// Perlin noise wind
float wx = map(noise(t),0,1,-1,1);
PVector wind = new PVector(wx, 0);
t += 0.01;
line(width/2,height/2,width/2+wind.x*100,height/2+wind.y*100);
m.applyForce(wind);
// Gravity
PVector gravity = new PVector(0, 0.1);
//m.applyForce(gravity);
// Shake force
//m.shake();
// Boundary force
if (m.location.x > width - 50) {
PVector boundary = new PVector(-1,0);
m.applyForce(boundary);
} else if (m.location.x < 50) {
PVector boundary = new PVector(1,0);
m.applyForce(boundary);
}
m.update();
m.display();
//m.checkEdges();
}
// Instant Force
void mousePressed() {
PVector cannon = PVector.random2D();
cannon.mult(5);
m.applyForce(cannon);
}
@@ -0,0 +1,69 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover() {
location = new PVector(width/2,height/2);
velocity = new PVector(0,0);
acceleration = new PVector(0,0);
mass = 1;
}
void shake() {
PVector force = PVector.random2D();
force.mult(0.7);
applyForce(force);
}
void applyForce(PVector force) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
// Simple friction
velocity.mult(0.95);
}
void display() {
stroke(0);
strokeWeight(2);
fill(127);
ellipse(location.x,location.y,48,48);
}
void checkEdges() {
if (location.x > width) {
location.x = width;
velocity.x *= -1;
} else if (location.x < 0) {
velocity.x *= -1;
location.x = 0;
}
if (location.y > height) {
velocity.y *= -1;
location.y = height;
}
}
}
@@ -5,7 +5,7 @@
Mover m;
void setup() {
size(800,200);
size(640,360);
m = new Mover();
}
@@ -6,7 +6,7 @@ Mover m;
Attractor a;
void setup() {
size(800,200);
size(640,360);
m = new Mover();
a = new Attractor();
}
@@ -12,9 +12,9 @@ class Crawler {
PVector vel;
PVector acc;
float mass;
Oscillator osc;
Crawler() {
acc = new PVector();
vel = new PVector(random(-1,1),random(-1,1));
@@ -22,9 +22,9 @@ class Crawler {
mass = random(8,16);
osc = new Oscillator(mass*2);
}
void applyForce(PVector force) {
PVector f = force.get();
PVector f = force.get();
f.div(mass);
acc.add(f);
}
@@ -35,13 +35,13 @@ class Crawler {
loc.add(vel);
// Multiplying by 0 sets the all the components to 0
acc.mult(0);
osc.update(vel.mag()/10);
}
// Method to display
void display() {
float angle = vel.heading();
float angle = vel.heading2D();
pushMatrix();
translate(loc.x,loc.y);
rotate(angle);
@@ -49,10 +49,10 @@ class Crawler {
stroke(0);
fill(175,100);
ellipse(0,0,mass*2,mass*2);
osc.display(loc);
popMatrix();
}
}
@@ -0,0 +1,44 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
class CannonBall {
// All of our regular motion stuff
PVector location;
PVector velocity;
PVector acceleration;
// Size
float r = 8;
float topspeed = 10;
CannonBall(float x, float y) {
location = new PVector(x,y);
velocity = new PVector();
acceleration = new PVector();
}
// Standard Euler integration
void update() {
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
acceleration.mult(0);
}
void applyForce(PVector force) {
acceleration.add(force);
}
void display() {
stroke(0);
strokeWeight(2);
pushMatrix();
translate(location.x,location.y);
ellipse(0,0,r*2,r*2);
popMatrix();
}
}
@@ -0,0 +1,54 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// All of this stuff should go into a Cannon class
float angle = -PI/4;
PVector location = new PVector(50, 300);
boolean shot = false;
CannonBall ball;
void setup() {
size(640, 360);
ball = new CannonBall(location.x, location.y);
}
void draw() {
background(255);
pushMatrix();
translate(location.x, location.y);
rotate(angle);
rect(0, -5, 50, 10);
popMatrix();
if (shot) {
PVector gravity = new PVector(0, 0.2);
ball.applyForce(gravity);
ball.update();
}
ball.display();
if (ball.location.y > height) {
ball = new CannonBall(location.x, location.y);
shot = false;
}
}
void keyPressed() {
if (key == CODED && keyCode == RIGHT) {
angle += 0.1;
}
else if (key == CODED && keyCode == LEFT) {
angle -= 0.1;
}
else if (key == ' ') {
shot = true;
PVector force = PVector.fromAngle(angle);
force.mult(10);
ball.applyForce(force);
}
}
@@ -8,7 +8,7 @@
Spaceship ship;
void setup() {
size(750, 200);
size(640, 360);
ship = new Spaceship();
}
@@ -0,0 +1,57 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Mover object
Bob b1;
Bob b2;
Bob b3;
Spring s1;
Spring s2;
Spring s3;
void setup() {
size(640, 360);
// Create objects at starting location
// Note third argument in Spring constructor is "rest length"
b1 = new Bob(width/2, 100);
b2 = new Bob(width/2, 200);
b3 = new Bob(width/2, 300);
s1 = new Spring(b1,b2,100);
s2 = new Spring(b2,b3,100);
s3 = new Spring(b1,b3,100);
}
void draw() {
background(255);
s1.update();
s2.update();
s3.update();
s1.display();
s2.display();
s3.display();
b1.update();
b1.display();
b2.update();
b2.display();
b3.update();
b3.display();
b1.drag(mouseX, mouseY);
}
void mousePressed() {
b1.clicked(mouseX, mouseY);
}
void mouseReleased() {
b1.stopDragging();
}
@@ -0,0 +1,78 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Bob class, just like our regular Mover (location, velocity, acceleration, mass)
class Bob {
PVector location;
PVector velocity;
PVector acceleration;
float mass = 12;
// Arbitrary damping to simulate friction / drag
float damping = 0.95;
// For mouse interaction
PVector dragOffset;
boolean dragging = false;
// Constructor
Bob(float x, float y) {
location = new PVector(x,y);
velocity = new PVector();
acceleration = new PVector();
dragOffset = new PVector();
}
// Standard Euler integration
void update() {
velocity.add(acceleration);
velocity.mult(damping);
location.add(velocity);
acceleration.mult(0);
}
// Newton's law: F = M * A
void applyForce(PVector force) {
PVector f = force.get();
f.div(mass);
acceleration.add(f);
}
// Draw the bob
void display() {
stroke(0);
strokeWeight(2);
fill(175);
if (dragging) {
fill(50);
}
ellipse(location.x,location.y,mass*2,mass*2);
}
// The methods below are for mouse interaction
// This checks to see if we clicked on the mover
void clicked(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
dragging = true;
dragOffset.x = location.x-mx;
dragOffset.y = location.y-my;
}
}
void stopDragging() {
dragging = false;
}
void drag(int mx, int my) {
if (dragging) {
location.x = mx + dragOffset.x;
location.y = my + dragOffset.y;
}
}
}
@@ -0,0 +1,52 @@
// Nature of Code 2011
// Daniel Shiffman
// Chapter 3: Oscillation
// Class to describe an anchor point that can connect to "Bob" objects via a spring
// Thank you: http://www.myphysicslab.com/spring2d.html
class Spring {
// Location
PVector anchor;
// Rest length and spring constant
float len;
float k = 0.2;
Bob a;
Bob b;
// Constructor
Spring(Bob a_, Bob b_, int l) {
a = a_;
b = b_;
len = l;
}
// Calculate spring force
void update() {
// Vector pointing from anchor to bob location
PVector force = PVector.sub(a.location, b.location);
// What is distance
float d = force.mag();
// Stretch is difference between current distance and rest length
float stretch = d - len;
// Calculate force according to Hooke's Law
// F = k * stretch
force.normalize();
force.mult(-1 * k * stretch);
a.applyForce(force);
force.mult(-1);
b.applyForce(force);
}
void display() {
strokeWeight(2);
stroke(0);
line(a.location.x, a.location.y, b.location.x, b.location.y);
}
}
@@ -0,0 +1,50 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Mover object
Bob[] bobs = new Bob[5];
Spring[] springs = new Spring[4];
void setup() {
size(640, 360);
// Create objects at starting location
// Note third argument in Spring constructor is "rest length"
for (int i = 0; i < bobs.length; i++) {
bobs[i] = new Bob(width/2, i*40);
}
for (int i = 0; i < springs.length; i++) {
springs[i] = new Spring(bobs[i], bobs[i+1],40);
}
}
void draw() {
background(255);
for (Spring s : springs) {
s.update();
s.display();
}
for (Bob b : bobs) {
b.update();
b.display();
b.drag(mouseX, mouseY);
}
}
void mousePressed() {
for (Bob b : bobs) {
b.clicked(mouseX, mouseY);
}
}
void mouseReleased() {
for (Bob b : bobs) {
b.stopDragging();
}
}
@@ -0,0 +1,78 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Bob class, just like our regular Mover (location, velocity, acceleration, mass)
class Bob {
PVector location;
PVector velocity;
PVector acceleration;
float mass = 8;
// Arbitrary damping to simulate friction / drag
float damping = 0.95;
// For mouse interaction
PVector dragOffset;
boolean dragging = false;
// Constructor
Bob(float x, float y) {
location = new PVector(x,y);
velocity = new PVector();
acceleration = new PVector();
dragOffset = new PVector();
}
// Standard Euler integration
void update() {
velocity.add(acceleration);
velocity.mult(damping);
location.add(velocity);
acceleration.mult(0);
}
// Newton's law: F = M * A
void applyForce(PVector force) {
PVector f = force.get();
f.div(mass);
acceleration.add(f);
}
// Draw the bob
void display() {
stroke(0);
strokeWeight(2);
fill(175,120);
if (dragging) {
fill(50);
}
ellipse(location.x,location.y,mass*2,mass*2);
}
// The methods below are for mouse interaction
// This checks to see if we clicked on the mover
void clicked(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
dragging = true;
dragOffset.x = location.x-mx;
dragOffset.y = location.y-my;
}
}
void stopDragging() {
dragging = false;
}
void drag(int mx, int my) {
if (dragging) {
location.x = mx + dragOffset.x;
location.y = my + dragOffset.y;
}
}
}
@@ -0,0 +1,52 @@
// Nature of Code 2011
// Daniel Shiffman
// Chapter 3: Oscillation
// Class to describe an anchor point that can connect to "Bob" objects via a spring
// Thank you: http://www.myphysicslab.com/spring2d.html
class Spring {
// Location
PVector anchor;
// Rest length and spring constant
float len;
float k = 0.2;
Bob a;
Bob b;
// Constructor
Spring(Bob a_, Bob b_, int l) {
a = a_;
b = b_;
len = l;
}
// Calculate spring force
void update() {
// Vector pointing from anchor to bob location
PVector force = PVector.sub(a.location, b.location);
// What is distance
float d = force.mag();
// Stretch is difference between current distance and rest length
float stretch = d - len;
// Calculate force according to Hooke's Law
// F = k * stretch
force.normalize();
force.mult(-1 * k * stretch);
a.applyForce(force);
force.mult(-1);
b.applyForce(force);
}
void display() {
strokeWeight(2);
stroke(0);
line(a.location.x, a.location.y, b.location.x, b.location.y);
}
}
@@ -0,0 +1,78 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// A class for a draggable attractive body in our world
class Attractor {
float mass; // Mass, tied to size
float G; // Gravitational Constant
PVector location; // Location
boolean dragging = false; // Is the object being dragged?
boolean rollover = false; // Is the mouse over the ellipse?
PVector dragOffset; // holds the offset for when object is clicked on
Attractor() {
location = new PVector(width/2,height/2);
mass = 20;
G = 1;
dragOffset = new PVector(0.0,0.0);
}
PVector attract(Mover m) {
PVector force = PVector.sub(location,m.location); // Calculate direction of force
float d = force.mag(); // Distance between objects
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction)
float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> magnitude * direction
return force;
}
// Method to display
void display() {
ellipseMode(CENTER);
strokeWeight(4);
stroke(0);
if (dragging) fill (50);
else if (rollover) fill(100);
else fill(175,200);
ellipse(location.x,location.y,mass*2,mass*2);
}
// The methods below are for mouse interaction
void clicked(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
dragging = true;
dragOffset.x = location.x-mx;
dragOffset.y = location.y-my;
}
}
void hover(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
rollover = true;
}
else {
rollover = false;
}
}
void stopDragging() {
dragging = false;
}
void drag() {
if (dragging) {
location.x = mouseX + dragOffset.x;
location.y = mouseY + dragOffset.y;
}
}
}
@@ -0,0 +1,40 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
Mover m;
Attractor a;
void setup() {
size(640,360);
m = new Mover();
a = new Attractor();
}
void draw() {
background(255);
PVector force = a.attract(m);
m.applyForce(force);
m.update();
a.drag();
a.hover(mouseX,mouseY);
a.display();
m.display();
}
void mousePressed() {
a.clicked(mouseX,mouseY);
}
void mouseReleased() {
a.stopDragging();
}
@@ -0,0 +1,64 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover() {
location = new PVector(400,50);
velocity = new PVector(1,0);
acceleration = new PVector(0,0);
mass = 1;
}
void applyForce(PVector force) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
strokeWeight(2);
fill(127);
pushMatrix();
translate(location.x,location.y);
float heading = velocity.heading();
rotate(heading);
ellipse(0,0,16,16);
rectMode(CENTER);
// "20" should be a variable that is oscillating
// with sine function
rect(20,0,10,10);
popMatrix();
}
void checkEdges() {
if (location.x > width) {
location.x = 0;
} else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
velocity.y *= -1;
location.y = height;
}
}
}
@@ -0,0 +1,15 @@
float angle = 0;
void setup() {
size(400,400);
}
void draw() {
background(255);
float y = 100*sin(angle);
angle += 0.02;
fill(127);
translate(width/2,height/2);
line(0,0,0,y);
ellipse(0,y,16,16);
}
@@ -0,0 +1,34 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
float angle1 = 0;
float aVelocity1 = 0.01;
float amplitude1 = 300;
float angle2 = 0;
float aVelocity2 = 0.3;
float amplitude2 = 10;
void setup() {
size(640,360);
}
void draw() {
background(255);
float x = 0;
x += amplitude1 * cos(angle1);
x += amplitude2 * sin(angle2);
angle1 += aVelocity1;
angle2 += aVelocity2;
ellipseMode(CENTER);
stroke(0);
fill(175);
translate(width/2,height/2);
line(0,0,x,0);
ellipse(x,0,20,20);
}
@@ -35,7 +35,7 @@ class Mover {
}
void display() {
float theta = velocity.heading();
float theta = velocity.heading2D();
stroke(0);
strokeWeight(2);
@@ -52,14 +52,14 @@ class Mover {
if (location.x > width) {
location.x = 0;
}
}
else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
location.y = 0;
}
}
else if (location.y < 0) {
location.y = height;
}
@@ -9,7 +9,7 @@ Bob bob;
Spring spring;
void setup() {
size(800,200);
size(640,360);
// Create objects at starting location
// Note third argument in Spring constructor is "rest length"
spring = new Spring(width/2,10,100);
@@ -1,2 +0,0 @@
mode.id=processing.mode.java.JavaMode
mode=Standard
@@ -0,0 +1,26 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
ParticleSystem ps;
void setup() {
size(640,360);
ps = new ParticleSystem(new PVector(width/2,50));
}
void draw() {
background(255);
// Option #1 (move the Particle System origin)
ps.origin.set(mouseX,mouseY,0);
ps.addParticle();
ps.run();
// Option #2 (move the Particle System origin)
// ps.addParticle(mouseX,mouseY);
}
@@ -0,0 +1,50 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Simple Particle System
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l) {
acceleration = new PVector(0,0.05);
velocity = new PVector(random(-1,1),random(-2,0));
location = l.get();
lifespan = 255.0;
}
void run() {
update();
display();
}
// Method to update location
void update() {
velocity.add(acceleration);
location.add(velocity);
lifespan -= 2.0;
}
// Method to display
void display() {
stroke(0,lifespan);
strokeWeight(2);
fill(127,lifespan);
ellipse(location.x,location.y,12,12);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
@@ -0,0 +1,36 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Using Generics now! comment and annotate, etc.
class ParticleSystem {
ArrayList<Particle> particles;
PVector origin;
ParticleSystem(PVector location) {
origin = location.get();
particles = new ArrayList<Particle>();
}
void addParticle() {
particles.add(new Particle(origin));
}
void addParticle(float x, float y) {
particles.add(new Particle(new PVector(x, y)));
}
void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
@@ -0,0 +1,41 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Chapter 3: Asteroids exercise
// Mover object
Spaceship ship;
void setup() {
size(640, 360);
ship = new Spaceship();
}
void draw() {
background(255);
// Update location
ship.update();
// Wrape edges
ship.wrapEdges();
// Draw ship
ship.display();
fill(0);
//text("left right arrows to turn, z to thrust",10,height-5);
// Turn or thrust the ship depending on what key is pressed
if (keyPressed) {
if (key == CODED && keyCode == LEFT) {
ship.turn(-0.03);
} else if (key == CODED && keyCode == RIGHT) {
ship.turn(0.03);
} else if (key == 'z' || key == 'Z') {
ship.thrust();
}
}
}
@@ -0,0 +1,49 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Simple Particle System
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l,PVector dir) {
acceleration = dir.get();
velocity = PVector.random2D();
location = l.get();
lifespan = 255.0;
}
void run() {
update();
display();
}
// Method to update location
void update() {
velocity.add(acceleration);
location.add(velocity);
lifespan -= 2.0;
}
// Method to display
void display() {
noStroke();
fill(127,0,0,lifespan);
ellipse(location.x,location.y,12,12);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
@@ -0,0 +1,30 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Using Generics now! comment and annotate, etc.
class ParticleSystem {
ArrayList<Particle> particles;
ParticleSystem() {
particles = new ArrayList<Particle>();
}
void addParticle(float x, float y, PVector force) {
particles.add(new Particle(new PVector(x, y),force));
}
void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
@@ -0,0 +1,110 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Chapter 3: Asteroids
class Spaceship {
// All of our regular motion stuff
PVector location;
PVector velocity;
PVector acceleration;
ParticleSystem ps;
// Arbitrary damping to slow down ship
float damping = 0.995;
float topspeed = 6;
// Variable for heading!
float heading = 0;
// Size
float r = 16;
// Are we thrusting (to color boosters)
boolean thrusting = false;
Spaceship() {
location = new PVector(width/2,height/2);
velocity = new PVector();
acceleration = new PVector();
ps = new ParticleSystem();
}
// Standard Euler integration
void update() {
velocity.add(acceleration);
velocity.mult(damping);
velocity.limit(topspeed);
location.add(velocity);
acceleration.mult(0);
ps.run();
}
// Newton's law: F = M * A
void applyForce(PVector force) {
PVector f = force.get();
//f.div(mass); // ignoring mass right now
acceleration.add(f);
}
// Turn changes angle
void turn(float a) {
heading += a;
}
// Apply a thrust force
void thrust() {
// Offset the angle since we drew the ship vertically
float angle = heading - PI/2;
// Polar to cartesian for force vector!
PVector force = PVector.fromAngle(angle);
force.mult(0.1);
applyForce(force);
force.mult(-2);
ps.addParticle(location.x,location.y+r,force);
// To draw booster
thrusting = true;
}
void wrapEdges() {
float buffer = r*2;
if (location.x > width + buffer) location.x = -buffer;
else if (location.x < -buffer) location.x = width+buffer;
if (location.y > height + buffer) location.y = -buffer;
else if (location.y < -buffer) location.y = height+buffer;
}
// Draw the ship
void display() {
stroke(0);
strokeWeight(2);
pushMatrix();
translate(location.x,location.y+r);
rotate(heading);
fill(175);
if (thrusting) fill(255,0,0);
// Booster rockets
rect(-r/2,r,r/3,r/2);
rect(r/2,r,r/3,r/2);
fill(175);
// A triangle
beginShape();
vertex(-r,r);
vertex(0,-r);
vertex(r,r);
endShape(CLOSE);
rectMode(CENTER);
popMatrix();
thrusting = false;
}
}
@@ -0,0 +1,21 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
ParticleSystem ps;
void setup() {
size(640,360);
ps = new ParticleSystem(100,100,5);
}
void draw() {
background(255);
ps.display();
ps.update();
}
void mousePressed() {
ps.shatter();
}
@@ -0,0 +1,54 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Simple Particle System
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;
float r;
Particle(float x, float y, float r_) {
acceleration = new PVector(0,0.01);
velocity = PVector.random2D();
velocity.mult(0.5);
location = new PVector(x,y);
lifespan = 255.0;
r = r_;
}
void run() {
update();
display();
}
// Method to update location
void update() {
velocity.add(acceleration);
location.add(velocity);
lifespan -= 2.0;
}
// Method to display
void display() {
stroke(0);
fill(0);
rectMode(CENTER);
rect(location.x,location.y,r,r);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
@@ -0,0 +1,45 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Using Generics now! comment and annotate, etc.
class ParticleSystem {
ArrayList<Particle> particles;
int rows = 20;
int cols = 20;
boolean intact = true;
ParticleSystem(float x, float y, float r) {
particles = new ArrayList<Particle>();
for (int i = 0; i < rows*cols; i++) {
addParticle(x + (i%cols)*r, y + (i/rows)*r, r);
}
}
void addParticle(float x, float y, float r) {
particles.add(new Particle(x, y, r));
}
void display() {
for (Particle p : particles) {
p.display();
}
}
void shatter() {
intact = false;
}
void update() {
if (!intact) {
for (Particle p : particles) {
p.update();
}
}
}
}
@@ -0,0 +1,18 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
ParticleSystem ps;
void setup() {
size(640,360);
ps = new ParticleSystem(new PVector(width/2,50));
}
void draw() {
background(255);
ps.addParticle(mouseX,mouseY);
ps.update();
ps.intersection();
ps.display();
}
@@ -0,0 +1,73 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Simple Particle System
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;
float r = 6;
boolean highlight;
Particle(float x, float y) {
acceleration = new PVector(0, 0.05);
velocity = new PVector(random(-1, 1), random(-2, 0));
location = new PVector(x, y);
lifespan = 255.0;
}
void run() {
update();
display();
}
void intersects(ArrayList<Particle> particles) {
highlight = false;
for (Particle other : particles) {
if (other != this) {
float d = PVector.dist(other.location, location);
if (d < r + other.r) {
highlight = true;
}
}
}
}
void applyForce(PVector f) {
acceleration.add(f);
}
// Method to update location
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
lifespan -= 2.0;
}
// Method to display
void display() {
stroke(0, lifespan);
strokeWeight(2);
fill(127, lifespan);
if (highlight) {
fill(127,0,0);
}
ellipse(location.x, location.y, r*2, r*2);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
}
else {
return false;
}
}
}
@@ -0,0 +1,43 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Using Generics now! comment and annotate, etc.
class ParticleSystem {
ArrayList<Particle> particles;
ParticleSystem(PVector location) {
particles = new ArrayList<Particle>();
}
void addParticle(float x, float y) {
particles.add(new Particle(x, y));
}
void display() {
for (Particle p : particles) {
p.display();
}
}
void intersection() {
for (Particle p : particles) {
p.intersects(particles);
}
}
void update() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.update();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
@@ -0,0 +1,21 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
ParticleSystem ps;
void setup() {
size(640,360);
ps = new ParticleSystem(new PVector(width/2,50));
}
void draw() {
background(255);
ps.addParticle(random(width),random(height));
//PVector gravity = new PVector(0,0.1);
//ps.applyForce(gravity);
ps.update();
ps.intersection();
ps.display();
}
@@ -0,0 +1,70 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Simple Particle System
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;
float r = 6;
Particle(float x, float y) {
acceleration = new PVector();
velocity = PVector.random2D();
location = new PVector(x, y);
lifespan = 255.0;
}
void run() {
update();
display();
}
void intersects(ArrayList<Particle> particles) {
for (Particle other : particles) {
if (other != this) {
PVector dir = PVector.sub(location, other.location);
if (dir.mag() < r*2) {
dir.setMag(0.5);
applyForce(dir);
}
}
}
}
void applyForce(PVector f) {
acceleration.add(f);
}
// Method to update location
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
lifespan -= 0.5;
}
// Method to display
void display() {
stroke(0, lifespan);
strokeWeight(2);
fill(127, lifespan);
ellipse(location.x, location.y, r*2, r*2);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
}
else {
return false;
}
}
}
@@ -0,0 +1,48 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Using Generics now! comment and annotate, etc.
class ParticleSystem {
ArrayList<Particle> particles;
ParticleSystem(PVector location) {
particles = new ArrayList<Particle>();
}
void addParticle(float x, float y) {
particles.add(new Particle(x, y));
}
void display() {
for (Particle p : particles) {
p.display();
}
}
void applyForce(PVector f) {
for (Particle p : particles) {
p.applyForce(f);
}
}
void intersection() {
for (Particle p : particles) {
p.intersects(particles);
}
}
void update() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.update();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
@@ -0,0 +1,39 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Array of Images for particle textures
ParticleSystem ps;
PImage[] imgs;
void setup() {
size(640, 360, P2D);
imgs = new PImage[5];
imgs[0] = loadImage("corona.png");
imgs[1] = loadImage("emitter.png");
imgs[2] = loadImage("particle.png");
imgs[3] = loadImage("texture.png");
imgs[4] = loadImage("reflection.png");
ps = new ParticleSystem(imgs, new PVector(width/2, 50));
}
void draw() {
// Additive blending!
blendMode(ADD);
background(0);
PVector up = new PVector(0,-0.2);
ps.applyForce(up);
ps.run();
for (int i = 0; i < 5; i++) {
ps.addParticle(mouseX,mouseY);
}
}
@@ -0,0 +1,59 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Simple Particle System
class Particle {
PVector loc;
PVector vel;
PVector acc;
float lifespan;
PImage img;
// Another constructor (the one we are using here)
Particle(float x, float y, PImage img_) {
// Boring example with constant acceleration
acc = new PVector(0, 0);
vel = PVector.random2D();
loc = new PVector(x, y);
lifespan = 255;
img = img_;
}
void run() {
update();
render();
}
void applyForce(PVector f) {
acc.add(f);
}
// Method to update location
void update() {
vel.add(acc);
loc.add(vel);
acc.mult(0);
lifespan -= 2.0;
}
// Method to display
void render() {
imageMode(CENTER);
tint(lifespan);
image(img, loc.x, loc.y, 32, 32);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan <= 0.0) {
return true;
}
else {
return false;
}
}
}
@@ -0,0 +1,55 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles
class ParticleSystem {
ArrayList<Particle> particles; // An arraylist for all the particles
PImage[] textures;
ParticleSystem(PImage[] imgs, PVector v) {
textures = imgs;
particles = new ArrayList(); // Initialize the arraylist
}
void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
void addParticle(float x, float y) {
int r = int(random(textures.length));
particles.add(new Particle(x,y,textures[r]));
}
void applyForce(PVector f) {
for (Particle p : particles) {
p.applyForce(f);
}
}
void addParticle(Particle p) {
particles.add(p);
}
// A method to test if the particle system still has particles
boolean dead() {
if (particles.isEmpty()) {
return true;
}
else {
return false;
}
}
}
@@ -5,9 +5,8 @@
ArrayList<Particle> particles;
void setup() {
size(800,200);
size(640,360);
particles = new ArrayList<Particle>();
smooth();
}
void draw() {
@@ -15,13 +14,12 @@ void draw() {
particles.add(new Particle(new PVector(width/2,50)));
// Using the iterator
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
// Looping through backwards to delete
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
it.remove();
particles.remove(i);
}
}
}
@@ -5,7 +5,7 @@
ParticleSystem ps;
void setup() {
size(800,200);
size(640,360);
ps = new ParticleSystem(new PVector(width/2,50));
}
@@ -18,12 +18,11 @@ class ParticleSystem {
}
void run() {
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
it.remove();
particles.remove(i);
}
}
}
@@ -32,4 +31,3 @@ class ParticleSystem {
@@ -11,9 +11,8 @@
ArrayList<ParticleSystem> systems;
void setup() {
size(800,200);
size(640,360);
systems = new ArrayList<ParticleSystem>();
smooth();
}
void draw() {
@@ -21,21 +21,15 @@ class ParticleSystem {
}
void run() {
// Using the Iterator b/c we are deleting from list while iterating
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
it.remove();
particles.remove(i);
}
}
}
void addParticle() {
particles.add(new Particle(origin));
}
void addParticle(Particle p) {
particles.add(p);
}
@@ -44,12 +38,11 @@ class ParticleSystem {
boolean dead() {
if (particles.isEmpty()) {
return true;
} else {
}
else {
return false;
}
}
}
@@ -5,7 +5,7 @@
ParticleSystem ps;
void setup() {
size(800,200);
size(640,360);
ps = new ParticleSystem(new PVector(width/2,50));
}
@@ -15,18 +15,18 @@ class ParticleSystem {
float r = random(1);
if (r < 0.5) {
particles.add(new Particle(origin));
} else {
}
else {
particles.add(new Confetti(origin));
}
}
void run() {
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
it.remove();
particles.remove(i);
}
}
}
@@ -34,4 +34,3 @@ class ParticleSystem {
@@ -5,7 +5,7 @@
ParticleSystem ps;
void setup() {
size(800,200);
size(640,360);
ps = new ParticleSystem(new PVector(width/2,50));
}
@@ -23,15 +23,13 @@ class ParticleSystem {
}
void run() {
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
it.remove();
particles.remove(i);
}
}
}
}
@@ -6,7 +6,7 @@ ParticleSystem ps;
Repeller repeller;
void setup() {
size(800,200);
size(640,360);
ps = new ParticleSystem(new PVector(width/2,50));
repeller = new Repeller(width/2-20,height/2);
}
@@ -31,16 +31,14 @@ class ParticleSystem {
void run() {
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
it.remove();
particles.remove(i);
}
}
}
}
@@ -10,19 +10,16 @@
/* @pjs preload="processingjs/chapter04/_4_08_ParticleSystemSmoke/data/texture.png"; */
ParticleSystem ps;
Random generator;
void setup() {
size(383,200);
generator = new Random();
size(640,360);
PImage img = loadImage("texture.png");
ps = new ParticleSystem(0,new PVector(width/2,height-25),img);
smooth();
ps = new ParticleSystem(0,new PVector(width/2,height-75),img);
}
void draw() {
background(0);
// Calculate a "wind" force based on mouse horizontal position
float dx = map(mouseX,0,width,-0.2,0.2);
PVector wind = new PVector(dx,0);
@@ -31,7 +28,7 @@ void draw() {
for (int i = 0; i < 2; i++) {
ps.addParticle();
}
// Draw an arrow representing the wind force
drawVector(wind, new PVector(width/2,50,0),500);
@@ -45,7 +42,7 @@ void drawVector(PVector v, PVector loc, float scayl) {
translate(loc.x,loc.y);
stroke(255);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading());
rotate(v.heading2D());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
@@ -11,8 +11,8 @@ class Particle {
Particle(PVector l,PImage img_) {
acc = new PVector(0,0);
float vx = (float) generator.nextGaussian()*0.3;
float vy = (float) generator.nextGaussian()*0.3 - 1.0;
float vx = randomGaussian()*0.3;
float vy = randomGaussian()*0.3 - 1.0;
vel = new PVector(vx,vy);
loc = l.get();
lifespan = 100.0;
@@ -50,7 +50,7 @@ class Particle {
}
// Is the particle still useful?
boolean dead() {
boolean isDead() {
if (lifespan <= 0.0) {
return true;
} else {
@@ -12,7 +12,7 @@ class ParticleSystem {
ArrayList<Particle> particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
PImage img;
ParticleSystem(int num, PVector v, PImage img_) {
particles = new ArrayList<Particle>(); // Initialize the arraylist
origin = v.get(); // Store the origin point
@@ -23,43 +23,27 @@ class ParticleSystem {
}
void run() {
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.dead()) {
it.remove();
if (p.isDead()) {
particles.remove(i);
}
}
}
// Method to add a force vector to all particles currently in the system
void applyForce(PVector dir) {
// Enhanced loop!!!
for (Particle p: particles) {
p.applyForce(dir);
}
}
void addParticle() {
particles.add(new Particle(origin,img));
}
void addParticle(Particle p) {
particles.add(p);
}
// A method to test if the particle system still has particles
boolean dead() {
if (particles.isEmpty()) {
return true;
} else {
return false;
}
particles.add(new Particle(origin, img));
}
}
@@ -9,20 +9,22 @@
/* @pjs preload="processingjs/chapter04/_4_08_ParticleSystemSmoke/data/texture.png"; */
import java.util.Random;
ParticleSystem ps;
Random generator;
void setup() {
size(383,200);
size(640,360);
generator = new Random();
PImage img = loadImage("texture.png");
ps = new ParticleSystem(0,new PVector(width/2,height-25),img);
ps = new ParticleSystem(0,new PVector(width/2,height-75),img);
smooth();
}
void draw() {
background(0);
// Calculate a "wind" force based on mouse horizontal position
float dx = map(mouseX,0,width,-0.2,0.2);
PVector wind = new PVector(dx,0);
@@ -31,7 +33,7 @@ void draw() {
for (int i = 0; i < 2; i++) {
ps.addParticle();
}
// Draw an arrow representing the wind force
drawVector(wind, new PVector(width/2,50,0),500);
@@ -45,7 +47,7 @@ void drawVector(PVector v, PVector loc, float scayl) {
translate(loc.x,loc.y);
stroke(255);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading());
rotate(v.heading2D());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
@@ -53,7 +53,7 @@ class Particle {
}
// Is the particle still useful?
boolean dead() {
boolean isDead() {
if (lifespan <= 0.0) {
return true;
} else {
@@ -22,15 +22,14 @@ class ParticleSystem {
}
void run() {
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
p.run();
if (p.dead()) {
it.remove();
}
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
// Method to add a force vector to all particles currently in the system
void applyForce(PVector dir) {
@@ -2,28 +2,31 @@
// Daniel Shiffman
// http://natureofcode.com
// Smoke Particle System
// Additive Blending
// A basic smoke effect using a particle system
// Each particle is rendered as an alpha masked image
// This example demonstrates a "glow" like effect using
// additive blending with a Particle system. By playing
// with colors, textures, etc. you can achieve a variety
// of looks.
ParticleSystem ps;
PImage img;
void setup() {
size(800, 200, P2D);
size(640, 360, P2D);
// Create an alpha masked image to be applied as the particle's texture
img = loadImage("texture.png");
ps = new ParticleSystem(0, new PVector(width/2, 50));
}
}
void draw() {
// Additive blending!
blendMode(ADD);
background(0);
ps.run();
@@ -40,7 +40,7 @@ class Particle {
}
// Is the particle still useful?
boolean dead() {
boolean isDead() {
if (lifespan <= 0.0) {
return true;
} else {
@@ -9,7 +9,7 @@ class ParticleSystem {
ArrayList<Particle> particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
PImage tex;
ParticleSystem(int num, PVector v) {
@@ -21,12 +21,11 @@ class ParticleSystem {
}
void run() {
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.dead()) {
it.remove();
if (p.isDead()) {
particles.remove(i);
}
}
}
@@ -43,12 +42,11 @@ class ParticleSystem {
boolean dead() {
if (particles.isEmpty()) {
return true;
} else {
}
else {
return false;
}
}
}
@@ -15,18 +15,18 @@ class ParticleSystem {
float r = random(1);
if (r < 0.5) {
particles.add(new Particle(origin));
} else {
}
else {
particles.add(new ParticleChild(origin));
}
}
void run() {
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
it.remove();
particles.remove(i);
}
}
}
@@ -34,4 +34,3 @@ class ParticleSystem {
@@ -33,7 +33,7 @@ class Emitter{
iterateListExist();
render();
gl.glDisable( GL.GL_TEXTURE_2D );
pgl.disable( PGL.TEXTURE_2D );
if( ALLOWTRAILS )
iterateListRenderTrails();
@@ -3,10 +3,7 @@
// http://natureofcode.com
// Updated version of flight404 Particle Emitter release 1
// This works with Processing 1.0
// All of the advanced openGL direct calls that use display lists, etc. have been stripped out
// It's my intention to redo this example using GlGraphics (http://glgraphics.sourceforge.net/)
// But for now, just want to make sure it works in principal
// This works with Processing 2.0
// February 28 2011
// Daniel Shiffman
@@ -49,12 +46,9 @@
import toxi.geom.*;
import processing.opengl.*;
import javax.media.opengl.*;
PGraphicsOpenGL pgl;
GL gl;
import java.util.*;
PGL pgl;
Emitter emitter;
Vec3D gravity;
@@ -74,17 +68,17 @@ boolean ALLOWFLOOR; // add a floor?
// slow down.
void setup(){
size( 600, 600, OPENGL );
size( 600, 600, P3D );
smooth(4);
// Lately I have gotten into the habit of limiting the color range to be
// 0.0 to 1.0. It works this way in OpenGL so I might as well get used to it.
colorMode( RGB, 1.0 );
// Turn on 4X antialiasing
hint( ENABLE_OPENGL_4X_SMOOTH );
//hint( ENABLE_OPENGL_4X_SMOOTH );
// More OpenGL necessity.
pgl = (PGraphicsOpenGL) g;
gl = pgl.gl;
pgl = ((PGraphicsOpenGL) g).pgl;
// Loads in a particle image from the data folder. Image size should be a power of 2.
particleImg = loadImage( "particle.png" );
@@ -101,9 +95,9 @@ void draw(){
// Turns on additive blending so we can draw a bunch of glowing images without
// needing to do any depth testing.
gl.glDepthMask(false);
gl.glEnable( GL.GL_BLEND );
gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE);
pgl.depthMask(false);
pgl.enable( PGL.BLEND );
pgl.blendFunc(PGL.SRC_ALPHA, PGL.ONE);
emitter.exist();
@@ -28,7 +28,7 @@ class Emitter{
iterateListExist();
render();
gl.glDisable( GL.GL_TEXTURE_2D );
pgl.disable( PGL.TEXTURE_2D );
if( ALLOWTRAILS )
iterateListRenderTrails();
@@ -51,7 +51,7 @@ class Emitter{
}
void iterateListExist(){
gl.glEnable( GL.GL_TEXTURE_2D );
pgl.enable( PGL.TEXTURE_2D );
int mylength = particles.size();
@@ -58,11 +58,9 @@
import damkjer.ocd.*;
import toxi.geom.*;
import processing.opengl.*;
import javax.media.opengl.*;
import java.util.*;
PGraphicsOpenGL pgl;
GL gl;
PGL pgl;
POV pov;
Images images;
@@ -72,9 +70,6 @@ Cursor mouse;
Vec3D gravity;
float floorLevel;
int counter;
int saveCount;
int xSize, ySize;
@@ -90,13 +85,11 @@ boolean ALLOWFLOOR = true;
void setup() {
size( 750, 750, OPENGL );
hint( ENABLE_OPENGL_4X_SMOOTH );
size( 750, 750, P3D );
smooth(4);
colorMode( RGB, 1.0 );
pgl = (PGraphicsOpenGL) g;
gl = pgl.gl;
pgl = ((PGraphicsOpenGL) g).pgl;
xSize = width;
ySize = height;
@@ -117,13 +110,13 @@ void draw() {
pov.exist();
mouse.exist();
gl.glDepthMask(false);
gl.glEnable( GL.GL_BLEND );
gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE);
pgl.depthMask(false);
pgl.enable( PGL.BLEND );
pgl.blendFunc(PGL.SRC_ALPHA,PGL.ONE);
emitter.exist();
if( mousePressed && mouseButton == LEFT )
if (mousePressed)
emitter.addParticles(2);
counter ++;
@@ -9,7 +9,7 @@ void drawVector(PVector v, PVector loc, float scayl) {
translate(loc.x,loc.y);
stroke(0);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading());
rotate(v.heading2D());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
@@ -6,7 +6,7 @@
ArrayList<Box> boxes;
void setup() {
size(800,200);
size(640,360);
// Create ArrayLists
boxes = new ArrayList<Box>();
}
@@ -13,7 +13,7 @@ ArrayList<Box> boxes;
PBox2D box2d;
void setup() {
size(800, 200);
size(640, 360);
// Initialize and create the Box2D world
box2d = new PBox2D(this);
box2d.createWorld();
@@ -18,7 +18,7 @@ ArrayList<Boundary> boundaries;
ArrayList<Box> boxes;
void setup() {
size(800,200);
size(640,360);
// Initialize box2d physics and create the world
box2d = new PBox2D(this);
box2d.createWorld();
@@ -21,7 +21,7 @@ ArrayList<Particle> particles;
Surface surface;
void setup() {
size(800,200);
size(640,360);
// Initialize box2d physics and create the world
box2d = new PBox2D(this);
box2d.createWorld();
@@ -13,7 +13,7 @@ class Surface {
surface = new ArrayList<Vec2>();
// Here we keep track of the screen coordinates of the chain
surface.add(new Vec2(0, height/2));
//surface.add(new Vec2(width/2, height/2+50));
surface.add(new Vec2(width/2, height/2+50));
surface.add(new Vec2(width, height/2));
// This is what box2d uses to put the surface in its world
@@ -18,7 +18,7 @@ ArrayList<Boundary> boundaries;
ArrayList<CustomShape> polygons;
void setup() {
size(800,200);
size(640,360);
// Initialize box2d physics and create the world
box2d = new PBox2D(this);
box2d.createWorld();
@@ -18,7 +18,7 @@ ArrayList<Boundary> boundaries;
ArrayList<Lollipop> pops;
void setup() {
size(800,200);
size(640,360);
// Initialize box2d physics and create the world
box2d = new PBox2D(this,20);
box2d.createWorld();
@@ -40,7 +40,7 @@ void draw() {
background(255);
// We must always step through time!
if (mousePressed) box2d.step();
box2d.step();
// Display all the boundaries
for (Boundary wall: boundaries) {
@@ -25,7 +25,7 @@ ArrayList<Boundary> boundaries;
ArrayList<Pair> pairs;
void setup() {
size(800,200);
size(640,360);
// Initialize box2d physics and create the world
box2d = new PBox2D(this);
box2d.createWorld();
@@ -23,7 +23,7 @@ Windmill windmill;
ArrayList<Particle> particles;
void setup() {
size(800,200);
size(640,360);
// Initialize box2d physics and create the world
box2d = new PBox2D(this);
box2d.createWorld();
@@ -25,7 +25,7 @@ Box box;
Spring spring;
void setup() {
size(800,200);
size(640,360);
// Initialize box2d physics and create the world
box2d = new PBox2D(this);
box2d.createWorld();
@@ -23,7 +23,7 @@ ArrayList<Particle> particles;
Boundary wall;
void setup() {
size(800, 200);
size(640, 360);
// Initialize box2d physics and create the world
box2d = new PBox2D(this);
box2d.createWorld();
@@ -46,7 +46,7 @@ Blanket b;
void setup() {
size(800,240);
size(640,360);
physics=new VerletPhysics2D();
physics.addBehavior(new GravityBehavior(new Vec2D(0,0.1)));
@@ -6,7 +6,7 @@
*/
/*
* Copyright (c) 2010 Daniel Schiffmann
* Copyright (c) 2010 Daniel Shiffman
*
* This demo & library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -43,7 +43,7 @@ boolean showParticles = true;
PFont f;
void setup() {
size(800,300);
size(640,360);
f = createFont("Georgia",12,true);
// Initialize the physics
@@ -15,8 +15,7 @@ Particle p1;
Particle p2;
void setup() {
size(800,200);
frameRate(30);
size(640,360);
// Initialize the physics
physics=new VerletPhysics2D();
@@ -27,12 +26,12 @@ void setup() {
// Make two particles
p1 = new Particle(new Vec2D(width/2,20));
p2 = new Particle(new Vec2D(width,180));
p2 = new Particle(new Vec2D(width/2+160,20));
// Lock one in place
p1.lock();
// Make a spring connecting both Particles
VerletSpring2D spring=new VerletSpring2D(p1,p2,80,0.01);
VerletSpring2D spring=new VerletSpring2D(p1,p2,160,0.01);
// Anything we make, we have to add into the physics world
physics.addParticle(p1);
@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2010 Daniel Shiffmann
* Copyright (c) 2010 Daniel Shiffman
*
* This demo & library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -35,7 +35,7 @@ VerletPhysics2D physics;
Chain chain;
void setup() {
size(800, 200);
size(640, 360);
// Initialize the physics world
physics=new VerletPhysics2D();
physics.addBehavior(new GravityBehavior(new Vec2D(0, 0.1)));
@@ -22,8 +22,7 @@ boolean showParticles = true;
PFont f;
void setup() {
size(800, 200);
frameRate(30);
size(640, 360);
f = createFont("Georgia", 12, true);
// Initialize the physics
@@ -8,7 +8,7 @@ Attractor attractor;
VerletPhysics2D physics;
void setup () {
size (800, 200);
size (640, 360);
physics = new VerletPhysics2D ();
physics.setDrag (0.01);
@@ -52,16 +52,16 @@ class Vehicle {
circleloc.normalize(); // Normalize to get heading
circleloc.mult(wanderD); // Multiply by distance
circleloc.add(location); // Make it relative to boid's location
float h = velocity.heading(); // We need to know the heading to offset wandertheta
float h = velocity.heading2D(); // We need to know the heading to offset wandertheta
PVector circleOffSet = new PVector(wanderR*cos(wandertheta+h),wanderR*sin(wandertheta+h));
PVector target = PVector.add(circleloc,circleOffSet);
seek(target);
// Render wandering circle, etc.
// Render wandering circle, etc.
if (debug) drawWanderStuff(location,circleloc,target,wanderR);
}
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
@@ -86,7 +86,7 @@ class Vehicle {
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading() + radians(90);
float theta = velocity.heading2D() + radians(90);
fill(127);
stroke(0);
pushMatrix();
@@ -112,7 +112,7 @@ class Vehicle {
// A method just to draw the circle associated with wandering
void drawWanderStuff(PVector location, PVector circle, PVector target, float rad) {
stroke(0);
stroke(0);
noFill();
ellipseMode(CENTER);
ellipse(circle.x,circle.y,rad*2,rad*2);
@@ -15,7 +15,7 @@ void draw() {
// A "vector" (really a point) to store the mouse location and screen center location
PVector mouseLoc = new PVector(mouseX, mouseY);
PVector centerLoc = new PVector(width/2, height/2);
PVector centerLoc = new PVector(width/2, height/2);
// Aha, a vector to store the displacement between the mouse and center
PVector v = PVector.sub(mouseLoc, centerLoc);
@@ -43,10 +43,10 @@ void drawVector(PVector v, PVector loc, float scayl) {
stroke(0);
strokeWeight(2);
// Call vector heading function to get direction (pointing up is a heading of 0)
rotate(v.heading());
rotate(v.heading2D());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow
// Draw three lines to make an arrow
line(0, 0, len, 0);
line(len, 0, len-arrowsize, +arrowsize/2);
line(len, 0, len-arrowsize, -arrowsize/2);
@@ -12,9 +12,8 @@
Vehicle v;
void setup() {
size(800, 200);
size(640, 360);
v = new Vehicle(width/2, height/2);
smooth();
}
void draw() {
@@ -7,7 +7,7 @@
// The "Vehicle" class
class Vehicle {
PVector location;
PVector velocity;
PVector acceleration;
@@ -44,20 +44,20 @@ class Vehicle {
// STEER = DESIRED MINUS VELOCITY
void seek(PVector target) {
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Steering = Desired minus velocity
PVector steer = PVector.sub(desired,velocity);
steer.limit(maxforce); // Limit to maximum steering force
applyForce(steer);
}
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading() + PI/2;
float theta = velocity.heading2D() + PI/2;
fill(127);
stroke(0);
strokeWeight(1);
@@ -70,8 +70,8 @@ class Vehicle {
vertex(r, r*2);
endShape(CLOSE);
popMatrix();
}
}

Some files were not shown because too many files have changed in this diff Show More