Stuiterende Bal OSC


Hier de code voor de Stuiterende Ballen met OSC in de les van 16 feb 2023


class Ball {
  constructor(_x, _y, _s, _id) {
    //neem de waarden van de constructor argumenten op in deze instnatie van de class
    this.x = _x;
    this.y = _y;
    this.s = _s;
    this.id = _id;

    //geef de bal random snelheden op de x en y as
    this.xSpeed = random(-5,5);
    this.ySpeed = random(-5,5);

    //maak een random kleur
    this.c = color(random(255),random(255),random(255));

  }

  draw() { 
    fill(this.c);
    circle(this.x, this.y ,this.s)
    fill(0);
    textSize(48);
    text(this.id, this.x, this.y)
  }

  move() { 
    //beweeg met de snelheid die in deze class opgeslagen is
    this.x = this.x + this.xSpeed;
    this.y = this.y + this.ySpeed;

    if (this.x + (this.s / 2) > width || this.x - (this.s / 2) < 0) { //de rechterkant
      this.xSpeed = this.xSpeed * -1;
      client.sendMessage("/" + this.id + "/boink", "boink");
    }

    if (this.y + (this.s /2 )> height || this.y - (this.s /2) < 0) {  //de onderkant
      this.ySpeed = this.ySpeed * -1;
      client.sendMessage("/" + this.id + "/boink", "boink");
    }

    client.sendMessage("/" + this.id + "/x",this.x / width);
    client.sendMessage("/" + this.id + "/y",this.y / height);
  }
}

let ballObject;
//aanmaken van de benodigde variabelen.
let client;
//lege lijst voor bal objecten
let balls = [];

function setup() {
  createCanvas(innerWidth,innerHeight);

  for (let i = 0; i < 3; i ++) {
    balls.push(new Ball(width / 2, height / 2, random(10,20), i))
  }

  background(255);

  //maak een nieuwe client-object aan.
  client = new Client();

  //start de client en laat deze berichten sturen naar het ip-adres 127.0.0.1 en poort 9000
    client.startClient("localhost",9000); 
    //plaats hier de code die maar ÊÊn keer hoeft te worden uitgevoerd
}

function draw() {
  //plaats daar de code die continue herhaald moet worden.
  background(255);
  for (let i = 0; i < balls.length; i ++) {
    balls[i].draw();
    balls[i].move();

  }
}

en de Max path:

max_oscOntvang