1.碰撞函数參数由两个变成一个了

2.检測不到碰撞。须要设置那三个參数。同一时候还要设置成动态的。

body进行设置。

3.初始入口文件也发生了改变。

附录上我近期调试好的cocos2d-x 3.1 使用最新物理引擎的一个源代码实例,是一个小猫碰撞小车的样例。最新的引擎都可用。

资源文件和代码下载:http://download.csdn.net/detail/u014734779/7417009

部分代码參考:

Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::createWithPhysics(); scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
Vect gravity = Vect(0.0f, 0.0f);
scene->getPhysicsWorld()->setGravity(gravity); // 'layer' is an autorelease object
auto layer = HelloWorld::create(); layer->setPhyWorld(scene->getPhysicsWorld()); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
} _spriteSheet = SpriteBatchNode::create("sprites.png", 2);
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("sprites.plist", "sprites.png");
this->addChild(_spriteSheet);
this->spawnCar();
this->schedule(schedule_selector(HelloWorld::secondUpadte), 1.0f);
this->scheduleUpdate(); return true;
} void HelloWorld::spawnCar()
{
SpriteFrame* frame = SpriteFrameCache::getInstance()->spriteFrameByName("car.png");
Sprite* car = Sprite::createWithSpriteFrame(frame);
car->setPosition(Point(100, 100));
addBoxBodyForSprite(car);
car->setTag(2);
car->runAction(MoveTo::create(1.0f, Point(300, 300)));
car->runAction(RepeatForever::create(Sequence::create(MoveTo::create(1.0,Point(300, 100)),MoveTo::create(1.0,Point(200, 200)),MoveTo::create(1.0,Point(100, 100)),NULL)));
_spriteSheet->addChild(car);
} void HelloWorld::spawnCat()
{
auto winSize = Director::getInstance()->getWinSize(); auto cat = Sprite::createWithSpriteFrameName("cat.png"); int minY = cat->getContentSize().height / 2;
int maxY = winSize.height - (cat->getContentSize().height / 2);
int rangeY = maxY - minY;
int actualY = CCRANDOM_0_1() * rangeY; int startX = winSize.width + (cat->getContentSize().width / 2);
int endX = -(cat->getContentSize().width / 2); Point startPos = Point(startX, actualY);
Point endPos = Point(endX, actualY); cat->setPosition(startPos);
addBoxBodyForSprite(cat);
cat->setTag(1);
cat->runAction(Sequence::create(MoveTo::create(1.0, endPos), CallFuncN::create(this, callfuncN_selector(HelloWorld::spriteDone)),NULL)); _spriteSheet->addChild(cat); } void HelloWorld::addBoxBodyForSprite(Sprite* sprite)
{
auto body = PhysicsBody::createBox(sprite->getContentSize());
body->setDynamic(true);
body->setCategoryBitmask(1); // 0001
body->setCollisionBitmask(-1); // 0001
body->setContactTestBitmask(-1); // 0001 必需要设置,并且设置为setDynamic(true);并且必须是0重力能够解决 sprite->setPhysicsBody(body);
} void HelloWorld::secondUpadte(float dt)
{
this->spawnCat();
} void HelloWorld::spriteDone(Node* sender)
{
Sprite *sprite = (Sprite*)sender;
_spriteSheet->removeChild(sprite,true);
} void HelloWorld::onEnter()
{
Layer::onEnter(); auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this); auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
} bool HelloWorld::onContactBegin( PhysicsContact& contact)
{
auto spriteA = (Sprite*)contact.getShapeA()->getBody()->getNode();
auto spriteB = (Sprite*)contact.getShapeB()->getBody()->getNode(); if (spriteA->getTag() == 1)
{
spriteA->removeFromParentAndCleanup(true);
} if (spriteB->getTag() == 1)
{
spriteB->removeFromParentAndCleanup(true);
} return true;
}

cocos2d-x 3.0 使用最新物理引擎的一个源代码实例的更多相关文章

  1. three.js cannon.js物理引擎制作一个保龄球游戏

    关于cannon.js我们已经学习了一些知识,今天郭先生就使用已学的cannon.js物理引擎的知识配合three基础知识来做一个保龄球小游戏,效果如下图,在线案例请点击博客原文. 我们需要掌握的技能 ...

  2. Cocos2d-x 3.0 简捷的物理引擎

    Cocos2d-x 3.0 开发(九)使用Physicals取代Box2D和chipmunk http://www.cocos2d-x.org/docs/manual/framework/native ...

  3. 全文检索 使用最新lucene3.0.3+最新盘古分词 pangu2.4 .net 实例

    开发环境 vs2015 winform 程序 1 首先需要下载对应的DLL 文章后面统一提供程序下载地址 里面都有 2 配置pangu的参数 也可以不配置 采用默认的即可 3 创建索引,将索引存放到本 ...

  4. 物理引擎-Physx的源代码去哪里找

    前几天无意中看到了Physx开源了,就连自己的领导也高兴了一下,让本道士去下载源代码琢磨一下,顺便做几个例子跑起来.结果没成想这个nvidia的github上的源代码被移除了,而且csdn,pudn上 ...

  5. 实例介绍Cocos2d-x物理引擎:碰撞检测

    碰撞检测是使用物理引擎的一个重要目的,使用物理引擎可以进行精确的碰撞检测,而且执行的效率也很高.在Cocos2d-x 3.x中使用事件派发机制管理碰撞事件,EventListenerPhysicsCo ...

  6. cocos2d-x中的Box2D物理引擎

    在Cocos2d-x中集成了2个物理引擎,一个是Chipmunk,一个是Box2D.前者是用C语言编写的,文档和例子相对较少:Box2D是用C++写的,并且有比较完善的文档和资料.所以在需要使用物理引 ...

  7. cocos2dx-3.x物理引擎Box2D介绍

    理引擎 Cocos2d-x引擎内置了两种物理引擎,它们分别是Box2D和Chipmunk,都是非常优秀的2D物理引擎,而且x引擎将它们都内置在SDK中.Box2D使用较为广泛,在这里选择Box2D来进 ...

  8. 003-unity3d 物理引擎简介以及示例

    一.概述 物理引擎就是模拟真实世界中物体碰撞.跌落等反应的引擎,通过ballence.愤怒的小鸟等理解.Unity3D的物理引擎使用的是Nvidia的PhysX. 物理引擎是一个计算机程序模拟牛顿力学 ...

  9. cocos2d-x3.0 Physics新的物理引擎

    1.说明: 3.0以后将box2d和chipmunk这两个物理引擎进行了封装,使用起来很的便利 2.详细用法: 1.创建物理世界场景 auto scene = Scene::createWithPhy ...

随机推荐

  1. 如何在VMware中修改Mac OS的屏幕分辨率

    关于mac os分辨率问题:方法一:临时方法,只对当次启动有效,即在启动倒计时的时候,回车,等待输入参数是输入如下文本:“Graphics Mode"="1280x800x32@6 ...

  2. CF 319C(Kalila and Dimna in the Logging Industry-斜率DP,注意叉积LL溢出)

    C. Kalila and Dimna in the Logging Industry time limit per test 2 seconds memory limit per test 256 ...

  3. hdu 4861 Couple doubi(数论)

    题目链接:hdu 4861 Couple doubi 题目大意:两个人进行游戏,桌上有k个球,第i个球的值为1i+2i+⋯+(p−1)i%p,两个人轮流取,假设DouBiNan的值大的话就输出YES, ...

  4. Unity 3D 建立开发环境

    之后的基本方向 ios游戏开发,基础教程http://www.devdiv.com/unity_d_-thread-128068-1-1.html,学习Unity 3D游戏开发. 应该昨天表示,读了一 ...

  5. STL之iterator(迭代器)

    3.迭代器简单介绍 除了使用下标来訪问vector对象的元素外,标准库还提供了訪问元素的方法:使用迭代器.迭代器是一种检查容器内元素而且遍历元素的数据类型. 百科释义: 迭代器(iterator)是一 ...

  6. spring利用扫描方式对bean的处理(对任何版本如何获取xml配置信息的处理)

    利用扫描的方式将组件注入容器,就也可以不用操作bean来实例化对象了. 下面我做一个例子 我用的spring3.2.2版本的 首先写一个spring.xml. <?xml version=&qu ...

  7. textarea内容有换行时存入数据库丢失问题的解决 (转载)

    http://blog.csdn.net/zhang_j_h/article/details/44563167 存入: function GetInputData(id, cmd) { var pos ...

  8. PHP - 点击更换头像

    原理: 操作流程: 首先点击头像图片,弹出选择窗口,选中其中一个则窗口推出头像更换. 效果: 主页面代码: <tr> <td>头像:</td> <td> ...

  9. Mysql zip 安装(windows)

    Mysql Windows zip包安装 Mysql 下载地址: http://dev.mysql.com/downloads/mysql/ 下载windows 版本对应的zip,之后解压 在C:\P ...

  10. linux 进程线程拓展

    依次参考: 多线程和多进程的区别(小结) Linux内核源代码分析——fork()原理&多进程网络模型 Linux写时拷贝技术(copy-on-write) linux内核 do_fork 函 ...