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 ...
随机推荐
- 没有显示器且IP未知的情况下登录树莓派
如果是没有显示器操作树莓派,可能会不知道树莓派有线网卡自动分配到的IP地址,不知道登录到哪儿.以下提供详细操作步骤解决这个问题. 网段扫描法这个是推荐的办法.网段扫描工具很多,推荐一个Advanced ...
- Charles是mac的iddler抓包工具
windows下面我们经常使用 Fiddler 抓包工具进行代理等一系列操作.然而,在 Mac 下 http://en.softonic.com/s/java-se-6:mac
- java 文件text的写入
日志文件 OutputStream out = new FileOutputStream(f, true); if (list1.size() > 0) { int h1 = 0; for (i ...
- JAVA设计模式之【抽象工厂模式】
1.产品接口,电视和空调 public interface Television // 电视接口 { public void play(); } public interface AirConditi ...
- 推荐开源Api文档生成工具——Doxygen
http://www.stack.nl/~dimitri/doxygen/index.html 非常的方便. 2步生成API文档. 具体信息见官网哟!
- 64位Ubuntu 13.04 安装Bochs 2.3.5
bochs 2.3.5源码编译 网上编译bochs的资料非常多,基本的问题都有解决方案,我重点讲不常见的问题. 基本安装步骤 tar vxzf bochs-2.3.5.tar.gz cd bochs- ...
- visual studio 2015常用快捷键
常用快捷键 技巧 0.0 删除文件中的当前行: Home + Shife-End + Delete 技巧 1.1 避免意外复制一个空白行 工具->选项->文本编辑器->所有语言-&g ...
- UVa 1609 (博弈) Foul Play
姑且把它归类为一道博弈吧,毕竟这也是在找必胜方案. 十分有意思的一道题目,设计一种方案让你支持的1队获胜. 题目给出了两个很重要的条件: 1队能打败至少一半的队伍 对于1队不能打败的黑队,一定存在一个 ...
- kthread_stop引起的OOP
1 使用kthread_create创建线程: struct task_struct *kthread_create(int (*threadfn)(void *data), void *da ...
- 关于inline-block的间隙问题
很久之前写过一个星星评级的样式,当时开发人员在嵌套代码的时候出现很多问题,同样的一个样式有的页面正常有的页面就出现星星错位的问题,仔细研究了一下代码,发现问题原来出在了inline-block上. 目 ...