Add completed dominoes
This commit is contained in:
16
CM2030 Graphics Programming/Topic 4/4.4.3/index.html
Normal file
16
CM2030 Graphics Programming/Topic 4/4.4.3/index.html
Normal file
@ -0,0 +1,16 @@
|
||||
<!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 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.4.3/libraries/.DS_Store
vendored
Normal file
BIN
CM2030 Graphics Programming/Topic 4/4.4.3/libraries/.DS_Store
vendored
Normal file
Binary file not shown.
10361
CM2030 Graphics Programming/Topic 4/4.4.3/libraries/matter.js
Normal file
10361
CM2030 Graphics Programming/Topic 4/4.4.3/libraries/matter.js
Normal file
File diff suppressed because it is too large
Load Diff
3
CM2030 Graphics Programming/Topic 4/4.4.3/libraries/p5.min.js
vendored
Normal file
3
CM2030 Graphics Programming/Topic 4/4.4.3/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.4.3/libraries/p5.sound.min.js
vendored
Normal file
28
CM2030 Graphics Programming/Topic 4/4.4.3/libraries/p5.sound.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
85
CM2030 Graphics Programming/Topic 4/4.4.3/sketch.js
Normal file
85
CM2030 Graphics Programming/Topic 4/4.4.3/sketch.js
Normal file
@ -0,0 +1,85 @@
|
||||
// Example is based on examples from:
|
||||
// http://brm.io/matter-js/
|
||||
// https://github.com/shiffman/p5-matter
|
||||
// https://github.com/b-g/p5-matter-examples
|
||||
|
||||
// module aliases
|
||||
let Engine = Matter.Engine;
|
||||
let Render = Matter.Render;
|
||||
let World = Matter.World;
|
||||
let Bodies = Matter.Bodies;
|
||||
let Composites = Matter.Composites;
|
||||
let Composite = Matter.Composite;
|
||||
let Body = Matter.Body;
|
||||
|
||||
let engine;
|
||||
let ball;
|
||||
let ground;
|
||||
let stack;
|
||||
|
||||
function setup() {
|
||||
createCanvas(1200, 600);
|
||||
|
||||
// create an engine
|
||||
engine = Engine.create();
|
||||
|
||||
ball = Bodies.circle(30, 0, 20, {
|
||||
restitution: 1,
|
||||
friction: 0,
|
||||
density: 0.003,
|
||||
});
|
||||
|
||||
let options = { isStatic: true, angle: Math.PI * 0 };
|
||||
ground = Bodies.rectangle(width / 2, 500, width, 10, options);
|
||||
smallGround = Bodies.rectangle(50, 470, 100, 10, {
|
||||
isStatic: true,
|
||||
angle: Math.PI * 0.2,
|
||||
});
|
||||
|
||||
// add all of the bodies to the world
|
||||
World.add(engine.world, [ground, ball, smallGround]);
|
||||
|
||||
setupDominoes();
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
function draw() {
|
||||
background(0);
|
||||
Engine.update(engine);
|
||||
|
||||
fill(255);
|
||||
drawVertices(ball.vertices);
|
||||
|
||||
drawDominoes();
|
||||
|
||||
fill(128);
|
||||
drawVertices(ground.vertices);
|
||||
drawVertices(smallGround.vertices);
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
function setupDominoes() {
|
||||
let size = 10;
|
||||
stack = Composites.stack(200, height - 305, 30,1,20,0,
|
||||
function (x, y) {
|
||||
let partA = Bodies.rectangle(x, y, 10, 200);
|
||||
return Body.create({ parts: [partA] });
|
||||
}
|
||||
);
|
||||
Composite.add(engine.world, [stack]);
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
function drawDominoes() {
|
||||
fill(255);
|
||||
for (let i = 0; i < stack.bodies.length; i++) {
|
||||
drawVertices(stack.bodies[i].vertices);
|
||||
}
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
// ********* HELPER FUNCTIONS **********
|
||||
/////////////////////////////////////////////////////////
|
||||
function drawVertices(vertices) {
|
||||
beginShape();
|
||||
for (let i = 0; i < vertices.length; i++) {
|
||||
vertex(vertices[i].x, vertices[i].y);
|
||||
}
|
||||
endShape(CLOSE);
|
||||
}
|
||||
Reference in New Issue
Block a user