Particles together: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
TomasM (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „Datei:particlesTogether.PNG“) |
TomasM (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
||
| Zeile 1: | Zeile 1: | ||
[[Datei:particlesTogether.PNG]] | [[Datei:particlesTogether.PNG]] | ||
<source lang="java" line start="2" highlight="4-6"> | |||
///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"); | |||
} | |||
</source> | |||
Version vom 5. Mai 2020, 18:08 Uhr
///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");
}