|   版权声明:本文为博主原创文章,未经博主允许不得转载。

在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之碰撞检测的更多相关文章

  1. 如何使用box2d做碰撞检测

    cocos2dx3.0+vs2012编译通过. 主要是通过body->SetTransform来设置body的位置和角度,然后自己写个ContactListener来监听碰撞事件 源代码下载 # ...

  2. Cocos2d Box2D之浮动刚体

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. b2_kinematicBody 运动学物体在模拟环境中根据自身的速度进行移动.运动学物体自身不受力的作用.虽然用户可以手动移动它,但是通 ...

  3. Cocos2d Box2D之静态刚体

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. b2_staticBody 在模拟环境下静态物体是不会移动的,就好像有无限大的质量.在Box2D的内部会将质量至反,存储为零.静态物体也可 ...

  4. Cocos2d Box2D之动态刚体

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. b2_dynamicBody 动态物体可以进行全模拟.用户可以用手手动移动动态刚体,也可以由动态刚体自己受力而自运动.动态物体可以和任何物 ...

  5. Cocos2d Box2D之简介

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. Box2D是一个用于模拟2D刚体物体的C++引擎.Box2D集成了大量的物理力学和运动学的计算,并将物理模拟过程封装到类对象中,将对物体的 ...

  6. 我常用的iphone开发学习网站[原创]

    引用地址:http://www.cnblogs.com/fuleying/archive/2011/08/13/2137032.html Google 翻译 Box2d 托德的Box2D的教程! Bo ...

  7. (转) [it-ebooks]电子书列表

    [it-ebooks]电子书列表   [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...

  8. [转]eoe社区cocos2d-x游戏引擎知识大汇总

    [eoeAndroid 社区]特意为大家汇总了cocos2d-x知识贴,分量十足,纯正干或.从基础教程到游戏应用的开发,我们不让知识流失,我们要做知识的搬运工还有加工 师.希望大家能够一起的学习,和大 ...

  9. 《Cocos2d-x实战 工具卷》上线了

    感谢大家一直以来的支持! 各大商店均开始销售:京东:http://item.jd.com/11659696.html当当:http://product.dangdang.com/23659809.ht ...

随机推荐

  1. Vue2.0源码阅读笔记(四):nextTick

      在阅读 nextTick 的源码之前,要先弄明白 JS 执行环境运行机制,介绍 JS 执行环境的事件循环机制的文章很多,大部分都阐述的比较笼统,甚至有些文章说的是错误的,以下为个人理解,如有错误, ...

  2. html标签的target属性应用

    1. 定义和用法 target 属性规定在何处打开页面上的所有链接. <head> <base target="_blank" /> </head&g ...

  3. 安卓构架组件——向项目添加组件(Adding Components to your Project)

    在开始之前,建议阅读 应用架构指南. Before getting started, we recommend reading the Architecture Components Guide to ...

  4. C语言字符串复制

    strcpy(arg1,arg2);//将arg2内容赋值到arg1 strncpy(arg1,arg2,size);//赋值多少由size决定,如果要截取某一部分,可以将arg2指针进行arg2+x ...

  5. python常用函数 M

    max(iterable) 求最大值,可以传入key. 例子: min(iterable) 求最小值,支持传入key. 例子: match(regular expression, string) 字符 ...

  6. 数据库系统实现 第一章 DBMS实现概述

    DBMS提供的能力 1)持久存储 DBMS在灵活性方面比文件系统要好,同时支持对非常大量数据的存储 2)编程接口 3)事务管理 DBMS支持对数据的并发存取,即多个不同的进程(称作事物)同时存取操作, ...

  7. 【LeetCode】线段树 segment-tree(共9题)+ 树状数组 binary-indexed-tree(共5题)

    第一部分---线段树:https://leetcode.com/tag/segment-tree/ [218]The Skyline Problem [307]Range Sum Query - Mu ...

  8. count(1)、count(*)、count(字段)的区别

    count(1)和count(*): 都为统计所有记录数,包括null 执行效率上:当数据量1W+时count(*)用时较少,1w以内count(1)用时较少 count(字段): 统计字段列的行数, ...

  9. center os 下redis安装以及基本使用

    解压并进入其目录 make cd src make install 默认情况,Redis不是在后台运行,我们需要把redis放在后台运行 vim /usr/local/redis/etc/redis. ...

  10. MySQL MHA相关测试

    接上篇文章,介绍了如何安装mysql mha,地址如下:http://blog.csdn.net/yiyuf/article/details/40340895 下面接着进行mha的相关测试: SSH  ...