Cocos2d Box2D之碰撞检测
| 版权声明:本文为博主原创文章,未经博主允许不得转载。
在Box2D中碰撞事件由b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:
/// Called when two fixtures begin to touch.两个物体开始接触时会响应,但只调用一次。
virtual void BeginContact(b2Contact* contact) { B2_NOT_USED(contact); } /// Called when two fixtures cease to touch.分离时响应。但只调用一次。
virtual void EndContact(b2Contact* contact) { B2_NOT_USED(contact); } /// This is called after a contact is updated. This allows you to inspect a
/// contact before it goes to the solver. If you are careful, you can modify the
/// contact manifold (e.g. disable contact).
/// A copy of the old manifold is provided so that you can detect changes.
/// Note: this is called only for awake bodies.
/// Note: this is called even when the number of contact points is zero.
/// Note: this is not called for sensors.
/// Note: if you set the number of contact points to zero, you will not
/// get an EndContact callback. However, you may get a BeginContact callback
/// the next step.持续接触时响应,它会被多次调用。
virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
B2_NOT_USED(contact);
B2_NOT_USED(oldManifold);
} /// This lets you inspect a contact after the solver is finished. This is useful
/// for inspecting impulses.
/// Note: the contact manifold does not include time of impact impulses, which can be
/// arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly
/// in a separate data structure.
/// Note: this is only called for contacts that are touching, solid, and awake. /// 持续接触时响应,调用完preSolve后调用。
virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{
B2_NOT_USED(contact);
B2_NOT_USED(impulse);
}
第一步:我们检测碰撞的类要继承自public b2ContactListener

第二步:创建物理世界的监听对象(在init()中添加)

第三步:在类中添加检测碰撞的函数(上面的四个中选择一个)

第四步:进行碰撞检测

如上:我要检测小鸟是否受到了碰撞,那么我通过contact->GetFixtureA()和contact->GetFixtureB()去取得当前的物理世界中产生碰撞的物体是否等于小鸟的刚体变量(birdBody)。如果相等说明产生了碰撞,如果不相等则说明没有碰撞。
摘自:http://www.cocos2d-x.org/wiki/Box2D
To find out when a fixture collides with another fixture in Box2D, we need to register a contact listener. A contact listener is a C++ object that we give Box2D, and it will call methods on that object to let us know when two objects begin to touch and stop touching. we can’t just store references to the contact points that are sent to the listener, because they are reused by Box2D. So we have to store copies of them instead.
void MyContactListener::BeginContact(b2Contact* contact)
{
// We need to copy out the data because the b2Contact passed in
// is reused.
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
}
The following iterates through all of the buffered contact points, and checks to see if any of them are a match between the ball and the bottom of the screen.
void MyContactListener::EndContact(b2Contact* contact)
{
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::vector::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end())
{
_contacts.erase(pos);
}
}
In the Helloworld.cpp,“tick” method wil use _contactListener to go through the contact points of bodies that are colliding. If a sprite is intersecting with a block, we add the block to a list of objects to destroy.
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
CCSprite *spriteB = (CCSprite *) bodyB->GetUserData(); // Sprite A = ball, Sprite B = Block
if (spriteA->getTag() == 1 && spriteB->getTag() == 2) {
if (std::find(toDestroy.begin(), toDestroy.end(), bodyB)
== toDestroy.end()) {
toDestroy.push_back(bodyB);
}
}
// Sprite B = block, Sprite A = ball
else if (spriteA->getTag() == 2 && spriteB->getTag() == 1) {
if (std::find(toDestroy.begin(), toDestroy.end(), bodyA)
== toDestroy.end()) {
toDestroy.push_back(bodyA);
}
}
Now you have even more techniques to add to Box2D in your game. I look forward to seeing some cool physics games from you guys!
运行代码:
.h files #ifndef _GAME_PLAY_H_
#define _GAME_PLAY_H_ #define PTM_RATIO 32 #include "cocos2d.h"
#include "Box2D\Box2D.h" USING_NS_CC;
using namespace CocosDenshion; class GamePlay : public cocos2d::Layer, public b2ContactListener
{
private:
cocos2d::Sprite* backgroundA;
cocos2d::Sprite* backgroundB;
cocos2d::Sprite* ready;
cocos2d::Sprite* tutorial;
cocos2d::Sprite* bird;
cocos2d::Sprite* land;
cocos2d::Sprite* num;
cocos2d::Sprite* upPipe;
cocos2d::Sprite* downPipe;
cocos2d::Sprite* pipeContainer;
cocos2d::Sprite* model;
cocos2d::Sprite* gameEnd;
cocos2d::LabelTTF* score;
cocos2d::LabelTTF* best;
cocos2d::MenuItemImage* play;
cocos2d::MenuItemImage* exit;
b2World* world;
b2Body* birdBody;
b2Body* landBody;
b2Body* downBody;
b2Body* upBody;
int bestScore;
int times = 0; private:
void replaceBackground(int);
void tipInformation();
void addBird();
void addLand();
void addPipe(float dt);
void gameBegin(float dt);
void gameOver();
void timeAnimate();
void upperBoundary();
//int birdSelect(float); public:
static cocos2d::Scene* createScene();
virtual bool init();
void initPhysicsWorld();
virtual void update(float); /// Called when two fixtures begin to touch.
virtual void BeginContact(b2Contact* contact); /** Callback function for multiple touches began.
*
* @param touches Touches information.
* @param unused_event Event information.
* @js NA
*/
virtual void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);
void goPlay(cocos2d::Ref* pSender);
void goExit();
CREATE_FUNC(GamePlay);
}; #endif // _GAME_PLAY_H_ .cpp files
#include "GamePlay.h"
#include "GameUnit.h"
#include "GameData.h" unit u3; cocos2d::Scene* GamePlay::createScene()
{
auto scene = Scene::create();
auto layer = GamePlay::create();
scene->addChild(layer);
return scene;
} bool GamePlay::init()
{
if (!Layer::init())
{
return false;
}
this->initPhysicsWorld();
this->replaceBackground(1);
this->tipInformation();
this->upperBoundary();
this->addLand();
this->addBird();
this->timeAnimate(); //设置多点触屏事件的监听器
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(GamePlay::onTouchesBegan, this);
//注册监听器
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //设置物理世界监听
world->SetContactListener(this); scheduleOnce(schedule_selector(GamePlay::gameBegin), 3);
//scheduleUpdate(); return true;
} void GamePlay::initPhysicsWorld()
{
//设置重力加速度为9.8;方向向下
b2Vec2 gravity;
gravity.Set(0.0f, -9.8f);
//创建一个新的物理世界
world = new b2World(gravity); //设置是否允许物体休眠
world->SetAllowSleeping(true);
//连续物理测试,防止发生非子弹特性的物体发生穿透现象
world->SetContinuousPhysics(true);
} void GamePlay::replaceBackground(int flag)
{
switch (flag)
{
case 1:
backgroundA = Sprite::create("background/light.png");
backgroundA->setPosition(Vec2(u3.winOrigin().x + u3.winSize().width / 2,
u3.winOrigin().y + u3.winSize().height / 2));
backgroundA->setScale(u3.scaleX(backgroundA, u3.winSize()),
u3.scaleY(backgroundA, u3.winSize()));
this->addChild(backgroundA, 0);
break;
default:
break;
}
} void GamePlay::tipInformation()
{
......
//exit
exit = MenuItemImage::create(
"button/exit_f.png",
"button/exit_b.png",
CC_CALLBACK_0(GamePlay::goExit, this)
);
Menu* menu = Menu::create(exit, NULL);
menu->setPosition(Vec2(u3.winOrigin().x + exit->getContentSize().width / 2,
u3.winOrigin().y + exit->getContentSize().height / 2));
this->addChild(menu, 7);
} void GamePlay::addBird()
{
bird = Sprite::create("bird/littleBird.png");
bird->setPosition(Vec2(u3.winOrigin().x + u3.winSize().width / 3,
u3.winOrigin().y + u3.winSize().height - 5 * (ready->getContentSize().height)));
bird->setScale(1.5);
auto repeat = RepeatForever::create(Animate::create(u3.gameAnimate(1)));
bird->runAction(repeat);
this->addChild(bird, 1); //创建刚体
b2BodyDef birdBodyDef;
birdBodyDef.type = b2_dynamicBody;
birdBodyDef.position.Set(bird->getPosition().x / PTM_RATIO,
bird->getPosition().y / PTM_RATIO);
//将刚体,精灵与世界关联起来
birdBody = world->CreateBody(&birdBodyDef);
birdBody->SetUserData(bird); //定义一个盒子
b2PolygonShape birdBox;
birdBox.SetAsBox(bird->getContentSize().width / 3 / PTM_RATIO,
bird->getContentSize().height / 3 / PTM_RATIO);
//夹具
b2FixtureDef fixtureDef;
//设置夹具的形状
fixtureDef.shape = &birdBox;
birdBody->CreateFixture(&fixtureDef);
} void GamePlay::update(float dt)
{
world->Step(dt, 8, 3);
for (b2Body* bb = world->GetBodyList(); bb; bb = bb->GetNext())
{
if (bb->GetUserData() != nullptr)
{
Sprite* sprite = (Sprite*)bb->GetUserData();
//设置精灵的当前的位置,通过取得精灵的位置乘上相应的像素,这里的PTM_RATIO为32个像素为1m,就可以判断出精灵的位置处于物理世界的何处
sprite->setPosition(Vec2(bb->GetPosition().x*PTM_RATIO,
bb->GetPosition().y*PTM_RATIO));
//设置精灵的角度偏转
sprite->setRotation(0);
}
}
} //添加地面
void GamePlay::addLand()
{
//地板也是物理世界的一个刚体,是一个静态的刚体
land = Sprite::create("background/land.png");
land->setPosition(Vec2(u3.winOrigin().x + u3.winSize().width / 2,
u3.winOrigin().y + land->getContentSize().height / 2));
land->setScaleX(u3.winSize().width / 1.5*land->getContentSize().width);
land->setScaleY(u3.winSize().height / (land->getContentSize().height * 3));
this->addChild(land, 4); //创建刚体
b2BodyDef landBodyDef;
landBodyDef.type = b2_staticBody;
landBodyDef.position.Set(u3.winSize().width / 2 / PTM_RATIO,
land->getPosition().y / PTM_RATIO); landBody = world->CreateBody(&landBodyDef);
landBody->SetUserData(land); b2PolygonShape landShape;
landShape.SetAsBox(u3.winSize().width / 2 / PTM_RATIO,
backgroundA->getContentSize().height / 2 / PTM_RATIO);
//1.4*land->getContentSize().height / PTM_RATIO
b2FixtureDef landFixtureDef;
landFixtureDef.shape = &landShape;
landBody->CreateFixture(&landFixtureDef);
} void GamePlay::upperBoundary()
{
.......
} void GamePlay::addPipe(float dt)
{
times++; float randPipe = -rand() % 3;
//down bar
downPipe = Sprite::create("pipe/down.png");
downPipe->setScaleY(u3.winSize().height / (downPipe->getContentSize().height*1.5));
this->addChild(downPipe, 3);
b2BodyDef downBodyDef;
downBodyDef.position = b2Vec2(u3.winSize().width / PTM_RATIO + 2,
downPipe->getContentSize().height / 2 / PTM_RATIO + randPipe);
// + land->getContentSize().height / PTM_RATIO
downBodyDef.type = b2_kinematicBody;
//修改速度可以提升游戏的难度
downBodyDef.linearVelocity = b2Vec2(-2, 0);
downBody = world->CreateBody(&downBodyDef);
downBody->SetUserData(downPipe);
b2PolygonShape downShape;
downShape.SetAsBox(downPipe->getContentSize().width / 2 / PTM_RATIO,
1.6*downPipe->getContentSize().height / PTM_RATIO);
b2FixtureDef downPipeFixture;
downPipeFixture.shape = &downShape;
downBody->CreateFixture(&downPipeFixture); ......
} void GamePlay::gameOver()
{
//显示Game Over的Logo
.......
//显示奖章牌
...... //奖章
......
//显示成绩
......
//设置字体的颜色为黑色,并将成绩显示在panel面板上
...... unscheduleUpdate();
unschedule(schedule_selector(GamePlay::addPipe));
} void GamePlay::gameBegin(float dt)
{
scheduleUpdate();
//修改时间可以提升游戏的难易程度
schedule(schedule_selector(GamePlay::addPipe), 2);
} void GamePlay::BeginContact(b2Contact* contact)
{
if (contact->GetFixtureA()->GetBody() == birdBody || contact->GetFixtureB()->GetBody() == birdBody)
{
this->gameOver();
}
}
运行截图:


Cocos2d Box2D之碰撞检测的更多相关文章
- 如何使用box2d做碰撞检测
cocos2dx3.0+vs2012编译通过. 主要是通过body->SetTransform来设置body的位置和角度,然后自己写个ContactListener来监听碰撞事件 源代码下载 # ...
- Cocos2d Box2D之浮动刚体
| 版权声明:本文为博主原创文章,未经博主允许不得转载. b2_kinematicBody 运动学物体在模拟环境中根据自身的速度进行移动.运动学物体自身不受力的作用.虽然用户可以手动移动它,但是通 ...
- Cocos2d Box2D之静态刚体
| 版权声明:本文为博主原创文章,未经博主允许不得转载. b2_staticBody 在模拟环境下静态物体是不会移动的,就好像有无限大的质量.在Box2D的内部会将质量至反,存储为零.静态物体也可 ...
- Cocos2d Box2D之动态刚体
| 版权声明:本文为博主原创文章,未经博主允许不得转载. b2_dynamicBody 动态物体可以进行全模拟.用户可以用手手动移动动态刚体,也可以由动态刚体自己受力而自运动.动态物体可以和任何物 ...
- Cocos2d Box2D之简介
| 版权声明:本文为博主原创文章,未经博主允许不得转载. Box2D是一个用于模拟2D刚体物体的C++引擎.Box2D集成了大量的物理力学和运动学的计算,并将物理模拟过程封装到类对象中,将对物体的 ...
- 我常用的iphone开发学习网站[原创]
引用地址:http://www.cnblogs.com/fuleying/archive/2011/08/13/2137032.html Google 翻译 Box2d 托德的Box2D的教程! Bo ...
- (转) [it-ebooks]电子书列表
[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...
- [转]eoe社区cocos2d-x游戏引擎知识大汇总
[eoeAndroid 社区]特意为大家汇总了cocos2d-x知识贴,分量十足,纯正干或.从基础教程到游戏应用的开发,我们不让知识流失,我们要做知识的搬运工还有加工 师.希望大家能够一起的学习,和大 ...
- 《Cocos2d-x实战 工具卷》上线了
感谢大家一直以来的支持! 各大商店均开始销售:京东:http://item.jd.com/11659696.html当当:http://product.dangdang.com/23659809.ht ...
随机推荐
- Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法. 重启服务
今天用Oracle突然出现Oracle ORA-01033: ORACLE initialization or shutdown in progress. 想起前两天删掉了几个DBF文件,幸好还没有清 ...
- Asp.Netcore使用Filter来实现接口的全局异常拦截,以及前置拦截和后置拦截
原文链接:https://blog.csdn.net/qq_38762313/article/details/85234594 全局异常拦截器: 解决写每个接口都需要去做容错而添加try{ ...
- react native 之 Android studio
https\://services.gradle.org/distributions/gradle-4.4-all.zip 下载失败 https://blog.csdn.net/u013132758/ ...
- tensorflow的reshape操作tf.reshape()
在处理图像数据的时候总会遇到输入图像的维数不符合的情况,此时tensorflow中reshape()就很好的解决了这个问题. 更为详细的可以参考官方文档说明: numpy.reshape reshap ...
- samba - 为 UNIX 实现的 Windows SMB/CIFS 文件服务器
SYNOPSIS 总览 Samba DESCRIPTION 描述 samba 套件是在 UNIX 系统上实现“服务器信息块”(通常简称 SMB) 协议的一组程序.这个协议有时也称为“通用互联网文件系统 ...
- 一、表单和ajax中的post请求&&后台获取数据方法
一.表单和ajax中的post请求&&后台获取数据方法 最近要做后台数据接收,因为前台传来的数据太过于混乱,所以总结了一下前台数据post请求方法,顺便写了下相对应的后台接收方法. 前 ...
- springcloud整合分布式事务LCN
一.创建eureka注册中心 a.pom文件 <properties> <java.version>1.8</java.version> <spring-cl ...
- PL SQL安装
首先,在官网下载PL SQL 的对应版本,本机是64位的就下载64位的,网址:https://www.allroundautomations.com/downloads.html#PLS 点击应用程序 ...
- pycharm远程连接的步骤(有一项需特别注意)
1.设置远程服务器,在这里配置服务器地址等: 要注意下边的这个mappings设置好,跟编译器的path mapping设置为同一个,这里我没设置为同一个,后边就出现了点问题. 2.配置远程的编译器 ...
- find命令进阶(三):xargs
The xargs command performs an interesting function. It accepts input from standard input and convert ...