Advertisement
Guest User

Untitled

a guest
Nov 11th, 2012
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Adjusted by http://www.box2d.org/manual.html#_Toc258082976
  2. // http://www.box2dflash.org/docs/2.1a/reference/
  3. // http://blog.sethladd.com/2011/09/box2d-javascript-example-walkthrough.html
  4. // http://code.google.com/p/box2dweb/source/browse/trunk/example.html
  5.  
  6. // Box2d vars
  7.     var b2AABB = Box2D.Collision.b2AABB;  
  8.     var b2Vec2 = Box2D.Common.Math.b2Vec2;
  9.     var b2World = Box2D.Dynamics.b2World;
  10.     //var b2BoxDef = Box2D.Collision.Shapes.b2EdgeChainDef;
  11.     var b2BodyDef = Box2D.Dynamics.b2BodyDef;
  12.     var b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;
  13.     var b2FixtureDef = Box2D.Dynamics.b2FixtureDef;
  14.     var b2BodyDef = Box2D.Dynamics.b2BodyDef;
  15.     var b2Body = Box2D.Dynamics.b2Body;
  16.     var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape;
  17.     var b2Shape = Box2D.Collision.Shapes.b2Shape;
  18.     var b2Math = Box2D.Common.Math.b2Math22;
  19.     var b2DebugDraw = Box2D.Dynamics.b2DebugDraw;
  20.  
  21. function drawWorld(world, context) {  
  22.     for (var j = world.m_jointList; j; j = j.m_next) {  
  23.         drawJoint(j, context);  
  24.     }  
  25.     for (var b = world.m_bodyList; b; b = b.m_next) {  
  26.  
  27.         //console.log(b);
  28.  
  29.         //for (var s = b.GetShapeList(); s != null; s = s.GetNext()) {  
  30.         for (var s = b.GetFixtureList(); s != null; s = s.GetNext()) {  
  31.             drawShape(s, context);  
  32.         }  
  33.     }  
  34. }  
  35. function drawJoint(joint, context) {  
  36.     var b1 = joint.m_body1;  
  37.     var b2 = joint.m_body2;  
  38.     var x1 = b1.m_position;  
  39.     var x2 = b2.m_position;  
  40.     var p1 = joint.GetAnchor1();  
  41.     var p2 = joint.GetAnchor2();  
  42.     context.strokeStyle = '#00eeee';  
  43.     context.beginPath();  
  44.     switch (joint.m_type) {  
  45.     case b2Joint.e_distanceJoint:  
  46.         context.moveTo(p1.x, p1.y);  
  47.         context.lineTo(p2.x, p2.y);  
  48.         break;  
  49.     case b2Joint.e_pulleyJoint:  
  50.         // TODO  
  51.         break;  
  52.     default:  
  53.         if (b1 == world.m_groundBody) {  
  54.             context.moveTo(p1.x, p1.y);  
  55.             context.lineTo(x2.x, x2.y);  
  56.         }  
  57.         else if (b2 == world.m_groundBody) {  
  58.             context.moveTo(p1.x, p1.y);  
  59.             context.lineTo(x1.x, x1.y);  
  60.         }  
  61.         else {  
  62.             context.moveTo(x1.x, x1.y);  
  63.             context.lineTo(p1.x, p1.y);  
  64.             context.lineTo(x2.x, x2.y);  
  65.             context.lineTo(p2.x, p2.y);  
  66.         }  
  67.         break;  
  68.     }  
  69.     context.stroke();  
  70. }  
  71. function drawShape(shape, context) {  
  72.  
  73.     context.strokeStyle = '#000000';  
  74.     context.beginPath();  
  75.  
  76.     console.log('type:');
  77.     console.log(shape.GetType()); //0 or 1
  78.  
  79.     switch (shape.m_type) {  
  80.     //switch (shape.GetType()) {  
  81.     case b2Shape.e_circleShape:  
  82.         {  
  83.             return;
  84.             var circle = shape;  
  85.             var pos = circle.m_position;  
  86.             var r = circle.m_radius;  
  87.             var segments = 16.0;  
  88.             var theta = 0.0;  
  89.             var dtheta = 2.0 * Math.PI / segments;  
  90.             // draw circle  
  91.             context.moveTo(pos.x + r, pos.y);  
  92.             for (var i = 0; i < segments; i++) {  
  93.                 var d = new b2Vec2(r * Math.cos(theta), r * Math.sin(theta));  
  94.                 var v = b2Math.AddVV(pos, d);  
  95.                 context.lineTo(v.x, v.y);  
  96.                 theta += dtheta;  
  97.             }  
  98.             context.lineTo(pos.x + r, pos.y);  
  99.             // draw radius  
  100.             context.moveTo(pos.x, pos.y);  
  101.             var ax = circle.m_R.col1;  
  102.             var pos2 = new b2Vec2(pos.x + r * ax.x, pos.y + r * ax.y);  
  103.             context.lineTo(pos2.x, pos2.y);  
  104.         }  
  105.         break;  
  106.     case b2Shape.e_polyShape:  
  107.         {  
  108.            
  109.             var poly = shape;  
  110.  
  111.             //console.log(poly);
  112.  
  113.             //var tV = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[0]));  
  114.             var tV = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.GetVertices()[0]));  
  115.             /*
  116.             context.moveTo(tV.x, tV.y);  
  117.             for (var i = 0; i < poly.m_vertexCount; i++) {  
  118.                 var v = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[i]));  
  119.                 context.lineTo(v.x, v.y);  
  120.             }  
  121.             context.lineTo(tV.x, tV.y);  
  122.             */
  123.         }  
  124.         break;  
  125.     }  
  126.     context.stroke();  
  127. }  
  128. function createWorld() {  
  129.     var worldAABB = new b2AABB();  
  130.    
  131.     //worldAABB.minVertex.Set(-1000, -1000);  
  132.     worldAABB.lowerBound.Set(-1000, -1000);  
  133.     //worldAABB.maxVertex.Set(1000, 1000);  
  134.     worldAABB.upperBound.Set(1000, 1000);  
  135.     var gravity = new b2Vec2(0, 300);  
  136.     var doSleep = true;  
  137.     var world = new b2World(worldAABB, gravity, doSleep);  
  138.     return world;  
  139. }  
  140. function createGround(world) {  
  141.     var groundSd = new b2BoxDef();  
  142.     groundSd.extents.Set(1000, 50);  
  143.     groundSd.restitution = 0.2;  
  144.     var groundBd = new b2BodyDef();  
  145.     groundBd.AddShape(groundSd);  
  146.     groundBd.position.Set(-500, 340);  
  147.     return world.CreateBody(groundBd)  
  148. }  
  149. function createBall(world, x, y) {  //not used
  150.     var ballSd = new b2CircleDef();  
  151.     ballSd.density = 1.0;  
  152.     ballSd.radius = 20;  
  153.     ballSd.restitution = 1.0;  
  154.     ballSd.friction = 0;  
  155.     var ballBd = new b2BodyDef();  
  156.     ballBd.AddShape(ballSd);  
  157.     ballBd.position.Set(x,y);  
  158.     return world.CreateBody(ballBd);  
  159. }  
  160. function createBox(world, x, y, width, height, fixed, userData) {  
  161.     if (typeof(fixed) == 'undefined') fixed = true;  
  162.     //var boxSd = new b2BoxDef();  
  163.  
  164.     //console.log(boxSd);
  165. /*
  166.     if (!fixed) boxSd.density = 1.0;  
  167.     boxSd.userData = userData;  
  168.     boxSd.extents.Set(width, height);  
  169.     */
  170.  
  171.     /* is kit psl **/
  172.  
  173.     var bodyDef = new b2BodyDef;
  174.  
  175.     bodyDef.type = b2Body.b2_dynamicBody;
  176.  
  177.     var fixDef = new b2FixtureDef;
  178.     fixDef.shape = new b2PolygonShape;
  179.     fixDef.shape.SetAsBox(
  180.           width //half width
  181.        ,  height //half height
  182.     );
  183.    
  184.     bodyDef.position.x = x;
  185.     bodyDef.position.y = y;
  186.     return world.CreateBody(bodyDef).CreateFixture(fixDef);
  187.     /* end mano */
  188. /*
  189.     var boxBd = new b2BodyDef();  
  190.     boxBd.AddShape(boxSd);  
  191.     boxBd.position.Set(x,y);  
  192.     return world.CreateBody(boxBd)  
  193.     */
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement