Skip to content

Instantly share code, notes, and snippets.

@tkojitu
Last active August 29, 2015 13:57
Show Gist options
  • Save tkojitu/9892847 to your computer and use it in GitHub Desktop.
Save tkojitu/9892847 to your computer and use it in GitHub Desktop.
<html>
<head>
<style>
#viewport{
border: 1px solid #000000;
}
</style>
<script src="/PhysicsJS/dist/physicsjs-full-0.5.4.js"></script>
<script src="HelloPhysicsJS.js"></script>
</head>
<body onload="onLoad();">
<canvas id="viewport"></canvas>
</body>
</html>
function onLoad() {
var world = Physics();
world.add(newRenderer());
world.add(newViewportBehavior());
world.add(Physics.behavior('body-impulse-response'));
world.add(Physics.behavior('constant-acceleration'));
world.add(newCircle());
world.subscribe('step', function() {
world.render();
});
Physics.util.ticker.subscribe(function(time, dt) {
world.step(time);
});
Physics.util.ticker.start();
}
function getViewportWidth() {
return 500;
}
function getViewportHeight() {
return 300;
}
function newRenderer() {
return Physics.renderer('canvas', {
el: 'viewport',
width: getViewportWidth(),
height: getViewportHeight()
});
}
function newViewportBehavior() {
return Physics.behavior('edge-collision-detection', {
aabb: getViewportBounds(),
restitution: 0.99,
cof: 0.99
});
}
function getViewportBounds() {
return Physics.aabb(0, 0, getViewportWidth(), getViewportHeight());
}
function newCircle() {
return Physics.body('circle', {
x: 40,
y: 30,
vx: 0.2,
vy: 0.01,
radius: 60
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment