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 ...
随机推荐
- P5030 长脖子鹿放置
题目背景 众周所知,在西洋棋中,我们有城堡.骑士.皇后.主教和长脖子鹿. 题目描述 如图所示,西洋棋的"长脖子鹿",类似于中国象棋的马,但按照"目"字攻击,且没 ...
- [fw]Linux系统使用time计算命令执行的时间
Linux系统使用time计算命令执行的时间 当测试一个程序或比较不同算法时,执行时间是非常重要的,一个好的算法应该是用时最短的.所有类UNIX系统都包含time命令,使用这个命令可以统计时间消耗.例 ...
- JavaScript—创建正则对象
创建正则对象 方式1: var reg = new RegExp('\d', 'i');var reg = new RegExp('\d', 'gi'); 方式2: var reg = /\d/i;v ...
- 常见前端面试题http部分
1.常见http状态码 100 Continue 继续,一般在发送post请求时,已发送了http header之后服务端将返回此信息,表示确认,之后发送具体参数信息 200 OK 正常返回信息 20 ...
- 灰常牛逼的命令行备忘录 navi
灰常牛逼的命令行备忘录 navi 1. navi命令简介 1.1 navi命令简介 命令行是非常高效的工具,但一个很常见的现象是,很多命令行过一段时间就容易忘.举个栗子,如果我们常用 git 命令行管 ...
- 《x的奇幻之旅》:有趣的数学科普
本书是相对比较少见的数学方面的科普书.从最简单的阿拉伯数字.加减法,一直到概率统计.微积分.群论.拓扑.微分几何,每个主题都用几千字做一些深入浅出的介绍.写的相当的有趣. 在书中又一次看到这个有趣的事 ...
- (ACM模板)二元组pair
#include<iostream> #include<cstdio> #include<utility> using namespace std; typedef ...
- ElasticSearch1.7 java api
package cn.xdf.wlyy.solr.utils; import java.util.ArrayList;import java.util.HashMap;import java.util ...
- python关于window文件写入后,换行默认\r\n的问题
因为python兼容各种平台,所以当在window打开文本文件写入后,换行会默认写成\r\n linux是\n 如果想去掉换行的\r 解决方法:在open函数里写入换行要求即可 with open(f ...
- mysql跟踪sql
mysql中执行的sql跟踪比oracle简单多了,它自身有log.我们只要打开这个log记录,执行sql语句,再查看这个log就可以了.1. 首先要打开log的开关使用命令:show variabl ...