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测试的更多相关文章

  1. box2d.js

    https://github.com/kripken/box2d.js/ Demo: http://kripken.github.io/box2d.js/webgl_demo/box2d.html 演 ...

  2. 使用 Box2D 做一个 JansenWalker 机器人

    在 Box2DFlash 的官网的首页有一个小 Demo,这个 Demo 中有11个例子,可以通过左右方向键查看不同的例子,里面的每个例子都非常有趣,但最让我感兴趣的,是其中一个叫 JansenWal ...

  3. 实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D

    我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用Box2D物理引擎的开发过程,熟悉这些API的使用.这个实例运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触 ...

  4. box2d中的物理世界

    box2d中的物理世界,即b2World类就是一个包含了各种物体(body,物理体,或者叫刚体),固定附着物(fixture,形状与物理体的绑定物)以及各种约束体(比如关节),并使其在当中完成各种交互 ...

  5. HTML5之2D物理引擎 Box2D for javascript Games 系列 第三部分之创建图腾破坏者的关卡

    创建图腾破坏者的关卡 现在你有能力创建你的第一个游戏原型,我们将从创建图腾破坏者的级别开始. 为了展示我们所做事情的真实性,我们将流行的Flash游戏图腾破坏者的一关作为 我们模仿的对象.请看下面的截 ...

  6. HTML5之2D物理引擎 Box2D for javascript Games 系列 第二部分

    这是系列第二部分,之前部分在本博客中找 源码demo存放在https://github.com/willian12345/Box2D-for-Javascript-Games 向世界添加刚体 刚体(B ...

  7. HTML5之2D物理引擎 Box2D for javascript Games 系列 第一部分

    我要的是能在H5页面上跑的javascript版的Box2D啊!!! 最近想学习Javascript版本的Box2D JS物理引擎,无奈搜了半天也没找到相对比较系统的资料 官方网站也只是简单的介绍,A ...

  8. Cocos2d Box2D之碰撞检测

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 在Box2D中碰撞事件由b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象 ...

  9. 强化学习-Windows安装gym、atari和box2d环境

    安装gym pip3 install gym pip3 install gym[accept-rom-license] 安装atari环境[可选] 下载安装VS build tools 如果出现 OS ...

随机推荐

  1. [POJ2777]Count Color(线段树)

    题目链接:http://poj.org/problem?id=2777 给你一个长为L想线段,向上面染色,颜色不超过30种,一共有O次操作,操作有两种: C a b c 在[a,b]上染上c颜色 P ...

  2. Java深入学习之--初始化

    目录 1.方法重载 2.默认构造器 3.this关键字 4.static关键字 5.初始化 1.方法重载 java中方法重载的意思是在同一个类中可以存在方法名相同的方法,而方法的参数类型不同,即使两个 ...

  3. github创建repo,本地导入git项目到github

    一般地,在注册好github账号之后,你需要做的事情就是在github上创建一个repo,该repo将成为你的origin(central)repo,随后你就可以将本地的项目git repo导入到这个 ...

  4. js兼容多浏览器的关闭当前页面

    关闭当前页面,相信不少人在开发中都遇到过这个需求,但面对这么多的浏览器,要做到js的兼容还需要做特殊的处理.关于这方面网上有很多的资料,但大多都是复制粘贴的,没有达到兼容的效果,或者是效果不好. 下面 ...

  5. Yii2 CSRF

    一.CSRF 即Cross-site request forgery跨站请求伪造,是指有人冒充你的身份进行一些恶意操作. 比如你登录了网站A,网站A在你的电脑设置了cookie用以标识身份和状态,然后 ...

  6. (转载)UITableView使用详解

    在开发iphone的应用时基本上都要用到UITableView,这里讲解一下UITableView的使用方法及代理的调用情况 UITableView使用详解 - (void)viewDidLoad { ...

  7. 一个小面试题sql

    一.            问答题 1简要说明分页是如何实现的. A:sqlserver: Select top(pagesize)  * from  student where id not in( ...

  8. UVALive 5532 King(差分约束,spfa)

    题意:假设一个序列S有n个元素,现在有一堆约束,限制在某些连续子序列之和上,分别有符号>和<.问序列S是否存在?(看题意都看了半小时了!) 注意所给的形式是(a,b,c,d),表示:区间之 ...

  9. mac vim shell配置

    一 : vim 配置 1 目录/usr/share/vim/vimrc 2 Python 自动缩进 http://blog.csdn.net/ikerpeng/article/details/1866 ...

  10. Heritrix源码分析(七) Heritrix总体介绍(转)

    本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/642794         本博客已迁移到本人独立博客: http://www.yun ...