Esempio di implementazione delle regole
// height=100 lines=auto
let pos; // PROPRIETÀ
let vel;
function setup() {
pos = createVector(random(width),random(height));
vel = createVector( 1.7, 1.9 );
}
function draw() {
background(220);
circle( pos.x,pos.y, 6 ); // VISUALIZZAZIONE
pos.add( vel ); // AGGIORNAMENTI
if (pos.x < 0 || pos.x > width) {
vel.x = -vel.x;
}
if (pos.y < 0 || pos.y > height) {
vel.y = -vel.y;
}
}
pos.add( vel );
if (pos.x < 0 || pos.x > width) {
vel.x = -vel.x;
}
if (pos.y < 0 || pos.y > height) {
vel.y = -vel.y;
}
Aggiornamento della posizione ed eventuale inversione della direzione di spostamento (vel.x = -vel.x e vel.y = -vel.y) nel caso la posizione finisca fuori dal canvas.