axmol/tests/js-tests/src/Box2dTest/Box2dTest.js

198 lines
7.5 KiB
JavaScript

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
var TAG_SPRITE_MANAGER = 1;
var PTM_RATIO = 32;
var Box2DTestLayer = cc.Layer.extend({
world:null,
//GLESDebugDraw *m_debugDraw;
ctor:function () {
if(window.sideIndexBar){
window.sideIndexBar.changeTest(0, 2);
}
//----start0----ctor
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;
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.
this.world = new b2World(new b2Vec2(0, -10), true);
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.5;
fixDef.restitution = 0.2;
var bodyDef = new b2BodyDef;
//create ground
bodyDef.type = b2Body.b2_staticBody;
fixDef.shape = new b2PolygonShape;
fixDef.shape.SetAsBox(20, 2);
// upper
bodyDef.position.Set(10, screenSize.height / PTM_RATIO + 1.8);
this.world.CreateBody(bodyDef).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 = new cc.SpriteBatchNode(s_pathBlock, 150);
this.addChild(mgr, 0, TAG_SPRITE_MANAGER);
this.addNewSpriteWithCoords(cc.p(screenSize.width / 2, screenSize.height / 2));
var label = new cc.LabelTTF("Tap screen", "Marker Felt", 32);
this.addChild(label, 0);
label.color = cc.color(0, 0, 255);
label.x = screenSize.width / 2;
label.y = screenSize.height - 50;
this.scheduleUpdate();
//----end0----
},
addNewSpriteWithCoords:function (p) {
//----start0----addNewSpriteWithCoords
//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 = new cc.Sprite(batch.texture, cc.rect(32 * idx, 32 * idy, 32, 32));
batch.addChild(sprite);
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);
//----end0----
},
update:function (dt) {
//----start0----update
//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.radiansToDegrees(b.GetAngle());
}
}
//----end0----
}
//CREATE_NODE(Box2DTestLayer);
});
var Box2DTestScene = TestScene.extend({
runThisTest:function () {
var layer = new Box2DTestLayer();
this.addChild(layer);
cc.director.runScene(this);
}
});
var arrayOfBox2DTest = [
Box2DTestLayer
];