Files
UoL/CM2030 Graphics Programming/Topic 2/2.3.3/sketch.js
2021-04-22 18:48:33 -05:00

60 lines
1.1 KiB
JavaScript

let ball;
function setup()
{
createCanvas(900, 600);
ball = new Ball();
}
function draw()
{
background(0);
ball.run();
}
class Ball
{
constructor()
{
this.velocity = new createVector(0, 0);
this.location = new createVector(width/2, height/2);
this.acceleration = new createVector(0, 0);
this.maxVelocity = 10;
}
run()
{
this.draw();
this.move();
this.edges();
}
draw()
{
fill(125);
ellipse(this.location.x, this.location.y, 40, 40);
}
move()
{
let mouse = createVector(mouseX, mouseY);
let dir = p5.Vector.sub(mouse, this.location);
console.log(dir);
dir.normalize();
dir.mult(0.3);
this.acceleration = dir;
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxVelocity);
this.location.add(this.velocity);
}
edges()
{
if (this.location.x < 0) this.location.x = width;
else if (this.location.x > width) this.location.x = 0;
else if (this.location.y < 0) this.location.y = height;
else if (this.location.y > height) this.location.y = 0;
}
}