Advertisement
Guest User

Untitled

a guest
Nov 11th, 2012
167
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. // http://code.google.com/p/jquery-box2dweb/source/browse/trunk/assets/?r=29
  6. //http://stackoverflow.com/questions/7628078/which-box2d-javascript-library-should-i-use
  7.  
  8. // Box2d vars
  9.  
  10.  
  11.     var b2AABB = Box2D.Collision.b2AABB;  
  12.     var b2Vec2 = Box2D.Common.Math.b2Vec2;
  13.     var b2World = Box2D.Dynamics.b2World;
  14.     //var b2BoxDef = Box2D.Collision.Shapes.b2EdgeChainDef;
  15.     var b2BodyDef = Box2D.Dynamics.b2BodyDef;
  16.     var b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;
  17.     var b2FixtureDef = Box2D.Dynamics.b2FixtureDef;
  18.     var b2BodyDef = Box2D.Dynamics.b2BodyDef;
  19.     var b2Body = Box2D.Dynamics.b2Body;
  20.     var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape;
  21.     var b2Shape = Box2D.Collision.Shapes.b2Shape;
  22.     var b2Math = Box2D.Common.Math.b2Math;
  23.     var b2DebugDraw = Box2D.Dynamics.b2DebugDraw;
  24.    
  25.  
  26. function drawWorld(world, context) {  
  27.     for (var j = world.m_jointList; j; j = j.m_next) {  
  28.         drawJoint(j, context);  
  29.     }  
  30.     for (var b = world.m_bodyList; b; b = b.m_next) {  
  31.  
  32.        // console.log(b);
  33.  
  34.         //for (var s = b.GetShapeList(); s != null; s = s.GetNext()) {  
  35.         for (var s = b.GetFixtureList(); s != null; s = s.GetNext()) {  
  36.  
  37.             console.log('shape:');
  38.             var shape = s.GetShape();
  39.             console.log(shape);
  40.  
  41.             var shapeType = shape.GetType();
  42.             //console.log(shapeType);
  43.            
  44.             //console.log(b2Shape.e_polygonShape);
  45.  
  46.             //drawShape(s, context);  
  47.             drawShape(shape, context);  
  48.  
  49.         }  
  50.  
  51.     }  
  52. }  
  53. function drawJoint(joint, context) {  
  54.     var b1 = joint.m_body1;  
  55.     var b2 = joint.m_body2;  
  56.     var x1 = b1.m_position;  
  57.     var x2 = b2.m_position;  
  58.     var p1 = joint.GetAnchor1();  
  59.     var p2 = joint.GetAnchor2();  
  60.     context.strokeStyle = '#00eeee';  
  61.     context.beginPath();  
  62.     switch (joint.m_type) {  
  63.     case b2Joint.e_distanceJoint:  
  64.         context.moveTo(p1.x, p1.y);  
  65.         context.lineTo(p2.x, p2.y);  
  66.         break;  
  67.     case b2Joint.e_pulleyJoint:  
  68.         // TODO  
  69.         break;  
  70.     default:  
  71.         if (b1 == world.m_groundBody) {  
  72.             context.moveTo(p1.x, p1.y);  
  73.             context.lineTo(x2.x, x2.y);  
  74.         }  
  75.         else if (b2 == world.m_groundBody) {  
  76.             context.moveTo(p1.x, p1.y);  
  77.             context.lineTo(x1.x, x1.y);  
  78.         }  
  79.         else {  
  80.             context.moveTo(x1.x, x1.y);  
  81.             context.lineTo(p1.x, p1.y);  
  82.             context.lineTo(x2.x, x2.y);  
  83.             context.lineTo(p2.x, p2.y);  
  84.         }  
  85.         break;  
  86.     }  
  87.     context.stroke();  
  88. }  
  89. function drawShape(shape, context) {  
  90.  
  91.    // console.log(shape);
  92.  
  93.     context.strokeStyle = '#000000';  
  94.     context.beginPath();  
  95.  
  96.     //console.log('type:');
  97.    // console.log(shape.GetType()); //0 or 1
  98.  
  99.     //switch (shape.m_type) {  
  100.     switch (shape.GetType()) {  
  101.     case b2Shape.e_circleShape:  
  102.         {  
  103.             return;
  104.             alert('circle');
  105.             var circle = shape;  
  106.             //var pos = circle.m_position;  
  107.             var pos = circle.GetLocalPosition();
  108.             var r = circle.m_radius;  
  109.             var segments = 16.0;  
  110.             var theta = 0.0;  
  111.             var dtheta = 2.0 * Math.PI / segments;  
  112.             // draw circle  
  113.             context.moveTo(pos.x + r, pos.y);  
  114.             for (var i = 0; i < segments; i++) {  
  115.                 var d = new b2Vec2(r * Math.cos(theta), r * Math.sin(theta));  
  116.                 var v = b2Math.AddVV(pos, d);  
  117.                 context.lineTo(v.x, v.y);  
  118.                 theta += dtheta;  
  119.             }  
  120.             context.lineTo(pos.x + r, pos.y);  
  121.             // draw radius  
  122.             context.moveTo(pos.x, pos.y);  
  123.             var ax = circle.m_R.col1;  
  124.             var pos2 = new b2Vec2(pos.x + r * ax.x, pos.y + r * ax.y);  
  125.             context.lineTo(pos2.x, pos2.y);  
  126.         }  
  127.         break;  
  128.     //case b2Shape.e_polyShape:  
  129.     case b2Shape.e_polygonShape:  
  130.         {  
  131.             //console.log(b2Shape.e_polyShape + 'te');
  132.            
  133.             //createBox(world, 3, 230, 60, 180, true, 'ground');  
  134.  
  135.             var poly = shape;  
  136.  
  137.             console.log('poly');
  138.             console.log(poly);
  139.  
  140.  
  141.  
  142.            // alert(poly.GetVertices());
  143.  
  144.          
  145.  
  146.             //var tV = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[0]));  
  147.             // var vert = poly.GetVertices();
  148.             // var tV = b2Math.AddVV(poly.m_position, b2Math.MulMV(poly.m_R, vert[0]));
  149.            
  150.             //context.moveTo(tV.x, tV.y);
  151.  
  152.             context.moveTo(poly.m_vertices[0].x, y_to_canvas(poly.m_vertices[0].y));            
  153.  
  154.             //for (var i = 0; i < poly.m_vertexCount; i++) {  
  155.             for (var i = 0; i < poly.GetVertexCount(); i++) {
  156.            
  157.                 //var v = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[i]));  
  158.                 //context.lineTo(v.x, v.y);  
  159.  
  160.                // context.lineTo(poly.m_vertices[i].x, y_to_canvas(poly.m_vertices[i].y));                  
  161.                 context.lineTo(poly.m_vertices[i].x + 50, y_to_canvas(poly.m_vertices[i].y));                  
  162.             }  ;
  163.             //context.lineTo(tV.x, tV.y);  
  164.  
  165.             context.lineTo(poly.m_vertices[0].x, y_to_canvas(poly.m_vertices[0].y));
  166.            
  167.         }  
  168.         break;
  169.     default:
  170.         {
  171.         //alert('default');
  172.         }
  173.    
  174.     }
  175.      
  176.     context.stroke();  
  177. }  
  178.  
  179. // Canvas Y coordinates start at opposite location, so we flip
  180. function y_to_canvas(y) {
  181.     return canvasHeight - y;
  182. }
  183.  
  184. function createWorld() {  
  185.     var worldAABB = new b2AABB();  
  186.    
  187.     //worldAABB.minVertex.Set(-1000, -1000);  
  188.     worldAABB.lowerBound.Set(-1000, -1000);  
  189.     //worldAABB.maxVertex.Set(1000, 1000);  
  190.     worldAABB.upperBound.Set(1000, 1000);  
  191.     var gravity = new b2Vec2(0, 300);  
  192.     var doSleep = true;  
  193.     var world = new b2World(worldAABB, gravity, doSleep);  
  194.     return world;  
  195. }  
  196. function createGround(world) {  
  197.     var groundSd = new b2BoxDef();  
  198.     groundSd.extents.Set(1000, 50);  
  199.     groundSd.restitution = 0.2;  
  200.     var groundBd = new b2BodyDef();  
  201.     groundBd.AddShape(groundSd);  
  202.     groundBd.position.Set(-500, 340);  
  203.     return world.CreateBody(groundBd)  
  204. }  
  205. function createBall(world, x, y) {  //not used
  206.     var ballSd = new b2CircleDef();  
  207.     ballSd.density = 1.0;  
  208.     ballSd.radius = 20;  
  209.     ballSd.restitution = 1.0;  
  210.     ballSd.friction = 0;  
  211.     var ballBd = new b2BodyDef();  
  212.     ballBd.AddShape(ballSd);  
  213.     ballBd.position.Set(x,y);  
  214.     return world.CreateBody(ballBd);  
  215. }  
  216. function createBox(world, x, y, width, height, fixed, userData) {  
  217.     if (typeof(fixed) == 'undefined') fixed = true;  
  218.     //var boxSd = new b2BoxDef();  
  219.  
  220.     //console.log(boxSd);
  221. /*
  222.     if (!fixed) boxSd.density = 1.0;  
  223.     boxSd.userData = userData;  
  224.     boxSd.extents.Set(width, height);  
  225.     */
  226.  
  227.     /* is kit psl **/
  228.  
  229.     var bodyDef = new b2BodyDef;
  230.  
  231.    // bodyDef.type = b2Body.b2_dynamicBody;
  232.     bodyDef.type = b2Body.b2_staticBody;
  233.  
  234.     /*
  235.     bodyDef.position.x = x ;
  236.     bodyDef.position.y = y;
  237.     */
  238.     bodyDef.position.Set(x / 2 , y/ 2);
  239.  
  240.     var fixDef = new b2FixtureDef;
  241.     fixDef.shape = new b2PolygonShape;
  242.     fixDef.shape.SetAsBox(
  243.           width / 2 //half width
  244.        ,  height / 2 //half height
  245.     );
  246.    
  247.     console.log('bodyDef');
  248.     console.log(bodyDef);
  249.  
  250.     var body = world.CreateBody(bodyDef);
  251.     console.log(body);
  252.  
  253.     box = body.CreateFixture(fixDef);
  254.  
  255.     console.log('box');
  256.     console.log(box);
  257.  
  258.     return box;
  259.     //return world.CreateBody(bodyDef).CreateFixture(fixDef);
  260.     //return world.CreateBody(bodyDef);
  261.     /* end mano */
  262. /*
  263.     var boxBd = new b2BodyDef();  
  264.     boxBd.AddShape(boxSd);  
  265.     boxBd.position.Set(x,y);  
  266.     return world.CreateBody(boxBd)  
  267.     */
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement