//variable to hold one object
ParticleEngine engine;
Particle lastParticle;
Particle dragging;
PFont font;
PImage mapImages2;
void setup() {
size(700, 400);
mapImages2 = loadImage("voetbalveld_480.gif");
//create a particle engine
engine = new ParticleEngine();
smooth();
font = createFont("SansSerif", 11);
textFont(font);
engine.addParticle(new Particle("Stekelenburg", random(0, 10), random(0, 10), 0, 0));
engine.addParticle(new Particle("Van der Wiel", random(0, 400), random(0, 400), 0, 0));
engine.addParticle(new Particle("Heitinga", random(0, 400), random(0, 400), 0, 0));
engine.addParticle(new Particle("Mathijsen", random(0, 400), random(0, 400), 0, 0));
engine.addParticle(new Particle("Van Bronckhorst", random(0, 400), random(0, 400), 0, 0));
engine.addParticle(new Particle("Van Bommel", random(0, 400), random(0, 400), 0, 0));
engine.addParticle(new Particle("De Jong", random(0, 400), random(0, 400), 0, 0));
engine.addParticle(new Particle("Sneijder", random(0, 400), random(0, 400), 0, 0));
engine.addParticle(new Particle("Robben", random(0, 400), random(0, 400), 0, 0));
engine.addParticle(new Particle("Elia", random(0, 400), random(0, 400), 0, 0));
engine.addParticle(new Particle("Van Persie", random(0, 400), random(0, 400), 0, 0));
//engine.addParticle(new Particle("Heitinga", random(0, 400), random(0, 400), 0, 0));
engine.findParticle("Stekelenburg").pin(320, 30);
engine.findParticle("Van der Wiel").pin(120, 100);
engine.findParticle("Van Bronckhorst").pin(550, 100);
engine.findParticle("Robben").pin(120, 320);
engine.findParticle("Elia").pin(550, 320);
engine.connectParticles("Stekelenburg", "Heitinga");
engine.connectParticles("Stekelenburg", "Van der Wiel");
engine.connectParticles("Stekelenburg", "Mathijsen");
engine.connectParticles("Stekelenburg", "Van Bronckhorst");
engine.connectParticles("Heitinga", "Van der Wiel");
engine.connectParticles("Heitinga", "Mathijsen");
engine.connectParticles("Van Bronckhorst", "Mathijsen");
engine.connectParticles("Van Bronckhorst", "De Jong");
engine.connectParticles("De Jong", "Mathijsen");
engine.connectParticles("De Jong", "Van Bommel");
engine.connectParticles("Van der Wiel", "Van Bommel");
engine.connectParticles("Heitinga", "Van Bommel");
engine.connectParticles("Van Bommel", "Sneijder");
engine.connectParticles("De Jong", "Sneijder");
engine.connectParticles("Robben", "Van Bommel");
engine.connectParticles("Robben", "Sneijder");
engine.connectParticles("Elia", "Sneijder");
engine.connectParticles("Elia", "De Jong");
engine.connectParticles("Elia", "Van Persie");
engine.connectParticles("Robben", "Van Persie");
engine.connectParticles("Van Persie", "Sneijder");
engine.connectParticles("Van Bronckhorst", "Elia");
engine.connectParticles("Robben", "Van der Wiel");
}
void draw() {
image(mapImages2, 0, 0, 700, 400);
//update the particles
engine.update();
//clear the background
//draw the particles
engine.draw();
fill(#000000);
strokeWeight(2);
fill(#FFFFFF);
text("Opstelling voor vanavond :-) ", 10, 30);
}
void mousePressed() {
dragging = engine.particleAt(mouseX, mouseY);
if (dragging!=null) {
dragging.dragged = true;
lastParticle = dragging;
}
}
void mouseReleased() {
if (dragging!=null) {
dragging.dragged=false;
dragging = null;
}
}
public class Particle {
//a reference to the engine
protected ParticleEngine engine;
//a position
public float x;
public float y;
// a velocity
public float velocityX;
public float velocityY;
// a gravity
protected float gravityX;
protected float gravityY;
// damping to simulate friction
protected float damping;
// color
protected color col;
//size
protected float size;
// variables to keep track of how long the particle is alive and how long it will live
protected int life;
protected int living;
PImage mapImages;
// flag to indicate whether or not the particle is destroyed
public boolean destroyed;
//flag to indicate the particle is being dragged
public boolean dragged;
public boolean pinned ;
public String label;
//constructor, set initial variables
public Particle(String label, float x, float y, float velocityX, float velocityY) {
engine = null;
this.label = label;
this.x = x;
this.y = y;
this.velocityX = velocityX;
this.velocityY = velocityY;
gravityX = random(-0.5,0.5);
gravityY = random(-0.5,0.5);
damping = 0.9;
//randomize the color
col = color(255, 255, 255);
//randomize the size
size = 30;
//set life to -1 to live forever
life = -1;
living = 0;
//set flags
destroyed = false;
dragged = false;
pinned = false;
mapImages = loadImage("shirt.png");
}
public void setEngine(ParticleEngine engine) {
this.engine = engine;
}
//a method to set the particles position and velocity
public void setPos(float x, float y, float velocityX, float velocityY) {
this.x = x;
this.y = y;
this.velocityX = velocityX;
this.velocityY = velocityY;
}
//a method to update the particle (should be called avery frame
public void update() {
if (dragged) {
x = mouseX;
y = mouseY;
velocityX =0;
velocityY =0;
return;
}
if (pinned) {
velocityX =0;
velocityY =0;
return;
}
//apply gravity
velocityX += gravityX;
velocityY += gravityY;
//apply damping
velocityX *= damping;
velocityY *= damping;
//apply speed
x += velocityX;
y += velocityY;
if (life>0) {
life--;
if (life<=0) destroyed = true;
}
}
public void draw() {
if (destroyed) return;
//draw the particle
//noStroke();
// use alpha to fade away when life gets low
fill(col);
image(mapImages, x, y, size, size);
fill(0);
text(label, x-textWidth(label)*0.2, y+50);
}
public void pin(float x, float y) {
this.x = x;
this.y = y;
pinned = true;
}
}