From ee3a8da13c2ab40ffb66d412b5811c21abf7e50e Mon Sep 17 00:00:00 2001 From: boyu0 Date: Fri, 8 Aug 2014 16:38:20 +0800 Subject: [PATCH] fix PhysicsBody bitmask bug. --- cocos/physics/CCPhysicsBody.cpp | 63 +++++++++++++++++++++++++-------- cocos/physics/CCPhysicsBody.h | 21 +++++------ cocos/physics/CCPhysicsShape.h | 5 +++ 3 files changed, 61 insertions(+), 28 deletions(-) diff --git a/cocos/physics/CCPhysicsBody.cpp b/cocos/physics/CCPhysicsBody.cpp index bc38e34e32..233e55f157 100644 --- a/cocos/physics/CCPhysicsBody.cpp +++ b/cocos/physics/CCPhysicsBody.cpp @@ -69,10 +69,6 @@ PhysicsBody::PhysicsBody() , _linearDamping(0.0f) , _angularDamping(0.0f) , _tag(0) -, _categoryBitmask(UINT_MAX) -, _collisionBitmask(0) -, _contactTestBitmask(UINT_MAX) -, _group(0) , _positionResetTag(false) , _rotationResetTag(false) , _rotationOffset(0) @@ -422,11 +418,6 @@ PhysicsShape* PhysicsBody::addShape(PhysicsShape* shape, bool addMassAndMoment/* } _shapes.pushBack(shape); - - if (_group != CP_NO_GROUP && shape->getGroup() == CP_NO_GROUP) - { - shape->setGroup(_group); - } } return shape; @@ -829,34 +820,64 @@ void PhysicsBody::update(float delta) void PhysicsBody::setCategoryBitmask(int bitmask) { - _categoryBitmask = bitmask; - for (auto& shape : _shapes) { shape->setCategoryBitmask(bitmask); } } +int PhysicsBody::getCategoryBitmask() const +{ + if (!_shapes.empty()) + { + return _shapes.front()->getCategoryBitmask(); + } + else + { + return UINT_MAX; + } +} + void PhysicsBody::setContactTestBitmask(int bitmask) { - _contactTestBitmask = bitmask; - for (auto& shape : _shapes) { shape->setContactTestBitmask(bitmask); } } +int PhysicsBody::getContactTestBitmask() const +{ + if (!_shapes.empty()) + { + return _shapes.front()->getContactTestBitmask(); + } + else + { + return 0x00000000; + } +} + void PhysicsBody::setCollisionBitmask(int bitmask) { - _collisionBitmask = bitmask; - for (auto& shape : _shapes) { shape->setCollisionBitmask(bitmask); } } +int PhysicsBody::getCollisionBitmask() const +{ + if (!_shapes.empty()) + { + return _shapes.front()->getCollisionBitmask(); + } + else + { + return UINT_MAX; + } +} + void PhysicsBody::setGroup(int group) { for (auto& shape : _shapes) @@ -865,6 +886,18 @@ void PhysicsBody::setGroup(int group) } } +int PhysicsBody::getGroup() const +{ + if (!_shapes.empty()) + { + return _shapes.front()->getGroup(); + } + else + { + return 0; + } +} + void PhysicsBody::setPositionOffset(const Vec2& position) { if (!_positionOffset.equals(position)) diff --git a/cocos/physics/CCPhysicsBody.h b/cocos/physics/CCPhysicsBody.h index 6ed512f1f2..53c2f23b21 100644 --- a/cocos/physics/CCPhysicsBody.h +++ b/cocos/physics/CCPhysicsBody.h @@ -171,12 +171,12 @@ public: * The default value is 0xFFFFFFFF (all bits set). */ void setCollisionBitmask(int bitmask); - /** get the category bit mask */ - inline int getCategoryBitmask() const { return _categoryBitmask; } - /** get the contact test bit mask */ - inline int getContactTestBitmask() const { return _contactTestBitmask; } - /** get the collision bit mask */ - inline int getCollisionBitmask() const { return _collisionBitmask; } + /** Return bitmask of first shape, if there is no shape in body, return default value.(0xFFFFFFFF) */ + int getCategoryBitmask() const; + /** Return bitmask of first shape, if there is no shape in body, return default value.(0x00000000) */ + int getContactTestBitmask() const; + /** Return bitmask of first shape, if there is no shape in body, return default value.(0xFFFFFFFF) */ + int getCollisionBitmask() const; /** * set the group of body @@ -184,8 +184,8 @@ public: * it have high priority than bit masks */ void setGroup(int group); - /** get the group of body */ - inline int getGroup() const { return _group; } + /** Return bitmask of first shape, if there is no shape in body, return default value.(0) */ + int getGroup() const; /** get the body position. */ Vec2 getPosition() const; @@ -339,11 +339,6 @@ protected: float _angularDamping; int _tag; - int _categoryBitmask; - int _collisionBitmask; - int _contactTestBitmask; - int _group; - bool _positionResetTag; /// To avoid reset the body position when body invoke Node::setPosition(). bool _rotationResetTag; /// To avoid reset the body rotation when body invoke Node::setRotation(). Vec2 _positionOffset; diff --git a/cocos/physics/CCPhysicsShape.h b/cocos/physics/CCPhysicsShape.h index 7f14066595..cc72a828ef 100644 --- a/cocos/physics/CCPhysicsShape.h +++ b/cocos/physics/CCPhysicsShape.h @@ -140,6 +140,11 @@ public: inline void setCollisionBitmask(int bitmask) { _collisionBitmask = bitmask; } inline int getCollisionBitmask() const { return _collisionBitmask; } + /** + * set the group of body + * Collision groups let you specify an integral group index. You can have all fixtures with the same group index always collide (positive index) or never collide (negative index) + * it have high priority than bit masks + */ void setGroup(int group); inline int getGroup() { return _group; }