-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<script language="javascript" type="text/javascript" src="../libraries/p5.js"></script> | ||
<script language="javascript" type="text/javascript" src="../libraries/matter.js"></script> | ||
<script language="javascript" type="text/javascript" src="../libraries/matter-wrap.js"></script> | ||
<script language="javascript" type="text/javascript" src="../libraries/helpers.js"></script> | ||
<script language="javascript" type="text/javascript" src="sketch.js"></script> | ||
<style> body {padding: 0; margin: 0;} </style> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Benedikt Groß | ||
// Example is based on examples from: http://brm.io/matter-js/, https://github.com/shiffman/p5-matter | ||
|
||
// setup wrap coordinates plugin | ||
Matter.use('matter-wrap'); | ||
|
||
const Engine = Matter.Engine; | ||
const Render = Matter.Render; | ||
const World = Matter.World; | ||
const Bodies = Matter.Bodies; | ||
|
||
const drawBody = Helpers.drawBody; | ||
|
||
let engine; | ||
let trampolineA; | ||
let trampolineB; | ||
let ball; | ||
|
||
let queueBall; | ||
|
||
|
||
function setup() { | ||
const canvas = createCanvas(800, 600); | ||
|
||
// queueBall which records e.g. 25 old positions | ||
queueBall = new Queue(25); | ||
|
||
// ball | ||
ball = Bodies.circle(300, 50, 40); | ||
ball.plugin.wrap = { | ||
min: { x: 0, y: 0 }, | ||
max: { x: width, y: height } | ||
}; | ||
|
||
// create two trampolines | ||
trampolineA = Bodies.rectangle(600, 450, 250, 50, { | ||
isStatic: true, angle: Math.PI * -0.15 | ||
}); | ||
trampolineA.restitution = 0.7; | ||
trampolineB = Bodies.rectangle(200, 450, 250, 50, { | ||
isStatic: true, angle: Math.PI * 0.15 | ||
}); | ||
trampolineB.restitution = 0.7; | ||
|
||
// setup world and engine | ||
engine = Engine.create(); | ||
World.add(engine.world, [trampolineA, trampolineB, ball]); | ||
Engine.run(engine); | ||
} | ||
|
||
function draw() { | ||
background(0); | ||
|
||
// draw queue | ||
noStroke(); | ||
const points = queueBall.all(); | ||
for (let i = 0; i < points.length; i++) { | ||
const fraction = 1 - i/points.length; | ||
const fillColor = lerpColor(color(255), color(50), fraction); | ||
fill(fillColor); | ||
const p = points[i]; | ||
ellipse(p.x, p.y, 80, 80); | ||
} | ||
queueBall.add({x: ball.position.x, y: ball.position.y}); | ||
|
||
// draw ball and trampolines | ||
noStroke(); | ||
fill(200); | ||
drawBody(trampolineA); | ||
drawBody(trampolineB); | ||
drawBody(ball); | ||
} | ||
|
||
|
||
class Queue { | ||
constructor(maxLength) { | ||
this.maxLength = maxLength; | ||
this.elements = []; | ||
} | ||
add(element) { | ||
if (this.elements.length < this.maxLength) { | ||
this.elements.push(element); | ||
} else { | ||
this.elements.push(element); | ||
this.elements.shift(); | ||
} | ||
} | ||
all() { | ||
return this.elements; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters