cocos2d-x之物理引擎之碰撞监测
#include "HelloWorldScene.h"
USING_NS_CC;
#define RED_BIT_MASK 0b0100
#define GREEN_BIT_MASK 0b0010
#define BLUE_BIT_MASK 0b0001
#define EDGE_BIT_MASK 0b1000
Scene* HelloWorld::createScene()
{
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
scene->getPhysicsWorld()->setGravity(Vec2(0,-1000));
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
visibleSize=Director::getInstance()->getVisibleSize();
auto touchListener=EventListenerTouchOneByOne::create();
touchListener->onTouchBegan =[this](Touch * t,Event * e){
this->addBox(t->getLocation());
return false;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener,this);
auto contactListener=EventListenerPhysicsContact::create();
contactListener->onContactBegin=[](PhysicsContact & contact){
switch (contact.getShapeA()->getBody()->getContactTestBitmask()|contact.getShapeB()->getBody()->getContactTestBitmask()) {
case RED_BIT_MASK|BLUE_BIT_MASK:
log("red rect contact blue rect");
break;
case RED_BIT_MASK|GREEN_BIT_MASK:
log("red rect contact green rect");
break;
case BLUE_BIT_MASK|GREEN_BIT_MASK:
log("blue rect contact green rect");
break;
case RED_BIT_MASK|EDGE_BIT_MASK:
log("red rect hit edge");
break;
case GREEN_BIT_MASK|EDGE_BIT_MASK:
log("green rect hit edge");
break;
case BLUE_BIT_MASK|EDGE_BIT_MASK:
log("blue rect hit edge");
break;
case GREEN_BIT_MASK:
log("two green rect hit");
break;
default:
break;
}
return true;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener,this);
return true;
}
void HelloWorld::onEnter(){
Layer::onEnter();
auto bounds=Node::create();
bounds->setContentSize(visibleSize);
bounds->setPhysicsBody(PhysicsBody::createEdgeBox(bounds->getContentSize()));
bounds->getPhysicsBody()->setContactTestBitmask(EDGE_BIT_MASK);
bounds->setPosition(visibleSize/2);
addChild(bounds);
addBox(visibleSize/2);
}
void HelloWorld::addBox(Vec2 position){
auto r=Sprite::create();
r->setTextureRect(Rect(50,50,50,50));
r->setPhysicsBody(PhysicsBody::createBox(r->getContentSize()));
addChild(r);
r->setPosition(position);
// switch (rand()%6) {
// case 0:
// r->setColor(Color3B(255,0,255));
// break;
// case 1:
// r->setColor(Color3B(255,0,0));
// break;
// case 2:
// r->setColor(Color3B(0,255,0));
// break;
// case 3:
// r->setColor(Color3B(0,0,255));
// break;
// case 4:
// r->setColor(Color3B(0,255,255));
// break;
// case 5:
// r->setColor(Color3B(255,255,0));
// break;
// default:
// break;
// }
switch (rand()%3) {
case 0:
r->setColor(Color3B(255,0,0));
r->getPhysicsBody()->setContactTestBitmask(RED_BIT_MASK);
break;
case 1:
r->setColor(Color3B(0,255,0));
r->getPhysicsBody()->setContactTestBitmask(GREEN_BIT_MASK);
break;
case 2:
r->setColor(Color3B(0,0,255));
r->getPhysicsBody()->setContactTestBitmask(BLUE_BIT_MASK);
break;
default:
break;
}
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
最新文章
- DBNEWNAME工具介绍
- PHP7中php.ini、php-fpm和www.conf的配置
- *HDU1598 并查集
- ADF_Data Binding系列2_使用URL Service Data Control
- java.io.stream
- 【LeetCode】16. 3Sum Closest
- ScrollView 与ListView 滑动冲突完美解决
- PHP内置Web Server探究(二)自定义PHP控制台输出console函数
- 获取第上一个兄弟元素 屏蔽浏览器的差异(PreviousElementSibling)
- appium问题整理
- 蓝桥杯java 关于大范围时间的
- $.each遍历json数组
- Python面向对象之反射
- linux常用命令(个人总结)
- Oracle Ora 错误解决方案合集
- vim技巧3
- python 更换 版本
- Zookeeper安装使用及JavaAPI使用
- HashMap和LinkedHashMap的比较使用
- mybatis 控制台打印sql脚本