xxxxxxxxxx
36
let ball;
function setup() {
createCanvas(400, 400);
ball = new Ball(random(width),random(height));
}
function draw() {
background(220);
ball.update();
ball.show();
}
class Ball{
constructor(x,y){
this.pos = createVector(x,y);
this.vel = createVector(0,0);
this.acc = createVector(cos(random(0,2*PI)),sin(random(0,2*PI)));
}
update(){
if (this.pos.x > width || this.pos.x < 0) this.vel.x *= -1;
if (this.pos.y > height || this.pos.y < 0) this.vel.y *= -1;
this.pos.add(this.vel);
this.vel.add(this.acc);
this.acc.mult(0.00001)
}
show(){
stroke(0);
fill(255,0,0);
circle(this.pos.x,this.pos.y,10);
}
}