Advertisement
Guest User

Untitled

a guest
Nov 16th, 2012
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @author J Barker & P Kirven
  3.  *
  4.  * This is the main entry point of the programme.
  5.  *
  6.  */
  7.  
  8. var b2Vec2 = Box2D.Common.Math.b2Vec2,
  9.     b2AABB = Box2D.Collision.b2AABB,
  10.     b2BodyDef = Box2D.Dynamics.b2BodyDef,
  11.     b2Body = Box2D.Dynamics.b2Body,
  12.     b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
  13.     b2Fixture = Box2D.Dynamics.b2Fixture,
  14.     b2World = Box2D.Dynamics.b2World,
  15.     b2MassData = Box2D.Collision.Shapes.b2MassData,
  16.     b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
  17.     b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
  18.     b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
  19.     b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef,
  20.     b2Shape = Box2D.Collision.Shapes.b2Shape,
  21.     b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef,
  22.     b2Joint = Box2D.Dynamics.Joints.b2Joint,
  23.     b2Settings = Box2D.Common.b2Settings;
  24.  
  25. var world;
  26. var ctx;
  27. var canvas_width;
  28. var canvas_height;
  29. var scale = 100;
  30.  
  31. $(function() {
  32.  
  33.   //first create the world
  34.   world = createWorld();
  35.  
  36.   ctx = $('#canvas').get(0).getContext('2d');
  37.   var canvas = $('#canvas');
  38.  
  39.   canvas_width = parseInt(canvas.width());
  40.   canvas_height = parseInt(canvas.height());
  41.  
  42.   createBall(world, 5, 3.75);
  43.  
  44.   //testy(world);
  45.  
  46.   //draw_hammer_2(world);
  47.   step();
  48.  
  49. });
  50.  
  51. function createWorld() {
  52.   //Gravity vector x, y - 10 m/s2 - thats earth!!
  53.   var gravity = new b2Vec2(0, 10);
  54.  
  55.   world = new b2World(gravity, true);
  56.  
  57.  
  58.   createGround(world);
  59.  
  60.   return world;
  61. }
  62.  
  63. function createGround(world) {
  64.   var bodyDef = new b2BodyDef();
  65.  
  66.   fixDef = new b2FixtureDef;
  67.   fixDef.density = 1.0;
  68.   fixDef.friction = .005;
  69.   fixDef.restitution = 0.5;
  70.  
  71.   fixDef.shape = new b2PolygonShape;
  72.  
  73.   fixDef.shape.SetAsBox(6.00, 1.50);
  74.  
  75.   bodyDef.position.Set(6, 6);
  76.  
  77. /*var bodyDef_2 = new b2BodyDef();
  78.    
  79.     fixDef_2 = new b2FixtureDef;
  80.     fixDef_2.density = 1.0;
  81.     fixDef_2.friction = .005;
  82.     fixDef_2.restitution = 0.5;
  83.    
  84.     fixDef_2.shape = new b2PolygonShape;
  85.    
  86.     fixDef_2.shape.SetAsBox(6.00 , 1.50);
  87.    
  88.     bodyDef_2.position.Set(6 , 6);
  89.    
  90.     var body_2 = world.CreateBody(bodyDef_2);*/
  91.  
  92.   var body = world.CreateBody(bodyDef);
  93.  
  94.   //var radians = 5 * Math.PI / 180;
  95.   //body.SetAngle(radians);
  96.   body.CreateFixture(fixDef);
  97.   //body_2.CreateFixture(fixDef_2);
  98. }
  99.  
  100.  
  101.  
  102. /*function createBox(world, x, y, width, height, options)
  103. {
  104.      //default setting
  105.     options = $.extend(true, {
  106.         'density' : 1.0 ,
  107.         'friction' : 1.0 ,
  108.         'restitution' : 0.5 ,
  109.        
  110.         'linearDamping' : 0.0 ,
  111.         'angularDamping' : 0.0 ,
  112.        
  113.         'type' : b2Body.b2_dynamicBody
  114.     }, options);
  115.      
  116.     var body_def = new b2BodyDef();
  117.     var fix_def = new b2FixtureDef;
  118.    
  119.     fix_def.density = options.density;
  120.     fix_def.friction = options.friction;
  121.     fix_def.restitution = options.restitution;
  122.    
  123.     fix_def.shape = new b2PolygonShape();
  124.        
  125.     fix_def.shape.SetAsBox( width , height );
  126.    
  127.     body_def.position.Set(x , y);
  128.    
  129.     body_def.linearDamping = options.linearDamping;
  130.     body_def.angularDamping = options.angularDamping;
  131.    
  132.     body_def.type = options.type;
  133.    
  134.     var b = world.CreateBody( body_def );
  135.     var f = b.CreateFixture(fix_def);
  136.    
  137.     return b;
  138. }
  139. */
  140.  
  141. function createBall(world, x, y) {
  142.  
  143.   // first circle shape and def
  144.   var fix_def1 = new b2FixtureDef;
  145.  
  146.   fix_def1.density = 1.0;
  147.   fix_def1.friction = 0.5;
  148.   fix_def1.restitution = .65;
  149.   fix_def1.bullet = false;
  150.  
  151.   var shape1 = new b2CircleShape();
  152.  
  153.   fix_def1.shape = shape1;
  154.   fix_def1.shape.SetLocalPosition(new b2Vec2(-.5, -.5));
  155.   fix_def1.shape.SetRadius(.3);
  156.  
  157.   // second circle def and shape
  158.   var fix_def2 = new b2FixtureDef;
  159.  
  160.   fix_def2.density = 1.0;
  161.   fix_def2.friction = 0.5;
  162.   fix_def2.restitution = .65;
  163.   fix_def2.bullet = false;
  164.  
  165.   var shape2 = new b2CircleShape();
  166.  
  167.   fix_def2.shape = shape2;
  168.   fix_def2.shape.SetLocalPosition(new b2Vec2(.5, .5));
  169.   fix_def2.shape.SetRadius(.3);
  170.  
  171.   // creating the body
  172.   var body_def = new b2BodyDef();
  173.  
  174.   body_def.type = b2Body.b2_dynamicBody;
  175.  
  176.   body_def.position.Set(5, 1);
  177.  
  178.   var b = world.CreateBody(body_def);
  179.  
  180.   b.CreateFixture(fix_def1);
  181.   b.CreateFixture(fix_def2);
  182.  
  183.   return b;
  184.  
  185. /*var fix_def = new b2FixtureDef;
  186.    
  187.     fix_def.density = 1.0;
  188.     fix_def.friction = 0.5;
  189.     fix_def.restitution = .65;
  190.     fix_def.bullet = false;
  191.    
  192.     var shape = new b2CircleShape(0.2);
  193.     fix_def.shape = shape;
  194.    
  195.     var fix_def2 = new b2FixtureDef;
  196.    
  197.     fix_def2.density = 1.0;
  198.     fix_def2.friction = 0.5;
  199.     fix_def2.restitution = .65;
  200.     fix_def2.bullet = false;
  201.    
  202.     var shape2 = new b2CircleShape();
  203.     alert(shape2.SetLocalPosition);
  204.     //shape2.SetLocalPosition(new b2Vec2(-.1, -.1));
  205.    
  206.     fix_def2.shape = shape2;
  207.     //alert(shape2.m_p.Set);
  208.    
  209.    
  210.     var body_def = new b2BodyDef();
  211.     body_def.position.Set(x , y);
  212.    
  213.     body_def.linearDamping = 0;
  214.     body_def.angularDamping = 0;
  215.    
  216.     body_def.type = b2Body.b2_dynamicBody;
  217.    
  218.     var b = world.CreateBody( body_def );
  219.     //b.CreateFixture(fix_def);
  220.     b.CreateFixture(fix_def2);
  221.    
  222.     return b;*/
  223. }
  224.  
  225. function step(cnt) {
  226.   var stepping = false;
  227.  
  228.   //fps = 60 , time steps
  229.   var fps = 100;
  230.   var timeStep = 1.0 / fps;
  231.  
  232.   //move the world ahead , step ahead man!!
  233.   world.Step(timeStep, 8, 3);
  234.   world.ClearForces();
  235.  
  236.   //first clear the canvas
  237.   ctx.clearRect(0, 0, canvas_width, canvas_height);
  238.  
  239.   //redraw the world
  240.   draw_world(world, ctx);
  241.  
  242.   //call this function again after 10 seconds
  243.   setTimeout('step(' + (cnt || 0) + ')', 10);
  244. }
  245.  
  246. function draw_world(world, context) {
  247.   //Draw the joints
  248.   for (var j = world.GetJointList(); j; j = j.GetNext()) {
  249.     drawJoint(j, context);
  250.   }
  251.  
  252.   //Draw the bodies
  253.   for (var b = world.GetBodyList(); b; b = b.GetNext()) {
  254.     //A body has many fixtures
  255.     for (var f = b.GetFixtureList(); f != null; f = f.GetNext()) {
  256.       var shape = f.GetShape();
  257.       var shapeType = shape.GetType();
  258.       if (isNaN(b.GetPosition().x)) {
  259.         alert('Invalid Position : ' + b.GetPosition().x);
  260.       } else {
  261.         drawShape(b, shape, context);
  262.       }
  263.     }
  264.   }
  265. }
  266.  
  267. function drawJoint(joint, context) {
  268.   var b1 = joint.GetBodyA();
  269.   var b2 = joint.GetBodyB();
  270.  
  271.   var x1 = b1.GetPosition();
  272.   var x2 = b2.GetPosition();
  273.  
  274.   var p1 = joint.GetAnchorA();
  275.   var p2 = joint.GetAnchorB();
  276.   context.strokeStyle = '#00eeee';
  277.   context.beginPath();
  278.  
  279.   switch (joint.m_type) {
  280.   case b2Joint.e_distanceJoint:
  281.     context.moveTo(p1.x, p1.y);
  282.     context.lineTo(p2.x, p2.y);
  283.     break;
  284.  
  285.   case b2Joint.e_pulleyJoint:
  286.     // TODO
  287.     break;
  288.  
  289.   default:
  290.     if (b1 == world.m_groundBody) {
  291.       context.moveTo(p1.x, p1.y);
  292.       context.lineTo(x2.x, x2.y);
  293.     } else if (b2 == world.m_groundBody) {
  294.       context.moveTo(p1.x, p1.y);
  295.       context.lineTo(x1.x, x1.y);
  296.     } else {
  297.       context.moveTo(x1.x, x1.y);
  298.       context.lineTo(p1.x, p1.y);
  299.       context.lineTo(x2.x, x2.y);
  300.       context.lineTo(p2.x, p2.y);
  301.     }
  302.  
  303.     break;
  304.   }
  305.  
  306.   context.stroke();
  307. }
  308.  
  309. function drawShape(body, shape, context) {
  310.   context.strokeStyle = '#000';
  311.  
  312.   if (shape.density == 1.0) {
  313.     context.fillStyle = "red";
  314.   } else {
  315.     context.fillStyle = "#fff";
  316.   }
  317.  
  318.   context.beginPath();
  319.  
  320.   switch (shape.GetType()) {
  321.   case b2Shape.e_circleShape:
  322.     {
  323.       var circle = shape;
  324.       var pos = body.GetPosition();
  325.       var r = shape.GetRadius();
  326.       var segments = 16.0;
  327.       var theta = 0.0;
  328.       var dtheta = 2.0 * Math.PI / segments;
  329.  
  330.       // draw circle
  331.       context.moveTo((pos.x + r) * scale, pos.y * scale);
  332.  
  333.       for (var i = 0; i < segments; i++) {
  334.         var d = new b2Vec2(r * Math.cos(theta), r * Math.sin(theta));
  335.  
  336.         var v = pos.Copy();
  337.         v.Add(d);
  338.         context.lineTo(v.x * scale, v.y * scale);
  339.         theta += dtheta;
  340.       }
  341.  
  342.       context.lineTo((pos.x + r) * scale, pos.y * scale);
  343.  
  344.       // draw radius line
  345.       context.moveTo(pos.x * scale, pos.y * scale);
  346.       var ax = body.GetTransform().R.col1;
  347.  
  348.       var pos2 = new b2Vec2(pos.x + r * ax.x, pos.y + r * ax.y);
  349.       context.lineTo(pos2.x * scale, pos2.y * scale);
  350.     }
  351.     break;
  352.  
  353.   case b2Shape.e_polygonShape:
  354.     {
  355.       var poly = shape;
  356.       var vert = shape.GetVertices();
  357.  
  358.       var position = body.GetPosition();
  359.  
  360.       var tV = position.Copy();
  361.       var a = vert[0].Copy();
  362.       a.MulM(body.GetTransform().R);
  363.       tV.Add(a);
  364.  
  365.       context.moveTo(tV.x * scale, tV.y * scale);
  366.  
  367.       for (var i = 0; i < vert.length; i++) {
  368.         var v = vert[i].Copy();
  369.         v.MulM(body.GetTransform().R);
  370.         v.Add(position);
  371.         context.lineTo(v.x * scale, v.y * scale);
  372.       }
  373.  
  374.       context.lineTo(tV.x * scale, tV.y * scale);
  375.     }
  376.  
  377.     break;
  378.   }
  379.   //this will fill a shape
  380.   context.fill();
  381.  
  382.   //this will create the outline of a shape
  383.   context.stroke();
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement