Add Initial commit with template
This commit is contained in:
17
CM2030 Graphics Programming/Topic 4/4.5.1/index.html
Normal file
17
CM2030 Graphics Programming/Topic 4/4.5.1/index.html
Normal file
@ -0,0 +1,17 @@
|
||||
<!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 language="javascript" type="text/javascript" src="libraries/matter.js"></script>
|
||||
<script language="javascript" type="text/javascript" src="physics.js"></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 4/4.5.1/libraries/.DS_Store
vendored
Normal file
BIN
CM2030 Graphics Programming/Topic 4/4.5.1/libraries/.DS_Store
vendored
Normal file
Binary file not shown.
10361
CM2030 Graphics Programming/Topic 4/4.5.1/libraries/matter.js
Normal file
10361
CM2030 Graphics Programming/Topic 4/4.5.1/libraries/matter.js
Normal file
File diff suppressed because it is too large
Load Diff
3
CM2030 Graphics Programming/Topic 4/4.5.1/libraries/p5.min.js
vendored
Normal file
3
CM2030 Graphics Programming/Topic 4/4.5.1/libraries/p5.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
28
CM2030 Graphics Programming/Topic 4/4.5.1/libraries/p5.sound.min.js
vendored
Normal file
28
CM2030 Graphics Programming/Topic 4/4.5.1/libraries/p5.sound.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
74
CM2030 Graphics Programming/Topic 4/4.5.1/physics.js
Normal file
74
CM2030 Graphics Programming/Topic 4/4.5.1/physics.js
Normal file
@ -0,0 +1,74 @@
|
||||
////////////////////////////////////////////////////////////////
|
||||
function setupGround(){
|
||||
ground = Bodies.rectangle(500, 600, 1000, 40, {
|
||||
isStatic: true, angle: 0
|
||||
});
|
||||
World.add(engine.world, [ground]);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
function drawGround(){
|
||||
push();
|
||||
fill(128);
|
||||
drawVertices(ground.vertices);
|
||||
pop();
|
||||
}
|
||||
////////////////////////////////////////////////////////////////
|
||||
function setupPropeller(){
|
||||
// your code here
|
||||
}
|
||||
////////////////////////////////////////////////////////////////
|
||||
//updates and draws the propeller
|
||||
function drawPropeller(){
|
||||
push();
|
||||
// your code here
|
||||
pop();
|
||||
}
|
||||
////////////////////////////////////////////////////////////////
|
||||
function setupBird(){
|
||||
var bird = Bodies.circle(mouseX, mouseY, 20, {friction: 0,
|
||||
restitution: 0.95 });
|
||||
Matter.Body.setMass(bird, bird.mass*10);
|
||||
World.add(engine.world, [bird]);
|
||||
birds.push(bird);
|
||||
}
|
||||
////////////////////////////////////////////////////////////////
|
||||
function drawBirds(){
|
||||
push();
|
||||
//your code here
|
||||
pop();
|
||||
}
|
||||
////////////////////////////////////////////////////////////////
|
||||
//creates a tower of boxes
|
||||
function setupTower(){
|
||||
//you code here
|
||||
}
|
||||
////////////////////////////////////////////////////////////////
|
||||
//draws tower of boxes
|
||||
function drawTower(){
|
||||
push();
|
||||
//your code here
|
||||
pop();
|
||||
}
|
||||
////////////////////////////////////////////////////////////////
|
||||
function setupSlingshot(){
|
||||
//your code here
|
||||
}
|
||||
////////////////////////////////////////////////////////////////
|
||||
//draws slingshot bird and its constraint
|
||||
function drawSlingshot(){
|
||||
push();
|
||||
// your code here
|
||||
pop();
|
||||
}
|
||||
/////////////////////////////////////////////////////////////////
|
||||
function setupMouseInteraction(){
|
||||
var mouse = Mouse.create(canvas.elt);
|
||||
var mouseParams = {
|
||||
mouse: mouse,
|
||||
constraint: { stiffness: 0.05 }
|
||||
}
|
||||
mouseConstraint = MouseConstraint.create(engine, mouseParams);
|
||||
mouseConstraint.mouse.pixelRatio = pixelDensity();
|
||||
World.add(engine.world, mouseConstraint);
|
||||
}
|
||||
133
CM2030 Graphics Programming/Topic 4/4.5.1/sketch.js
Normal file
133
CM2030 Graphics Programming/Topic 4/4.5.1/sketch.js
Normal file
@ -0,0 +1,133 @@
|
||||
// Example is based on examples from: http://brm.io/matter-js/, https://github.com/shiffman/p5-matter
|
||||
// add also Benedict Gross credit
|
||||
|
||||
var Engine = Matter.Engine;
|
||||
var Render = Matter.Render;
|
||||
var World = Matter.World;
|
||||
var Bodies = Matter.Bodies;
|
||||
var Body = Matter.Body;
|
||||
var Constraint = Matter.Constraint;
|
||||
var Mouse = Matter.Mouse;
|
||||
var MouseConstraint = Matter.MouseConstraint;
|
||||
|
||||
var engine;
|
||||
var propeller;
|
||||
var boxes = [];
|
||||
var birds = [];
|
||||
var colors = [];
|
||||
var ground;
|
||||
var slingshotBird, slingshotConstraint;
|
||||
var angle=0;
|
||||
var angleSpeed=0;
|
||||
var canvas;
|
||||
////////////////////////////////////////////////////////////
|
||||
function setup() {
|
||||
canvas = createCanvas(1000, 600);
|
||||
|
||||
engine = Engine.create(); // create an engine
|
||||
|
||||
setupGround();
|
||||
|
||||
setupPropeller();
|
||||
|
||||
setupTower();
|
||||
|
||||
setupSlingshot();
|
||||
|
||||
setupMouseInteraction();
|
||||
}
|
||||
////////////////////////////////////////////////////////////
|
||||
function draw() {
|
||||
background(0);
|
||||
|
||||
Engine.update(engine);
|
||||
|
||||
drawGround();
|
||||
|
||||
drawPropeller();
|
||||
|
||||
drawTower();
|
||||
|
||||
drawBirds();
|
||||
|
||||
drawSlingshot();
|
||||
}
|
||||
////////////////////////////////////////////////////////////
|
||||
//use arrow keys to control propeller
|
||||
function keyPressed(){
|
||||
if (keyCode == LEFT_ARROW){
|
||||
//your code here
|
||||
}
|
||||
else if (keyCode == RIGHT_ARROW){
|
||||
//your code here
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////////////////////////
|
||||
function keyTyped(){
|
||||
//if 'b' create a new bird to use with propeller
|
||||
if (key==='b'){
|
||||
setupBird();
|
||||
}
|
||||
|
||||
//if 'r' reset the slingshot
|
||||
if (key==='r'){
|
||||
removeFromWorld(slingshotBird);
|
||||
removeFromWorld(slingshotConstraint);
|
||||
setupSlingshot();
|
||||
}
|
||||
}
|
||||
|
||||
//**********************************************************************
|
||||
// HELPER FUNCTIONS - DO NOT WRITE BELOW THIS line
|
||||
//**********************************************************************
|
||||
|
||||
//if mouse is released destroy slingshot constraint so that
|
||||
//slingshot bird can fly off
|
||||
function mouseReleased(){
|
||||
setTimeout(() => {
|
||||
slingshotConstraint.bodyB = null;
|
||||
slingshotConstraint.pointA = { x: 0, y: 0 };
|
||||
}, 100);
|
||||
}
|
||||
////////////////////////////////////////////////////////////
|
||||
//tells you if a body is off-screen
|
||||
function isOffScreen(body){
|
||||
var pos = body.position;
|
||||
return (pos.y > height || pos.x<0 || pos.x>width);
|
||||
}
|
||||
////////////////////////////////////////////////////////////
|
||||
//removes a body from the physics world
|
||||
function removeFromWorld(body) {
|
||||
World.remove(engine.world, body);
|
||||
}
|
||||
////////////////////////////////////////////////////////////
|
||||
function drawVertices(vertices) {
|
||||
beginShape();
|
||||
for (var i = 0; i < vertices.length; i++) {
|
||||
vertex(vertices[i].x, vertices[i].y);
|
||||
}
|
||||
endShape(CLOSE);
|
||||
}
|
||||
////////////////////////////////////////////////////////////
|
||||
function drawConstraint(constraint) {
|
||||
push();
|
||||
var offsetA = constraint.pointA;
|
||||
var posA = {x:0, y:0};
|
||||
if (constraint.bodyA) {
|
||||
posA = constraint.bodyA.position;
|
||||
}
|
||||
var offsetB = constraint.pointB;
|
||||
var posB = {x:0, y:0};
|
||||
if (constraint.bodyB) {
|
||||
posB = constraint.bodyB.position;
|
||||
}
|
||||
strokeWeight(5);
|
||||
stroke(255);
|
||||
line(
|
||||
posA.x + offsetA.x,
|
||||
posA.y + offsetA.y,
|
||||
posB.x + offsetB.x,
|
||||
posB.y + offsetB.y
|
||||
);
|
||||
pop();
|
||||
}
|
||||
Reference in New Issue
Block a user