BOX2D测试
var TAG_SPRITE_MANAGER = ;
var PTM_RATIO = ; 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[];
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(,-); //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 = ; //弹性 //创建刚体定义数据对象
var bodyDef = new b2BodyDef; //create ground //为静态刚体, 即不受碰撞影响
bodyDef.type = b2Body.b2_staticBody;
//设备形状为圆形
fixDef.shape = new b2PolygonShape;//多边形
//设置为矩形
fixDef.shape.SetAsBox(, );
// upper
// PTM_RATIO代表32个像素是一米
bodyDef.position.Set(, screenSize.height / PTM_RATIO); //世界创建刚体, 刚体创建设备, 设备拥有形状
var body = this.world.CreateBody(bodyDef);
body.CreateFixture(fixDef); // bottom
bodyDef.position.Set(, -1.8);
this.world.CreateBody(bodyDef).CreateFixture(fixDef); fixDef.shape.SetAsBox(, );
// left
bodyDef.position.Set(-1.8, );
this.world.CreateBody(bodyDef).CreateFixture(fixDef);
// right
bodyDef.position.Set(26.8, );
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,,TAG_SPRITE_MANAGER); this.addNewSpriteWithCoords(cc.p(screenSize.width / , screenSize.height / )); 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 = ;
var positionIterations = ; // 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 = - * 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);
}
});
BOX2D测试的更多相关文章
- box2d.js
https://github.com/kripken/box2d.js/ Demo: http://kripken.github.io/box2d.js/webgl_demo/box2d.html 演 ...
- 使用 Box2D 做一个 JansenWalker 机器人
在 Box2DFlash 的官网的首页有一个小 Demo,这个 Demo 中有11个例子,可以通过左右方向键查看不同的例子,里面的每个例子都非常有趣,但最让我感兴趣的,是其中一个叫 JansenWal ...
- 实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D
我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用Box2D物理引擎的开发过程,熟悉这些API的使用.这个实例运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触 ...
- box2d中的物理世界
box2d中的物理世界,即b2World类就是一个包含了各种物体(body,物理体,或者叫刚体),固定附着物(fixture,形状与物理体的绑定物)以及各种约束体(比如关节),并使其在当中完成各种交互 ...
- HTML5之2D物理引擎 Box2D for javascript Games 系列 第三部分之创建图腾破坏者的关卡
创建图腾破坏者的关卡 现在你有能力创建你的第一个游戏原型,我们将从创建图腾破坏者的级别开始. 为了展示我们所做事情的真实性,我们将流行的Flash游戏图腾破坏者的一关作为 我们模仿的对象.请看下面的截 ...
- HTML5之2D物理引擎 Box2D for javascript Games 系列 第二部分
这是系列第二部分,之前部分在本博客中找 源码demo存放在https://github.com/willian12345/Box2D-for-Javascript-Games 向世界添加刚体 刚体(B ...
- HTML5之2D物理引擎 Box2D for javascript Games 系列 第一部分
我要的是能在H5页面上跑的javascript版的Box2D啊!!! 最近想学习Javascript版本的Box2D JS物理引擎,无奈搜了半天也没找到相对比较系统的资料 官方网站也只是简单的介绍,A ...
- Cocos2d Box2D之碰撞检测
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Box2D中碰撞事件由b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象 ...
- 强化学习-Windows安装gym、atari和box2d环境
安装gym pip3 install gym pip3 install gym[accept-rom-license] 安装atari环境[可选] 下载安装VS build tools 如果出现 OS ...
随机推荐
- 添加crontab为什么要重定向输出到/dev/null
如果crontab不重定向输出,并且crontab所执行的命令有输出内容的话,是一件非常危险的事情.因为该输出内容会以邮件的形式发送给用户,内容存储在邮件文件 /var/spool/mail/$use ...
- 51nod1403 有趣的堆栈
看成括号序列的话第二种方法其实就是左括号和右括号之间有多少对完整的括号. #include<cstdio> #include<cstring> #include<ccty ...
- HDU 1869 六度分离【floyd】
题意:给出n个人,m个关系,问是否满足任意两个人之间的距离通过6个人就可以连接 用floyd就可以了,注意距离是大于7 #include<iostream> #include<cst ...
- win7x64安装wince6
Windows Embedded CE 安装方法 Wince的安装相对比较复杂,即使是一个Wince的老手,也可能遇到这样那样的问题.想来真是悲摧,Windows XP, Windows 7,64位, ...
- vijos 1379 字符串的展开
23333333333333333 #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- BZOJ 1787 紧急集合
LCA.注意细节. #include<iostream> #include<cstdio> #include<cstring> #include<algori ...
- 封装sharedPreferences SettingsSPData
/*************************************************************************** * 封装sharedPreferences S ...
- scala学习笔记(8): 列表的map,flatMap,zip和reduce
map,flatMap,zip和reduce函数可以让我们更容易处理列表函数. 1 map函数map将一个函数应用于列表的每一个元素并且将其作为一个新的列表返回.我们可以这样对列表的元素进行平方: s ...
- (六)6.5 Neurons Networks Implements of Sparse Autoencoder
一大波matlab代码正在靠近.- -! sparse autoencoder的一个实例练习,这个例子所要实现的内容大概如下:从给定的很多张自然图片中截取出大小为8*8的小patches图片共1000 ...
- swun 1184
解题思路:这题其实还是有点麻烦的,思路要清晰,关键是要找出中间的那个点. 已知不共线的三点:A(x1,y1),B(x2,y2),C(x3,y3),平行四边形ABCD的点D的坐标由对角线AC与BD互相平 ...