mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
changes to simulate examples
This commit is contained in:
@@ -5,26 +5,16 @@ class Particle {
|
||||
PVector loc;
|
||||
PVector vel;
|
||||
PVector acc;
|
||||
float timer;
|
||||
float lifespan;
|
||||
PImage img;
|
||||
|
||||
// One constructor
|
||||
Particle(PVector a, PVector v, PVector l, PImage img_) {
|
||||
acc = a.get();
|
||||
vel = v.get();
|
||||
loc = l.get();
|
||||
timer = 100.0;
|
||||
img = img_;
|
||||
}
|
||||
|
||||
// Another constructor (the one we are using here)
|
||||
Particle(PVector l,PImage img_) {
|
||||
acc = new PVector(0.0,0.0,0.0);
|
||||
float x = (float) generator.nextGaussian()*0.3f;
|
||||
float y = (float) generator.nextGaussian()*0.3f - 1.0f;
|
||||
vel = new PVector(x,y,0);
|
||||
acc = new PVector(0,0);
|
||||
float vx = (float) generator.nextGaussian()*0.3;
|
||||
float vy = (float) generator.nextGaussian()*0.3 - 1.0;
|
||||
vel = new PVector(vx,vy);
|
||||
loc = l.get();
|
||||
timer = 100.0;
|
||||
lifespan = 100.0;
|
||||
img = img_;
|
||||
}
|
||||
|
||||
@@ -35,7 +25,7 @@ class Particle {
|
||||
|
||||
// Method to apply a force vector to the Particle object
|
||||
// Note we are ignoring "mass" here
|
||||
void add_force(PVector f) {
|
||||
void applyForce(PVector f) {
|
||||
acc.add(f);
|
||||
}
|
||||
|
||||
@@ -43,20 +33,20 @@ class Particle {
|
||||
void update() {
|
||||
vel.add(acc);
|
||||
loc.add(vel);
|
||||
timer -= 2.5;
|
||||
acc.mult(0);
|
||||
acc.mult(0); // clear Acceleration
|
||||
lifespan -= 2.5;
|
||||
}
|
||||
|
||||
// Method to display
|
||||
void render() {
|
||||
imageMode(CORNER);
|
||||
tint(255,timer);
|
||||
image(img,loc.x-img.width/2,loc.y-img.height/2);
|
||||
imageMode(CENTER);
|
||||
tint(255,lifespan);
|
||||
image(img,loc.x,loc.y);
|
||||
}
|
||||
|
||||
// Is the particle still useful?
|
||||
boolean dead() {
|
||||
if (timer <= 0.0) {
|
||||
if (lifespan <= 0.0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
|
||||
class ParticleSystem {
|
||||
|
||||
ArrayList particles; // An arraylist for all the particles
|
||||
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(); // Initialize the arraylist
|
||||
particles = new ArrayList<Particle>(); // Initialize the arraylist
|
||||
origin = v.get(); // Store the origin point
|
||||
img = img_;
|
||||
for (int i = 0; i < num; i++) {
|
||||
@@ -17,21 +17,21 @@ class ParticleSystem {
|
||||
}
|
||||
|
||||
void run() {
|
||||
// Cycle through the ArrayList backwards b/c we are deleting
|
||||
for (int i = particles.size()-1; i >= 0; i--) {
|
||||
Particle p = (Particle) particles.get(i);
|
||||
Iterator<Particle> it = particles.iterator();
|
||||
while (it.hasNext()) {
|
||||
Particle p = it.next();
|
||||
p.run();
|
||||
if (p.dead()) {
|
||||
particles.remove(i);
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Method to add a force vector to all particles currently in the system
|
||||
void add_force(PVector dir) {
|
||||
for (int i = particles.size()-1; i >= 0; i--) {
|
||||
Particle p = (Particle) particles.get(i);
|
||||
p.add_force(dir);
|
||||
void applyForce(PVector dir) {
|
||||
// Enhanced loop!!!
|
||||
for (Particle p: particles) {
|
||||
p.applyForce(dir);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -55,3 +55,4 @@ class ParticleSystem {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -8,48 +8,42 @@
|
||||
|
||||
// @pjs preload must be used to preload media if the program is
|
||||
// running with Processing.js
|
||||
/* @pjs preload="texture.gif"; */
|
||||
/* @pjs preload="texture.png"; */
|
||||
|
||||
ParticleSystem ps;
|
||||
Random generator;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
colorMode(RGB, 255, 255, 255, 100);
|
||||
|
||||
// Using a Java random number generator for Gaussian random numbers
|
||||
size(640,360);
|
||||
generator = new Random();
|
||||
|
||||
// Create an alpha masked image to be applied as the particle's texture
|
||||
PImage msk = loadImage("texture.gif");
|
||||
PImage img = createImage(msk.width, msk.height, RGB);
|
||||
for (int i = 0; i < img.pixels.length; i++) {
|
||||
img.pixels[i] = color(255);
|
||||
}
|
||||
img.mask(msk);
|
||||
ps = new ParticleSystem(0, new PVector(width/2, height-20), img);
|
||||
|
||||
PImage img = loadImage("texture.png");
|
||||
ps = new ParticleSystem(0,new PVector(width/2,height-60),img);
|
||||
smooth();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(75);
|
||||
|
||||
background(0);
|
||||
|
||||
// Calculate a "wind" force based on mouse horizontal position
|
||||
float dx = (mouseX - width/2) / 1000.0;
|
||||
PVector wind = new PVector(dx,0,0);
|
||||
displayVector(wind,width/2,50,500);
|
||||
ps.add_force(wind);
|
||||
float dx = map(mouseX,0,width,-0.2,0.2);
|
||||
PVector wind = new PVector(dx,0);
|
||||
ps.applyForce(wind);
|
||||
ps.run();
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
void displayVector(PVector v, float x, float y, float scayl) {
|
||||
// Renders a vector object 'v' as an arrow and a location 'loc'
|
||||
void drawVector(PVector v, PVector loc, float scayl) {
|
||||
pushMatrix();
|
||||
float arrowsize = 4;
|
||||
// Translate to location to render vector
|
||||
translate(x,y);
|
||||
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.heading2D());
|
||||
|
||||
Reference in New Issue
Block a user