Add Initial commit
This commit is contained in:
59
CM2030 Graphics Programming/Topic 2/2.3.3/sketch.js
Normal file
59
CM2030 Graphics Programming/Topic 2/2.3.3/sketch.js
Normal file
@ -0,0 +1,59 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user