论 setCollisionBitMask 与 setContactBitMask 区别

本文解析了collision与contact在游戏开发中的不同应用:collision影响物体碰撞表现,contact用于接触监听。两者虽相似但在使用上存在本质区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前完全抄书, 结果碰撞监听一直没有响应, 后来在晚上找到,  又犯了不仔细看的错误, 原来一个是 collision 一个是 contact, 接触 和 碰撞, 看似相似, 但用法上截然不同, collision 直接影响物体碰撞表现, 而contact 则是接触监听, 前者影响物体运动, 而后者对外给出碰撞结果,两者互不影响。

 论 setCollisionBitMask 与 setContactBitMask 区别

检查VScocos2dx4.0代码问题,并修改错误 #include "Thorn.h" #include "HelloWorldScene.h" // StaticThorn StaticThorn* StaticThorn::create(const cocos2d::Size& size, const cocos2d::Vec2& position) { StaticThorn* thorn = new (std::nothrow) StaticThorn(); if (thorn && thorn->initWithFile("Thorn.png")) { thorn->autorelease(); thorn->setPosition(position); thorn->setContentSize(size); thorn->setupPhysics(); return thorn; } CC_SAFE_DELETE(thorn); return nullptr; } void StaticThorn::setupPhysics() { damageBody = cocos2d::PhysicsBody::createBox(this->getContentSize(), cocos2d::PhysicsMaterial(0.0f, 0.0f, 1.0f)); damageBody->setDynamic(false); damageBody->setCategoryBitmask(0x08); damageBody->setCollisionBitmask(0x01); damageBody->setContactTestBitmask(0x01); damageBody->setTag(THORN_DAMAGE_TAG); this->setPhysicsBody(damageBody); triggerBody = cocos2d::PhysicsBody::createBox(this->getContentSize(), cocos2d::PhysicsMaterial(0.0f, 0.0f, 1.0f)); triggerBody->setDynamic(false); triggerBody->setCategoryBitmask(0x10); triggerBody->setCollisionBitmask(0x01); triggerBody->setContactTestBitmask(0x01); triggerBody->setTag(THORN_TRIGGER_TAG); this->addChild(cocos2d::Node::create()); this->getChildByTag(0)->setPhysicsBody(triggerBody); } void StaticThorn::onTrigger() { // Do nothing for static thorn } // MovingThorn MovingThorn* MovingThorn::create(const cocos2d::Size& size, const cocos2d::Vec2& position) { MovingThorn* thorn = new (std::nothrow) MovingThorn(); if (thorn && thorn->initWithFile("Thorn.png")) { thorn->autorelease(); thorn->setPosition(position); thorn->setContentSize(size); thorn->setupPhysics(); return thorn; } CC_SAFE_DELETE(thorn); return nullptr; } void MovingThorn::setupPhysics() { Thorn::setupPhysics(); } void MovingThorn::onTrigger() { auto moveAction = cocos2d::MoveBy::create(1.0f, cocos2d::Vec2(0, 100)); auto removeAction = cocos2d::RemoveSelf::create(); auto sequence = cocos2d::Sequence::create(moveAction, removeAction, nullptr); this->runAction(sequence); } // InvisibleThorn InvisibleThorn* InvisibleThorn::create(const cocos2d::Size& size, const cocos2d::Vec2& position) { InvisibleThorn* thorn = new (std::nothrow) InvisibleThorn(); if (thorn && thorn->initWithFile("Thorn.png")) { thorn->autorelease(); thorn->setPosition(position); thorn->setContentSize(size); thorn->setOpacity(0); thorn->setupPhysics(); return thorn; } CC_SAFE_DELETE(thorn); return nullptr; } void InvisibleThorn::setupPhysics() { Thorn::setupPhysics(); } void InvisibleThorn::onTrigger() { auto fadeIn = cocos2d::FadeIn::create(0.5f); auto moveAction = cocos2d::MoveBy::create(1.0f, cocos2d::Vec2(0, 100)); auto removeAction = cocos2d::RemoveSelf::create(); auto sequence = cocos2d::Sequence::create(fadeIn, moveAction, removeAction, nullptr); this->runAction(sequence); } // StretchingThorn StretchingThorn* StretchingThorn::create(const cocos2d::Size& size, const cocos2d::Vec2& position, float stretchLength) { StretchingThorn* thorn = new (std::nothrow) StretchingThorn(); if (thorn && thorn->initWithFile("Thorn.png")) { thorn->autorelease(); thorn->setPosition(position); thorn->setContentSize(size); thorn->stretchLength = stretchLength; thorn->setupPhysics(); return thorn; } CC_SAFE_DELETE(thorn); return nullptr; } void StretchingThorn::setupPhysics() { Thorn::setupPhysics(); } void StretchingThorn::onTrigger() { auto stretchAction = cocos2d::ScaleBy::create(1.0f, 1.0f, stretchLength / this->getContentSize().height); auto removeAction = cocos2d::RemoveSelf::create(); auto sequence = cocos2d::Sequence::create(stretchAction, removeAction, nullptr); this->runAction(sequence); } #ifndef THORN_H #define THORN_H #include "cocos2d.h" class Thorn : public cocos2d::Sprite { public: static const int THORN_DAMAGE_TAG = 400; static const int THORN_TRIGGER_TAG = 500; virtual void setupPhysics() = 0; // 纯虚函数 virtual void onTrigger() = 0; // 纯虚函数 protected: cocos2d::PhysicsBody* damageBody; cocos2d::PhysicsBody* triggerBody; }; class StaticThorn : public Thorn { public: static StaticThorn* create(const cocos2d::Size& size, const cocos2d::Vec2& position); virtual void setupPhysics() override; virtual void onTrigger() override; }; class MovingThorn : public Thorn { public: static MovingThorn* create(const cocos2d::Size& size, const cocos2d::Vec2& position); virtual void setupPhysics() override; virtual void onTrigger() override; }; class InvisibleThorn : public Thorn { public: static InvisibleThorn* create(const cocos2d::Size& size, const cocos2d::Vec2& position); virtual void setupPhysics() override; virtual void onTrigger() override; }; class StretchingThorn : public Thorn { public: static StretchingThorn* create(const cocos2d::Size& size, const cocos2d::Vec2& position, float stretchLength); virtual void setupPhysics() override; virtual void onTrigger() override; private: float stretchLength; }; #endif // THORN_H
最新发布
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值