cocos2d-x 3.0 使用最新物理引擎的一个源代码实例
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 使用最新物理引擎的一个源代码实例的更多相关文章
- three.js cannon.js物理引擎制作一个保龄球游戏
关于cannon.js我们已经学习了一些知识,今天郭先生就使用已学的cannon.js物理引擎的知识配合three基础知识来做一个保龄球小游戏,效果如下图,在线案例请点击博客原文. 我们需要掌握的技能 ...
- Cocos2d-x 3.0 简捷的物理引擎
Cocos2d-x 3.0 开发(九)使用Physicals取代Box2D和chipmunk http://www.cocos2d-x.org/docs/manual/framework/native ...
- 全文检索 使用最新lucene3.0.3+最新盘古分词 pangu2.4 .net 实例
开发环境 vs2015 winform 程序 1 首先需要下载对应的DLL 文章后面统一提供程序下载地址 里面都有 2 配置pangu的参数 也可以不配置 采用默认的即可 3 创建索引,将索引存放到本 ...
- 物理引擎-Physx的源代码去哪里找
前几天无意中看到了Physx开源了,就连自己的领导也高兴了一下,让本道士去下载源代码琢磨一下,顺便做几个例子跑起来.结果没成想这个nvidia的github上的源代码被移除了,而且csdn,pudn上 ...
- 实例介绍Cocos2d-x物理引擎:碰撞检测
碰撞检测是使用物理引擎的一个重要目的,使用物理引擎可以进行精确的碰撞检测,而且执行的效率也很高.在Cocos2d-x 3.x中使用事件派发机制管理碰撞事件,EventListenerPhysicsCo ...
- cocos2d-x中的Box2D物理引擎
在Cocos2d-x中集成了2个物理引擎,一个是Chipmunk,一个是Box2D.前者是用C语言编写的,文档和例子相对较少:Box2D是用C++写的,并且有比较完善的文档和资料.所以在需要使用物理引 ...
- cocos2dx-3.x物理引擎Box2D介绍
理引擎 Cocos2d-x引擎内置了两种物理引擎,它们分别是Box2D和Chipmunk,都是非常优秀的2D物理引擎,而且x引擎将它们都内置在SDK中.Box2D使用较为广泛,在这里选择Box2D来进 ...
- 003-unity3d 物理引擎简介以及示例
一.概述 物理引擎就是模拟真实世界中物体碰撞.跌落等反应的引擎,通过ballence.愤怒的小鸟等理解.Unity3D的物理引擎使用的是Nvidia的PhysX. 物理引擎是一个计算机程序模拟牛顿力学 ...
- cocos2d-x3.0 Physics新的物理引擎
1.说明: 3.0以后将box2d和chipmunk这两个物理引擎进行了封装,使用起来很的便利 2.详细用法: 1.创建物理世界场景 auto scene = Scene::createWithPhy ...
随机推荐
- MySQL中select * for update锁表的问题(转)
由于InnoDB预设是Row-Level Lock,所以只有「明确」的指定主键,MySQL才会执行Row lock (只锁住被选取的资料例) ,否则MySQL将会执行Table Lock (将整个资料 ...
- java设计模式之——适配器模式
适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 适配器模式的用途 用电器做例子,笔记本电脑的插头一般都是三相的,即除了阳极.阴极 ...
- c#基础练习之if结构
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace if语句 ...
- CSS属性总结——思路很清晰
CSS 属性总结 CSS的属性是用来改变文档元素的状态的,其中主要改变两方面的东西,即位置.样式,现在我们就将CSS的属性分为定位和样式两方面来总结,知识结构图如下: 用来定位的属性: 在同一 ...
- android自定义控件---添加表情
android自定义控件---添加表情 一.定义layout文件,图片不提供了 <?xml version="1.0" encoding="utf-8"? ...
- Table中的JCheckBox TableHeader的全选(全反选)功能
菜鸟学习ing class CheckDefaultModel extends DefaultTableModel /* * To change this template, choose Tools ...
- 网页内容的html标签补全和过滤的两种方法
网页内容的html标签补全和过滤的两种方法: 假设你的网页内容的html标签显示不全,有些表格标签不完整而导致页面混乱,或者把你的内容之外的局部html页面给包括进去了,我们能够写个函数方法来补全ht ...
- EasyUI - 后台管理系统 - 增加,删除,修改
效果: html代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ad ...
- c语言,gdb
Get gdb call stack http://blog.csdn.net/zoufeiyy/article/details/1490241 Debugging with GDB - Examin ...
- office文档转pdf
这里贴下代码吧,没啥好说的. using System; using System.Collections.Generic; using System.Linq; using System.Text; ...