Add Initial commit
This commit is contained in:
BIN
CM2030 Graphics Programming/Topic 3/3.1.4/.DS_Store
vendored
Normal file
BIN
CM2030 Graphics Programming/Topic 3/3.1.4/.DS_Store
vendored
Normal file
Binary file not shown.
15
CM2030 Graphics Programming/Topic 3/3.1.4/index.html
Normal file
15
CM2030 Graphics Programming/Topic 3/3.1.4/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 3/3.1.4/libraries/.DS_Store
vendored
Normal file
BIN
CM2030 Graphics Programming/Topic 3/3.1.4/libraries/.DS_Store
vendored
Normal file
Binary file not shown.
3
CM2030 Graphics Programming/Topic 3/3.1.4/libraries/p5.min.js
vendored
Normal file
3
CM2030 Graphics Programming/Topic 3/3.1.4/libraries/p5.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
28
CM2030 Graphics Programming/Topic 3/3.1.4/libraries/p5.sound.min.js
vendored
Normal file
28
CM2030 Graphics Programming/Topic 3/3.1.4/libraries/p5.sound.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
87
CM2030 Graphics Programming/Topic 3/3.1.4/sketch.js
Normal file
87
CM2030 Graphics Programming/Topic 3/3.1.4/sketch.js
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
//////////////////////////////////
|
||||||
|
// COURSERA GRAPHICS PROGRAMMING
|
||||||
|
//////////////////////////////////
|
||||||
|
// Adapted from https://github.com/nature-of-code/
|
||||||
|
// released under MIT license
|
||||||
|
|
||||||
|
var ball;
|
||||||
|
////////////////////////////////////////////////////
|
||||||
|
function setup()
|
||||||
|
{
|
||||||
|
createCanvas(900, 600);
|
||||||
|
ball = new Ball();
|
||||||
|
}
|
||||||
|
////////////////////////////////////////////////////
|
||||||
|
function draw()
|
||||||
|
{
|
||||||
|
background(0);
|
||||||
|
|
||||||
|
let mouse = createVector(mouseX, mouseY);
|
||||||
|
let dir = p5.Vector.sub(mouse, ball.location);
|
||||||
|
dir.normalize();
|
||||||
|
dir.mult(0.3);
|
||||||
|
ball.applyForce(dir);
|
||||||
|
|
||||||
|
ball.run();
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////
|
||||||
|
class Ball
|
||||||
|
{
|
||||||
|
constructor()
|
||||||
|
{
|
||||||
|
this.acceleration = new createVector(0, 0);
|
||||||
|
this.velocity = new createVector(0, 0);
|
||||||
|
this.location = new createVector(width/2, height/2);
|
||||||
|
this.size = 40;
|
||||||
|
}
|
||||||
|
|
||||||
|
run()
|
||||||
|
{
|
||||||
|
this.draw();
|
||||||
|
this.move();
|
||||||
|
this.bounce();
|
||||||
|
}
|
||||||
|
|
||||||
|
draw()
|
||||||
|
{
|
||||||
|
fill(125);
|
||||||
|
ellipse(this.location.x, this.location.y, this.size, this.size);
|
||||||
|
}
|
||||||
|
|
||||||
|
move()
|
||||||
|
{
|
||||||
|
this.velocity.add(this.acceleration);
|
||||||
|
this.location.add(this.velocity);
|
||||||
|
this.acceleration.mult(0);
|
||||||
|
this.velocity.limit(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
bounce()
|
||||||
|
{
|
||||||
|
if (this.location.x > width - this.size / 2)
|
||||||
|
{
|
||||||
|
this.location.x = width - this.size / 2;
|
||||||
|
this.velocity.x *= -1;
|
||||||
|
}
|
||||||
|
else if (this.location.x < this.size / 2)
|
||||||
|
{
|
||||||
|
this.velocity.x *= -1;
|
||||||
|
this.location.x = this.size / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.location.y > height - this.size / 2)
|
||||||
|
{
|
||||||
|
this.velocity.y *= -1;
|
||||||
|
this.location.y = height - this.size / 2;
|
||||||
|
}
|
||||||
|
else if(this.location.y < this.size /2)
|
||||||
|
{
|
||||||
|
this.velocity.y *= -1;
|
||||||
|
this.location.y = this.size/2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
applyForce(force) {
|
||||||
|
this.acceleration.add(force);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user