mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Merge branch 'master' of github.com:processing/processing
This commit is contained in:
@@ -236,23 +236,23 @@ editor.token.function2.style = #006699,plain
|
||||
editor.token.function3.style = #669900,plain
|
||||
editor.token.function4.style = #006699,bold
|
||||
|
||||
editor.token.keyword1.style = #72a7c2,plain
|
||||
editor.token.keyword2.style = #72a7c2,plain
|
||||
editor.token.keyword1.style = #33997e,plain
|
||||
editor.token.keyword2.style = #33997e,plain
|
||||
editor.token.keyword3.style = #669900,plain
|
||||
editor.token.keyword4.style = #ff6699,plain
|
||||
editor.token.keyword5.style = #e37139,plain
|
||||
editor.token.keyword4.style = #d94a7a,plain
|
||||
editor.token.keyword5.style = #e2661a,plain
|
||||
|
||||
editor.token.literal1.style = #7D4793,plain
|
||||
editor.token.literal2.style = #617952,plain
|
||||
editor.token.literal2.style = #718a62,plain
|
||||
|
||||
editor.token.operator.style = #006699,plain
|
||||
|
||||
editor.token.label.style = #7e7e7e,bold
|
||||
editor.token.label.style = #666666,bold
|
||||
|
||||
editor.token.comment1.style = #7e7e7e,plain
|
||||
editor.token.comment2.style = #7e7e7e,plain
|
||||
editor.token.comment1.style = #666666,plain
|
||||
editor.token.comment2.style = #666666,plain
|
||||
|
||||
editor.token.invalid.style = #7e7e7e,bold
|
||||
editor.token.invalid.style = #666666,bold
|
||||
|
||||
|
||||
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
@@ -5185,7 +5185,23 @@ public class PApplet extends Applet
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ( begin auto-generated from randomGaussian.xml )
|
||||
*
|
||||
* Returns a float from a random series of numbers having a mean of 0
|
||||
* and standard deviation of 1. Each time the <b>randomGaussian()</b>
|
||||
* function is called, it returns a number fitting a Gaussian, or
|
||||
* normal, distribution. There is theoretically no minimum or maximum
|
||||
* value that <b>randomGaussian()</b> might return. Rather, there is
|
||||
* just a very low probability that values far from the mean will be
|
||||
* returned; and a higher probability that numbers near the mean will
|
||||
* be returned.
|
||||
*
|
||||
* ( end auto-generated )
|
||||
* @webref math:random
|
||||
* @see PApplet#random(float,float)
|
||||
* @see PApplet#noise(float, float, float)
|
||||
*/
|
||||
public final float randomGaussian() {
|
||||
if (internalRandom == null) {
|
||||
internalRandom = new Random();
|
||||
@@ -10958,6 +10974,7 @@ public class PApplet extends Applet
|
||||
* @webref image:textures
|
||||
* @param mode either IMAGE or NORMAL
|
||||
* @see PGraphics#texture(PImage)
|
||||
* @see PGraphics#textureWrap(int)
|
||||
*/
|
||||
public void textureMode(int mode) {
|
||||
if (recorder != null) recorder.textureMode(mode);
|
||||
@@ -10974,6 +10991,8 @@ public class PApplet extends Applet
|
||||
*
|
||||
* @webref image:textures
|
||||
* @param wrap Either CLAMP (default) or REPEAT
|
||||
* @see PGraphics#texture(PImage)
|
||||
* @see PGraphics#textureMode(int)
|
||||
*/
|
||||
public void textureWrap(int wrap) {
|
||||
if (recorder != null) recorder.textureWrap(wrap);
|
||||
@@ -10995,6 +11014,7 @@ public class PApplet extends Applet
|
||||
* @webref image:textures
|
||||
* @param image reference to a PImage object
|
||||
* @see PGraphics#textureMode(int)
|
||||
* @see PGraphics#textureWrap(int)
|
||||
* @see PGraphics#beginShape(int)
|
||||
* @see PGraphics#endShape(int)
|
||||
* @see PGraphics#vertex(float, float, float, float, float)
|
||||
|
||||
@@ -1201,6 +1201,7 @@ public class PGraphics extends PImage implements PConstants {
|
||||
* @webref image:textures
|
||||
* @param mode either IMAGE or NORMAL
|
||||
* @see PGraphics#texture(PImage)
|
||||
* @see PGraphics#textureWrap(int)
|
||||
*/
|
||||
public void textureMode(int mode) {
|
||||
this.textureMode = mode;
|
||||
@@ -1215,6 +1216,8 @@ public class PGraphics extends PImage implements PConstants {
|
||||
*
|
||||
* @webref image:textures
|
||||
* @param wrap Either CLAMP (default) or REPEAT
|
||||
* @see PGraphics#texture(PImage)
|
||||
* @see PGraphics#textureMode(int)
|
||||
*/
|
||||
public void textureWrap(int wrap) {
|
||||
showMissingWarning("textureWrap");
|
||||
@@ -1235,6 +1238,7 @@ public class PGraphics extends PImage implements PConstants {
|
||||
* @webref image:textures
|
||||
* @param image reference to a PImage object
|
||||
* @see PGraphics#textureMode(int)
|
||||
* @see PGraphics#textureWrap(int)
|
||||
* @see PGraphics#beginShape(int)
|
||||
* @see PGraphics#endShape(int)
|
||||
* @see PGraphics#vertex(float, float, float, float, float)
|
||||
|
||||
@@ -67,6 +67,9 @@ import processing.core.PConstants;
|
||||
* <p>File names should end with .csv if they're comma separated.</p>
|
||||
*
|
||||
* @webref data:composite
|
||||
* @see PApplet#createTable()
|
||||
* @see PApplet#loadTable(String)
|
||||
* @see PApplet#saveTable(Table, String)
|
||||
*/
|
||||
public class Table {
|
||||
protected int rowCount;
|
||||
@@ -987,16 +990,26 @@ public class Table {
|
||||
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
|
||||
|
||||
/**
|
||||
* @webref table:method
|
||||
* @brief Adds a new column to the table
|
||||
*/
|
||||
public void addColumn() {
|
||||
addColumn(null, STRING);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param title the title to be used for the new column
|
||||
*/
|
||||
public void addColumn(String title) {
|
||||
addColumn(title, STRING);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param type the type to be used for the new column: INT, LONG, FLOAT, DOUBLE, STRING, or CATEGORICAL
|
||||
*/
|
||||
public void addColumn(String title, int type) {
|
||||
insertColumn(columns.length, title, type);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Random Gaussian.
|
||||
*
|
||||
* This sketch draws ellipses with x and y locations tied to a gaussian distribution of random numbers.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
background(0);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
// Get a gaussian random number w/ mean of 0 and standard deviation of 1.0
|
||||
float val = randomGaussian();
|
||||
|
||||
float sd = 60; // Define a standard deviation
|
||||
float mean = width/2; // Define a mean value (middle of the screen along the x-axis)
|
||||
float x = ( val * sd ) + mean; // Scale the gaussian random number by standard deviation and mean
|
||||
|
||||
noStroke();
|
||||
fill(255, 10);
|
||||
noStroke();
|
||||
ellipse(x, height/2, 32, 32); // Draw an ellipse at our "normal" random location
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
+57
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ Mover m;
|
||||
Attractor a;
|
||||
|
||||
void setup() {
|
||||
size(800,200);
|
||||
size(640,360);
|
||||
m = new Mover();
|
||||
a = new Attractor();
|
||||
}
|
||||
|
||||
+9
-9
@@ -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();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+44
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
+54
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@
|
||||
Spaceship ship;
|
||||
|
||||
void setup() {
|
||||
size(750, 200);
|
||||
size(640, 360);
|
||||
ship = new Spaceship();
|
||||
}
|
||||
|
||||
|
||||
+57
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+50
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
+78
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+52
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+78
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
+40
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
+15
@@ -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);
|
||||
}
|
||||
+34
@@ -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);
|
||||
}
|
||||
+3
-3
@@ -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;
|
||||
}
|
||||
|
||||
+1
-1
@@ -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
|
||||
+26
@@ -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);
|
||||
|
||||
|
||||
|
||||
}
|
||||
+50
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+36
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+41
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+30
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+110
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+21
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+45
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+18
@@ -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();
|
||||
}
|
||||
+73
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+43
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+21
@@ -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();
|
||||
}
|
||||
+70
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+48
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+39
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+59
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+55
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Binary file not shown.
+5
-7
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
ParticleSystem ps;
|
||||
|
||||
void setup() {
|
||||
size(800,200);
|
||||
size(640,360);
|
||||
ps = new ParticleSystem(new PVector(width/2,50));
|
||||
}
|
||||
|
||||
|
||||
+3
-5
@@ -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 {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+1
-2
@@ -11,9 +11,8 @@
|
||||
ArrayList<ParticleSystem> systems;
|
||||
|
||||
void setup() {
|
||||
size(800,200);
|
||||
size(640,360);
|
||||
systems = new ArrayList<ParticleSystem>();
|
||||
smooth();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
+5
-12
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
ParticleSystem ps;
|
||||
|
||||
void setup() {
|
||||
size(800,200);
|
||||
size(640,360);
|
||||
ps = new ParticleSystem(new PVector(width/2,50));
|
||||
}
|
||||
|
||||
|
||||
+5
-6
@@ -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 {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
ParticleSystem ps;
|
||||
|
||||
void setup() {
|
||||
size(800,200);
|
||||
size(640,360);
|
||||
ps = new ParticleSystem(new PVector(width/2,50));
|
||||
}
|
||||
|
||||
|
||||
+3
-5
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -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);
|
||||
}
|
||||
|
||||
+3
-5
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
+5
-8
@@ -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)
|
||||
|
||||
+3
-3
@@ -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 {
|
||||
|
||||
+7
-23
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
+7
-5
@@ -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)
|
||||
|
||||
+1
-1
@@ -53,7 +53,7 @@ class Particle {
|
||||
}
|
||||
|
||||
// Is the particle still useful?
|
||||
boolean dead() {
|
||||
boolean isDead() {
|
||||
if (lifespan <= 0.0) {
|
||||
return true;
|
||||
} else {
|
||||
|
||||
+6
-7
@@ -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) {
|
||||
|
||||
+10
-7
@@ -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();
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ class Particle {
|
||||
}
|
||||
|
||||
// Is the particle still useful?
|
||||
boolean dead() {
|
||||
boolean isDead() {
|
||||
if (lifespan <= 0.0) {
|
||||
return true;
|
||||
} else {
|
||||
|
||||
+7
-9
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
+5
-6
@@ -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 {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
ParticleSystem ps;
|
||||
|
||||
void setup() {
|
||||
size(200,200);
|
||||
size(640,360);
|
||||
ps = new ParticleSystem(new PVector(width/2,50));
|
||||
}
|
||||
|
||||
Regular → Executable
Regular → Executable
+1
-1
@@ -33,7 +33,7 @@ class Emitter{
|
||||
iterateListExist();
|
||||
render();
|
||||
|
||||
gl.glDisable( GL.GL_TEXTURE_2D );
|
||||
pgl.disable( PGL.TEXTURE_2D );
|
||||
|
||||
if( ALLOWTRAILS )
|
||||
iterateListRenderTrails();
|
||||
|
||||
+10
-16
@@ -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();
|
||||
|
||||
|
||||
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
+2
-2
@@ -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();
|
||||
|
||||
+9
-16
@@ -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 ++;
|
||||
|
||||
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
+1
-1
@@ -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)
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@
|
||||
ArrayList<Box> boxes;
|
||||
|
||||
void setup() {
|
||||
size(800,200);
|
||||
size(640,360);
|
||||
// Create ArrayLists
|
||||
boxes = new ArrayList<Box>();
|
||||
}
|
||||
|
||||
+1
-1
@@ -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();
|
||||
|
||||
+1
-1
@@ -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();
|
||||
|
||||
+1
-1
@@ -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();
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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();
|
||||
|
||||
+2
-2
@@ -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) {
|
||||
|
||||
+1
-1
@@ -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();
|
||||
|
||||
+1
-1
@@ -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();
|
||||
|
||||
+1
-1
@@ -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();
|
||||
|
||||
+1
-1
@@ -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();
|
||||
|
||||
+1
-1
@@ -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)));
|
||||
|
||||
|
||||
+2
-2
@@ -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
|
||||
|
||||
+3
-4
@@ -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);
|
||||
|
||||
+2
-2
@@ -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)));
|
||||
|
||||
+1
-2
@@ -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
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ Attractor attractor;
|
||||
VerletPhysics2D physics;
|
||||
|
||||
void setup () {
|
||||
size (800, 200);
|
||||
size (640, 360);
|
||||
physics = new VerletPhysics2D ();
|
||||
physics.setDrag (0.01);
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user