Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Re-adding box2d-js benchmark, because it's a bit faster than box2d-we…

…b (though it's older and doesn't include all box2d features).
  • Loading branch information...
commit 8fafbeeece6cabf8e2b0742f7213d66e033b2e7f 1 parent bb339cf
@joelgwebber authored
Showing with 19,140 additions and 0 deletions.
  1. +81 −0 box2d-js/bench2d-render.html
  2. +64 −0 box2d-js/bench2d-render.js
  3. +90 −0 box2d-js/bench2d-run.html
  4. +29 −0 box2d-js/bench2d-run.js
  5. +64 −0 box2d-js/bench2d.js
  6. +14 −0 box2d-js/box2d-js/LICENSE.txt
  7. +3 −0  box2d-js/box2d-js/README.txt
  8. +57 −0 box2d-js/box2d-js/demos/compound.js
  9. +69 −0 box2d-js/box2d-js/demos/crank.js
  10. +48 −0 box2d-js/box2d-js/demos/demo_base.js
  11. +49 −0 box2d-js/box2d-js/demos/demos.js
  12. +93 −0 box2d-js/box2d-js/demos/draw_world.js
  13. +20 −0 box2d-js/box2d-js/demos/pendulum.js
  14. +26 −0 box2d-js/box2d-js/demos/stack.js
  15. +42 −0 box2d-js/box2d-js/demos/top.js
  16. +213 −0 box2d-js/box2d-js/index.html
  17. +35 −0 box2d-js/box2d-js/js/box2d/collision/ClipVertex.js
  18. +61 −0 box2d-js/box2d-js/js/box2d/collision/Features.js
  19. +45 −0 box2d-js/box2d-js/js/box2d/collision/b2AABB.js
  20. +43 −0 box2d-js/box2d-js/js/box2d/collision/b2Bound.js
  21. +31 −0 box2d-js/box2d-js/js/box2d/collision/b2BoundValues.js
  22. +898 −0 box2d-js/box2d-js/js/box2d/collision/b2BroadPhase.js
  23. +26 −0 box2d-js/box2d-js/js/box2d/collision/b2BufferedPair.js
  24. +738 −0 box2d-js/box2d-js/js/box2d/collision/b2Collision.js
  25. +52 −0 box2d-js/box2d-js/js/box2d/collision/b2ContactID.js
  26. +35 −0 box2d-js/box2d-js/js/box2d/collision/b2ContactPoint.js
  27. +333 −0 box2d-js/box2d-js/js/box2d/collision/b2Distance.js
  28. +34 −0 box2d-js/box2d-js/js/box2d/collision/b2Manifold.js
  29. +34 −0 box2d-js/box2d-js/js/box2d/collision/b2OBB.js
  30. +60 −0 box2d-js/box2d-js/js/box2d/collision/b2Pair.js
  31. +34 −0 box2d-js/box2d-js/js/box2d/collision/b2PairCallback.js
  32. +386 −0 box2d-js/box2d-js/js/box2d/collision/b2PairManager.js
  33. +40 −0 box2d-js/box2d-js/js/box2d/collision/b2Proxy.js
  34. +49 −0 box2d-js/box2d-js/js/box2d/collision/shapes/b2BoxDef.js
  35. +49 −0 box2d-js/box2d-js/js/box2d/collision/shapes/b2CircleDef.js
  36. +198 −0 box2d-js/box2d-js/js/box2d/collision/shapes/b2CircleShape.js
  37. +36 −0 box2d-js/box2d-js/js/box2d/collision/shapes/b2MassData.js
  38. +58 −0 box2d-js/box2d-js/js/box2d/collision/shapes/b2PolyDef.js
  39. +492 −0 box2d-js/box2d-js/js/box2d/collision/shapes/b2PolyShape.js
  40. +339 −0 box2d-js/box2d-js/js/box2d/collision/shapes/b2Shape.js
  41. +109 −0 box2d-js/box2d-js/js/box2d/collision/shapes/b2ShapeDef.js
  42. +72 −0 box2d-js/box2d-js/js/box2d/common/b2Settings.js
  43. +130 −0 box2d-js/box2d-js/js/box2d/common/math/b2Mat22.js
  44. +218 −0 box2d-js/box2d-js/js/box2d/common/math/b2Math.js
  45. +131 −0 box2d-js/box2d-js/js/box2d/common/math/b2Vec2.js
  46. +469 −0 box2d-js/box2d-js/js/box2d/dynamics/b2Body.js
  47. +69 −0 box2d-js/box2d-js/js/box2d/dynamics/b2BodyDef.js
  48. +42 −0 box2d-js/box2d-js/js/box2d/dynamics/b2CollisionFilter.js
  49. +337 −0 box2d-js/box2d-js/js/box2d/dynamics/b2ContactManager.js
  50. +331 −0 box2d-js/box2d-js/js/box2d/dynamics/b2Island.js
  51. +27 −0 box2d-js/box2d-js/js/box2d/dynamics/b2TimeStep.js
  52. +522 −0 box2d-js/box2d-js/js/box2d/dynamics/b2World.js
  53. +52 −0 box2d-js/box2d-js/js/box2d/dynamics/b2WorldListener.js
  54. +102 −0 box2d-js/box2d-js/js/box2d/dynamics/contacts/b2CircleContact.js
  55. +228 −0 box2d-js/box2d-js/js/box2d/dynamics/contacts/b2Conservative.js
  56. +201 −0 box2d-js/box2d-js/js/box2d/dynamics/contacts/b2Contact.js
  57. +45 −0 box2d-js/box2d-js/js/box2d/dynamics/contacts/b2ContactConstraint.js
  58. +40 −0 box2d-js/box2d-js/js/box2d/dynamics/contacts/b2ContactConstraintPoint.js
  59. +33 −0 box2d-js/box2d-js/js/box2d/dynamics/contacts/b2ContactNode.js
  60. +30 −0 box2d-js/box2d-js/js/box2d/dynamics/contacts/b2ContactRegister.js
  61. +537 −0 box2d-js/box2d-js/js/box2d/dynamics/contacts/b2ContactSolver.js
  62. +65 −0 box2d-js/box2d-js/js/box2d/dynamics/contacts/b2NullContact.js
  63. +103 −0 box2d-js/box2d-js/js/box2d/dynamics/contacts/b2PolyAndCircleContact.js
  64. +163 −0 box2d-js/box2d-js/js/box2d/dynamics/contacts/b2PolyContact.js
  65. +264 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2DistanceJoint.js
  66. +49 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2DistanceJointDef.js
  67. +307 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2GearJoint.js
  68. +50 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2GearJointDef.js
  69. +49 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2Jacobian.js
  70. +200 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2Joint.js
  71. +40 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2JointDef.js
  72. +33 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2JointNode.js
  73. +234 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2MouseJoint.js
  74. +53 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2MouseJointDef.js
  75. +676 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2PrismaticJoint.js
  76. +56 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2PrismaticJointDef.js
  77. +618 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2PulleyJoint.js
  78. +70 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2PulleyJointDef.js
  79. +491 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2RevoluteJoint.js
  80. +55 −0 box2d-js/box2d-js/js/box2d/dynamics/joints/b2RevoluteJointDef.js
  81. +10 −0 box2d-js/box2d-js/lib/AUTHORS
  82. +202 −0 box2d-js/box2d-js/lib/COPYING
  83. +22 −0 box2d-js/box2d-js/lib/README
  84. +19 −0 box2d-js/box2d-js/lib/excanvas-compressed.js
  85. +785 −0 box2d-js/box2d-js/lib/excanvas.js
  86. +4,221 −0 box2d-js/box2d-js/lib/prototype-1.6.0.2.js
  87. +61 −0 box2d-js/box2d-js/style/box2d.css
  88. +61 −0 box2d-js/box2d-js/style/box2d.css~
  89. +124 −0 box2d-js/box2d-js/style/distant-planet.css
  90. BIN  box2d-js/box2d-js/style/images/Distant-Planet_01.gif
  91. BIN  box2d-js/box2d-js/style/images/Distant-Planet_02.gif
  92. BIN  box2d-js/box2d-js/style/images/Distant-Planet_03.gif
  93. BIN  box2d-js/box2d-js/style/images/Distant-Planet_04.gif
  94. BIN  box2d-js/box2d-js/style/images/Distant-Planet_05.gif
  95. BIN  box2d-js/box2d-js/style/images/Distant-Planet_06.gif
  96. BIN  box2d-js/box2d-js/style/images/Distant-Planet_07.gif
  97. BIN  box2d-js/box2d-js/style/images/Distant-Planet_08.gif
  98. BIN  box2d-js/box2d-js/style/images/bodybox_bg.png
  99. +27 −0 box2d-js/box2d-js/style/prettify.css
  100. +1,030 −0 box2d-js/box2d-js/style/prettify.js
  101. +142 −0 box2d-js/prototype-shim.js
  102. +67 −0 box2d-js/run-d8
View
81 box2d-js/bench2d-render.html
@@ -0,0 +1,81 @@
+<!DOCTYPE html>
+<html>
+ <head> <title>Bench2d</title> </head>
+
+ <body>
+ <canvas id="canvas" style='background-color:blue;' width='854' height='400'></canvas>
+
+ <!-- prototype -->
+ <script src="box2d-js/lib/prototype-1.6.0.2.js"></script>
+
+ <!-- box2djs -->
+ <script src='box2d-js/js/box2d/common/b2Settings.js'></script>
+ <script src='box2d-js/js/box2d/common/math/b2Vec2.js'></script>
+ <script src='box2d-js/js/box2d/common/math/b2Mat22.js'></script>
+ <script src='box2d-js/js/box2d/common/math/b2Math.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2AABB.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2Bound.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2BoundValues.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2Pair.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2PairCallback.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2BufferedPair.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2PairManager.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2BroadPhase.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2Collision.js'></script>
+ <script src='box2d-js/js/box2d/collision/Features.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2ContactID.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2ContactPoint.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2Distance.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2Manifold.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2OBB.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2Proxy.js'></script>
+ <script src='box2d-js/js/box2d/collision/ClipVertex.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2Shape.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2ShapeDef.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2BoxDef.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2CircleDef.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2CircleShape.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2MassData.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2PolyDef.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2PolyShape.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2Body.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2BodyDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2CollisionFilter.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2Island.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2TimeStep.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2ContactNode.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2Contact.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2ContactConstraint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2ContactConstraintPoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2ContactRegister.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2ContactSolver.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2CircleContact.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2Conservative.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2NullContact.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2PolyAndCircleContact.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2PolyContact.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2ContactManager.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2World.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2WorldListener.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2JointNode.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2Joint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2JointDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2DistanceJoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2DistanceJointDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2Jacobian.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2GearJoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2GearJointDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2MouseJoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2MouseJointDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2PrismaticJoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2PrismaticJointDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2PulleyJoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2PulleyJointDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2RevoluteJoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2RevoluteJointDef.js'></script>
+
+ <script src='bench2d.js'></script>
+ <script src='bench2d-render.js'></script>
+ </body>
+</html>
+
View
64 box2d-js/bench2d-render.js
@@ -0,0 +1,64 @@
+var canvas, ctx;
+
+function drawWorld(world, context) {
+ context.save();
+ context.transform(8, 0, 0, -8, 854/2, 400);
+
+ for (var b = world.m_bodyList; b; b = b.m_next) {
+ for (var s = b.GetShapeList(); s != null; s = s.GetNext()) {
+ drawShape(s, context);
+ }
+ }
+
+ context.restore();
+}
+
+function drawShape(shape, context) {
+ context.fillStyle = '#ffffff';
+ context.beginPath();
+ switch (shape.m_type) {
+ case b2Shape.e_circleShape: {
+ var circle = shape;
+ var pos = circle.m_position;
+ var r = circle.m_radius;
+ var segments = 16.0;
+ var theta = 0.0;
+ var dtheta = 2.0 * Math.PI / segments;
+ // draw circle
+ context.moveTo(pos.x + r, pos.y);
+ for (var i = 0; i < segments; i++) {
+ var d = new b2Vec2(r * Math.cos(theta), r * Math.sin(theta));
+ var v = b2Math.AddVV(pos, d);
+ context.lineTo(v.x, v.y);
+ theta += dtheta;
+ }
+ context.lineTo(pos.x + r, pos.y);
+ break;
+ }
+ case b2Shape.e_polyShape: {
+ var poly = shape;
+ var tV = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[0]));
+ context.moveTo(tV.x, tV.y);
+ for (var i = 0; i < poly.m_vertexCount; i++) {
+ var v = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[i]));
+ context.lineTo(v.x, v.y);
+ }
+ context.lineTo(tV.x, tV.y);
+ break;
+ }
+ }
+ context.fill();
+}
+
+function render() {
+ canvas = $('canvas');
+ ctx = canvas.getContext('2d');
+ setInterval(function() {
+ step();
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ drawWorld(world, ctx);
+ }, 10);
+}
+
+render();
+
View
90 box2d-js/bench2d-run.html
@@ -0,0 +1,90 @@
+<!DOCTYPE html>
+<html>
+ <head> <title>Bench2d</title> </head>
+
+ <body>
+ <div>This is going to take a while to warm up and run. Your browser window will be locked up while it does so.</div>
+
+ <div id='echo'></div>
+
+ <script>
+ function print(msg) {
+ var echo = document.getElementById('echo');
+ echo.innerHTML += msg + '<br>';
+ }
+ </script>
+
+ <!-- prototype -->
+ <script src="box2d-js/lib/prototype-1.6.0.2.js"></script>
+
+ <!-- box2djs -->
+ <script src='box2d-js/js/box2d/common/b2Settings.js'></script>
+ <script src='box2d-js/js/box2d/common/math/b2Vec2.js'></script>
+ <script src='box2d-js/js/box2d/common/math/b2Mat22.js'></script>
+ <script src='box2d-js/js/box2d/common/math/b2Math.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2AABB.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2Bound.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2BoundValues.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2Pair.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2PairCallback.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2BufferedPair.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2PairManager.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2BroadPhase.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2Collision.js'></script>
+ <script src='box2d-js/js/box2d/collision/Features.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2ContactID.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2ContactPoint.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2Distance.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2Manifold.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2OBB.js'></script>
+ <script src='box2d-js/js/box2d/collision/b2Proxy.js'></script>
+ <script src='box2d-js/js/box2d/collision/ClipVertex.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2Shape.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2ShapeDef.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2BoxDef.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2CircleDef.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2CircleShape.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2MassData.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2PolyDef.js'></script>
+ <script src='box2d-js/js/box2d/collision/shapes/b2PolyShape.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2Body.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2BodyDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2CollisionFilter.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2Island.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2TimeStep.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2ContactNode.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2Contact.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2ContactConstraint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2ContactConstraintPoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2ContactRegister.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2ContactSolver.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2CircleContact.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2Conservative.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2NullContact.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2PolyAndCircleContact.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/contacts/b2PolyContact.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2ContactManager.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2World.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/b2WorldListener.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2JointNode.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2Joint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2JointDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2DistanceJoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2DistanceJointDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2Jacobian.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2GearJoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2GearJointDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2MouseJoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2MouseJointDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2PrismaticJoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2PrismaticJointDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2PulleyJoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2PulleyJointDef.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2RevoluteJoint.js'></script>
+ <script src='box2d-js/js/box2d/dynamics/joints/b2RevoluteJointDef.js'></script>
+
+ <script src='bench2d.js'></script>
+ <script src='bench2d-run.js'></script>
+ </body>
+</html>
+
View
29 box2d-js/bench2d-run.js
@@ -0,0 +1,29 @@
+var WARMUP = 64;
+var FRAMES = 256;
+
+function warmup() {
+ for (var i = 0; i < FRAMES; ++i) {
+ step();
+ }
+}
+
+function bench() {
+ var times = [];
+ for (var i = 0; i < FRAMES; ++i) {
+ var begin = new Date().getTime();
+ step();
+ var end = new Date().getTime();
+ times[i] = end - begin;
+ print(times[i]);
+ }
+
+ var total = 0;
+ for (var i = 0; i < FRAMES; ++i) {
+ total += times[i];
+ }
+ print('Average: ' + (total / FRAMES));
+}
+
+warmup(FRAMES);
+bench();
+
View
64 box2d-js/bench2d.js
@@ -0,0 +1,64 @@
+var world;
+var PYRAMID_SIZE = 40;
+
+function setAsBox(sd, hx, hy) {
+ sd.vertexCount = 4;
+ sd.vertices[0].Set(-hx, -hy);
+ sd.vertices[1].Set(hx, -hy);
+ sd.vertices[2].Set(hx, hy);
+ sd.vertices[3].Set(-hx, hy);
+}
+
+function init() {
+ var gravity = new b2Vec2(0, -10);
+ var worldAABB = new b2AABB();
+ worldAABB.minVertex.Set(-1000, -1000);
+ worldAABB.maxVertex.Set(1000, 1000);
+ world = new b2World(worldAABB, gravity, true);
+
+ { // Floor
+ var sd = new b2PolyDef();
+ setAsBox(sd, 50.0, 10.0);
+
+ var bd = new b2BodyDef();
+ bd.AddShape(sd);
+ bd.position = new b2Vec2(0.0, -10.0);
+ world.CreateBody(bd);
+ }
+
+ {
+ var a = .5;
+ var shape = new b2PolyDef();
+ shape.density = 1;
+ shape.friction = 1;
+ shape.restitution = 1;
+ setAsBox(shape, a, a);
+
+ var x = new b2Vec2(-7.0, 0.75);
+ var y = new b2Vec2(0, 0);
+ var deltaX = new b2Vec2(0.5625, 1);
+ var deltaY = new b2Vec2(1.125, 0.0);
+
+ for (var i = 0; i < PYRAMID_SIZE; ++i){
+ y.Set(x.x, x.y);
+
+ for (var j = i; j < PYRAMID_SIZE; ++j) {
+ var bd = new b2BodyDef();
+ bd.AddShape(shape);
+ bd.position = new b2Vec2(y.x, y.y);
+ bd.rotation = 0;
+ var body = world.CreateBody(bd);
+ y.Add(deltaY);
+ }
+
+ x.Add(deltaX);
+ }
+ }
+}
+
+function step() {
+ world.Step(1.0/60, 3);
+}
+
+init();
+
View
14 box2d-js/box2d-js/LICENSE.txt
@@ -0,0 +1,14 @@
+The zlib/libpng License
+
+Copyright (c) 2008 ANDO Yasushi
+
+This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
+
+Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
+
+ 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
+
+ 3. This notice may not be removed or altered from any source distribution.
+
View
3  box2d-js/box2d-js/README.txt
@@ -0,0 +1,3 @@
+How to use
+1. put js/ and lib/ in your app dir
+2. add script tags refering the header of index.html
View
57 box2d-js/box2d-js/demos/compound.js
@@ -0,0 +1,57 @@
+demos.compound = {};
+demos.compound.createCompoundBall = function(world, x, y) {
+ var ballSd1 = new b2CircleDef();
+ ballSd1.density = 1.0;
+ ballSd1.radius = 20;
+ ballSd1.restitution = 0.2;
+ ballSd1.localPosition.Set(-20, 0);
+ var ballSd2 = new b2CircleDef();
+ ballSd2.density = 1.0;
+ ballSd2.radius = 20;
+ ballSd2.restitution = 0.2;
+ ballSd2.localPosition.Set(20, 0);
+ var ballBd = new b2BodyDef();
+ ballBd.AddShape(ballSd1);
+ ballBd.AddShape(ballSd2);
+ ballBd.position.Set(x, y);
+ return world.CreateBody(ballBd);
+}
+
+demos.compound.createCompoundPoly = function(world, x, y) {
+ var points = [[-30, 0], [30, 0], [0, 15]];
+ var polySd1 = new b2PolyDef();
+ polySd1.vertexCount = points.length;
+ for (var i = 0; i < points.length; i++) {
+ polySd1.vertices[i].Set(points[i][0], points[i][1]);
+ }
+ polySd1.localRotation = 0.3524 * Math.PI;
+ var R1 = new b2Mat22(polySd1.localRotation);
+ polySd1.localPosition = b2Math.b2MulMV(R1, new b2Vec2(30, 0));
+ polySd1.density = 1.0;
+ var polySd2 = new b2PolyDef();
+ polySd2.vertexCount = points.length;
+ for (var i = 0; i < points.length; i++) {
+ polySd2.vertices[i].Set(points[i][0], points[i][1]);
+ }
+ polySd2.localRotation = -0.3524 * Math.PI;
+ var R2 = new b2Mat22(polySd2.localRotation);
+ polySd2.localPosition = b2Math.b2MulMV(R2, new b2Vec2(-30, 0));
+ var polyBd = new b2BodyDef();
+ polyBd.AddShape(polySd1);
+ polyBd.AddShape(polySd2);
+ polyBd.position.Set(x,y);
+ return world.CreateBody(polyBd)
+}
+
+demos.compound.initWorld = function(world) {
+ var i;
+ for (i = 1; i <= 4; i++) {
+ demos.compound.createCompoundPoly(world, 150 + 3 * Math.random(), 40 * i);
+ }
+ for (i = 1; i <= 4; i++) {
+ demos.compound.createCompoundBall(world, 350 + 3 * Math.random(), 45 * i);
+ }
+}
+demos.InitWorlds.push(demos.compound.initWorld);
+
+
View
69 box2d-js/box2d-js/demos/crank.js
@@ -0,0 +1,69 @@
+demos.crank = {};
+demos.crank.initWorld = function(world) {
+ var ground = world.m_groundBody;
+
+ // Define crank.
+ var sd = new b2BoxDef();
+ sd.extents.Set(5, 25);
+ sd.density = 1.0;
+
+ var bd = new b2BodyDef();
+ bd.AddShape(sd);
+
+ var rjd = new b2RevoluteJointDef();
+
+ var prevBody = ground;
+
+ bd.position.Set(500/2, 210);
+ var body = world.CreateBody(bd);
+
+ rjd.anchorPoint.Set(500/2, 235);
+ rjd.body1 = prevBody;
+ rjd.body2 = body;
+ rjd.motorSpeed = -1.0 * Math.PI;
+ rjd.motorTorque = 500000000.0;
+ rjd.enableMotor = true;
+ world.CreateJoint(rjd);
+
+ prevBody = body;
+
+ // Define follower.
+ sd.extents.Set(5, 45);
+ bd.position.Set(500/2, 140);
+ body = world.CreateBody(bd);
+
+ rjd.anchorPoint.Set(500/2, 185);
+ rjd.body1 = prevBody;
+ rjd.body2 = body;
+ rjd.enableMotor = false;
+ world.CreateJoint(rjd);
+
+ prevBody = body;
+
+ // Define piston
+ sd.extents.Set(20, 20);
+ bd.position.Set(500/2, 95);
+ body = world.CreateBody(bd);
+
+ rjd.anchorPoint.Set(500/2, 95);
+ rjd.body1 = prevBody;
+ rjd.body2 = body;
+ world.CreateJoint(rjd);
+
+ var pjd = new b2PrismaticJointDef();
+ pjd.anchorPoint.Set(500/2, 95);
+ pjd.body1 = ground;
+ pjd.body2 = body;
+ pjd.axis.Set(0.0, 1.0);
+ pjd.motorSpeed = 0.0; // joint friction
+ pjd.motorForce = 100000.0;
+ pjd.enableMotor = true;
+
+ world.CreateJoint(pjd);
+
+ // Create a payload
+ sd.density = 2.0;
+ bd.position.Set(500/2, 10);
+ world.CreateBody(bd);
+}
+demos.InitWorlds.push(demos.crank.initWorld);
View
48 box2d-js/box2d-js/demos/demo_base.js
@@ -0,0 +1,48 @@
+function createWorld() {
+ var worldAABB = new b2AABB();
+ worldAABB.minVertex.Set(-1000, -1000);
+ worldAABB.maxVertex.Set(1000, 1000);
+ var gravity = new b2Vec2(0, 300);
+ var doSleep = true;
+ var world = new b2World(worldAABB, gravity, doSleep);
+ createGround(world);
+ createBox(world, 0, 125, 10, 250);
+ createBox(world, 500, 125, 10, 250);
+ return world;
+}
+
+function createGround(world) {
+ var groundSd = new b2BoxDef();
+ groundSd.extents.Set(1000, 50);
+ groundSd.restitution = 0.2;
+ var groundBd = new b2BodyDef();
+ groundBd.AddShape(groundSd);
+ groundBd.position.Set(-500, 340);
+ return world.CreateBody(groundBd)
+}
+
+function createBall(world, x, y) {
+ var ballSd = new b2CircleDef();
+ ballSd.density = 1.0;
+ ballSd.radius = 20;
+ ballSd.restitution = 1.0;
+ ballSd.friction = 0;
+ var ballBd = new b2BodyDef();
+ ballBd.AddShape(ballSd);
+ ballBd.position.Set(x,y);
+ return world.CreateBody(ballBd);
+}
+
+function createBox(world, x, y, width, height, fixed) {
+ if (typeof(fixed) == 'undefined') fixed = true;
+ var boxSd = new b2BoxDef();
+ if (!fixed) boxSd.density = 1.0;
+ boxSd.extents.Set(width, height);
+ var boxBd = new b2BodyDef();
+ boxBd.AddShape(boxSd);
+ boxBd.position.Set(x,y);
+ return world.CreateBody(boxBd)
+}
+
+var demos = {};
+demos.InitWorlds = [];
View
49 box2d-js/box2d-js/demos/demos.js
@@ -0,0 +1,49 @@
+var initId = 0;
+var world = createWorld();
+var ctx;
+var canvasWidth;
+var canvasHeight;
+var canvasTop;
+var canvasLeft;
+
+function setupWorld(did) {
+ if (!did) did = 0;
+ world = createWorld();
+ initId += did;
+ initId %= demos.InitWorlds.length;
+ if (initId < 0) initId = demos.InitWorlds.length + initId;
+ demos.InitWorlds[initId](world);
+}
+function setupNextWorld() { setupWorld(1); }
+function setupPrevWorld() { setupWorld(-1); }
+function step(cnt) {
+ var stepping = false;
+ var timeStep = 1.0/60;
+ var iteration = 1;
+ world.Step(timeStep, iteration);
+ ctx.clearRect(0, 0, canvasWidth, canvasHeight);
+ drawWorld(world, ctx);
+ setTimeout('step(' + (cnt || 0) + ')', 10);
+}
+Event.observe(window, 'load', function() {
+ setupWorld();
+ ctx = $('canvas').getContext('2d');
+ var canvasElm = $('canvas');
+ canvasWidth = parseInt(canvasElm.width);
+ canvasHeight = parseInt(canvasElm.height);
+ canvasTop = parseInt(canvasElm.style.top);
+ canvasLeft = parseInt(canvasElm.style.left);
+ Event.observe('canvas', 'click', function(e) {
+ //setupNextWorld();
+ if (Math.random() < 0.5)
+ demos.top.createBall(world, Event.pointerX(e) - canvasLeft, Event.pointerY(e) - canvasTop);
+ else
+ createBox(world, Event.pointerX(e) - canvasLeft, Event.pointerY(e) - canvasTop, 10, 10, false);
+ });
+ Event.observe('canvas', 'contextmenu', function(e) {
+ if (e.preventDefault) e.preventDefault();
+ setupPrevWorld();
+ return false;
+ });
+ step();
+});
View
93 box2d-js/box2d-js/demos/draw_world.js
@@ -0,0 +1,93 @@
+function drawWorld(world, context) {
+ for (var j = world.m_jointList; j; j = j.m_next) {
+ drawJoint(j, context);
+ }
+ for (var b = world.m_bodyList; b; b = b.m_next) {
+ for (var s = b.GetShapeList(); s != null; s = s.GetNext()) {
+ drawShape(s, context);
+ }
+ }
+}
+function drawJoint(joint, context) {
+ var b1 = joint.m_body1;
+ var b2 = joint.m_body2;
+ var x1 = b1.m_position;
+ var x2 = b2.m_position;
+ var p1 = joint.GetAnchor1();
+ var p2 = joint.GetAnchor2();
+ context.strokeStyle = '#00eeee';
+ context.beginPath();
+ switch (joint.m_type) {
+ case b2Joint.e_distanceJoint:
+ context.moveTo(p1.x, p1.y);
+ context.lineTo(p2.x, p2.y);
+ break;
+
+ case b2Joint.e_pulleyJoint:
+ // TODO
+ break;
+
+ default:
+ if (b1 == world.m_groundBody) {
+ context.moveTo(p1.x, p1.y);
+ context.lineTo(x2.x, x2.y);
+ }
+ else if (b2 == world.m_groundBody) {
+ context.moveTo(p1.x, p1.y);
+ context.lineTo(x1.x, x1.y);
+ }
+ else {
+ context.moveTo(x1.x, x1.y);
+ context.lineTo(p1.x, p1.y);
+ context.lineTo(x2.x, x2.y);
+ context.lineTo(p2.x, p2.y);
+ }
+ break;
+ }
+ context.stroke();
+}
+function drawShape(shape, context) {
+ context.strokeStyle = '#ffffff';
+ context.beginPath();
+ switch (shape.m_type) {
+ case b2Shape.e_circleShape:
+ {
+ var circle = shape;
+ var pos = circle.m_position;
+ var r = circle.m_radius;
+ var segments = 16.0;
+ var theta = 0.0;
+ var dtheta = 2.0 * Math.PI / segments;
+ // draw circle
+ context.moveTo(pos.x + r, pos.y);
+ for (var i = 0; i < segments; i++) {
+ var d = new b2Vec2(r * Math.cos(theta), r * Math.sin(theta));
+ var v = b2Math.AddVV(pos, d);
+ context.lineTo(v.x, v.y);
+ theta += dtheta;
+ }
+ context.lineTo(pos.x + r, pos.y);
+
+ // draw radius
+ context.moveTo(pos.x, pos.y);
+ var ax = circle.m_R.col1;
+ var pos2 = new b2Vec2(pos.x + r * ax.x, pos.y + r * ax.y);
+ context.lineTo(pos2.x, pos2.y);
+ }
+ break;
+ case b2Shape.e_polyShape:
+ {
+ var poly = shape;
+ var tV = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[0]));
+ context.moveTo(tV.x, tV.y);
+ for (var i = 0; i < poly.m_vertexCount; i++) {
+ var v = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[i]));
+ context.lineTo(v.x, v.y);
+ }
+ context.lineTo(tV.x, tV.y);
+ }
+ break;
+ }
+ context.stroke();
+}
+
View
20 box2d-js/box2d-js/demos/pendulum.js
@@ -0,0 +1,20 @@
+demos.pendulum = {};
+demos.pendulum.initWorld = function(world) {
+ var i;
+ var ground = world.GetGroundBody();
+ var jointDef = new b2RevoluteJointDef();
+ var L = 150;
+ for (i = 0; i < 4; i++) {
+ jointDef.anchorPoint.Set(250 + 40 * i, 200 - L);
+ jointDef.body1 = ground;
+ jointDef.body2 = createBall(world, 250 + 40 * i, 200);
+ world.CreateJoint(jointDef);
+ }
+ jointDef.anchorPoint.Set(250 - 40, 200 - L);
+ jointDef.body1 = ground;
+ jointDef.body2 = createBall(world, 250 - 40 - L, 200 - L);
+ world.CreateJoint(jointDef);
+}
+demos.InitWorlds.push(demos.pendulum.initWorld);
+
+
View
26 box2d-js/box2d-js/demos/stack.js
@@ -0,0 +1,26 @@
+demos.stack = {};
+demos.stack.initWorld = function(world) {
+ var sd = new b2BoxDef();
+ var bd = new b2BodyDef();
+ bd.AddShape(sd);
+ sd.density = 1.0;
+ sd.friction = 0.5;
+ sd.extents.Set(10, 10);
+
+ var i;
+ for (i = 0; i < 8; i++) {
+ bd.position.Set(500/2-Math.random()*2-1, (250-5-i*22));
+ world.CreateBody(bd);
+ }
+ for (i = 0; i < 8; i++) {
+ bd.position.Set(500/2-100-Math.random()*5+i, (250-5-i*22));
+ world.CreateBody(bd);
+ }
+ for (i = 0; i < 8; i++) {
+ bd.position.Set(500/2+100+Math.random()*5-i, (250-5-i*22));
+ world.CreateBody(bd);
+ }
+}
+demos.InitWorlds.push(demos.stack.initWorld);
+
+
View
42 box2d-js/box2d-js/demos/top.js
@@ -0,0 +1,42 @@
+demos.top = {};
+demos.top.createBall = function(world, x, y, rad, fixed) {
+ var ballSd = new b2CircleDef();
+ if (!fixed) ballSd.density = 1.0;
+ ballSd.radius = rad || 10;
+ ballSd.restitution = 0.2;
+ var ballBd = new b2BodyDef();
+ ballBd.AddShape(ballSd);
+ ballBd.position.Set(x,y);
+ return world.CreateBody(ballBd);
+};
+demos.top.createPoly = function(world, x, y, points, fixed) {
+ var polySd = new b2PolyDef();
+ if (!fixed) polySd.density = 1.0;
+ polySd.vertexCount = points.length;
+ for (var i = 0; i < points.length; i++) {
+ polySd.vertices[i].Set(points[i][0], points[i][1]);
+ }
+ var polyBd = new b2BodyDef();
+ polyBd.AddShape(polySd);
+ polyBd.position.Set(x,y);
+ return world.CreateBody(polyBd)
+};
+demos.top.initWorld = function(world) {
+ demos.top.createBall(world, 350, 100, 50, true);
+ demos.top.createPoly(world, 100, 100, [[0, 0], [10, 30], [-10, 30]], true);
+ demos.top.createPoly(world, 150, 150, [[0, 0], [10, 30], [-10, 30]], true);
+ var pendulum = createBox(world, 150, 100, 20, 20, false);
+ var jointDef = new b2RevoluteJointDef();
+ jointDef.body1 = pendulum;
+ jointDef.body2 = world.GetGroundBody();
+ jointDef.anchorPoint = pendulum.GetCenterPosition();
+ world.CreateJoint(jointDef);
+
+ var seesaw = demos.top.createPoly(world, 300, 200, [[0, 0], [100, 30], [-100, 30]]);
+ jointDef.body1 = seesaw;
+ jointDef.anchorPoint = seesaw.GetCenterPosition();
+ world.CreateJoint(jointDef);
+};
+demos.InitWorlds.push(demos.top.initWorld);
+
+
View
213 box2d-js/box2d-js/index.html
@@ -0,0 +1,213 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <title>Box2DJS - Physics Engine for JavaScript</title>
+
+ <!--=============================-->
+ <!-- Copy this part to your app. -->
+ <!-- START -->
+ <!--=============================-->
+ <!-- libs -->
+ <!--[if IE]><script type="text/javascript" src="lib/excanvas.js"></script><![endif]-->
+ <script src="lib/prototype-1.6.0.2.js"></script>
+
+ <!-- box2djs -->
+ <script src='js/box2d/common/b2Settings.js'></script>
+ <script src='js/box2d/common/math/b2Vec2.js'></script>
+ <script src='js/box2d/common/math/b2Mat22.js'></script>
+ <script src='js/box2d/common/math/b2Math.js'></script>
+ <script src='js/box2d/collision/b2AABB.js'></script>
+ <script src='js/box2d/collision/b2Bound.js'></script>
+ <script src='js/box2d/collision/b2BoundValues.js'></script>
+ <script src='js/box2d/collision/b2Pair.js'></script>
+ <script src='js/box2d/collision/b2PairCallback.js'></script>
+ <script src='js/box2d/collision/b2BufferedPair.js'></script>
+ <script src='js/box2d/collision/b2PairManager.js'></script>
+ <script src='js/box2d/collision/b2BroadPhase.js'></script>
+ <script src='js/box2d/collision/b2Collision.js'></script>
+ <script src='js/box2d/collision/Features.js'></script>
+ <script src='js/box2d/collision/b2ContactID.js'></script>
+ <script src='js/box2d/collision/b2ContactPoint.js'></script>
+ <script src='js/box2d/collision/b2Distance.js'></script>
+ <script src='js/box2d/collision/b2Manifold.js'></script>
+ <script src='js/box2d/collision/b2OBB.js'></script>
+ <script src='js/box2d/collision/b2Proxy.js'></script>
+ <script src='js/box2d/collision/ClipVertex.js'></script>
+ <script src='js/box2d/collision/shapes/b2Shape.js'></script>
+ <script src='js/box2d/collision/shapes/b2ShapeDef.js'></script>
+ <script src='js/box2d/collision/shapes/b2BoxDef.js'></script>
+ <script src='js/box2d/collision/shapes/b2CircleDef.js'></script>
+ <script src='js/box2d/collision/shapes/b2CircleShape.js'></script>
+ <script src='js/box2d/collision/shapes/b2MassData.js'></script>
+ <script src='js/box2d/collision/shapes/b2PolyDef.js'></script>
+ <script src='js/box2d/collision/shapes/b2PolyShape.js'></script>
+ <script src='js/box2d/dynamics/b2Body.js'></script>
+ <script src='js/box2d/dynamics/b2BodyDef.js'></script>
+ <script src='js/box2d/dynamics/b2CollisionFilter.js'></script>
+ <script src='js/box2d/dynamics/b2Island.js'></script>
+ <script src='js/box2d/dynamics/b2TimeStep.js'></script>
+ <script src='js/box2d/dynamics/contacts/b2ContactNode.js'></script>
+ <script src='js/box2d/dynamics/contacts/b2Contact.js'></script>
+ <script src='js/box2d/dynamics/contacts/b2ContactConstraint.js'></script>
+ <script src='js/box2d/dynamics/contacts/b2ContactConstraintPoint.js'></script>
+ <script src='js/box2d/dynamics/contacts/b2ContactRegister.js'></script>
+ <script src='js/box2d/dynamics/contacts/b2ContactSolver.js'></script>
+ <script src='js/box2d/dynamics/contacts/b2CircleContact.js'></script>
+ <script src='js/box2d/dynamics/contacts/b2Conservative.js'></script>
+ <script src='js/box2d/dynamics/contacts/b2NullContact.js'></script>
+ <script src='js/box2d/dynamics/contacts/b2PolyAndCircleContact.js'></script>
+ <script src='js/box2d/dynamics/contacts/b2PolyContact.js'></script>
+ <script src='js/box2d/dynamics/b2ContactManager.js'></script>
+ <script src='js/box2d/dynamics/b2World.js'></script>
+ <script src='js/box2d/dynamics/b2WorldListener.js'></script>
+ <script src='js/box2d/dynamics/joints/b2JointNode.js'></script>
+ <script src='js/box2d/dynamics/joints/b2Joint.js'></script>
+ <script src='js/box2d/dynamics/joints/b2JointDef.js'></script>
+ <script src='js/box2d/dynamics/joints/b2DistanceJoint.js'></script>
+ <script src='js/box2d/dynamics/joints/b2DistanceJointDef.js'></script>
+ <script src='js/box2d/dynamics/joints/b2Jacobian.js'></script>
+ <script src='js/box2d/dynamics/joints/b2GearJoint.js'></script>
+ <script src='js/box2d/dynamics/joints/b2GearJointDef.js'></script>
+ <script src='js/box2d/dynamics/joints/b2MouseJoint.js'></script>
+ <script src='js/box2d/dynamics/joints/b2MouseJointDef.js'></script>
+ <script src='js/box2d/dynamics/joints/b2PrismaticJoint.js'></script>
+ <script src='js/box2d/dynamics/joints/b2PrismaticJointDef.js'></script>
+ <script src='js/box2d/dynamics/joints/b2PulleyJoint.js'></script>
+ <script src='js/box2d/dynamics/joints/b2PulleyJointDef.js'></script>
+ <script src='js/box2d/dynamics/joints/b2RevoluteJoint.js'></script>
+ <script src='js/box2d/dynamics/joints/b2RevoluteJointDef.js'></script>
+ <!--=============================-->
+ <!-- Copy this part to your app. -->
+ <!-- END -->
+ <!--=============================-->
+
+ <!-- demos -->
+ <script src='demos/draw_world.js'></script>
+ <script src='demos/demo_base.js'></script>
+ <script src='demos/top.js'></script>
+ <script src='demos/stack.js'></script>
+ <script src='demos/compound.js'></script>
+ <script src='demos/pendulum.js'></script>
+ <script src='demos/crank.js'></script>
+ <script src='demos/demos.js'></script>
+
+ <script src='style/prettify.js'></script>
+ <link href="style/prettify.css" rel="stylesheet" type="text/css" />
+ <script>Event.observe(window, 'load', function() {prettyPrint()});</script>
+
+ <link href="style/distant-planet.css" rel="stylesheet" type="text/css" />
+ <link href="style/box2d.css" rel="stylesheet" type="text/css" />
+ </head>
+<body>
+<div id="CONTAINER">
+ <div id="HEADER">
+ <h1>Box2D<br/><b>JS</b></h1>
+ </div>
+ <div id="NAVIGATION">
+ <div class="Nav">
+ <span class="links">
+ <a href="http://sourceforge.net/projects/box2d-js/">Home</a>
+ - <a href="http://sourceforge.net/project/showfiles.php?group_id=227549">Download</a>
+ - <a href="#about">About</a>
+ - <a href="#howto">Howto</a>
+ - <a href="#links">Links</a>
+ - <a href="#contact">Contact</a>
+ </span>
+ </div>
+ </div>
+ <div id="BODYBOX">
+ <div class="LEFT"></div>
+ <div class="MIDDLE">
+ <canvas id="canvas" width='500' height='300' style="top:260px; left:125px;"></canvas>
+ <div id="spacer"> </div>
+ <p style="text-align:center">
+ <span class="key">Left-click</span> to create an object.
+ <span class="key">Right-click</span> to show next example.
+ </p>
+
+ <a name="about"></a>
+ <h1>What's this</h1>
+ <p>
+ Box2DJS is a JavaScript port of <a href="http://www.box2d.org/">Box2D Physics Engine</a>.
+ To tell the truth, this is converted from <a href="http://box2dflash.sourceforge.net/">Box2DFlashAS3</a>_1.4.3.1 in an automatic manner.
+ (The reason why not Box2DFlashAS3_2.0.0 based is simply because I overlooked the renewal.)
+ </p>
+
+ <a name="howto"></a>
+ <h1>How to use</h1>
+ <p>
+ Because this libray does not have a lazy-loading system now, you should load all classes before starting your simulation.
+ To make things worse, each library has a bit complecated dependency each other so that loading libraries in wrong order may cause a fatal error.
+ To avoid such a trouble, it is strongly recomended to copy the header part of this file or `index.html' including the downloaded zip file.
+ </p>
+ <p>
+ Concerning the Box2D APIs of the library, they are completely same as Box2DFlashAS3. Please google information about it.
+ </p>
+
+ <h2>Sample code</h2>
+ <h3>Create a world</h3>
+ <pre class="prettyprint">var worldAABB = new b2AABB();
+worldAABB.minVertex.Set(-1000, -1000);
+worldAABB.maxVertex.Set(1000, 1000);
+var gravity = new b2Vec2(0, 300);
+var doSleep = true;
+var world = new b2World(worldAABB, gravity, doSleep); </pre>
+
+ <h3>Create a circle body</h3>
+ <pre class="prettyprint">var circleSd = new b2CircleDef();
+circleSd.density = 1.0;
+circleSd.radius = 20;
+circleSd.restitution = 1.0;
+circleSd.friction = 0;
+var circleBd = new b2BodyDef();
+circleBd.AddShape(circleSd);
+circleBd.position.Set(x,y);
+var circleBody = world.CreateBody(circleBd);</pre>
+
+ <h3>Create a revolute joint</h3>
+ <pre class="prettyprint">var jointDef = new b2RevoluteJointDef();
+jointDef.anchorPoint.Set(250, 200);
+jointDef.body1 = world.GetGroundBody();
+jointDef.body2 = circleBody;
+world.CreateJoint(jointDef);</pre>
+
+ <h3>Step a world</h3>
+ <pre class="prettyprint">world.Step();</pre>
+
+ <a name="dependencies"></a>
+ <h1>Dependencies</h1>
+ <ul>
+ <li><a href="http://www.prototypejs.org/">prototype.js</a></li>
+ <li><a href="http://sourceforge.net/projects/iecanvas">IECanvas</a> (when you use a canvas tag to display the result of your physics simulation)</li>
+ </ul>
+
+ <a name="links"></a>
+ <h1>Links</h1>
+ <ul>
+ <li><a href="http://sourceforge.net/projects/box2d-js/">Project Page (SF)</a></li>
+ <li><a href="http://sourceforge.net/project/showfiles.php?group_id=227549">Download Page (SF)</a></li>
+ <li><a href="http://www.box2d.org/">Box2D</a></li>
+ <li><a href="http://box2dflash.sourceforge.net/">Box2DFlashAS3</a></li>
+ <li><a href="http://www.kyucon.com/doc/box2d/">API Doc (Box2DFlashAS3)</a></li>
+ </ul>
+
+ <a name="contact"></a>
+ <h1>Contact</h1>
+ <p>
+ If you have some requests or find any bugs, please tell me about them.<br />
+ <div class="contact">
+ E-mail: andyjpn _at_ gmail _dot_ com<br />
+ Blog: <a href="http://d.hatena.ne.jp/technohippy/">http://d.hatena.ne.jp/technohippy/</a> (written in Japanese)
+ </div>
+ </p>
+
+ </div>
+ <div class="RIGHT"></div>
+ <div id="FOOTER">
+ <div class="footertext"><span class="links">&copy;Copyright 2008 ANDO Yasushi - <a href="http://www.rankrover.com">Website Design &amp; SEO by RankRover</a></span></div>
+ </div>
+ </div>
+</div>
+</body>
+</html>
View
35 box2d-js/box2d-js/js/box2d/collision/ClipVertex.js
@@ -0,0 +1,35 @@
+/*
+* Copyright (c) 2006-2007 Erin Catto http:
+*
+* This software is provided 'as-is', without any express or implied
+* warranty. In no event will the authors be held liable for any damages
+* arising from the use of this software.
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+* 1. The origin of this software must not be misrepresented; you must not
+* claim that you wrote the original software. If you use this software
+* in a product, an acknowledgment in the product documentation would be
+* appreciated but is not required.
+* 2. Altered source versions must be plainly marked, and must not be
+* misrepresented the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+*/
+
+
+
+
+
+var ClipVertex = Class.create();
+ClipVertex.prototype =
+{
+ v: new b2Vec2(),
+ id: new b2ContactID(),
+ initialize: function() {
+ // initialize instance variables for references
+ this.v = new b2Vec2();
+ this.id = new b2ContactID();
+ //
+}};
+
+
View
61 box2d-js/box2d-js/js/box2d/collision/Features.js
@@ -0,0 +1,61 @@
+/*
+* Copyright (c) 2006-2007 Erin Catto http:
+*
+* This software is provided 'as-is', without any express or implied
+* warranty. In no event will the authors be held liable for any damages
+* arising from the use of this software.
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+* 1. The origin of this software must not be misrepresented; you must not
+* claim that you wrote the original software. If you use this software
+* in a product, an acknowledgment in the product documentation would be
+* appreciated but is not required.
+* 2. Altered source versions must be plainly marked, and must not be
+* misrepresented the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+*/
+
+
+// We use contact ids to facilitate warm starting.
+var Features = Class.create();
+Features.prototype =
+{
+ //
+ set_referenceFace: function(value){
+ this._referenceFace = value;
+ this._m_id._key = (this._m_id._key & 0xffffff00) | (this._referenceFace & 0x000000ff)
+ },
+ get_referenceFace: function(){
+ return this._referenceFace;
+ },
+ _referenceFace: 0,
+ //
+ set_incidentEdge: function(value){
+ this._incidentEdge = value;
+ this._m_id._key = (this._m_id._key & 0xffff00ff) | ((this._incidentEdge << 8) & 0x0000ff00)
+ },
+ get_incidentEdge: function(){
+ return this._incidentEdge;
+ },
+ _incidentEdge: 0,
+ //
+ set_incidentVertex: function(value){
+ this._incidentVertex = value;
+ this._m_id._key = (this._m_id._key & 0xff00ffff) | ((this._incidentVertex << 16) & 0x00ff0000)
+ },
+ get_incidentVertex: function(){
+ return this._incidentVertex;
+ },
+ _incidentVertex: 0,
+ //
+ set_flip: function(value){
+ this._flip = value;
+ this._m_id._key = (this._m_id._key & 0x00ffffff) | ((this._flip << 24) & 0xff000000)
+ },
+ get_flip: function(){
+ return this._flip;
+ },
+ _flip: 0,
+ _m_id: null,
+ initialize: function() {}};
View
45 box2d-js/box2d-js/js/box2d/collision/b2AABB.js
@@ -0,0 +1,45 @@
+/*
+* Copyright (c) 2006-2007 Erin Catto http:
+*
+* This software is provided 'as-is', without any express or implied
+* warranty. In no event will the authors be held liable for any damages
+* arising from the use of this software.
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+* 1. The origin of this software must not be misrepresented; you must not
+* claim that you wrote the original software. If you use this software
+* in a product, an acknowledgment in the product documentation would be
+* appreciated but is not required.
+* 2. Altered source versions must be plainly marked, and must not be
+* misrepresented the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+*/
+
+
+
+// A manifold for two touching convex shapes.
+var b2AABB = Class.create();
+b2AABB.prototype =
+{
+ IsValid: function(){
+ //var d = b2Math.SubtractVV(this.maxVertex, this.minVertex);
+ var dX = this.maxVertex.x;
+ var dY = this.maxVertex.y;
+ dX = this.maxVertex.x;
+ dY = this.maxVertex.y;
+ dX -= this.minVertex.x;
+ dY -= this.minVertex.y;
+ var valid = dX >= 0.0 && dY >= 0.0;
+ valid = valid && this.minVertex.IsValid() && this.maxVertex.IsValid();
+ return valid;
+ },
+
+ minVertex: new b2Vec2(),
+ maxVertex: new b2Vec2(),
+ initialize: function() {
+ // initialize instance variables for references
+ this.minVertex = new b2Vec2();
+ this.maxVertex = new b2Vec2();
+ //
+}};
View
43 box2d-js/box2d-js/js/box2d/collision/b2Bound.js
@@ -0,0 +1,43 @@
+/*
+* Copyright (c) 2006-2007 Erin Catto http:
+*
+* This software is provided 'as-is', without any express or implied
+* warranty. In no event will the authors be held liable for any damages
+* arising from the use of this software.
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+* 1. The origin of this software must not be misrepresented; you must not
+* claim that you wrote the original software. If you use this software
+* in a product, an acknowledgment in the product documentation would be
+* appreciated but is not required.
+* 2. Altered source versions must be plainly marked, and must not be
+* misrepresented the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+*/
+
+
+
+var b2Bound = Class.create();
+b2Bound.prototype = {
+ IsLower: function(){ return (this.value & 1) == 0; },
+ IsUpper: function(){ return (this.value & 1) == 1; },
+ Swap: function(b){
+ var tempValue = this.value;
+ var tempProxyId = this.proxyId;
+ var tempStabbingCount = this.stabbingCount;
+
+ this.value = b.value;
+ this.proxyId = b.proxyId;
+ this.stabbingCount = b.stabbingCount;
+
+ b.value = tempValue;
+ b.proxyId = tempProxyId;
+ b.stabbingCount = tempStabbingCount;
+ },
+
+ value: 0,
+ proxyId: 0,
+ stabbingCount: 0,
+
+ initialize: function() {}}
View
31 box2d-js/box2d-js/js/box2d/collision/b2BoundValues.js
@@ -0,0 +1,31 @@
+/*
+* Copyright (c) 2006-2007 Erin Catto http:
+*
+* This software is provided 'as-is', without any express or implied
+* warranty. In no event will the authors be held liable for any damages
+* arising from the use of this software.
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+* 1. The origin of this software must not be misrepresented; you must not
+* claim that you wrote the original software. If you use this software
+* in a product, an acknowledgment in the product documentation would be
+* appreciated but is not required.
+* 2. Altered source versions must be plainly marked, and must not be
+* misrepresented the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+*/
+
+
+
+var b2BoundValues = Class.create();
+b2BoundValues.prototype = {
+ lowerValues: [0,0],
+ upperValues: [0,0],
+
+ initialize: function() {
+ // initialize instance variables for references
+ this.lowerValues = [0,0];
+ this.upperValues = [0,0];
+ //
+}}
View
898 box2d-js/box2d-js/js/box2d/collision/b2BroadPhase.js
@@ -0,0 +1,898 @@
+/*
+* Copyright (c) 2006-2007 Erin Catto http:
+*
+* This software is provided 'as-is', without any express or implied
+* warranty. In no event will the authors be held liable for any damages
+* arising from the use of this software.
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+* 1. The origin of this software must not be misrepresented; you must not
+* claim that you wrote the original software. If you use this software
+* in a product, an acknowledgment in the product documentation would be
+* appreciated but is not required.
+* 2. Altered source versions must be plainly marked, and must not be
+* misrepresented the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+*/
+
+
+
+
+
+/*
+This broad phase uses the Sweep and Prune algorithm in:
+Collision Detection in Interactive 3D Environments by Gino van den Bergen
+Also, some ideas, such integral values for fast compares comes from
+Bullet (http:/www.bulletphysics.com).
+*/
+
+
+// Notes:
+// - we use bound arrays instead of linked lists for cache coherence.
+// - we use quantized integral values for fast compares.
+// - we use short indices rather than pointers to save memory.
+// - we use a stabbing count for fast overlap queries (less than order N).
+// - we also use a time stamp on each proxy to speed up the registration of
+// overlap query results.
+// - where possible, we compare bound indices instead of values to reduce
+// cache misses (TODO_ERIN).
+// - no broadphase is perfect and neither is this one: it is not great for huge
+// worlds (use a multi-SAP instead), it is not great for large objects.
+
+var b2BroadPhase = Class.create();
+b2BroadPhase.prototype =
+{
+//public:
+ initialize: function(worldAABB, callback){
+ // initialize instance variables for references
+ this.m_pairManager = new b2PairManager();
+ this.m_proxyPool = new Array(b2Settings.b2_maxPairs);
+ this.m_bounds = new Array(2*b2Settings.b2_maxProxies);
+ this.m_queryResults = new Array(b2Settings.b2_maxProxies);
+ this.m_quantizationFactor = new b2Vec2();
+ //
+
+ //b2Settings.b2Assert(worldAABB.IsValid());
+ var i = 0;
+
+ this.m_pairManager.Initialize(this, callback);
+
+ this.m_worldAABB = worldAABB;
+
+ this.m_proxyCount = 0;
+
+ // query results
+ for (i = 0; i < b2Settings.b2_maxProxies; i++){
+ this.m_queryResults[i] = 0;
+ }
+
+ // bounds array
+ this.m_bounds = new Array(2);
+ for (i = 0; i < 2; i++){
+ this.m_bounds[i] = new Array(2*b2Settings.b2_maxProxies);
+ for (var j = 0; j < 2*b2Settings.b2_maxProxies; j++){
+ this.m_bounds[i][j] = new b2Bound();
+ }
+ }
+
+ //var d = b2Math.SubtractVV(worldAABB.maxVertex, worldAABB.minVertex);
+ var dX = worldAABB.maxVertex.x;
+ var dY = worldAABB.maxVertex.y;
+ dX -= worldAABB.minVertex.x;
+ dY -= worldAABB.minVertex.y;
+
+ this.m_quantizationFactor.x = b2Settings.USHRT_MAX / dX;
+ this.m_quantizationFactor.y = b2Settings.USHRT_MAX / dY;
+
+ var tProxy;
+ for (i = 0; i < b2Settings.b2_maxProxies - 1; ++i)
+ {
+ tProxy = new b2Proxy();
+ this.m_proxyPool[i] = tProxy;
+ tProxy.SetNext(i + 1);
+ tProxy.timeStamp = 0;
+ tProxy.overlapCount = b2BroadPhase.b2_invalid;
+ tProxy.userData = null;
+ }
+ tProxy = new b2Proxy();
+ this.m_proxyPool[b2Settings.b2_maxProxies-1] = tProxy;
+ tProxy.SetNext(b2Pair.b2_nullProxy);
+ tProxy.timeStamp = 0;
+ tProxy.overlapCount = b2BroadPhase.b2_invalid;
+ tProxy.userData = null;
+ this.m_freeProxy = 0;
+
+ this.m_timeStamp = 1;
+ this.m_queryResultCount = 0;
+ },
+ //~b2BroadPhase();
+
+ // Use this to see if your proxy is in range. If it is not in range,
+ // it should be destroyed. Otherwise you may get O(m^2) pairs, where m
+ // is the number of proxies that are out of range.
+ InRange: function(aabb){
+ //var d = b2Math.b2MaxV(b2Math.SubtractVV(aabb.minVertex, this.m_worldAABB.maxVertex), b2Math.SubtractVV(this.m_worldAABB.minVertex, aabb.maxVertex));
+ var dX;
+ var dY;
+ var d2X;
+ var d2Y;
+
+ dX = aabb.minVertex.x;
+ dY = aabb.minVertex.y;
+ dX -= this.m_worldAABB.maxVertex.x;
+ dY -= this.m_worldAABB.maxVertex.y;
+
+ d2X = this.m_worldAABB.minVertex.x;
+ d2Y = this.m_worldAABB.minVertex.y;
+ d2X -= aabb.maxVertex.x;
+ d2Y -= aabb.maxVertex.y;
+
+ dX = b2Math.b2Max(dX, d2X);
+ dY = b2Math.b2Max(dY, d2Y);
+
+ return b2Math.b2Max(dX, dY) < 0.0;
+ },
+
+ // Get a single proxy. Returns NULL if the id is invalid.
+ GetProxy: function(proxyId){
+ if (proxyId == b2Pair.b2_nullProxy || this.m_proxyPool[proxyId].IsValid() == false)
+ {
+ return null;
+ }
+
+ return this.m_proxyPool[ proxyId ];
+ },
+
+ // Create and destroy proxies. These call Flush first.
+ CreateProxy: function(aabb, userData){
+ var index = 0;
+ var proxy;
+
+ //b2Settings.b2Assert(this.m_proxyCount < b2_maxProxies);
+ //b2Settings.b2Assert(this.m_freeProxy != b2Pair.b2_nullProxy);
+
+ var proxyId = this.m_freeProxy;
+ proxy = this.m_proxyPool[ proxyId ];
+ this.m_freeProxy = proxy.GetNext();
+
+ proxy.overlapCount = 0;
+ proxy.userData = userData;
+
+ var boundCount = 2 * this.m_proxyCount;
+
+ var lowerValues = new Array();
+ var upperValues = new Array();
+ this.ComputeBounds(lowerValues, upperValues, aabb);
+
+ for (var axis = 0; axis < 2; ++axis)
+ {
+ var bounds = this.m_bounds[axis];
+ var lowerIndex = 0;
+ var upperIndex = 0;
+ var lowerIndexOut = [lowerIndex];
+ var upperIndexOut = [upperIndex];
+ this.Query(lowerIndexOut, upperIndexOut, lowerValues[axis], upperValues[axis], bounds, boundCount, axis);
+ lowerIndex = lowerIndexOut[0];
+ upperIndex = upperIndexOut[0];
+
+ // Replace memmove calls
+ //memmove(bounds + upperIndex + 2, bounds + upperIndex, (edgeCount - upperIndex) * sizeof(b2Bound));
+ var tArr = new Array();
+ var j = 0;
+ var tEnd = boundCount - upperIndex
+ var tBound1;
+ var tBound2;
+ // make temp array
+ for (j = 0; j < tEnd; j++){
+ tArr[j] = new b2Bound();
+ tBound1 = tArr[j];
+ tBound2 = bounds[upperIndex+j];
+ tBound1.value = tBound2.value;
+ tBound1.proxyId = tBound2.proxyId;
+ tBound1.stabbingCount = tBound2.stabbingCount;
+ }
+ // move temp array back in to bounds
+ tEnd = tArr.length;
+ var tIndex = upperIndex+2;
+ for (j = 0; j < tEnd; j++){
+ //bounds[tIndex+j] = tArr[j];
+ tBound2 = tArr[j];
+ tBound1 = bounds[tIndex+j]
+ tBound1.value = tBound2.value;
+ tBound1.proxyId = tBound2.proxyId;
+ tBound1.stabbingCount = tBound2.stabbingCount;
+ }
+ //memmove(bounds + lowerIndex + 1, bounds + lowerIndex, (upperIndex - lowerIndex) * sizeof(b2Bound));
+ // make temp array
+ tArr = new Array();
+ tEnd = upperIndex - lowerIndex;
+ for (j = 0; j < tEnd; j++){
+ tArr[j] = new b2Bound();
+ tBound1 = tArr[j];
+ tBound2 = bounds[lowerIndex+j];
+ tBound1.value = tBound2.value;
+ tBound1.proxyId = tBound2.proxyId;
+ tBound1.stabbingCount = tBound2.stabbingCount;
+ }
+ // move temp array back in to bounds
+ tEnd = tArr.length;
+ tIndex = lowerIndex+1;
+ for (j = 0; j < tEnd; j++){
+ //bounds[tIndex+j] = tArr[j];
+ tBound2 = tArr[j];
+ tBound1 = bounds[tIndex+j]
+ tBound1.value = tBound2.value;
+ tBound1.proxyId = tBound2.proxyId;
+ tBound1.stabbingCount = tBound2.stabbingCount;
+ }
+
+ // The upper index has increased because of the lower bound insertion.
+ ++upperIndex;
+
+ // Copy in the new bounds.
+ bounds[lowerIndex].value = lowerValues[axis];
+ bounds[lowerIndex].proxyId = proxyId;
+ bounds[upperIndex].value = upperValues[axis];
+ bounds[upperIndex].proxyId = proxyId;
+
+ bounds[lowerIndex].stabbingCount = lowerIndex == 0 ? 0 : bounds[lowerIndex-1].stabbingCount;
+ bounds[upperIndex].stabbingCount = bounds[upperIndex-1].stabbingCount;
+
+ // Adjust the stabbing count between the new bounds.
+ for (index = lowerIndex; index < upperIndex; ++index)
+ {
+ bounds[index].stabbingCount++;
+ }
+
+ // Adjust the all the affected bound indices.
+ for (index = lowerIndex; index < boundCount + 2; ++index)
+ {
+ var proxy2 = this.m_proxyPool[ bounds[index].proxyId ];
+ if (bounds[index].IsLower())
+ {
+ proxy2.lowerBounds[axis] = index;
+ }
+ else
+ {
+ proxy2.upperBounds[axis] = index;
+ }
+ }
+ }
+
+ ++this.m_proxyCount;
+
+ //b2Settings.b2Assert(this.m_queryResultCount < b2Settings.b2_maxProxies);
+
+ for (var i = 0; i < this.m_queryResultCount; ++i)
+ {
+ //b2Settings.b2Assert(this.m_queryResults[i] < b2_maxProxies);
+ //b2Settings.b2Assert(this.m_proxyPool[this.m_queryResults[i]].IsValid());
+
+ this.m_pairManager.AddBufferedPair(proxyId, this.m_queryResults[i]);
+ }
+
+ this.m_pairManager.Commit();
+
+ // Prepare for next query.
+ this.m_queryResultCount = 0;
+ this.IncrementTimeStamp();
+
+ return proxyId;
+ },
+
+ DestroyProxy: function(proxyId){
+
+ //b2Settings.b2Assert(0 < this.m_proxyCount && this.m_proxyCount <= b2_maxProxies);
+
+ var proxy = this.m_proxyPool[ proxyId ];
+ //b2Settings.b2Assert(proxy.IsValid());
+
+ var boundCount = 2 * this.m_proxyCount;
+
+ for (var axis = 0; axis < 2; ++axis)
+ {
+ var bounds = this.m_bounds[axis];
+
+ var lowerIndex = proxy.lowerBounds[axis];
+ var upperIndex = proxy.upperBounds[axis];
+ var lowerValue = bounds[lowerIndex].value;
+ var upperValue = bounds[upperIndex].value;
+
+ // replace memmove calls
+ //memmove(bounds + lowerIndex, bounds + lowerIndex + 1, (upperIndex - lowerIndex - 1) * sizeof(b2Bound));
+ var tArr = new Array();
+ var j = 0;
+ var tEnd = upperIndex - lowerIndex - 1;
+ var tBound1;
+ var tBound2;
+ // make temp array
+ for (j = 0; j < tEnd; j++){
+ tArr[j] = new b2Bound();
+ tBound1 = tArr[j];
+ tBound2 = bounds[lowerIndex+1+j];
+ tBound1.value = tBound2.value;
+ tBound1.proxyId = tBound2.proxyId;
+ tBound1.stabbingCount = tBound2.stabbingCount;
+ }
+ // move temp array back in to bounds
+ tEnd = tArr.length;
+ var tIndex = lowerIndex;
+ for (j = 0; j < tEnd; j++){
+ //bounds[tIndex+j] = tArr[j];
+ tBound2 = tArr[j];
+ tBound1 = bounds[tIndex+j]
+ tBound1.value = tBound2.value;
+ tBound1.proxyId = tBound2.proxyId;
+ tBound1.stabbingCount = tBound2.stabbingCount;
+ }
+ //memmove(bounds + upperIndex-1, bounds + upperIndex + 1, (edgeCount - upperIndex - 1) * sizeof(b2Bound));
+ // make temp array
+ tArr = new Array();
+ tEnd = boundCount - upperIndex - 1;
+ for (j = 0; j < tEnd; j++){
+ tArr[j] = new b2Bound();
+ tBound1 = tArr[j];
+ tBound2 = bounds[upperIndex+1+j];
+ tBound1.value = tBound2.value;
+ tBound1.proxyId = tBound2.proxyId;
+ tBound1.stabbingCount = tBound2.stabbingCount;
+ }
+ // move temp array back in to bounds
+ tEnd = tArr.length;
+ tIndex = upperIndex-1;
+ for (j = 0; j < tEnd; j++){
+ //bounds[tIndex+j] = tArr[j];
+ tBound2 = tArr[j];
+ tBound1 = bounds[tIndex+j]
+ tBound1.value = tBound2.value;
+ tBound1.proxyId = tBound2.proxyId;
+ tBound1.stabbingCount = tBound2.stabbingCount;
+ }
+
+ // Fix bound indices.
+ tEnd = boundCount - 2;
+ for (var index = lowerIndex; index < tEnd; ++index)
+ {
+ var proxy2 = this.m_proxyPool[ bounds[index].proxyId ];
+ if (bounds[index].IsLower())
+ {
+ proxy2.lowerBounds[axis] = index;
+ }
+ else
+ {
+ proxy2.upperBounds[axis] = index;
+ }
+ }
+
+ // Fix stabbing count.
+ tEnd = upperIndex - 1;
+ for (var index2 = lowerIndex; index2 < tEnd; ++index2)
+ {
+ bounds[index2].stabbingCount--;
+ }
+
+ // this.Query for pairs to be removed. lowerIndex and upperIndex are not needed.
+ // make lowerIndex and upper output using an array and do this for others if compiler doesn't pick them up
+ this.Query([0], [0], lowerValue, upperValue, bounds, boundCount - 2, axis);
+ }
+
+ //b2Settings.b2Assert(this.m_queryResultCount < b2Settings.b2_maxProxies);
+
+ for (var i = 0; i < this.m_queryResultCount; ++i)
+ {
+ //b2Settings.b2Assert(this.m_proxyPool[this.m_queryResults[i]].IsValid());
+
+ this.m_pairManager.RemoveBufferedPair(proxyId, this.m_queryResults[i]);
+ }
+
+ this.m_pairManager.Commit();
+
+ // Prepare for next query.
+ this.m_queryResultCount = 0;
+ this.IncrementTimeStamp();
+
+ // Return the proxy to the pool.
+ proxy.userData = null;
+ proxy.overlapCount = b2BroadPhase.b2_invalid;
+ proxy.lowerBounds[0] = b2BroadPhase.b2_invalid;
+ proxy.lowerBounds[1] = b2BroadPhase.b2_invalid;
+ proxy.upperBounds[0] = b2BroadPhase.b2_invalid;
+ proxy.upperBounds[1] = b2BroadPhase.b2_invalid;
+
+ proxy.SetNext(this.m_freeProxy);
+ this.m_freeProxy = proxyId;
+ --this.m_proxyCount;
+ },
+
+
+ // Call this.MoveProxy times like, then when you are done
+ // call this.Commit to finalized the proxy pairs (for your time step).
+ MoveProxy: function(proxyId, aabb){
+ var axis = 0;
+ var index = 0;
+ var bound;
+ var prevBound
+ var nextBound
+ var nextProxyId = 0;
+ var nextProxy;
+
+ if (proxyId == b2Pair.b2_nullProxy || b2Settings.b2_maxProxies <= proxyId)
+ {
+ //b2Settings.b2Assert(false);
+ return;
+ }
+
+ if (aabb.IsValid() == false)
+ {
+ //b2Settings.b2Assert(false);
+ return;
+ }
+
+ var boundCount = 2 * this.m_proxyCount;
+
+ var proxy = this.m_proxyPool[ proxyId ];
+ // Get new bound values
+ var newValues = new b2BoundValues();
+ this.ComputeBounds(newValues.lowerValues, newValues.upperValues, aabb);
+
+ // Get old bound values
+ var oldValues = new b2BoundValues();
+ for (axis = 0; axis < 2; ++axis)
+ {
+ oldValues.lowerValues[axis] = this.m_bounds[axis][proxy.lowerBounds[axis]].value;
+ oldValues.upperValues[axis] = this.m_bounds[axis][proxy.upperBounds[axis]].value;
+ }
+
+ for (axis = 0; axis < 2; ++axis)
+ {
+ var bounds = this.m_bounds[axis];
+
+ var lowerIndex = proxy.lowerBounds[axis];
+ var upperIndex = proxy.upperBounds[axis];
+
+ var lowerValue = newValues.lowerValues[axis];
+ var upperValue = newValues.upperValues[axis];
+
+ var deltaLower = lowerValue - bounds[lowerIndex].value;
+ var deltaUpper = upperValue - bounds[upperIndex].value;
+
+ bounds[lowerIndex].value = lowerValue;
+ bounds[upperIndex].value = upperValue;
+
+ //
+ // Expanding adds overlaps
+ //
+
+ // Should we move the lower bound down?
+ if (deltaLower < 0)
+ {
+ index = lowerIndex;
+ while (index > 0 && lowerValue < bounds[index-1].value)
+ {
+ bound = bounds[index];
+ prevBound = bounds[index - 1];
+
+ var prevProxyId = prevBound.proxyId;
+ var prevProxy = this.m_proxyPool[ prevBound.proxyId ];
+
+ prevBound.stabbingCount++;
+
+ if (prevBound.IsUpper() == true)
+ {
+ if (this.TestOverlap(newValues, prevProxy))
+ {
+ this.m_pairManager.AddBufferedPair(proxyId, prevProxyId);
+ }
+
+ prevProxy.upperBounds[axis]++;
+ bound.stabbingCount++;
+ }
+ else
+ {
+ prevProxy.lowerBounds[axis]++;
+ bound.stabbingCount--;
+ }
+
+ proxy.lowerBounds[axis]--;
+
+ // swap
+ //var temp = bound;
+ //bound = prevEdge;
+ //prevEdge = temp;
+ bound.Swap(prevBound);
+ //b2Math.b2Swap(bound, prevEdge);
+ --index;
+ }
+ }
+
+ // Should we move the upper bound up?
+ if (deltaUpper > 0)
+ {
+ index = upperIndex;
+ while (index < boundCount-1 && bounds[index+1].value <= upperValue)
+ {
+ bound = bounds[ index ];
+ nextBound = bounds[ index + 1 ];
+ nextProxyId = nextBound.proxyId;
+ nextProxy = this.m_proxyPool[ nextProxyId ];
+
+ nextBound.stabbingCount++;
+
+ if (nextBound.IsLower() == true)
+ {
+ if (this.TestOverlap(newValues, nextProxy))
+ {
+ this.m_pairManager.AddBufferedPair(proxyId, nextProxyId);
+ }
+
+ nextProxy.lowerBounds[axis]--;
+ bound.stabbingCount++;
+ }
+ else
+ {
+ nextProxy.upperBounds[axis]--;
+ bound.stabbingCount--;
+ }
+
+ proxy.upperBounds[axis]++;
+ // swap
+ //var temp = bound;
+ //bound = nextEdge;
+ //nextEdge = temp;
+ bound.Swap(nextBound);
+ //b2Math.b2Swap(bound, nextEdge);
+ index++;
+ }
+ }
+
+ //
+ // Shrinking removes overlaps
+ //
+
+ // Should we move the lower bound up?
+ if (deltaLower > 0)
+ {
+ index = lowerIndex;
+ while (index < boundCount-1 && bounds[index+1].value <= lowerValue)
+ {
+ bound = bounds[ index ];
+ nextBound = bounds[ index + 1 ];
+
+ nextProxyId = nextBound.proxyId;
+ nextProxy = this.m_proxyPool[ nextProxyId ];
+
+ nextBound.stabbingCount--;
+
+ if (nextBound.IsUpper())
+ {
+ if (this.TestOverlap(oldValues, nextProxy))
+ {
+ this.m_pairManager.RemoveBufferedPair(proxyId, nextProxyId);
+ }
+
+ nextProxy.upperBounds[axis]--;
+ bound.stabbingCount--;
+ }
+ else
+ {
+ nextProxy.lowerBounds[axis]--;
+ bound.stabbingCount++;
+ }
+
+ proxy.lowerBounds[axis]++;
+ // swap
+ //var temp = bound;
+ //bound = nextEdge;
+ //nextEdge = temp;
+ bound.Swap(nextBound);
+ //b2Math.b2Swap(bound, nextEdge);
+ index++;
+ }
+ }
+
+ // Should we move the upper bound down?
+ if (deltaUpper < 0)
+ {
+ index = upperIndex;
+ while (index > 0 && upperValue < bounds[index-1].value)
+ {
+ bound = bounds[index];
+ prevBound = bounds[index - 1];
+
+ prevProxyId = prevBound.proxyId;
+ prevProxy = this.m_proxyPool[ prevProxyId ];
+
+ prevBound.stabbingCount--;
+
+ if (prevBound.IsLower() == true)
+ {
+ if (this.TestOverlap(oldValues, prevProxy))
+ {
+ this.m_pairManager.RemoveBufferedPair(proxyId, prevProxyId);
+ }
+
+ prevProxy.lowerBounds[axis]++;
+ bound.stabbingCount--;
+ }
+ else
+ {
+ prevProxy.upperBounds[axis]++;
+ bound.stabbingCount++;
+ }
+
+ proxy.upperBounds[axis]--;
+ // swap
+ //var temp = bound;
+ //bound = prevEdge;
+ //prevEdge = temp;
+ bound.Swap(prevBound);
+ //b2Math.b2Swap(bound, prevEdge);
+ index--;
+ }
+ }
+ }
+ },
+
+ Commit: function(){
+ this.m_pairManager.Commit();
+ },
+
+ // this.Query an AABB for overlapping proxies, returns the user data and
+ // the count, up to the supplied maximum count.
+ QueryAABB: function(aabb, userData, maxCount){
+ var lowerValues = new Array();
+ var upperValues = new Array();
+ this.ComputeBounds(lowerValues, upperValues, aabb);
+
+ var lowerIndex = 0;
+ var upperIndex = 0;
+ var lowerIndexOut = [lowerIndex];
+ var upperIndexOut = [upperIndex];
+ this.Query(lowerIndexOut, upperIndexOut, lowerValues[0], upperValues[0], this.m_bounds[0], 2*this.m_proxyCount, 0);
+ this.Query(lowerIndexOut, upperIndexOut, lowerValues[1], upperValues[1], this.m_bounds[1], 2*this.m_proxyCount, 1);
+
+ //b2Settings.b2Assert(this.m_queryResultCount < b2Settings.b2_maxProxies);
+
+ var count = 0;
+ for (var i = 0; i < this.m_queryResultCount && count < maxCount; ++i, ++count)
+ {
+ //b2Settings.b2Assert(this.m_queryResults[i] < b2Settings.b2_maxProxies);
+ var proxy = this.m_proxyPool[ this.m_queryResults[i] ];
+ //b2Settings.b2Assert(proxy.IsValid());
+ userData[i] = proxy.userData;
+ }
+
+ // Prepare for next query.
+ this.m_queryResultCount = 0;
+ this.IncrementTimeStamp();
+
+ return count;
+ },
+
+ Validate: function(){
+ var pair;
+ var proxy1;
+ var proxy2;
+ var overlap;
+
+ for (var axis = 0; axis < 2; ++axis)
+ {
+ var bounds = this.m_bounds[axis];
+
+ var boundCount = 2 * this.m_proxyCount;
+ var stabbingCount = 0;
+
+ for (var i = 0; i < boundCount; ++i)
+ {
+ var bound = bounds[i];
+ //b2Settings.b2Assert(i == 0 || bounds[i-1].value <= bound->value);
+ //b2Settings.b2Assert(bound->proxyId != b2_nullProxy);
+ //b2Settings.b2Assert(this.m_proxyPool[bound->proxyId].IsValid());
+
+ if (bound.IsLower() == true)
+ {
+ //b2Settings.b2Assert(this.m_proxyPool[bound.proxyId].lowerBounds[axis] == i);
+ stabbingCount++;
+ }
+ else
+ {
+ //b2Settings.b2Assert(this.m_proxyPool[bound.proxyId].upperBounds[axis] == i);
+ stabbingCount--;
+ }
+
+ //b2Settings.b2Assert(bound.stabbingCount == stabbingCount);
+ }
+ }
+
+ },
+
+//private:
+ ComputeBounds: function(lowerValues, upperValues, aabb)
+ {
+ //b2Settings.b2Assert(aabb.maxVertex.x > aabb.minVertex.x);
+ //b2Settings.b2Assert(aabb.maxVertex.y > aabb.minVertex.y);
+
+ //var minVertex = b2Math.b2ClampV(aabb.minVertex, this.m_worldAABB.minVertex, this.m_worldAABB.maxVertex);
+ var minVertexX = aabb.minVertex.x;
+ var minVertexY = aabb.minVertex.y;
+ minVertexX = b2Math.b2Min(minVertexX, this.m_worldAABB.maxVertex.x);
+ minVertexY = b2Math.b2Min(minVertexY, this.m_worldAABB.maxVertex.y);
+ minVertexX = b2Math.b2Max(minVertexX, this.m_worldAABB.minVertex.x);
+ minVertexY = b2Math.b2Max(minVertexY, this.m_worldAABB.minVertex.y);
+
+ //var maxVertex = b2Math.b2ClampV(aabb.maxVertex, this.m_worldAABB.minVertex, this.m_worldAABB.maxVertex);
+ var maxVertexX = aabb.maxVertex.x;
+ var maxVertexY = aabb.maxVertex.y;
+ maxVertexX = b2Math.b2Min(maxVertexX, this.m_worldAABB.maxVertex.x);
+ maxVertexY = b2Math.b2Min(maxVertexY, this.m_worldAABB.maxVertex.y);
+ maxVertexX = b2Math.b2Max(maxVertexX, this.m_worldAABB.minVertex.x);
+ maxVertexY = b2Math.b2Max(maxVertexY, this.m_worldAABB.minVertex.y);
+
+ // Bump lower bounds downs and upper bounds up. This ensures correct sorting of
+ // lower/upper bounds that would have equal values.
+ // TODO_ERIN implement fast float to uint16 conversion.
+ lowerValues[0] = /*uint*/(this.m_quantizationFactor.x * (minVertexX - this.m_worldAABB.minVertex.x)) & (b2Settings.USHRT_MAX - 1);
+ upperValues[0] = (/*uint*/(this.m_quantizationFactor.x * (maxVertexX - this.m_worldAABB.minVertex.x))& 0x0000ffff) | 1;
+
+ lowerValues[1] = /*uint*/(this.m_quantizationFactor.y * (minVertexY - this.m_worldAABB.minVertex.y)) & (b2Settings.USHRT_MAX - 1);
+ upperValues[1] = (/*uint*/(this.m_quantizationFactor.y * (maxVertexY - this.m_worldAABB.minVertex.y))& 0x0000ffff) | 1;
+ },
+
+ // This one is only used for validation.
+ TestOverlapValidate: function(p1, p2){
+
+ for (var axis = 0; axis < 2; ++axis)
+ {
+ var bounds = this.m_bounds[axis];
+
+ //b2Settings.b2Assert(p1.lowerBounds[axis] < 2 * this.m_proxyCount);
+ //b2Settings.b2Assert(p1.upperBounds[axis] < 2 * this.m_proxyCount);
+ //b2Settings.b2Assert(p2.lowerBounds[axis] < 2 * this.m_proxyCount);
+ //b2Settings.b2Assert(p2.upperBounds[axis] < 2 * this.m_proxyCount);
+
+ if (bounds[p1.lowerBounds[axis]].value > bounds[p2.upperBounds[axis]].value)
+ return false;
+
+ if (bounds[p1.upperBounds[axis]].value < bounds[p2.lowerBounds[axis]].value)
+ return false;
+ }
+
+ return true;
+ },
+
+ TestOverlap: function(b, p)
+ {
+ for (var axis = 0; axis < 2; ++axis)
+ {
+ var bounds = this.m_bounds[axis];
+
+ //b2Settings.b2Assert(p.lowerBounds[axis] < 2 * this.m_proxyCount);
+ //b2Settings.b2Assert(p.upperBounds[axis] < 2 * this.m_proxyCount);
+
+ if (b.lowerValues[axis] > bounds[p.upperBounds[axis]].value)
+ return false;
+
+ if (b.upperValues[axis] < bounds[p.lowerBounds[axis]].value)
+ return false;
+ }
+
+ return true;
+ },
+
+ Query: function(lowerQueryOut, upperQueryOut, lowerValue, upperValue, bounds, boundCount, axis){
+
+ var lowerQuery = b2BroadPhase.BinarySearch(bounds, boundCount, lowerValue);
+ var upperQuery = b2BroadPhase.BinarySearch(bounds, boundCount, upperValue);
+
+ // Easy case: lowerQuery <= lowerIndex(i) < upperQuery
+ // Solution: search query range for min bounds.
+ for (var j = lowerQuery; j < upperQuery; ++j)
+ {
+ if (bounds[j].IsLower())
+ {
+ this.IncrementOverlapCount(bounds[j].proxyId);
+ }
+ }
+
+ // Hard case: lowerIndex(i) < lowerQuery < upperIndex(i)
+ // Solution: use the stabbing count to search down the bound array.
+ if (lowerQuery > 0)
+ {
+ var i = lowerQuery - 1;
+ var s = bounds[i].stabbingCount;
+
+ // Find the s overlaps.
+ while (s)
+ {
+ //b2Settings.b2Assert(i >= 0);
+
+ if (bounds[i].IsLower())
+ {
+ var proxy = this.m_proxyPool[ bounds[i].proxyId ];
+ if (lowerQuery <= proxy.upperBounds[axis])
+ {
+ this.IncrementOverlapCount(bounds[i].proxyId);
+ --s;
+ }
+ }
+ --i;
+ }
+ }
+
+ lowerQueryOut[0] = lowerQuery;
+ upperQueryOut[0] = upperQuery;
+ },
+
+
+ IncrementOverlapCount: function(proxyId){
+ var proxy = this.m_proxyPool[ proxyId ];
+ if (proxy.timeStamp < this.m_timeStamp)
+ {
+ proxy.timeStamp = this.m_timeStamp;
+ proxy.overlapCount = 1;
+ }
+ else
+ {
+ proxy.overlapCount = 2;
+ //b2Settings.b2Assert(this.m_queryResultCount < b2Settings.b2_maxProxies);
+ this.m_queryResults[this.m_queryResultCount] = proxyId;
+ ++this.m_queryResultCount;
+ }
+ },
+ IncrementTimeStamp: function(){
+ if (this.m_timeStamp == b2Settings.USHRT_MAX)
+ {
+ for (var i = 0; i < b2Settings.b2_maxProxies; ++i)
+ {
+ this.m_proxyPool[i].timeStamp = 0;
+ }
+ this.m_timeStamp = 1;
+ }
+ else
+ {
+ ++this.m_timeStamp;
+ }
+ },
+
+//public:
+ m_pairManager: new b2PairManager(),
+
+ m_proxyPool: new Array(b2Settings.b2_maxPairs),
+ m_freeProxy: 0,
+
+ m_bounds: new Array(2*b2Settings.b2_maxProxies),
+
+ m_queryResults: new Array(b2Settings.b2_maxProxies),
+ m_queryResultCount: 0,
+
+ m_worldAABB: null,
+ m_quantizationFactor: new b2Vec2(),
+ m_proxyCount: 0,
+ m_timeStamp: 0};
+b2BroadPhase.s_validate = false;
+b2BroadPhase.b2_invalid = b2Settings.USHRT_MAX;
+b2BroadPhase.b2_nullEdge = b2Settings.USHRT_MAX;
+b2BroadPhase.BinarySearch = function(bounds, count, value)
+ {
+ var low = 0;
+ var high = count - 1;
+ while (low <= high)
+ {
+ var mid = Math.floor((low + high) / 2);
+ if (bounds[mid].value > value)
+ {
+ high = mid - 1;
+ }
+ else if (bounds[mid].value < value)
+ {
+ low = mid + 1;
+ }
+ else
+ {
+ return /*uint*/(mid);
+ }
+ }
+
+ return /*uint*/(low);
+ };
View
26 box2d-js/box2d-js/js/box2d/collision/b2BufferedPair.js
@@ -0,0 +1,26 @@
+/*
+* Copyright (c) 2006-2007 Erin Catto http:
+*
+* This software is provided 'as-is', without any express or implied
+* warranty. In no event will the authors be held liable for any damages
+* arising from the use of this software.
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+* 1. The origin of this software must not be misrepresented; you must not
+* claim that you wrote the original software. If you use this software
+* in a product, an acknowledgment in the product documentation would be
+* appreciated but is not required.
+* 2. Altered source versions must be plainly marked, and must not be
+* misrepresented the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+*/
+
+
+
+var b2BufferedPair = Class.create();
+b2BufferedPair.prototype = {
+ proxyId1: 0,
+ proxyId2: 0,
+
+ initialize: function() {}}
View
738 box2d-js/box2d-js/js/box2d/collision/b2Collision.js
@@ -0,0 +1,738 @@
+/*
+* Copyright (c) 2006-2007 Erin Catto http: