Spostamento di un oggetto vettoriale
Cliccare sul canvas per creare il cerchio.
// height=100 lines=auto
class Cerchio {
constructor( posX, posY ) {
this.pos = createVector( posX, posY );
this.vel = createVector( random(-1,1), random(-1,1) );
}
display() {
this.pos.add( this.vel );
circle( this.pos.x, this.pos.y, 10 );
}
}
let cerchio;
function draw() {
background(220);
if (cerchio) {
cerchio.display();
}
}
function mouseClicked() {
cerchio = new Cerchio( mouseX, mouseY );
}
constructor( posX, posY ) {
this.pos = createVector( posX, posY );
this.vel = createVector( random(-1,1), random(-1,1) );
}
Creazione di un vettore casuale per definire la velocità e la direzione dello spostamento.
display() {
this.pos.add( this.vel );
circle( this.pos.x, this.pos.y, 10 );
}
Aggiunta dello spostamento (this.vel
) alla posizione attuale (this.pos
).