diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 494f267136..cb1fb62f6c 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -1389,12 +1389,12 @@ void Node::setPhysicsBody(PhysicsBody* body) { if (_physicsBody != nullptr) { - _physicsBody->_owner = nullptr; + _physicsBody->_node = nullptr; _physicsBody->release(); } _physicsBody = body; - _physicsBody->_owner = this; + _physicsBody->_node = this; _physicsBody->retain(); _physicsBody->setPosition(getPosition()); _physicsBody->setRotation(getRotation()); diff --git a/cocos/physics/CCPhysicsBody.cpp b/cocos/physics/CCPhysicsBody.cpp index 70bfe275b8..405687a21b 100644 --- a/cocos/physics/CCPhysicsBody.cpp +++ b/cocos/physics/CCPhysicsBody.cpp @@ -60,7 +60,7 @@ namespace } PhysicsBody::PhysicsBody() -: _owner(nullptr) +: _node(nullptr) , _shapes(nullptr) , _world(nullptr) , _info(nullptr) @@ -570,7 +570,7 @@ void PhysicsBody::setMoment(float moment) } } -PhysicsShape* PhysicsBody::getShapeByTag(int tag) +PhysicsShape* PhysicsBody::getShapeByTag(int tag) const { for (auto child : *_shapes) { @@ -666,7 +666,7 @@ void PhysicsBody::setEnable(bool enable) } } -bool PhysicsBody::isResting() +bool PhysicsBody::isResting() const { return cpBodyIsSleeping(_info->body) == cpTrue; } diff --git a/cocos/physics/CCPhysicsBody.h b/cocos/physics/CCPhysicsBody.h index 6073207dbd..a4bad406b8 100644 --- a/cocos/physics/CCPhysicsBody.h +++ b/cocos/physics/CCPhysicsBody.h @@ -122,12 +122,12 @@ public: /* * @brief get the body shapes. */ - inline Array* getShapes() { return _shapes; } + inline Array* getShapes() const { return _shapes; } /* * @brief get the first body shapes. */ - inline PhysicsShape* getShape() { return _shapes->count() >= 1 ? dynamic_cast(_shapes->getObjectAtIndex(0)) : nullptr; } - PhysicsShape* getShapeByTag(int tag); + inline PhysicsShape* getShape() const { return _shapes->count() >= 1 ? dynamic_cast(_shapes->getObjectAtIndex(0)) : nullptr; } + PhysicsShape* getShapeByTag(int tag) const; /* * @brief remove a shape from body */ @@ -152,7 +152,7 @@ public: /* * @brief get the sprite the body set to. */ - inline Node* getOwner() const { return _owner; } + inline Node* getNode() const { return _node; } void setCategoryBitmask(int bitmask); void setContactTestBitmask(int bitmask); @@ -162,7 +162,7 @@ public: inline int getCollisionBitmask() const { return _collisionBitmask; } void setGroup(int group); - inline int getGroup() { return _group; } + inline int getGroup() const { return _group; } /* * @brief get the body position. @@ -177,7 +177,7 @@ public: * @brief test the body is dynamic or not. * a dynamic body will effect with gravity. */ - inline bool isDynamic() { return _dynamic; } + inline bool isDynamic() const { return _dynamic; } /* * @brief set dynamic to body. * a dynamic body will effect with gravity. @@ -192,7 +192,7 @@ public: /* * @brief get the body mass. */ - inline float getMass() { return _mass; } + inline float getMass() const { return _mass; } /* * @brief add mass to body. * if _mass(mass of the body) == PHYSICS_INFINITY, it remains. @@ -211,7 +211,7 @@ public: /* * @brief get the body moment of inertia. */ - inline float getMoment(float moment) { return _moment; } + inline float getMoment(float moment) const { return _moment; } /* * @brief add moment of inertia to body. * if _moment(moment of the body) == PHYSICS_INFINITY, it remains. @@ -228,25 +228,25 @@ public: /* * @brief get angular damping. */ - inline float getLinearDamping() { return _linearDamping; } + inline float getLinearDamping() const { return _linearDamping; } inline void setLinearDamping(float damping) { _linearDamping = damping; } - inline float getAngularDamping() { return _angularDamping; } + inline float getAngularDamping() const { return _angularDamping; } inline void setAngularDamping(float damping) { _angularDamping = damping; } //virtual Clonable* clone() const override; - bool isResting(); - inline bool isEnable() { return _enable; } + bool isResting() const; + inline bool isEnable() const { return _enable; } void setEnable(bool enable); - inline bool isRotationEnable() { return _rotationEnable; } + inline bool isRotationEnable() const { return _rotationEnable; } void setRotationEnable(bool enable); - inline bool isGravityEnable() { return _gravityEnable; } + inline bool isGravityEnable() const { return _gravityEnable; } void setGravityEnable(bool enable); - inline int getTag() { return _tag; } + inline int getTag() const { return _tag; } inline void setTag(int tag) { _tag = tag; } Point world2Local(const Point& point); @@ -266,7 +266,7 @@ protected: virtual ~PhysicsBody(); protected: - Node* _owner; + Node* _node; std::vector _joints; Array* _shapes; PhysicsWorld* _world; diff --git a/cocos/physics/CCPhysicsContact.cpp b/cocos/physics/CCPhysicsContact.cpp index 744302cb46..1d0cfd8bdd 100644 --- a/cocos/physics/CCPhysicsContact.cpp +++ b/cocos/physics/CCPhysicsContact.cpp @@ -41,6 +41,8 @@ NS_CC_BEGIN +const char* PHYSICSCONTACT_EVENT_NAME = "PhysicsContactEvent"; + PhysicsContact::PhysicsContact() : Event(Event::Type::CUSTOM) , _world(nullptr) @@ -100,7 +102,7 @@ void PhysicsContact::generateContactData() return; } - cpArbiter* arb = (cpArbiter*)_contactInfo; + cpArbiter* arb = static_cast(_contactInfo); _contactData = new PhysicsContactData(); _contactData->count = cpArbiterGetCount(arb); for (int i=0; i<_contactData->count; ++i) @@ -118,34 +120,34 @@ PhysicsContactPreSolve::PhysicsContactPreSolve(PhysicsContactData* data, void* c { } -float PhysicsContactPreSolve::getElasticity() +float PhysicsContactPreSolve::getElasticity() const { - return ((cpArbiter*)_contactInfo)->e; + return static_cast(_contactInfo)->e; } -float PhysicsContactPreSolve::getFriciton() +float PhysicsContactPreSolve::getFriciton() const { - return ((cpArbiter*)_contactInfo)->u; + return static_cast(_contactInfo)->u; } -Point PhysicsContactPreSolve::getSurfaceVelocity() +Point PhysicsContactPreSolve::getSurfaceVelocity() const { - return PhysicsHelper::cpv2point(((cpArbiter*)_contactInfo)->surface_vr); + return PhysicsHelper::cpv2point(static_cast(_contactInfo)->surface_vr); } void PhysicsContactPreSolve::setElasticity(float elasticity) { - ((cpArbiter*)_contactInfo)->e = elasticity; + static_cast(_contactInfo)->e = elasticity; } void PhysicsContactPreSolve::setFriction(float friction) { - ((cpArbiter*)_contactInfo)->u = friction; + static_cast(_contactInfo)->u = friction; } void PhysicsContactPreSolve::setSurfaceVelocity(Point surfaceVelocity) { - ((cpArbiter*)_contactInfo)->surface_vr = PhysicsHelper::point2cpv(surfaceVelocity); + static_cast(_contactInfo)->surface_vr = PhysicsHelper::point2cpv(surfaceVelocity); } PhysicsContactPreSolve::~PhysicsContactPreSolve() @@ -165,19 +167,19 @@ PhysicsContactPostSolve::~PhysicsContactPostSolve() } -float PhysicsContactPostSolve::getElasticity() +float PhysicsContactPostSolve::getElasticity() const { - return ((cpArbiter*)_contactInfo)->e; + return static_cast(_contactInfo)->e; } -float PhysicsContactPostSolve::getFriciton() +float PhysicsContactPostSolve::getFriciton() const { - return ((cpArbiter*)_contactInfo)->u; + return static_cast(_contactInfo)->u; } -Point PhysicsContactPostSolve::getSurfaceVelocity() +Point PhysicsContactPostSolve::getSurfaceVelocity() const { - return PhysicsHelper::cpv2point(((cpArbiter*)_contactInfo)->surface_vr); + return PhysicsHelper::cpv2point(static_cast(_contactInfo)->surface_vr); } EventListenerPhysicsContact::EventListenerPhysicsContact() diff --git a/cocos/physics/CCPhysicsContact.h b/cocos/physics/CCPhysicsContact.h index a4b0454e37..32127da7a5 100644 --- a/cocos/physics/CCPhysicsContact.h +++ b/cocos/physics/CCPhysicsContact.h @@ -81,22 +81,22 @@ public: /* * @brief get data. */ - inline void* getData() { return _data; } + inline void* getData() const { return _data; } /* * @brief set data to contact. you must manage the memory yourself, Generally you can set data at contact begin, and distory it at contact end. */ inline void setData(void* data) { _data = data; } - EventCode getEventCode() { return _eventCode; }; + EventCode getEventCode() const { return _eventCode; }; private: static PhysicsContact* create(PhysicsShape* a, PhysicsShape* b); bool init(PhysicsShape* a, PhysicsShape* b); void setEventCode(EventCode eventCode) { _eventCode = eventCode; }; - inline bool getNotify() { return _notify; } + inline bool getNotify() const { return _notify; } inline void setNotify(bool notify) { _notify = notify; } - inline PhysicsWorld* getWorld() { return _world; } + inline PhysicsWorld* getWorld() const { return _world; } inline void setWorld(PhysicsWorld* world) { _world = world; } inline void setResult(bool result) { _result = result; } inline bool resetResult() { bool ret = _result; _result = true; return ret; } @@ -133,9 +133,9 @@ class PhysicsContactPreSolve { public: // getter/setter - float getElasticity(); - float getFriciton(); - Point getSurfaceVelocity(); + float getElasticity() const; + float getFriciton() const; + Point getSurfaceVelocity() const; void setElasticity(float elasticity); void setFriction(float friction); void setSurfaceVelocity(Point surfaceVelocity); @@ -161,9 +161,9 @@ class PhysicsContactPostSolve { public: // getter - float getElasticity(); - float getFriciton(); - Point getSurfaceVelocity(); + float getElasticity() const; + float getFriciton() const; + Point getSurfaceVelocity() const; private: PhysicsContactPostSolve(void* contactInfo); @@ -175,8 +175,6 @@ private: friend class EventListenerPhysicsContact; }; -static const char* PHYSICSCONTACT_EVENT_NAME = "PhysicsContactEvent"; - /* * @brief contact listener. */ @@ -222,7 +220,7 @@ class EventListenerPhysicsContactWithBodies : public EventListenerPhysicsContact public: static EventListenerPhysicsContactWithBodies* create(PhysicsBody* bodyA, PhysicsBody* bodyB); - virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB); + virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB) override; virtual EventListenerPhysicsContactWithBodies* clone() override; protected: @@ -239,7 +237,7 @@ class EventListenerPhysicsContactWithShapes : public EventListenerPhysicsContact public: static EventListenerPhysicsContactWithShapes* create(PhysicsShape* shapeA, PhysicsShape* shapeB); - virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB); + virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB) override; virtual EventListenerPhysicsContactWithShapes* clone() override; protected: @@ -256,7 +254,7 @@ class EventListenerPhysicsContactWithGroup : public EventListenerPhysicsContact public: static EventListenerPhysicsContactWithGroup* create(int group); - virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB); + virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB) override; virtual EventListenerPhysicsContactWithGroup* clone() override; protected: diff --git a/cocos/physics/CCPhysicsJoint.cpp b/cocos/physics/CCPhysicsJoint.cpp index e35b8d241c..99fed53d8b 100644 --- a/cocos/physics/CCPhysicsJoint.cpp +++ b/cocos/physics/CCPhysicsJoint.cpp @@ -155,14 +155,14 @@ PhysicsJointDistance::~PhysicsJointDistance() } #if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) -PhysicsBodyInfo* PhysicsJoint::bodyInfo(PhysicsBody* body) const +PhysicsBodyInfo* PhysicsJoint::getBodyInfo(PhysicsBody* body) const { return body->_info; } -Node* PhysicsJoint::bodyOwner(PhysicsBody* body) const +Node* PhysicsJoint::getBodyNode(PhysicsBody* body) const { - return body->_owner; + return body->_node; } @@ -198,17 +198,17 @@ bool PhysicsJointFixed::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr) { CC_BREAK_IF(!PhysicsJoint::init(a, b)); - bodyOwner(a)->setPosition(anchr); - bodyOwner(b)->setPosition(anchr); + getBodyNode(a)->setPosition(anchr); + getBodyNode(b)->setPosition(anchr); // add a pivot joint to fixed two body together - cpConstraint* joint = cpPivotJointNew(bodyInfo(a)->body, bodyInfo(b)->body, + cpConstraint* joint = cpPivotJointNew(getBodyInfo(a)->body, getBodyInfo(b)->body, PhysicsHelper::point2cpv(anchr)); CC_BREAK_IF(joint == nullptr); _info->add(joint); // add a gear joint to make two body have the same rotation. - joint = cpGearJointNew(bodyInfo(a)->body, bodyInfo(b)->body, 0, 1); + joint = cpGearJointNew(getBodyInfo(a)->body, getBodyInfo(b)->body, 0, 1); CC_BREAK_IF(joint == nullptr); _info->add(joint); @@ -238,7 +238,7 @@ bool PhysicsJointPin::init(PhysicsBody *a, PhysicsBody *b, const Point& anchr) do { CC_BREAK_IF(!PhysicsJoint::init(a, b)); - cpConstraint* joint = cpPivotJointNew(bodyInfo(a)->body, bodyInfo(b)->body, + cpConstraint* joint = cpPivotJointNew(getBodyInfo(a)->body, getBodyInfo(b)->body, PhysicsHelper::point2cpv(anchr)); CC_BREAK_IF(joint == nullptr); @@ -258,7 +258,7 @@ void PhysicsJointPin::setMaxForce(float force) _info->joints.front()->maxForce = PhysicsHelper::float2cpfloat(force); } -float PhysicsJointPin::getMaxForce() +float PhysicsJointPin::getMaxForce() const { return PhysicsHelper::cpfloat2float(_info->joints.front()->maxForce); } @@ -282,7 +282,7 @@ bool PhysicsJointSliding::init(PhysicsBody* a, PhysicsBody* b, const Point& groo { CC_BREAK_IF(!PhysicsJoint::init(a, b)); - cpConstraint* joint = cpGrooveJointNew(bodyInfo(a)->body, bodyInfo(b)->body, + cpConstraint* joint = cpGrooveJointNew(getBodyInfo(a)->body, getBodyInfo(b)->body, PhysicsHelper::point2cpv(grooveA), PhysicsHelper::point2cpv(grooveB), PhysicsHelper::point2cpv(anchr)); @@ -317,7 +317,7 @@ bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1 { CC_BREAK_IF(!PhysicsJoint::init(a, b)); - cpConstraint* joint = cpSlideJointNew(bodyInfo(a)->body, bodyInfo(b)->body, + cpConstraint* joint = cpSlideJointNew(getBodyInfo(a)->body, getBodyInfo(b)->body, PhysicsHelper::point2cpv(anchr1), PhysicsHelper::point2cpv(anchr2), 0, @@ -333,7 +333,7 @@ bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1 return false; } -float PhysicsJointLimit::getMin() +float PhysicsJointLimit::getMin() const { return PhysicsHelper::cpfloat2float(cpSlideJointGetMin(_info->joints.front())); } @@ -343,7 +343,7 @@ void PhysicsJointLimit::setMin(float min) cpSlideJointSetMin(_info->joints.front(), PhysicsHelper::float2cpfloat(min)); } -float PhysicsJointLimit::getMax() +float PhysicsJointLimit::getMax() const { return PhysicsHelper::cpfloat2float(cpSlideJointGetMax(_info->joints.front())); } @@ -372,8 +372,8 @@ bool PhysicsJointDistance::init(PhysicsBody* a, PhysicsBody* b, const Point& anc { CC_BREAK_IF(!PhysicsJoint::init(a, b)); - cpConstraint* joint = cpPinJointNew(bodyInfo(a)->body, - bodyInfo(b)->body, + cpConstraint* joint = cpPinJointNew(getBodyInfo(a)->body, + getBodyInfo(b)->body, PhysicsHelper::point2cpv(anchr1), PhysicsHelper::point2cpv(anchr2)); CC_BREAK_IF(joint == nullptr); diff --git a/cocos/physics/CCPhysicsJoint.h b/cocos/physics/CCPhysicsJoint.h index 298992c5f3..2970d992d0 100644 --- a/cocos/physics/CCPhysicsJoint.h +++ b/cocos/physics/CCPhysicsJoint.h @@ -47,13 +47,13 @@ protected: virtual ~PhysicsJoint() = 0; public: - PhysicsBody* getBodyA() { return _bodyA; } - PhysicsBody* getBodyB() { return _bodyB; } - inline int getTag() { return _tag; } + PhysicsBody* getBodyA() const { return _bodyA; } + PhysicsBody* getBodyB() const { return _bodyB; } + inline int getTag() const { return _tag; } inline void setTag(int tag) { _tag = tag; } - inline bool isEnable() { return _enable; } + inline bool isEnable() const { return _enable; } void setEnable(bool enable); - inline bool isCollisionEnable() { return _collisionEnable; } + inline bool isCollisionEnable() const { return _collisionEnable; } void setCollisionEnable(bool enable); protected: @@ -62,8 +62,8 @@ protected: /** * PhysicsShape is PhysicsBody's friend class, but all the subclasses isn't. so this method is use for subclasses to catch the bodyInfo from PhysicsBody. */ - PhysicsBodyInfo* bodyInfo(PhysicsBody* body) const; - Node* bodyOwner(PhysicsBody* body) const; + PhysicsBodyInfo* getBodyInfo(PhysicsBody* body) const; + Node* getBodyNode(PhysicsBody* body) const; protected: PhysicsBody* _bodyA; @@ -133,9 +133,9 @@ class PhysicsJointLimit : public PhysicsJoint public: PhysicsJointLimit* create(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2); - float getMin(); + float getMin() const; void setMin(float min); - float getMax(); + float getMax() const; void setMax(float max); protected: @@ -155,7 +155,7 @@ public: static PhysicsJointPin* create(PhysicsBody* a, PhysicsBody* b, const Point& anchr); void setMaxForce(float force); - float getMaxForce(); + float getMaxForce() const; protected: bool init(PhysicsBody* a, PhysicsBody* b, const Point& anchr); diff --git a/cocos/physics/CCPhysicsShape.cpp b/cocos/physics/CCPhysicsShape.cpp index bb17c54501..68ef1cf351 100644 --- a/cocos/physics/CCPhysicsShape.cpp +++ b/cocos/physics/CCPhysicsShape.cpp @@ -353,7 +353,7 @@ float PhysicsShapeCircle::calculateDefaultMoment() cpCircleShapeGetOffset(shape))); } -float PhysicsShapeCircle::getRadius() +float PhysicsShapeCircle::getRadius() const { return PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(_info->shapes.front())); } @@ -405,12 +405,12 @@ bool PhysicsShapeEdgeSegment::init(Point a, Point b, PhysicsMaterial material/* return false; } -Point PhysicsShapeEdgeSegment::getPointA() +Point PhysicsShapeEdgeSegment::getPointA() const { return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->shapes.front()))->ta); } -Point PhysicsShapeEdgeSegment::getPointB() +Point PhysicsShapeEdgeSegment::getPointB() const { return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->shapes.front()))->tb); } @@ -503,7 +503,7 @@ float PhysicsShapeBox::calculateDefaultMoment() : PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero)); } -Point* PhysicsShapeBox::getPoints(Point* points) +Point* PhysicsShapeBox::getPoints(Point* points) const { cpShape* shape = _info->shapes.front(); return PhysicsHelper::cpvs2points(((cpPolyShape*)shape)->verts, points, ((cpPolyShape*)shape)->numVerts); @@ -511,7 +511,7 @@ Point* PhysicsShapeBox::getPoints(Point* points) return points; } -Size PhysicsShapeBox::getSize() +Size PhysicsShapeBox::getSize() const { cpShape* shape = _info->shapes.front(); return PhysicsHelper::cpv2size(cpv(cpvdist(cpPolyShapeGetVert(shape, 0), cpPolyShapeGetVert(shape, 1)), @@ -594,13 +594,13 @@ float PhysicsShapePolygon::calculateDefaultMoment() : PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero)); } -Point* PhysicsShapePolygon::getPoints(Point* points) +Point* PhysicsShapePolygon::getPoints(Point* points) const { cpShape* shape = _info->shapes.front(); return PhysicsHelper::cpvs2points(((cpPolyShape*)shape)->verts, points, ((cpPolyShape*)shape)->numVerts); } -int PhysicsShapePolygon::getPointsCount() +int PhysicsShapePolygon::getPointsCount() const { return ((cpPolyShape*)_info->shapes.front())->numVerts; } @@ -715,7 +715,7 @@ Point PhysicsShapeEdgePolygon::getCenter() return _center; } -int PhysicsShapeEdgePolygon::getPointsCount() +int PhysicsShapeEdgePolygon::getPointsCount() const { return _info->shapes.size() + 1; } @@ -776,7 +776,7 @@ Point PhysicsShapeEdgeChain::getCenter() return _center; } -int PhysicsShapeEdgeChain::getPointsCount() +int PhysicsShapeEdgeChain::getPointsCount() const { return _info->shapes.size() + 1; } diff --git a/cocos/physics/CCPhysicsShape.h b/cocos/physics/CCPhysicsShape.h index 8bc9224bf1..31497d14a2 100644 --- a/cocos/physics/CCPhysicsShape.h +++ b/cocos/physics/CCPhysicsShape.h @@ -156,8 +156,8 @@ public: float calculateDefaultArea() override; float calculateDefaultMoment() override; - float getRadius(); - Point getOffset(); + float getRadius() const; + Point getOffset() override; protected: bool init(float radius, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0)); @@ -177,8 +177,8 @@ public: float calculateDefaultArea() override; float calculateDefaultMoment() override; - Point* getPoints(Point* points); - Size getSize(); + Point* getPoints(Point* points) const; + Size getSize() const; Point getOffset() override { return _offset; } protected: @@ -203,8 +203,8 @@ public: float calculateDefaultArea() override; float calculateDefaultMoment() override; - Point* getPoints(Point* points); - int getPointsCount(); + Point* getPoints(Point* points) const; + int getPointsCount() const; Point getCenter() override; protected: bool init(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0)); @@ -223,8 +223,8 @@ class PhysicsShapeEdgeSegment : public PhysicsShape public: static PhysicsShapeEdgeSegment* create(Point a, Point b, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); - Point getPointA(); - Point getPointB(); + Point getPointA() const; + Point getPointB() const; Point getCenter() override; protected: @@ -246,8 +246,8 @@ class PhysicsShapeEdgeBox : public PhysicsShape public: static PhysicsShapeEdgeBox* create(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 0, Point offset = Point(0, 0)); Point getOffset() override { return _offset; } - Point* getPoints(Point* points); - int getPointsCount(); + Point* getPoints(Point* points) const; + int getPointsCount() const; protected: bool init(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1, Point offset = Point(0, 0)); @@ -268,8 +268,8 @@ class PhysicsShapeEdgePolygon : public PhysicsShape public: static PhysicsShapeEdgePolygon* create(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); Point getCenter() override; - Point* getPoints(Point* points); - int getPointsCount(); + Point* getPoints(Point* points) const; + int getPointsCount() const; protected: bool init(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); @@ -290,8 +290,8 @@ class PhysicsShapeEdgeChain : public PhysicsShape public: static PhysicsShapeEdgeChain* create(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); Point getCenter() override; - Point* getPoints(Point* points); - int getPointsCount(); + Point* getPoints(Point* points) const; + int getPointsCount() const; protected: bool init(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); diff --git a/cocos/physics/CCPhysicsWorld.cpp b/cocos/physics/CCPhysicsWorld.cpp index 58bf2ecec3..1cd2b936c5 100644 --- a/cocos/physics/CCPhysicsWorld.cpp +++ b/cocos/physics/CCPhysicsWorld.cpp @@ -60,6 +60,8 @@ NS_CC_BEGIN +extern const char* PHYSICSCONTACT_EVENT_NAME; + #if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) const float PHYSICS_INFINITY = INFINITY; @@ -269,7 +271,8 @@ PhysicsShape* PhysicsWorld::addShape(PhysicsShape* shape) if (cpBodyIsStatic(shape->getBody()->_info->body)) { cpSpaceAddStaticShape(_info->space, cps); - }else + } + else { cpSpaceAddShape(_info->space, cps); } @@ -473,7 +476,8 @@ void PhysicsWorld::drawWithJoint(DrawNode* node, PhysicsJoint* joint) cpBody *body_b = constraint->b; const cpConstraintClass *klass = constraint->klass_private; - if(klass == cpPinJointGetClass()){ + if(klass == cpPinJointGetClass()) + { cpPinJoint *joint = (cpPinJoint *)constraint; cpVect a = cpvadd(body_a->p, cpvrotate(joint->anchr1, body_a->rot)); @@ -482,7 +486,9 @@ void PhysicsWorld::drawWithJoint(DrawNode* node, PhysicsJoint* joint) node->drawSegment(PhysicsHelper::cpv2point(a), PhysicsHelper::cpv2point(b), 1, Color4F(0.0f, 0.0f, 1.0f, 1.0f)); node->drawDot(PhysicsHelper::cpv2point(a), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f)); node->drawDot(PhysicsHelper::cpv2point(b), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f)); - } else if(klass == cpSlideJointGetClass()){ + } + else if(klass == cpSlideJointGetClass()) + { cpSlideJoint *joint = (cpSlideJoint *)constraint; cpVect a = cpvadd(body_a->p, cpvrotate(joint->anchr1, body_a->rot)); @@ -491,7 +497,9 @@ void PhysicsWorld::drawWithJoint(DrawNode* node, PhysicsJoint* joint) node->drawSegment(PhysicsHelper::cpv2point(a), PhysicsHelper::cpv2point(b), 1, Color4F(0.0f, 0.0f, 1.0f, 1.0f)); node->drawDot(PhysicsHelper::cpv2point(a), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f)); node->drawDot(PhysicsHelper::cpv2point(b), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f)); - } else if(klass == cpPivotJointGetClass()){ + } + else if(klass == cpPivotJointGetClass()) + { cpPivotJoint *joint = (cpPivotJoint *)constraint; cpVect a = cpvadd(body_a->p, cpvrotate(joint->anchr1, body_a->rot)); @@ -499,7 +507,9 @@ void PhysicsWorld::drawWithJoint(DrawNode* node, PhysicsJoint* joint) node->drawDot(PhysicsHelper::cpv2point(a), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f)); node->drawDot(PhysicsHelper::cpv2point(b), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f)); - } else if(klass == cpGrooveJointGetClass()){ + } + else if(klass == cpGrooveJointGetClass()) + { cpGrooveJoint *joint = (cpGrooveJoint *)constraint; cpVect a = cpvadd(body_a->p, cpvrotate(joint->grv_a, body_a->rot)); @@ -508,7 +518,9 @@ void PhysicsWorld::drawWithJoint(DrawNode* node, PhysicsJoint* joint) node->drawSegment(PhysicsHelper::cpv2point(a), PhysicsHelper::cpv2point(b), 1, Color4F(0.0f, 0.0f, 1.0f, 1.0f)); node->drawDot(PhysicsHelper::cpv2point(c), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f)); - } else if(klass == cpDampedSpringGetClass()){ + } + else if(klass == cpDampedSpringGetClass()) + { } } @@ -607,7 +619,8 @@ int PhysicsWorld::collisionBeginCallback(PhysicsContact& contact) if (shapeA->getGroup() != 0 && shapeA->getGroup() == shapeB->getGroup()) { ret = shapeA->getGroup() > 0; - }else + } + else { if ((shapeA->getCategoryBitmask() & shapeB->getCollisionBitmask()) == 0 || (shapeB->getCategoryBitmask() & shapeA->getCollisionBitmask()) == 0) @@ -627,6 +640,11 @@ int PhysicsWorld::collisionBeginCallback(PhysicsContact& contact) int PhysicsWorld::collisionPreSolveCallback(PhysicsContact& contact) { + if (!contact.getNotify()) + { + return true; + } + contact.setEventCode(PhysicsContact::EventCode::PRESOLVE); contact.setWorld(this); EventCustom event(PHYSICSCONTACT_EVENT_NAME); @@ -638,6 +656,11 @@ int PhysicsWorld::collisionPreSolveCallback(PhysicsContact& contact) void PhysicsWorld::collisionPostSolveCallback(PhysicsContact& contact) { + if (!contact.getNotify()) + { + return; + } + contact.setEventCode(PhysicsContact::EventCode::POSTSOLVE); contact.setWorld(this); EventCustom event(PHYSICSCONTACT_EVENT_NAME); @@ -647,6 +670,11 @@ void PhysicsWorld::collisionPostSolveCallback(PhysicsContact& contact) void PhysicsWorld::collisionSeparateCallback(PhysicsContact& contact) { + if (!contact.getNotify()) + { + return; + } + contact.setEventCode(PhysicsContact::EventCode::SEPERATE); contact.setWorld(this); EventCustom event(PHYSICSCONTACT_EVENT_NAME); @@ -680,7 +708,7 @@ void PhysicsWorld::rayCast(PhysicsRayCastCallback& callback, Point point1, Point { if (callback.report != nullptr) { - RayCastCallbackInfo info = {this, &callback, point1, point2, data}; + RayCastCallbackInfo info = { this, &callback, point1, point2, data }; PhysicsWorldCallback::continues = true; cpSpaceSegmentQuery(this->_info->space, @@ -710,7 +738,7 @@ void PhysicsWorld::rectQuery(PhysicsRectQueryCallback& callback, Rect rect, void } } -Array* PhysicsWorld::getShapesAtPoint(Point point) +Array* PhysicsWorld::getShapesAtPoint(Point point) const { Array* arr = Array::create(); cpSpaceNearestPointQuery(this->_info->space, @@ -724,7 +752,7 @@ Array* PhysicsWorld::getShapesAtPoint(Point point) return arr; } -PhysicsShape* PhysicsWorld::getShapeAtPoint(Point point) +PhysicsShape* PhysicsWorld::getShapeAtPoint(Point point) const { cpShape* shape = cpSpaceNearestPointQueryNearest(this->_info->space, PhysicsHelper::point2cpv(point), @@ -741,7 +769,7 @@ Array* PhysicsWorld::getAllBodies() const return _bodies; } -PhysicsBody* PhysicsWorld::getBodyByTag(int tag) +PhysicsBody* PhysicsWorld::getBodyByTag(int tag) const { for (auto body : *_bodies) { diff --git a/cocos/physics/CCPhysicsWorld.h b/cocos/physics/CCPhysicsWorld.h index 6a7095da31..e719dce2c4 100644 --- a/cocos/physics/CCPhysicsWorld.h +++ b/cocos/physics/CCPhysicsWorld.h @@ -94,24 +94,24 @@ public: void rayCast(PhysicsRayCastCallback& callback, Point point1, Point point2, void* data); void rectQuery(PhysicsRectQueryCallback& callback, Rect rect, void* data); - Array* getShapesAtPoint(Point point); - PhysicsShape* getShapeAtPoint(Point point); + Array* getShapesAtPoint(Point point) const; + PhysicsShape* getShapeAtPoint(Point point) const; Array* getAllBodies() const; - PhysicsBody* getBodyByTag(int tag); + PhysicsBody* getBodyByTag(int tag) const; /** Register a listener to receive contact callbacks*/ //inline void registerContactListener(EventListenerPhysicsContact* delegate) { _listener = delegate; } /** Unregister a listener. */ //inline void unregisterContactListener() { _listener = nullptr; } - inline Scene& getScene() { return *_scene; } + inline Scene& getScene() const { return *_scene; } /** get the gravity value */ - inline Point getGravity() { return _gravity; } + inline Point getGravity() const { return _gravity; } /** set the gravity value */ void setGravity(Point gravity); /** test the debug draw is enabled */ - inline bool isDebugDraw() { return _debugDraw; } + inline bool isDebugDraw() const { return _debugDraw; } /** set the debug draw */ inline void setDebugDraw(bool debugDraw) { _debugDraw = debugDraw; } diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp index f36b11e7d5..db51829b3f 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp @@ -13,6 +13,7 @@ namespace CL(PhysicsDemoJoints), CL(PhysicsDemoActions), CL(PhysicsDemoPump), + CL(PhysicsDemoOneWayPlatform), }; static int sceneIdx=-1; @@ -974,7 +975,7 @@ void PhysicsDemoPump::update(float delta) PhysicsBody* body = dynamic_cast(obj); if (body->getTag() == DRAG_BODYS_TAG && body->getPosition().y < 0.0f) { - body->getOwner()->setPosition(VisibleRect::leftTop() + Point(75 + CCRANDOM_0_1() * 90, 0)); + body->getNode()->setPosition(VisibleRect::leftTop() + Point(75 + CCRANDOM_0_1() * 90, 0)); body->setVelocity(Point(0, 0)); } } @@ -992,7 +993,7 @@ void PhysicsDemoPump::update(float delta) } gear->setAngularVelocity(_rotationV); - _rotationV *= 0.995; + _rotationV *= 0.995f; } } @@ -1039,11 +1040,13 @@ void PhysicsDemoOneWayPlatform::onEnter() this->addChild(ground); auto platform = Node::create(); - platform->setPhysicsBody(PhysicsBody::createEdgeBox(Size(200, 50))); + platform->setPhysicsBody(PhysicsBody::createBox(Size(200, 50))); + platform->getPhysicsBody()->setDynamic(false); platform->setPosition(VisibleRect::center()); this->addChild(platform); - auto ball = makeBall(VisibleRect::center() + Point(0, 50), 5); + auto ball = makeBall(VisibleRect::center() - Point(0, 50), 5); + ball->getPhysicsBody()->setVelocity(Point(0, 200)); this->addChild(ball); auto contactListener = EventListenerPhysicsContactWithBodies::create(platform->getPhysicsBody(), ball->getPhysicsBody());