Advertisement
Guest User

PhysicsNode.js cocos2d

a guest
Apr 19th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var cocos = require("cocos2d"),
  2.     geom  = require("geometry"),
  3.     box2d = require("box2d");
  4.  
  5. function PhysicsNode() {
  6.     PhysicsNode.superclass.constructor.call(this);
  7. }
  8.  
  9. PhysicsNode.physicsScale = 30;
  10.  
  11. PhysicsNode.inherit(cocos.nodes.Node, {
  12.  
  13.     update: function() {
  14.         this.synchronizePosition();
  15.     },
  16.  
  17.     /*
  18.      * @param {b2World} world world which is used to create the Object
  19.      * @param {Object} args contains various arguments to create the physics. optional.
  20.      * @param {Number} args.density sets density.
  21.      * @param {Number} args.isStatic sets density to 0.0 to make the shape static.
  22.      *    ignored when args.density is set.
  23.      * @param {Number} args.restitution sets the restitution of the shape. By default 0.1
  24.      * @param {Number} args.friction sets the friction of the shape. By default 0.1
  25.      * @param {Number} args.boundingBox sets with and height of the bounding box.
  26.      *    if not set this.contentsize is used to get width and height.
  27.      * @param {String} args.shapeType string constant to determin what kind
  28.      *    of shape should be created. Valid values are "circle", "box"
  29.      *    if not set a "box" will be created.
  30.      */
  31.     createPhysics: function(world, args) {
  32.        
  33.         if(!args) {
  34.             args = {}
  35.         }
  36.    
  37.         var bodyDef = new box2d.b2BodyDef();
  38.        
  39.         bodyDef.type = args.density != 0.0 && !args.isStatic ?
  40.                        box2d.b2Body.b2_dynamicBody :
  41.                        box2d.b2Body.b2_staticBody;
  42.                        
  43.         bodyDef.position = new box2d.b2Vec2(this.position.x / PhysicsNode.physicsScale,
  44.                                             this.position.y / PhysicsNode.physicsScale);
  45.  
  46.         var fixDef = new box2d.b2FixtureDef();
  47.         fixDef.density = args.density ? args.density : (args.isStatic ? 0.0 : 1.0);
  48.         fixDef.friction = args.friction ? args.friction : 0.1;
  49.         fixDef.restitution = args.restitution ? args.restitution : 0.1;
  50.        
  51.         var boundingBox = args.boundingBox ?
  52.                 new geom.Rect(0,0, args.boundingBox.size.width / PhysicsNode.physicsScale,
  53.                                    args.boundingBox.size.height / PhysicsNode.physicsScale) :
  54.                 new geom.Rect(0,0, this.contentSize.width / PhysicsNode.physicsScale,
  55.                                    this.contentSize.height / PhysicsNode.physicsScale);
  56.        
  57.         var shape;
  58.         if (args.shapeType == "circle") {
  59.             shape = box2d.b2CircleShape();
  60.            
  61.         } else {
  62.             if (this.createCustomShapeType) {
  63.                 shape = this.createCustomShapeType(args);
  64.             }
  65.             if (!shape) {
  66.                 shape = new box2d.b2PolygonShape();
  67.                 shape.SetAsBox(boundingBox.size.width/2, boundingBox.size.height/2);
  68.             }
  69.         }
  70.        
  71.         fixDef.shape = shape;
  72.  
  73.         //create ground
  74.         this.body = world.CreateBody(bodyDef);
  75.         this.body.CreateFixture(fixDef);
  76.         this.body.SetUserData(this);
  77.        
  78.     },
  79.    
  80.     /*
  81.      * Synchronize the Node's position with the position of the physics body
  82.      */
  83.     synchronizePosition: function() {
  84.         this.position.x = this.body.GetPosition().x * PhysicsNode.physicsScale;
  85.         this.position.y = this.body.GetPosition().y * PhysicsNode.physicsScale;
  86.         this.rotation = geom.radiansToDegrees(-this.body.GetAngle());
  87.      //   console.log(this.position.x + "," + this.position.y);
  88.     }
  89. });
  90.  
  91. module.exports = PhysicsNode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement