Add Initial commit
This commit is contained in:
15
CM2030 Graphics Programming/Topic 2/2.3.3/index.html
Normal file
15
CM2030 Graphics Programming/Topic 2/2.3.3/index.html
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>graphicsProgramming - Coursera</title>
|
||||||
|
<script src="libraries/p5.min.js" type="text/javascript"></script>
|
||||||
|
<script src="libraries/p5.sound.min.js" type="text/javascript"></script>
|
||||||
|
|
||||||
|
<script src="sketch.js" type="text/javascript"></script>
|
||||||
|
|
||||||
|
<style> body {padding: 0; margin: 0;} canvas {vertical-align: top;} </style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
BIN
CM2030 Graphics Programming/Topic 2/2.3.3/libraries/.DS_Store
vendored
Normal file
BIN
CM2030 Graphics Programming/Topic 2/2.3.3/libraries/.DS_Store
vendored
Normal file
Binary file not shown.
3
CM2030 Graphics Programming/Topic 2/2.3.3/libraries/p5.min.js
vendored
Normal file
3
CM2030 Graphics Programming/Topic 2/2.3.3/libraries/p5.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
28
CM2030 Graphics Programming/Topic 2/2.3.3/libraries/p5.sound.min.js
vendored
Normal file
28
CM2030 Graphics Programming/Topic 2/2.3.3/libraries/p5.sound.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
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