Particles together

Aus hyperdramatik
Wechseln zu: Navigation, Suche

ParticlesTogether.PNG


///S&&O SS2020 // course: *From Space to Space* with Hannah Pearl - Wilson
//press the mouse to create a particle system
// l or r to change wind direction

import mqtt.*; // import MQTT library
MQTTClient client; // declare class MQTTClient

ArrayList<ParticleSystem> sparksystems; //declare ArrayList of Particle Systems

String mePlayer = "/particles-together/pink/p2/xy";
String otherPlayer = "/particles-together/pink/p1/xy";

int otherMouseX = 0;  //for incoming values from other player's mouse xy
int otherMouseY = 0;

void setup() {
  client = new MQTTClient(this);
  client.connect("mqtt://tomasAxolotl:spielundobjekt@broker.shiftr.io", "tomas");  // "connect to namespace with clientID"
  size(1200, 900);
 
  sparksystems = new ArrayList<ParticleSystem>();  //initialize the Particle System class
}

// RECEIVE // .suscribe <3

void clientConnected() {
  println("client connected");
  client.subscribe(otherPlayer);
}

//transforms the incoming xy string from the other player into x, y coordinates on my canvas
// which are the new coordinates for the particle system origin point 

void messageReceived(String topic, byte[] payload) {
  // the incoming MQTT "message" is a String: "xValue,yValue"
  // to unpack it we use the "split" function:
  String incomingPayload = new String(payload);  
  String[] xy = split(incomingPayload, ',');
  println("new message: " + topic + " x: " + xy[0] + " y: " +xy[1]);
  otherMouseX = int(xy[0]);
  otherMouseY = int(xy[1]);
  sparksystems.add(new ParticleSystem(new PVector(otherMouseX, otherMouseY)));
}

//create my particle system at an emmiting point  
// .publish to send mouse x and y to coplayer!

void mousePressed(){ 
sparksystems.add(new ParticleSystem(new PVector(mouseX, mouseY)));
 client.publish(mePlayer, mouseX + "," + mouseY); // ("topic", "message") 
}

void draw(){
background(0);
// enhanced for loop which can go through arrays which change length
for (ParticleSystem ps : sparksystems){
ps.run();
}
}

void connectionLost() {
  println("connection lost");
}



//in this class is the information for each particle in itself
// exercise inspired by "The Nature of Code" Chapter 4 Particle Systems, Dan Schiffman he rocks
//link to particle system tutorials
// https://www.youtube.com/watch?v=vdgiqMkFygc&list=PLRqwX-V7Uu6Z9hI4mSgx2FlE5w8zvjmEy

class Particle {
  
  PVector location;
  PVector velocity; 
  PVector acceleration;
  float lifespan = 255;  // float for the alpha value, to make particles transparent over time aka disappear
  float mass;             //radio for the ellipse
  
  Particle (PVector l){
  acceleration = new PVector (0, 0.005);
  velocity = new PVector (random(-0.25,0.255),random(-0.5,0.5));
  location = l.get();
  mass = random(1,10);
  }
  
  //force = acceleration * mass
  // acceleration = force / mass
  //acceleration = force! (net force, that means, addition of all applied forces 
  void applyForce(PVector force){
  
    PVector f = PVector.div(force,mass);
    acceleration.add(f);
  }
  
  // boolean to check if the alpha = 0 
  boolean isDead(){
  if(lifespan <= 0){
  return true;
  }else{
  return false;
    }
  }
  
  void update(){
  velocity = velocity.add(acceleration);  // add up the vectors 
  location.add(velocity);                   // add up the vectors
  velocity.limit(5);
  //acceleration.limit(1); other variation to limit acceleration, which each frame increases and would soon go outta control
  acceleration.mult(0); // normalize acceleration
   lifespan -= 2;       // each particle will decrease in its alfa value over time
  }

// visualisation of the particles
void display(){
stroke(0, random(0, 255), random(0,255), lifespan);
strokeWeight(1);
fill(random(100, 255), 0, random(100, 255), lifespan);
ellipse(location.x, location.y, mass, mass);
}

}



//in this class is the information for each particle system

class ParticleSystem {
ArrayList<Particle> sparks;
PVector origin;
  
ParticleSystem(PVector location){
  origin = location.get();
sparks = new ArrayList<Particle>();
}

// in the main no_screens_1 script, we pass  into origin the vector where the mouse was pressed
// for loop to 
void run(){
sparks.add(new Particle(origin));
for (int i = 0; i < sparks.size(); i ++){
  Particle p = sparks.get(i);
  
  //wind force function: r blows wind from the right, l blows wind from the left
    if(keyPressed){
      if(key == 'r'|| key == 'R'){
  PVector rightwind = new PVector(-0.2, 0);
  p.applyForce(rightwind);
  }
    } if(keyPressed){
      if(key == 'l'|| key == 'L'){
  PVector leftwind = new PVector(0.2, 0);
  p.applyForce(leftwind);
  }
       }
  
p.update();
p.display();

// remove particles with alpha = 0 aka already transparent
if (p.isDead()){
sparks.remove(i);
   }
  }
 }
}