BOX 2 Dテスト

17077 ワード

var TAG_SPRITE_MANAGER = 1;
var PTM_RATIO = 32;

Box2DTestLayer = cc.Layer.extend({
    world:null,
    //GLESDebugDraw *m_debugDraw;

    ctor:function () {
        this._super();

        cc.eventManager.addListener(cc.EventListener.create({
            event: cc.EventListener.TOUCH_ALL_AT_ONCE,
            onTouchesEnded: function(touches, event){
                //Add a new body/atlas sprite at the touched location
                var touch = touches[0];
                var location = touch.getLocation();
                event.getCurrentTarget().addNewSpriteWithCoords(location);
            }
        }), this);

        var b2Vec2 = Box2D.Common.Math.b2Vec2
            , b2BodyDef = Box2D.Dynamics.b2BodyDef
            , b2Body = Box2D.Dynamics.b2Body
            , b2FixtureDef = Box2D.Dynamics.b2FixtureDef
            , b2World = Box2D.Dynamics.b2World
            , b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
            , b2CircleShape = Box2D.Collision.Shapes.b2CircleShape;

        var screenSize = cc.director.getWinSize();
        //UXLog(L"Screen width %0.2f screen height %0.2f",screenSize.width,screenSize.height);

        // Construct a world object, which will hold and simulate the rigid bodies.
        //  
        var gravity = new b2Vec2(0,-10); //new b2Vec2(0, -10)
        // 
        var allowSleep = true;
        this.world = new b2World(gravity, allowSleep);
        //  
        this.world.SetContinuousPhysics(true);

        // Define the ground body.
        //var groundBodyDef = new b2BodyDef(); // TODO
        //groundBodyDef.position.Set(screenSize.width / 2 / PTM_RATIO, screenSize.height / 2 / PTM_RATIO); // bottom-left corner

        // Call the body factory which allocates memory for the ground body
        // from a pool and creates the ground box shape (also from a pool).
        // The body is also added to the world.
        //var groundBody = this.world.CreateBody(groundBodyDef);

        var fixDef = new b2FixtureDef;
        fixDef.density = 1.0; // 
        fixDef.friction = 0.8; // 
        fixDef.restitution = 1; // 

        // 
        var bodyDef = new b2BodyDef;

        //create ground

        // ,  
        bodyDef.type = b2Body.b2_staticBody;
        // 
        fixDef.shape = new b2PolygonShape;// 
        // 
        fixDef.shape.SetAsBox(20, 2);
        // upper
        // PTM_RATIO 32 
        bodyDef.position.Set(10, screenSize.height / PTM_RATIO);

        // ,  ,  
        var body = this.world.CreateBody(bodyDef);
        body.CreateFixture(fixDef);

        // bottom
        bodyDef.position.Set(10, -1.8);
        this.world.CreateBody(bodyDef).CreateFixture(fixDef);

        fixDef.shape.SetAsBox(2, 14);
        // left
        bodyDef.position.Set(-1.8, 13);
        this.world.CreateBody(bodyDef).CreateFixture(fixDef);
        // right
        bodyDef.position.Set(26.8, 13);
        this.world.CreateBody(bodyDef).CreateFixture(fixDef);

        //Set up sprite

        // var mgr = cc.SpriteBatchNode.create(res.s_pathBlock, 150);
        // this.addChild(mgr, 0, TAG_SPRITE_MANAGER);
        var ball = new Ball(); //cc.Sprite.create(res.b_ball_01);
        this.addChild(ball,0,TAG_SPRITE_MANAGER);


        this.addNewSpriteWithCoords(cc.p(screenSize.width / 2, screenSize.height / 2));


        this.scheduleUpdate();
    },

    addNewSpriteWithCoords:function (p) {
        //UXLog(L"Add sprite %0.2f x %02.f",p.x,p.y);
        /*
         var batch = this.getChildByTag(TAG_SPRITE_MANAGER);

         //We have a 64x64 sprite sheet with 4 different 32x32 images.  The following code is
         //just randomly picking one of the images
         var idx = (Math.random() > .5 ? 0 : 1);
         var idy = (Math.random() > .5 ? 0 : 1);
         var sprite = cc.Sprite.create(batch.texture, cc.rect(32 * idx, 32 * idy, 32, 32));
         batch.addChild(sprite);
         */
        var sprite = this.getChildByTag(TAG_SPRITE_MANAGER);
        sprite.x = p.x;
        sprite.y = p.y;

        // Define the dynamic body.
        //Set up a 1m squared box in the physics world
        var b2BodyDef = Box2D.Dynamics.b2BodyDef
            , b2Body = Box2D.Dynamics.b2Body
            , b2FixtureDef = Box2D.Dynamics.b2FixtureDef
            , b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;

        var bodyDef = new b2BodyDef();
        bodyDef.type = b2Body.b2_dynamicBody;
        bodyDef.position.Set(p.x / PTM_RATIO, p.y / PTM_RATIO);
        bodyDef.userData = sprite;
        var body = this.world.CreateBody(bodyDef);

        // Define another box shape for our dynamic body.
        var dynamicBox = new b2PolygonShape();
        dynamicBox.SetAsBox(0.5, 0.5);//These are mid points for our 1m box

        // Define the dynamic body fixture.
        var fixtureDef = new b2FixtureDef();
        fixtureDef.shape = dynamicBox;
        fixtureDef.density = 1.0;
        fixtureDef.friction = 0.3;
        body.CreateFixture(fixtureDef);

    },
    update:function (dt) {
        //It is recommended that a fixed time step is used with Box2D for stability
        //of the simulation, however, we are using a variable time step here.
        //You need to make an informed choice, the following URL is useful
        //http://gafferongames.com/game-physics/fix-your-timestep/

        var velocityIterations = 8;
        var positionIterations = 1;

        // Instruct the world to perform a single step of simulation. It is
        // generally best to keep the time step and iterations fixed.
        this.world.Step(dt, velocityIterations, positionIterations);

        //Iterate over the bodies in the physics world
        for (var b = this.world.GetBodyList(); b; b = b.GetNext()) {
            if (b.GetUserData() != null) {
                //Synchronize the AtlasSprites position and rotation with the corresponding body
                var myActor = b.GetUserData();
                myActor.x = b.GetPosition().x * PTM_RATIO;
                myActor.y = b.GetPosition().y * PTM_RATIO;
                myActor.rotation = -1 * cc.RADIANS_TO_DEGREES(b.GetAngle());
            }
        }

    }
    //CREATE_NODE(Box2DTestLayer);
});

var Box2DTestScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var pLayer = new Box2DTestLayer();
        this.addChild(pLayer);
    }
});