Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simple working box2d test with rendering

  • Loading branch information...
commit dc545da9ff482b9285bd5ef0833666a696a17da4 1 parent 828757c
@stravid stravid authored
Showing with 45 additions and 9 deletions.
  1. +5 −1 demo/index.html
  2. +40 −8 demo/javascripts/base.js
View
6 demo/index.html
@@ -14,6 +14,10 @@
body {
background: #666666;
}
+
+ #canvas {
+ border: 1px solid red;
+ }
</style>
@@ -21,7 +25,7 @@
<body>
- <canvas id="canvas" width="1000" height="700"></canvas>
+ <canvas id="canvas" width="300" height="450"></canvas>
<script src='javascripts/utilities.js'></script>
<script src='javascripts/factory.js'></script>
View
48 demo/javascripts/base.js
@@ -12,21 +12,22 @@ Event.observe(window, 'load', function() {
elements = [];
worldAABB = new b2AABB();
-worldAABB.lowerBound.Set(-10000.0, -10000.0);
-worldAABB.upperBound.Set(10000.0, 10000.0);
+worldAABB.lowerBound.Set(-50, -50);
+worldAABB.upperBound.Set(200, 250);
-gravity = new b2Vec2(0.0, -9.8);
+gravity = new b2Vec2(0.0, 9.81);
world = new b2World(worldAABB, gravity, true);
+for (var i = 0; i < 1; i++) {
var bodyDefinition = new b2BodyDef(),
body,
shapeDefinition;
-bodyDefinition.position.Set(100, 1000);
+bodyDefinition.position.Set(55, 55);
body = world.CreateBody(bodyDefinition);
shapeDefinition = new b2PolygonDef();
-shapeDefinition.SetAsBox(1, 1);
+shapeDefinition.SetAsBox(2.5, 2.5);
shapeDefinition.restitution = 0;
shapeDefinition.density = 2;
shapeDefinition.friction = 0.9;
@@ -38,16 +39,47 @@ body.CreateShape(shapeDefinition);
body.SetMassFromShapes();
elements.push(body);
-
-for (var i = 0; i < elements.length; i++) {
- console.log(elements[i]);
}
+
+
// TODO: figure out what the drawing interval is, what a step is, and how these values correlate to each other!
setInterval(function() {
world.Step(1.0/60.0, 10);
+ canvas.width = canvas.width;
+
+
+ for (var i = 0; i < elements.length; i++) {
+
+ for (var shape = elements[i].GetShapeList(); shape; shape = shape.GetNext())
+ {
+ var position = elements[i].GetPosition(),
+ offset = {
+ x: 50,
+ y: 50
+ },
+ xFactor = canvasWidth / 100,
+ yFactor = canvasHeight / 150;
+
+ drawingContext.save();
+ drawingContext.translate(position.x * xFactor, position.y * yFactor);
+ drawingContext.strokeStyle = '#00ff00';
+ drawingContext.beginPath();
+
+ drawingContext.moveTo((shape.m_vertices[0].x - offset.x) * xFactor, (shape.m_vertices[0].y - offset.y) * yFactor);
+
+ for (var k = 1; k < shape.m_vertexCount; k++) {
+ drawingContext.lineTo((shape.m_vertices[k].x - offset.x) * xFactor, (shape.m_vertices[k].y - offset.y) * yFactor);
+ }
+
+ drawingContext.closePath();
+ drawingContext.stroke();
+ drawingContext.restore();
+ }
+}
+
}, 1000 / 30);
Please sign in to comment.
Something went wrong with that request. Please try again.