diff --git a/cocos/base/CCGeometry.cpp b/cocos/base/CCGeometry.cpp index 1d5c4896c6..8ab9e39dda 100644 --- a/cocos/base/CCGeometry.cpp +++ b/cocos/base/CCGeometry.cpp @@ -96,6 +96,16 @@ bool Point::operator!=(const Point& right) return this->x != right.x || this->y != right.y; } +bool Point::operator==(const Point& right) const +{ + return this->x == right.x && this->y == right.y; +} + +bool Point::operator!=(const Point& right) const +{ + return this->x != right.x || this->y != right.y; +} + Point Point::operator*(float a) const { return Point(this->x * a, this->y * a); diff --git a/cocos/base/CCGeometry.h b/cocos/base/CCGeometry.h index 847557cb35..3b6b8c2655 100644 --- a/cocos/base/CCGeometry.h +++ b/cocos/base/CCGeometry.h @@ -123,6 +123,16 @@ public: * @lua NA */ bool operator!=(const Point& right); + /** + * @js NA + * @lua NA + */ + bool operator==(const Point& right) const; + /** + * @js NA + * @lua NA + */ + bool operator!=(const Point& right) const; /** * @js NA * @lua NA diff --git a/cocos/physics/CCPhysicsBody.cpp b/cocos/physics/CCPhysicsBody.cpp index 8a177e6252..131bfdc3c5 100644 --- a/cocos/physics/CCPhysicsBody.cpp +++ b/cocos/physics/CCPhysicsBody.cpp @@ -157,7 +157,7 @@ PhysicsBody* PhysicsBody::create(float mass, float moment) } -PhysicsBody* PhysicsBody::createCircle(float radius, PhysicsMaterial material, Point offset) +PhysicsBody* PhysicsBody::createCircle(float radius, const PhysicsMaterial& material, const Point& offset) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) @@ -171,7 +171,7 @@ PhysicsBody* PhysicsBody::createCircle(float radius, PhysicsMaterial material, P return nullptr; } -PhysicsBody* PhysicsBody::createBox(Size size, PhysicsMaterial material, Point offset) +PhysicsBody* PhysicsBody::createBox(const Size& size, const PhysicsMaterial& material, const Point& offset) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) @@ -185,7 +185,7 @@ PhysicsBody* PhysicsBody::createBox(Size size, PhysicsMaterial material, Point o return nullptr; } -PhysicsBody* PhysicsBody::createPolygon(Point* points, int count, PhysicsMaterial material, Point offset) +PhysicsBody* PhysicsBody::createPolygon(const Point* points, int count, const PhysicsMaterial& material, const Point& offset) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) @@ -199,7 +199,7 @@ PhysicsBody* PhysicsBody::createPolygon(Point* points, int count, PhysicsMateria return nullptr; } -PhysicsBody* PhysicsBody::createEdgeSegment(Point a, Point b, PhysicsMaterial material, float border/* = 1*/) +PhysicsBody* PhysicsBody::createEdgeSegment(const Point& a, const Point& b, const PhysicsMaterial& material, float border/* = 1*/) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) @@ -214,7 +214,7 @@ PhysicsBody* PhysicsBody::createEdgeSegment(Point a, Point b, PhysicsMaterial ma return nullptr; } -PhysicsBody* PhysicsBody::createEdgeBox(Size size, PhysicsMaterial material, float border/* = 1*/, Point offset) +PhysicsBody* PhysicsBody::createEdgeBox(const Size& size, const PhysicsMaterial& material, float border/* = 1*/, const Point& offset) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) @@ -230,7 +230,7 @@ PhysicsBody* PhysicsBody::createEdgeBox(Size size, PhysicsMaterial material, flo return nullptr; } -PhysicsBody* PhysicsBody::createEdgePolygon(Point* points, int count, PhysicsMaterial material, float border/* = 1*/) +PhysicsBody* PhysicsBody::createEdgePolygon(const Point* points, int count, const PhysicsMaterial& material, float border/* = 1*/) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) @@ -246,7 +246,7 @@ PhysicsBody* PhysicsBody::createEdgePolygon(Point* points, int count, PhysicsMat return nullptr; } -PhysicsBody* PhysicsBody::createEdgeChain(Point* points, int count, PhysicsMaterial material, float border/* = 1*/) +PhysicsBody* PhysicsBody::createEdgeChain(const Point* points, int count, const PhysicsMaterial& material, float border/* = 1*/) { PhysicsBody* body = new PhysicsBody(); if (body && body->init()) @@ -272,9 +272,9 @@ bool PhysicsBody::init() CC_BREAK_IF(_shapes == nullptr); _shapes->retain(); - _info->body = cpBodyNew(PhysicsHelper::float2cpfloat(_mass), PhysicsHelper::float2cpfloat(_moment)); + _info->setBody(cpBodyNew(PhysicsHelper::float2cpfloat(_mass), PhysicsHelper::float2cpfloat(_moment))); - CC_BREAK_IF(_info->body == nullptr); + CC_BREAK_IF(_info->getBody() == nullptr); return true; } while (false); @@ -289,19 +289,19 @@ void PhysicsBody::setDynamic(bool dynamic) _dynamic = dynamic; if (dynamic) { - cpBodySetMass(_info->body, _mass); + cpBodySetMass(_info->getBody(), _mass); if (_world != nullptr) { - cpSpaceAddBody(_world->_info->space, _info->body); + cpSpaceAddBody(_world->_info->getSpace(), _info->getBody()); } }else { - cpBodySetMass(_info->body, PHYSICS_INFINITY); + cpBodySetMass(_info->getBody(), PHYSICS_INFINITY); if (_world != nullptr) { - cpSpaceRemoveBody(_world->_info->space, _info->body); + cpSpaceRemoveBody(_world->_info->getSpace(), _info->getBody()); } } @@ -312,7 +312,7 @@ void PhysicsBody::setRotationEnable(bool enable) { if (_rotationEnable != enable) { - cpBodySetMoment(_info->body, enable ? _moment : PHYSICS_INFINITY); + cpBodySetMoment(_info->getBody(), enable ? _moment : PHYSICS_INFINITY); _rotationEnable = enable; } } @@ -338,23 +338,23 @@ void PhysicsBody::setGravityEnable(bool enable) void PhysicsBody::setPosition(Point position) { - cpBodySetPos(_info->body, PhysicsHelper::point2cpv(position)); + cpBodySetPos(_info->getBody(), PhysicsHelper::point2cpv(position)); } void PhysicsBody::setRotation(float rotation) { - cpBodySetAngle(_info->body, PhysicsHelper::float2cpfloat(rotation)); + cpBodySetAngle(_info->getBody(), PhysicsHelper::float2cpfloat(rotation)); } Point PhysicsBody::getPosition() const { - cpVect vec = cpBodyGetPos(_info->body); + cpVect vec = cpBodyGetPos(_info->getBody()); return PhysicsHelper::cpv2point(vec); } float PhysicsBody::getRotation() const { - return -PhysicsHelper::cpfloat2float(cpBodyGetAngle(_info->body) / 3.14f * 180.0f); + return -PhysicsHelper::cpfloat2float(cpBodyGetAngle(_info->getBody()) / 3.14f * 180.0f); } PhysicsShape* PhysicsBody::addShape(PhysicsShape* shape) @@ -388,29 +388,29 @@ PhysicsShape* PhysicsBody::addShape(PhysicsShape* shape) return shape; } -void PhysicsBody::applyForce(Point force) +void PhysicsBody::applyForce(const Vect& force) { applyForce(force, Point::ZERO); } -void PhysicsBody::applyForce(Point force, Point offset) +void PhysicsBody::applyForce(const Vect& force, const Point& offset) { - cpBodyApplyForce(_info->body, PhysicsHelper::point2cpv(force), PhysicsHelper::point2cpv(offset)); + cpBodyApplyForce(_info->getBody(), PhysicsHelper::point2cpv(force), PhysicsHelper::point2cpv(offset)); } -void PhysicsBody::applyImpulse(Point impulse) +void PhysicsBody::applyImpulse(const Vect& impulse) { applyImpulse(impulse, Point()); } -void PhysicsBody::applyImpulse(Point impulse, Point offset) +void PhysicsBody::applyImpulse(const Vect& impulse, const Point& offset) { - cpBodyApplyImpulse(_info->body, PhysicsHelper::point2cpv(impulse), PhysicsHelper::point2cpv(offset)); + cpBodyApplyImpulse(_info->getBody(), PhysicsHelper::point2cpv(impulse), PhysicsHelper::point2cpv(offset)); } void PhysicsBody::applyTorque(float torque) { - cpBodySetTorque(_info->body, PhysicsHelper::float2cpfloat(torque)); + cpBodySetTorque(_info->getBody(), PhysicsHelper::float2cpfloat(torque)); } void PhysicsBody::setMass(float mass) @@ -439,7 +439,7 @@ void PhysicsBody::setMass(float mass) } } - cpBodySetMass(_info->body, PhysicsHelper::float2cpfloat(_mass)); + cpBodySetMass(_info->getBody(), PhysicsHelper::float2cpfloat(_mass)); } void PhysicsBody::addMass(float mass) @@ -481,7 +481,7 @@ void PhysicsBody::addMass(float mass) } } - cpBodySetMass(_info->body, PhysicsHelper::float2cpfloat(_mass)); + cpBodySetMass(_info->getBody(), PhysicsHelper::float2cpfloat(_mass)); } void PhysicsBody::addMoment(float moment) @@ -522,58 +522,58 @@ void PhysicsBody::addMoment(float moment) if (_rotationEnable) { - cpBodySetMoment(_info->body, PhysicsHelper::float2cpfloat(_moment)); + cpBodySetMoment(_info->getBody(), PhysicsHelper::float2cpfloat(_moment)); } } -void PhysicsBody::setVelocity(Point velocity) +void PhysicsBody::setVelocity(const Point& velocity) { - cpBodySetVel(_info->body, PhysicsHelper::point2cpv(velocity)); + cpBodySetVel(_info->getBody(), PhysicsHelper::point2cpv(velocity)); } Point PhysicsBody::getVelocity() { - return PhysicsHelper::cpv2point(cpBodyGetVel(_info->body)); + return PhysicsHelper::cpv2point(cpBodyGetVel(_info->getBody())); } -Point PhysicsBody::getVelocityAtLocalPoint(Point point) +Point PhysicsBody::getVelocityAtLocalPoint(const Point& point) { - return PhysicsHelper::cpv2point(cpBodyGetVelAtLocalPoint(_info->body, PhysicsHelper::point2cpv(point))); + return PhysicsHelper::cpv2point(cpBodyGetVelAtLocalPoint(_info->getBody(), PhysicsHelper::point2cpv(point))); } -Point PhysicsBody::getVelocityAtWorldPoint(Point point) +Point PhysicsBody::getVelocityAtWorldPoint(const Point& point) { - return PhysicsHelper::cpv2point(cpBodyGetVelAtWorldPoint(_info->body, PhysicsHelper::point2cpv(point))); + return PhysicsHelper::cpv2point(cpBodyGetVelAtWorldPoint(_info->getBody(), PhysicsHelper::point2cpv(point))); } void PhysicsBody::setAngularVelocity(float velocity) { - cpBodySetAngVel(_info->body, PhysicsHelper::float2cpfloat(velocity)); + cpBodySetAngVel(_info->getBody(), PhysicsHelper::float2cpfloat(velocity)); } float PhysicsBody::getAngularVelocity() { - return PhysicsHelper::cpfloat2float(cpBodyGetAngVel(_info->body)); + return PhysicsHelper::cpfloat2float(cpBodyGetAngVel(_info->getBody())); } void PhysicsBody::setVelocityLimit(float limit) { - cpBodySetVelLimit(_info->body, PhysicsHelper::float2cpfloat(limit)); + cpBodySetVelLimit(_info->getBody(), PhysicsHelper::float2cpfloat(limit)); } float PhysicsBody::getVelocityLimit() { - return PhysicsHelper::cpfloat2float(cpBodyGetVelLimit(_info->body)); + return PhysicsHelper::cpfloat2float(cpBodyGetVelLimit(_info->getBody())); } void PhysicsBody::setAngularVelocityLimit(float limit) { - cpBodySetVelLimit(_info->body, PhysicsHelper::float2cpfloat(limit)); + cpBodySetVelLimit(_info->getBody(), PhysicsHelper::float2cpfloat(limit)); } float PhysicsBody::getAngularVelocityLimit() { - return PhysicsHelper::cpfloat2float(cpBodyGetAngVelLimit(_info->body)); + return PhysicsHelper::cpfloat2float(cpBodyGetAngVelLimit(_info->getBody())); } void PhysicsBody::setMoment(float moment) @@ -583,11 +583,11 @@ void PhysicsBody::setMoment(float moment) if (_rotationEnable) { - cpBodySetMoment(_info->body, PhysicsHelper::float2cpfloat(_moment)); + cpBodySetMoment(_info->getBody(), PhysicsHelper::float2cpfloat(_moment)); } } -PhysicsShape* PhysicsBody::getShapeByTag(int tag) const +PhysicsShape* PhysicsBody::getShape(int tag) const { for (auto child : *_shapes) { @@ -601,7 +601,7 @@ PhysicsShape* PhysicsBody::getShapeByTag(int tag) const return nullptr; } -void PhysicsBody::removeShapeByTag(int tag) +void PhysicsBody::removeShape(int tag) { for (auto child : *_shapes) { @@ -685,7 +685,7 @@ void PhysicsBody::setEnable(bool enable) bool PhysicsBody::isResting() const { - return cpBodyIsSleeping(_info->body) == cpTrue; + return cpBodyIsSleeping(_info->getBody()) == cpTrue; } void PhysicsBody::update(float delta) @@ -693,9 +693,9 @@ void PhysicsBody::update(float delta) // damping compute if (_dynamic) { - _info->body->v.x *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f); - _info->body->v.y *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f); - _info->body->w *= cpfclamp(1.0f - delta * _angularDamping, 0.0f, 1.0f); + _info->getBody()->v.x *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f); + _info->getBody()->v.y *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f); + _info->getBody()->w *= cpfclamp(1.0f - delta * _angularDamping, 0.0f, 1.0f); } } @@ -739,12 +739,12 @@ void PhysicsBody::setGroup(int group) Point PhysicsBody::world2Local(const Point& point) { - return PhysicsHelper::cpv2point(cpBodyWorld2Local(_info->body, PhysicsHelper::point2cpv(point))); + return PhysicsHelper::cpv2point(cpBodyWorld2Local(_info->getBody(), PhysicsHelper::point2cpv(point))); } Point PhysicsBody::local2World(const Point& point) { - return PhysicsHelper::cpv2point(cpBodyLocal2World(_info->body, PhysicsHelper::point2cpv(point))); + return PhysicsHelper::cpv2point(cpBodyLocal2World(_info->getBody(), PhysicsHelper::point2cpv(point))); } #elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) diff --git a/cocos/physics/CCPhysicsBody.h b/cocos/physics/CCPhysicsBody.h index 60dd314966..7c0c79002e 100644 --- a/cocos/physics/CCPhysicsBody.h +++ b/cocos/physics/CCPhysicsBody.h @@ -59,62 +59,62 @@ public: /** * @brief Create a body contains a circle shape. */ - static PhysicsBody* createCircle(float radius, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, Point offset = Point::ZERO); + static PhysicsBody* createCircle(float radius, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO); /** * @brief Create a body contains a box shape. */ - static PhysicsBody* createBox(Size size, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, Point offset = Point::ZERO); + static PhysicsBody* createBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO); /** * @brief Create a body contains a polygon shape. * points is an array of Point structs defining a convex hull with a clockwise winding. */ - static PhysicsBody* createPolygon(Point* points, int count, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, Point offset = Point::ZERO); + static PhysicsBody* createPolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO); /** * @brief Create a body contains a EdgeSegment shape. */ - static PhysicsBody* createEdgeSegment(Point a, Point b, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1); + static PhysicsBody* createEdgeSegment(const Point& a, const Point& b, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1); /** * @brief Create a body contains a EdgeBox shape. */ - static PhysicsBody* createEdgeBox(Size size, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1, Point offset = Point::ZERO); + static PhysicsBody* createEdgeBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1, const Point& offset = Point::ZERO); /** * @brief Create a body contains a EdgePolygon shape. */ - static PhysicsBody* createEdgePolygon(Point* points, int count, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1); + static PhysicsBody* createEdgePolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1); /** * @brief Create a body contains a EdgeChain shape. */ - static PhysicsBody* createEdgeChain(Point* points, int count, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1); + static PhysicsBody* createEdgeChain(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1); virtual PhysicsShape* addShape(PhysicsShape* shape); /** * @brief Applies a immediate force to body. */ - virtual void applyForce(Point force); + virtual void applyForce(const Vect& force); /** * @brief Applies a immediate force to body. */ - virtual void applyForce(Point force, Point offset); + virtual void applyForce(const Vect& force, const Point& offset); /** * @brief Applies a continuous force to body. */ - virtual void applyImpulse(Point impulse); + virtual void applyImpulse(const Vect& impulse); /** * @brief Applies a continuous force to body. */ - virtual void applyImpulse(Point impulse, Point offset); + virtual void applyImpulse(const Vect& impulse, const Point& offset); /** * @brief Applies a torque force to body. */ virtual void applyTorque(float torque); - virtual void setVelocity(Point velocity); + virtual void setVelocity(const Vect& velocity); virtual Point getVelocity(); virtual void setAngularVelocity(float velocity); - virtual Point getVelocityAtLocalPoint(Point point); - virtual Point getVelocityAtWorldPoint(Point point); + virtual Point getVelocityAtLocalPoint(const Point& point); + virtual Point getVelocityAtWorldPoint(const Point& point); virtual float getAngularVelocity(); virtual void setVelocityLimit(float limit); virtual float getVelocityLimit(); @@ -129,12 +129,12 @@ public: * @brief get the first body shapes. */ inline PhysicsShape* getShape() const { return _shapes->count() >= 1 ? dynamic_cast(_shapes->getObjectAtIndex(0)) : nullptr; } - PhysicsShape* getShapeByTag(int tag) const; + PhysicsShape* getShape(int tag) const; /* * @brief remove a shape from body */ void removeShape(PhysicsShape* shape); - void removeShapeByTag(int tag); + void removeShape(int tag); /* * @brief remove all shapes */ diff --git a/cocos/physics/CCPhysicsContact.cpp b/cocos/physics/CCPhysicsContact.cpp index e3a4907a97..71bdf5a1d0 100644 --- a/cocos/physics/CCPhysicsContact.cpp +++ b/cocos/physics/CCPhysicsContact.cpp @@ -150,9 +150,9 @@ void PhysicsContactPreSolve::setFriction(float friction) static_cast(_contactInfo)->u = friction; } -void PhysicsContactPreSolve::setSurfaceVelocity(Point surfaceVelocity) +void PhysicsContactPreSolve::setSurfaceVelocity(const Vect& velocity) { - static_cast(_contactInfo)->surface_vr = PhysicsHelper::point2cpv(surfaceVelocity); + static_cast(_contactInfo)->surface_vr = PhysicsHelper::point2cpv(velocity); } void PhysicsContactPreSolve::ignore() diff --git a/cocos/physics/CCPhysicsContact.h b/cocos/physics/CCPhysicsContact.h index ee279ef474..3982f4fdd6 100644 --- a/cocos/physics/CCPhysicsContact.h +++ b/cocos/physics/CCPhysicsContact.h @@ -138,7 +138,7 @@ public: Point getSurfaceVelocity() const; void setElasticity(float elasticity); void setFriction(float friction); - void setSurfaceVelocity(Point surfaceVelocity); + void setSurfaceVelocity(const Vect& velocity); void ignore(); private: diff --git a/cocos/physics/CCPhysicsJoint.cpp b/cocos/physics/CCPhysicsJoint.cpp index 694559264e..791266f8ba 100644 --- a/cocos/physics/CCPhysicsJoint.cpp +++ b/cocos/physics/CCPhysicsJoint.cpp @@ -197,13 +197,13 @@ bool PhysicsJointFixed::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr) getBodyNode(b)->setPosition(anchr); // add a pivot joint to fixed two body together - cpConstraint* joint = cpPivotJointNew(getBodyInfo(a)->body, getBodyInfo(b)->body, + cpConstraint* joint = cpPivotJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(), 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(getBodyInfo(a)->body, getBodyInfo(b)->body, 0, 1); + joint = cpGearJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(), 0, 1); CC_BREAK_IF(joint == nullptr); _info->add(joint); @@ -233,7 +233,7 @@ bool PhysicsJointPin::init(PhysicsBody *a, PhysicsBody *b, const Point& anchr) do { CC_BREAK_IF(!PhysicsJoint::init(a, b)); - cpConstraint* joint = cpPivotJointNew(getBodyInfo(a)->body, getBodyInfo(b)->body, + cpConstraint* joint = cpPivotJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(), PhysicsHelper::point2cpv(anchr)); CC_BREAK_IF(joint == nullptr); @@ -248,12 +248,12 @@ bool PhysicsJointPin::init(PhysicsBody *a, PhysicsBody *b, const Point& anchr) void PhysicsJointPin::setMaxForce(float force) { - _info->joints.front()->maxForce = PhysicsHelper::float2cpfloat(force); + _info->getJoints().front()->maxForce = PhysicsHelper::float2cpfloat(force); } float PhysicsJointPin::getMaxForce() const { - return PhysicsHelper::cpfloat2float(_info->joints.front()->maxForce); + return PhysicsHelper::cpfloat2float(_info->getJoints().front()->maxForce); } PhysicsJointSliding* PhysicsJointSliding::create(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr) @@ -275,7 +275,7 @@ bool PhysicsJointSliding::init(PhysicsBody* a, PhysicsBody* b, const Point& groo { CC_BREAK_IF(!PhysicsJoint::init(a, b)); - cpConstraint* joint = cpGrooveJointNew(getBodyInfo(a)->body, getBodyInfo(b)->body, + cpConstraint* joint = cpGrooveJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(), PhysicsHelper::point2cpv(grooveA), PhysicsHelper::point2cpv(grooveB), PhysicsHelper::point2cpv(anchr)); @@ -310,7 +310,7 @@ bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1 { CC_BREAK_IF(!PhysicsJoint::init(a, b)); - cpConstraint* joint = cpSlideJointNew(getBodyInfo(a)->body, getBodyInfo(b)->body, + cpConstraint* joint = cpSlideJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(), PhysicsHelper::point2cpv(anchr1), PhysicsHelper::point2cpv(anchr2), 0, @@ -328,22 +328,22 @@ bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1 float PhysicsJointLimit::getMin() const { - return PhysicsHelper::cpfloat2float(cpSlideJointGetMin(_info->joints.front())); + return PhysicsHelper::cpfloat2float(cpSlideJointGetMin(_info->getJoints().front())); } void PhysicsJointLimit::setMin(float min) { - cpSlideJointSetMin(_info->joints.front(), PhysicsHelper::float2cpfloat(min)); + cpSlideJointSetMin(_info->getJoints().front(), PhysicsHelper::float2cpfloat(min)); } float PhysicsJointLimit::getMax() const { - return PhysicsHelper::cpfloat2float(cpSlideJointGetMax(_info->joints.front())); + return PhysicsHelper::cpfloat2float(cpSlideJointGetMax(_info->getJoints().front())); } void PhysicsJointLimit::setMax(float max) { - cpSlideJointSetMax(_info->joints.front(), PhysicsHelper::float2cpfloat(max)); + cpSlideJointSetMax(_info->getJoints().front(), PhysicsHelper::float2cpfloat(max)); } PhysicsJointDistance* PhysicsJointDistance::create(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2) @@ -365,8 +365,8 @@ bool PhysicsJointDistance::init(PhysicsBody* a, PhysicsBody* b, const Point& anc { CC_BREAK_IF(!PhysicsJoint::init(a, b)); - cpConstraint* joint = cpPinJointNew(getBodyInfo(a)->body, - getBodyInfo(b)->body, + cpConstraint* joint = cpPinJointNew(getBodyInfo(a)->getBody(), + getBodyInfo(b)->getBody(), PhysicsHelper::point2cpv(anchr1), PhysicsHelper::point2cpv(anchr2)); CC_BREAK_IF(joint == nullptr); diff --git a/cocos/physics/CCPhysicsSetting.h b/cocos/physics/CCPhysicsSetting.h index ee3214bcd7..9eb82f2fa2 100644 --- a/cocos/physics/CCPhysicsSetting.h +++ b/cocos/physics/CCPhysicsSetting.h @@ -47,6 +47,9 @@ namespace cocos2d { extern const float PHYSICS_INFINITY; + class Point; + typedef Point Vect; + #if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) static const int PHYSICS_CONTACT_POINT_MAX = 4; #else diff --git a/cocos/physics/CCPhysicsShape.cpp b/cocos/physics/CCPhysicsShape.cpp index fea8365c08..89d0551cff 100644 --- a/cocos/physics/CCPhysicsShape.cpp +++ b/cocos/physics/CCPhysicsShape.cpp @@ -105,7 +105,7 @@ void PhysicsShape::setMoment(float moment) _moment = moment; } -void PhysicsShape::setMaterial(PhysicsMaterial material) +void PhysicsShape::setMaterial(const PhysicsMaterial& material) { setDensity(material.density); setRestitution(material.restitution); @@ -216,7 +216,7 @@ void PhysicsShape::setRestitution(float restitution) { _material.restitution = restitution; - for (cpShape* shape : _info->shapes) + for (cpShape* shape : _info->getShapes()) { cpShapeSetElasticity(shape, PhysicsHelper::float2cpfloat(restitution)); } @@ -226,14 +226,14 @@ void PhysicsShape::setFriction(float friction) { _material.friction = friction; - for (cpShape* shape : _info->shapes) + for (cpShape* shape : _info->getShapes()) { cpShapeSetFriction(shape, PhysicsHelper::float2cpfloat(friction)); } } -Point* PhysicsShape::recenterPoints(Point* points, int count, Point center) +Point* PhysicsShape::recenterPoints(Point* points, int count, const Point& center) { cpVect* cpvs = new cpVect[count]; cpRecenterPoly(count, PhysicsHelper::points2cpvs(points, cpvs, count)); @@ -251,7 +251,7 @@ Point* PhysicsShape::recenterPoints(Point* points, int count, Point center) return points; } -Point PhysicsShape::getPolyonCenter(Point* points, int count) +Point PhysicsShape::getPolyonCenter(const Point* points, int count) { cpVect* cpvs = new cpVect[count]; cpVect center = cpCentroidForPoly(count, PhysicsHelper::points2cpvs(points, cpvs, count)); @@ -280,14 +280,14 @@ void PhysicsShape::setBody(PhysicsBody *body) _body = nullptr; }else { - _info->setBody(body->_info->body); + _info->setBody(body->_info->getBody()); //_info->setGroup(body->_info->group); _body = body; } } // PhysicsShapeCircle -PhysicsShapeCircle* PhysicsShapeCircle::create(float radius, PhysicsMaterial material/* = MaterialDefault*/, Point offset/* = Point(0, 0)*/) +PhysicsShapeCircle* PhysicsShapeCircle::create(float radius, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset/* = Point(0, 0)*/) { PhysicsShapeCircle* shape = new PhysicsShapeCircle(); if (shape && shape->init(radius, material, offset)) @@ -300,13 +300,13 @@ PhysicsShapeCircle* PhysicsShapeCircle::create(float radius, PhysicsMaterial mat return nullptr; } -bool PhysicsShapeCircle::init(float radius, PhysicsMaterial material/* = MaterialDefault*/, Point offset /*= Point(0, 0)*/) +bool PhysicsShapeCircle::init(float radius, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset /*= Point(0, 0)*/) { do { CC_BREAK_IF(!PhysicsShape::init(Type::CIRCLE)); - cpShape* shape = cpCircleShapeNew(_info->shareBody, radius, PhysicsHelper::point2cpv(offset)); + cpShape* shape = cpCircleShapeNew(_info->getSharedBody(), radius, PhysicsHelper::point2cpv(offset)); CC_BREAK_IF(shape == nullptr); @@ -328,7 +328,7 @@ float PhysicsShapeCircle::calculateArea(float radius) return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, radius)); } -float PhysicsShapeCircle::calculateMoment(float mass, float radius, Point offset) +float PhysicsShapeCircle::calculateMoment(float mass, float radius, const Point& offset) { return mass == PHYSICS_INFINITY ? PHYSICS_INFINITY : PhysicsHelper::cpfloat2float(cpMomentForCircle(PhysicsHelper::float2cpfloat(mass), @@ -339,12 +339,12 @@ float PhysicsShapeCircle::calculateMoment(float mass, float radius, Point offset float PhysicsShapeCircle::calculateDefaultArea() { - return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, cpCircleShapeGetRadius(_info->shapes.front()))); + return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, cpCircleShapeGetRadius(_info->getShapes().front()))); } float PhysicsShapeCircle::calculateDefaultMoment() { - cpShape* shape = _info->shapes.front(); + cpShape* shape = _info->getShapes().front(); return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY : PhysicsHelper::cpfloat2float(cpMomentForCircle(PhysicsHelper::float2cpfloat(_mass), @@ -355,16 +355,16 @@ float PhysicsShapeCircle::calculateDefaultMoment() float PhysicsShapeCircle::getRadius() const { - return PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(_info->shapes.front())); + return PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(_info->getShapes().front())); } Point PhysicsShapeCircle::getOffset() { - return PhysicsHelper::cpv2point(cpCircleShapeGetOffset(_info->shapes.front())); + return PhysicsHelper::cpv2point(cpCircleShapeGetOffset(_info->getShapes().front())); } // PhysicsShapeEdgeSegment -PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(Point a, Point b, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/) +PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(const Point& a, const Point& b, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/) { PhysicsShapeEdgeSegment* shape = new PhysicsShapeEdgeSegment(); if (shape && shape->init(a, b, material, border)) @@ -377,13 +377,13 @@ PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(Point a, Point b, Physi return nullptr; } -bool PhysicsShapeEdgeSegment::init(Point a, Point b, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/) +bool PhysicsShapeEdgeSegment::init(const Point& a, const Point& b, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/) { do { CC_BREAK_IF(!PhysicsShape::init(Type::EDGESEGMENT)); - cpShape* shape = cpSegmentShapeNew(_info->shareBody, + cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(), PhysicsHelper::point2cpv(a), PhysicsHelper::point2cpv(b), PhysicsHelper::float2cpfloat(border)); @@ -407,12 +407,12 @@ bool PhysicsShapeEdgeSegment::init(Point a, Point b, PhysicsMaterial material/* Point PhysicsShapeEdgeSegment::getPointA() const { - return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->shapes.front()))->ta); + return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->getShapes().front()))->ta); } Point PhysicsShapeEdgeSegment::getPointB() const { - return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->shapes.front()))->tb); + return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->getShapes().front()))->tb); } Point PhysicsShapeEdgeSegment::getCenter() @@ -421,7 +421,7 @@ Point PhysicsShapeEdgeSegment::getCenter() } // PhysicsShapeBox -PhysicsShapeBox* PhysicsShapeBox::create(Size size, PhysicsMaterial material/* = MaterialDefault*/, Point offset/* = Point(0, 0)*/) +PhysicsShapeBox* PhysicsShapeBox::create(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset/* = Point(0, 0)*/) { PhysicsShapeBox* shape = new PhysicsShapeBox(); if (shape && shape->init(size, material, offset)) @@ -434,7 +434,7 @@ PhysicsShapeBox* PhysicsShapeBox::create(Size size, PhysicsMaterial material/* = return nullptr; } -bool PhysicsShapeBox::init(Size size, PhysicsMaterial material/* = MaterialDefault*/, Point offset /*= Point(0, 0)*/) +bool PhysicsShapeBox::init(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset /*= Point(0, 0)*/) { do { @@ -446,7 +446,7 @@ bool PhysicsShapeBox::init(Size size, PhysicsMaterial material/* = MaterialDefau {-wh.x/2.0f, -wh.y/2.0f}, {-wh.x/2.0f, wh.y/2.0f}, {wh.x/2.0f, wh.y/2.0f}, {wh.x/2.0f, -wh.y/2.0f} }; - cpShape* shape = cpPolyShapeNew(_info->shareBody, 4, vec, PhysicsHelper::point2cpv(offset)); + cpShape* shape = cpPolyShapeNew(_info->getSharedBody(), 4, vec, PhysicsHelper::point2cpv(offset)); CC_BREAK_IF(shape == nullptr); @@ -465,7 +465,7 @@ bool PhysicsShapeBox::init(Size size, PhysicsMaterial material/* = MaterialDefau return false; } -float PhysicsShapeBox::calculateArea(Size size) +float PhysicsShapeBox::calculateArea(const Size& size) { cpVect wh = PhysicsHelper::size2cpv(size); cpVect vec[4] = @@ -475,7 +475,7 @@ float PhysicsShapeBox::calculateArea(Size size) return PhysicsHelper::cpfloat2float(cpAreaForPoly(4, vec)); } -float PhysicsShapeBox::calculateMoment(float mass, Size size, Point offset) +float PhysicsShapeBox::calculateMoment(float mass, const Size& size, const Point& offset) { cpVect wh = PhysicsHelper::size2cpv(size); cpVect vec[4] = @@ -492,20 +492,20 @@ float PhysicsShapeBox::calculateMoment(float mass, Size size, Point offset) float PhysicsShapeBox::calculateDefaultArea() { - cpShape* shape = _info->shapes.front(); + cpShape* shape = _info->getShapes().front(); return PhysicsHelper::cpfloat2float(cpAreaForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts)); } float PhysicsShapeBox::calculateDefaultMoment() { - cpShape* shape = _info->shapes.front(); + cpShape* shape = _info->getShapes().front(); return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY : PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero)); } Point* PhysicsShapeBox::getPoints(Point* points) const { - cpShape* shape = _info->shapes.front(); + cpShape* shape = _info->getShapes().front(); return PhysicsHelper::cpvs2points(((cpPolyShape*)shape)->verts, points, ((cpPolyShape*)shape)->numVerts); return points; @@ -513,13 +513,13 @@ Point* PhysicsShapeBox::getPoints(Point* points) const Size PhysicsShapeBox::getSize() const { - cpShape* shape = _info->shapes.front(); + cpShape* shape = _info->getShapes().front(); return PhysicsHelper::cpv2size(cpv(cpvdist(cpPolyShapeGetVert(shape, 0), cpPolyShapeGetVert(shape, 1)), cpvdist(cpPolyShapeGetVert(shape, 1), cpPolyShapeGetVert(shape, 2)))); } // PhysicsShapePolygon -PhysicsShapePolygon* PhysicsShapePolygon::create(Point* points, int count, PhysicsMaterial material/* = MaterialDefault*/, Point offset/* = Point(0, 0)*/) +PhysicsShapePolygon* PhysicsShapePolygon::create(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset/* = Point(0, 0)*/) { PhysicsShapePolygon* shape = new PhysicsShapePolygon(); if (shape && shape->init(points, count, material, offset)) @@ -532,7 +532,7 @@ PhysicsShapePolygon* PhysicsShapePolygon::create(Point* points, int count, Physi return nullptr; } -bool PhysicsShapePolygon::init(Point* points, int count, PhysicsMaterial material/* = MaterialDefault*/, Point offset/* = Point(0, 0)*/) +bool PhysicsShapePolygon::init(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset/* = Point(0, 0)*/) { do { @@ -540,7 +540,7 @@ bool PhysicsShapePolygon::init(Point* points, int count, PhysicsMaterial materia cpVect* vecs = new cpVect[count]; PhysicsHelper::points2cpvs(points, vecs, count); - cpShape* shape = cpPolyShapeNew(_info->shareBody, count, vecs, PhysicsHelper::point2cpv(offset)); + cpShape* shape = cpPolyShapeNew(_info->getSharedBody(), count, vecs, PhysicsHelper::point2cpv(offset)); CC_SAFE_DELETE(vecs); CC_BREAK_IF(shape == nullptr); @@ -560,7 +560,7 @@ bool PhysicsShapePolygon::init(Point* points, int count, PhysicsMaterial materia return false; } -float PhysicsShapePolygon::calculateArea(Point* points, int count) +float PhysicsShapePolygon::calculateArea(const Point* points, int count) { cpVect* vecs = new cpVect[count]; PhysicsHelper::points2cpvs(points, vecs, count); @@ -570,7 +570,7 @@ float PhysicsShapePolygon::calculateArea(Point* points, int count) return area; } -float PhysicsShapePolygon::calculateMoment(float mass, Point* points, int count, Point offset) +float PhysicsShapePolygon::calculateMoment(float mass, const Point* points, int count, const Point& offset) { cpVect* vecs = new cpVect[count]; PhysicsHelper::points2cpvs(points, vecs, count); @@ -583,31 +583,31 @@ float PhysicsShapePolygon::calculateMoment(float mass, Point* points, int count, float PhysicsShapePolygon::calculateDefaultArea() { - cpShape* shape = _info->shapes.front(); + cpShape* shape = _info->getShapes().front(); return PhysicsHelper::cpfloat2float(cpAreaForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts)); } float PhysicsShapePolygon::calculateDefaultMoment() { - cpShape* shape = _info->shapes.front(); + cpShape* shape = _info->getShapes().front(); return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY : PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero)); } Point PhysicsShapePolygon::getPoint(int i) const { - return PhysicsHelper::cpv2point(cpPolyShapeGetVert(_info->shapes.front(), i)); + return PhysicsHelper::cpv2point(cpPolyShapeGetVert(_info->getShapes().front(), i)); } Point* PhysicsShapePolygon::getPoints(Point* points) const { - cpShape* shape = _info->shapes.front(); + cpShape* shape = _info->getShapes().front(); return PhysicsHelper::cpvs2points(((cpPolyShape*)shape)->verts, points, ((cpPolyShape*)shape)->numVerts); } int PhysicsShapePolygon::getPointsCount() const { - return ((cpPolyShape*)_info->shapes.front())->numVerts; + return ((cpPolyShape*)_info->getShapes().front())->numVerts; } Point PhysicsShapePolygon::getCenter() @@ -616,7 +616,7 @@ Point PhysicsShapePolygon::getCenter() } // PhysicsShapeEdgeBox -PhysicsShapeEdgeBox* PhysicsShapeEdgeBox::create(Size size, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/, Point offset/* = Point(0, 0)*/) +PhysicsShapeEdgeBox* PhysicsShapeEdgeBox::create(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/, const Point& offset/* = Point(0, 0)*/) { PhysicsShapeEdgeBox* shape = new PhysicsShapeEdgeBox(); if (shape && shape->init(size, material, border, offset)) @@ -629,7 +629,7 @@ PhysicsShapeEdgeBox* PhysicsShapeEdgeBox::create(Size size, PhysicsMaterial mate return nullptr; } -bool PhysicsShapeEdgeBox::init(Size size, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/, Point offset/*= Point(0, 0)*/) +bool PhysicsShapeEdgeBox::init(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/, const Point& offset/*= Point(0, 0)*/) { do { @@ -644,7 +644,7 @@ bool PhysicsShapeEdgeBox::init(Size size, PhysicsMaterial material/* = MaterialD int i = 0; for (; i < 4; ++i) { - cpShape* shape = cpSegmentShapeNew(_info->shareBody, vec[i], vec[(i+1)%4], + cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(), vec[i], vec[(i+1)%4], PhysicsHelper::float2cpfloat(border)); CC_BREAK_IF(shape == nullptr); _info->add(shape); @@ -664,7 +664,7 @@ bool PhysicsShapeEdgeBox::init(Size size, PhysicsMaterial material/* = MaterialD } // PhysicsShapeEdgeBox -PhysicsShapeEdgePolygon* PhysicsShapeEdgePolygon::create(Point* points, int count, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/) +PhysicsShapeEdgePolygon* PhysicsShapeEdgePolygon::create(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/) { PhysicsShapeEdgePolygon* shape = new PhysicsShapeEdgePolygon(); if (shape && shape->init(points, count, material, border)) @@ -677,7 +677,7 @@ PhysicsShapeEdgePolygon* PhysicsShapeEdgePolygon::create(Point* points, int coun return nullptr; } -bool PhysicsShapeEdgePolygon::init(Point* points, int count, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/) +bool PhysicsShapeEdgePolygon::init(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/) { cpVect* vec = nullptr; do @@ -691,7 +691,7 @@ bool PhysicsShapeEdgePolygon::init(Point* points, int count, PhysicsMaterial mat int i = 0; for (; i < count; ++i) { - cpShape* shape = cpSegmentShapeNew(_info->shareBody, vec[i], vec[(i+1)%count], + cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(), vec[i], vec[(i+1)%count], PhysicsHelper::float2cpfloat(border)); CC_BREAK_IF(shape == nullptr); cpShapeSetElasticity(shape, 1.0f); @@ -722,11 +722,11 @@ Point PhysicsShapeEdgePolygon::getCenter() int PhysicsShapeEdgePolygon::getPointsCount() const { - return _info->shapes.size() + 1; + return _info->getShapes().size() + 1; } // PhysicsShapeEdgeChain -PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(Point* points, int count, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/) +PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/) { PhysicsShapeEdgeChain* shape = new PhysicsShapeEdgeChain(); if (shape && shape->init(points, count, material, border)) @@ -739,7 +739,7 @@ PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(Point* points, int count, P return nullptr; } -bool PhysicsShapeEdgeChain::init(Point* points, int count, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/) +bool PhysicsShapeEdgeChain::init(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/) { cpVect* vec = nullptr; do @@ -753,7 +753,7 @@ bool PhysicsShapeEdgeChain::init(Point* points, int count, PhysicsMaterial mater int i = 0; for (; i < count - 1; ++i) { - cpShape* shape = cpSegmentShapeNew(_info->shareBody, vec[i], vec[i+1], + cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(), vec[i], vec[i+1], PhysicsHelper::float2cpfloat(border)); CC_BREAK_IF(shape == nullptr); cpShapeSetElasticity(shape, 1.0f); @@ -783,23 +783,23 @@ Point PhysicsShapeEdgeChain::getCenter() int PhysicsShapeEdgeChain::getPointsCount() const { - return _info->shapes.size() + 1; + return _info->getShapes().size() + 1; } void PhysicsShape::setGroup(int group) { if (group < 0) { - for (auto shape : _info->shapes) + for (auto shape : _info->getShapes()) { cpShapeSetGroup(shape, (cpGroup)group); } } } -bool PhysicsShape::containsPoint(Point point) const +bool PhysicsShape::containsPoint(const Point& point) const { - for (auto shape : _info->shapes) + for (auto shape : _info->getShapes()) { if (cpShapePointQuery(shape, PhysicsHelper::point2cpv(point))) { diff --git a/cocos/physics/CCPhysicsShape.h b/cocos/physics/CCPhysicsShape.h index 4ddcfe8a18..4aba49d4d7 100644 --- a/cocos/physics/CCPhysicsShape.h +++ b/cocos/physics/CCPhysicsShape.h @@ -92,16 +92,16 @@ public: void setDensity(float density); void setRestitution(float restitution); void setFriction(float friction); - void setMaterial(PhysicsMaterial material); + void setMaterial(const PhysicsMaterial& material); virtual float calculateDefaultMoment() { return 0; } virtual float calculateDefaultArea() { return 0; } virtual Point getOffset() { return Point::ZERO; } virtual Point getCenter() { return getOffset(); } - bool containsPoint(Point point) const; + bool containsPoint(const Point& point) const; - static Point* recenterPoints(Point* points, int count, Point center = Point::ZERO); - static Point getPolyonCenter(Point* points, int count); + static Point* recenterPoints(Point* points, int count, const Point& center = Point::ZERO); + static Point getPolyonCenter(const Point* points, int count); inline void setCategoryBitmask(int bitmask) { _categoryBitmask = bitmask; } inline int getCategoryBitmask() const { return _categoryBitmask; } @@ -150,9 +150,9 @@ protected: class PhysicsShapeCircle : public PhysicsShape { public: - static PhysicsShapeCircle* create(float radius, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0)); + static PhysicsShapeCircle* create(float radius, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point(0, 0)); static float calculateArea(float radius); - static float calculateMoment(float mass, float radius, Point offset = Point(0, 0)); + static float calculateMoment(float mass, float radius, const Point& offset = Point::ZERO); float calculateDefaultArea() override; float calculateDefaultMoment() override; @@ -160,7 +160,7 @@ public: float getRadius() const; Point getOffset() override; protected: - bool init(float radius, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0)); + bool init(float radius, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO); protected: PhysicsShapeCircle(); @@ -171,9 +171,9 @@ protected: class PhysicsShapeBox : public PhysicsShape { public: - static PhysicsShapeBox* create(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0)); - static float calculateArea(Size size); - static float calculateMoment(float mass, Size size, Point offset = Point(0, 0)); + static PhysicsShapeBox* create(const Size& size, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO); + static float calculateArea(const Size& size); + static float calculateMoment(float mass, const Size& size, const Point& offset = Point::ZERO); float calculateDefaultArea() override; float calculateDefaultMoment() override; @@ -183,7 +183,7 @@ public: Point getOffset() override { return _offset; } protected: - bool init(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0)); + bool init(const Size& size, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO); protected: PhysicsShapeBox(); @@ -197,9 +197,9 @@ protected: class PhysicsShapePolygon : public PhysicsShape { public: - static PhysicsShapePolygon* create(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0)); - static float calculateArea(Point* points, int count); - static float calculateMoment(float mass, Point* points, int count, Point offset = Point(0, 0)); + static PhysicsShapePolygon* create(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO); + static float calculateArea(const Point* points, int count); + static float calculateMoment(float mass, const Point* points, int count, const Point& offset = Point::ZERO); float calculateDefaultArea() override; float calculateDefaultMoment() override; @@ -209,7 +209,7 @@ public: int getPointsCount() const; Point getCenter() override; protected: - bool init(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0)); + bool init(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO); protected: PhysicsShapePolygon(); @@ -223,14 +223,14 @@ protected: class PhysicsShapeEdgeSegment : public PhysicsShape { public: - static PhysicsShapeEdgeSegment* create(Point a, Point b, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); + static PhysicsShapeEdgeSegment* create(const Point& a, const Point& b, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); Point getPointA() const; Point getPointB() const; Point getCenter() override; protected: - bool init(Point a, Point b, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); + bool init(const Point& a, const Point& b, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); protected: PhysicsShapeEdgeSegment(); @@ -246,13 +246,13 @@ protected: class PhysicsShapeEdgeBox : public PhysicsShape { public: - static PhysicsShapeEdgeBox* create(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 0, Point offset = Point(0, 0)); + static PhysicsShapeEdgeBox* create(const Size& size, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 0, const Point& offset = Point::ZERO); Point getOffset() override { return _offset; } - Point* getPoints(Point* points) const; + Point* getPoints(const Point* points) const; int getPointsCount() const; protected: - bool init(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1, Point offset = Point(0, 0)); + bool init(const Size& size, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1, const Point& offset = Point::ZERO); protected: PhysicsShapeEdgeBox(); @@ -268,13 +268,13 @@ protected: class PhysicsShapeEdgePolygon : public PhysicsShape { public: - static PhysicsShapeEdgePolygon* create(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); + static PhysicsShapeEdgePolygon* create(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); Point getCenter() override; Point* getPoints(Point* points) const; int getPointsCount() const; protected: - bool init(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); + bool init(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); protected: PhysicsShapeEdgePolygon(); @@ -290,13 +290,13 @@ protected: class PhysicsShapeEdgeChain : public PhysicsShape { public: - static PhysicsShapeEdgeChain* create(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); + static PhysicsShapeEdgeChain* create(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); Point getCenter() override; Point* getPoints(Point* points) const; int getPointsCount() const; protected: - bool init(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); + bool init(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1); protected: PhysicsShapeEdgeChain(); diff --git a/cocos/physics/CCPhysicsWorld.cpp b/cocos/physics/CCPhysicsWorld.cpp index cafdb79be8..2a067720c1 100644 --- a/cocos/physics/CCPhysicsWorld.cpp +++ b/cocos/physics/CCPhysicsWorld.cpp @@ -106,11 +106,11 @@ int PhysicsWorldCallback::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSp { CP_ARBITER_GET_SHAPES(arb, a, b); - auto ita = PhysicsShapeInfo::map.find(a); - auto itb = PhysicsShapeInfo::map.find(b); - CC_ASSERT(ita != PhysicsShapeInfo::map.end() && itb != PhysicsShapeInfo::map.end()); + auto ita = PhysicsShapeInfo::getMap().find(a); + auto itb = PhysicsShapeInfo::getMap().find(b); + CC_ASSERT(ita != PhysicsShapeInfo::getMap().end() && itb != PhysicsShapeInfo::getMap().end()); - PhysicsContact* contact = PhysicsContact::create(ita->second->shape, itb->second->shape); + PhysicsContact* contact = PhysicsContact::create(ita->second->getShape(), itb->second->getShape()); arb->data = contact; contact->_contactInfo = arb; @@ -143,12 +143,12 @@ void PhysicsWorldCallback::rayCastCallbackFunc(cpShape *shape, cpFloat t, cpVect return; } - auto it = PhysicsShapeInfo::map.find(shape); - CC_ASSERT(it != PhysicsShapeInfo::map.end()); + auto it = PhysicsShapeInfo::getMap().find(shape); + CC_ASSERT(it != PhysicsShapeInfo::getMap().end()); PhysicsRayCastCallback::Info callbackInfo = { - it->second->shape, + it->second->getShape(), info->p1, info->p2, Point(info->p1.x+(info->p2.x-info->p1.x)*t, info->p1.y+(info->p2.y-info->p1.y)*t), @@ -161,9 +161,9 @@ void PhysicsWorldCallback::rayCastCallbackFunc(cpShape *shape, cpFloat t, cpVect void PhysicsWorldCallback::rectQueryCallbackFunc(cpShape *shape, RectQueryCallbackInfo *info) { - auto it = PhysicsShapeInfo::map.find(shape); + auto it = PhysicsShapeInfo::getMap().find(shape); - CC_ASSERT(it != PhysicsShapeInfo::map.end()); + CC_ASSERT(it != PhysicsShapeInfo::getMap().end()); if (!PhysicsWorldCallback::continues) { @@ -171,17 +171,17 @@ void PhysicsWorldCallback::rectQueryCallbackFunc(cpShape *shape, RectQueryCallba } PhysicsWorldCallback::continues = info->callback->report(*info->world, - *it->second->shape, + *it->second->getShape(), info->data); } void PhysicsWorldCallback::nearestPointQueryFunc(cpShape *shape, cpFloat distance, cpVect point, Array *arr) { - auto it = PhysicsShapeInfo::map.find(shape); + auto it = PhysicsShapeInfo::getMap().find(shape); - CC_ASSERT(it != PhysicsShapeInfo::map.end()); + CC_ASSERT(it != PhysicsShapeInfo::getMap().end()); - arr->addObject(it->second->shape); + arr->addObject(it->second->getShape()); } bool PhysicsWorld::init(Scene& scene) @@ -202,9 +202,9 @@ bool PhysicsWorld::init(Scene& scene) _scene = &scene; - cpSpaceSetGravity(_info->space, PhysicsHelper::point2cpv(_gravity)); + cpSpaceSetGravity(_info->getSpace(), PhysicsHelper::point2cpv(_gravity)); - cpSpaceSetDefaultCollisionHandler(_info->space, + cpSpaceSetDefaultCollisionHandler(_info->getSpace(), (cpCollisionBeginFunc)PhysicsWorldCallback::collisionBeginCallbackFunc, (cpCollisionPreSolveFunc)PhysicsWorldCallback::collisionPreSolveCallbackFunc, (cpCollisionPostSolveFunc)PhysicsWorldCallback::collisionPostSolveCallbackFunc, @@ -225,7 +225,7 @@ void PhysicsWorld::delayTestAddBody(PhysicsBody* body) return; } - if (_info->space->locked_private) + if (_info->getSpace()->locked_private) { if (_delayAddBodies->getIndexOfObject(body) == UINT_MAX) { @@ -246,7 +246,7 @@ void PhysicsWorld::delayTestRemoveBody(PhysicsBody* body) return; } - if (_info->space->locked_private) + if (_info->getSpace()->locked_private) { if (_delayRemoveBodies->getIndexOfObject(body) == UINT_MAX) { @@ -268,7 +268,7 @@ void PhysicsWorld::delayTestAddJoint(PhysicsJoint* joint) return; } - if (_info->space->locked_private) + if (_info->getSpace()->locked_private) { if (std::find(_delayAddJoints.begin(), _delayAddJoints.end(), joint) == _delayAddJoints.end()) { @@ -290,7 +290,7 @@ void PhysicsWorld::delayTestRemoveJoint(PhysicsJoint* joint) return; } - if (_info->space->locked_private) + if (_info->getSpace()->locked_private) { if (std::find(_delayRemoveJoints.begin(), _delayRemoveJoints.end(), joint) == _delayRemoveJoints.end()) { @@ -338,7 +338,7 @@ void PhysicsWorld::removeAllJoints() PhysicsShape* PhysicsWorld::addShape(PhysicsShape* shape) { - for (auto cps : shape->_info->shapes) + for (auto cps : shape->_info->getShapes()) { _info->addShape(cps); } @@ -348,7 +348,7 @@ PhysicsShape* PhysicsWorld::addShape(PhysicsShape* shape) void PhysicsWorld::realAddJoint(PhysicsJoint *joint) { - for (auto subjoint : joint->_info->joints) + for (auto subjoint : joint->_info->getJoints()) { _info->addJoint(subjoint); } @@ -374,7 +374,7 @@ void PhysicsWorld::realAddBody(PhysicsBody* body) // add body to space if (body->isDynamic()) { - _info->addBody(body->_info->body); + _info->addBody(body->_info->getBody()); } // add shapes to space @@ -401,7 +401,7 @@ void PhysicsWorld::removeBody(PhysicsBody* body) _bodies->removeObject(body); } -void PhysicsWorld::removeBodyByTag(int tag) +void PhysicsWorld::removeBody(int tag) { for (Object* obj : *_bodies) { @@ -442,14 +442,14 @@ void PhysicsWorld::realRemoveBody(PhysicsBody* body) } // remove body - _info->removeBody(body->_info->body); + _info->removeBody(body->_info->getBody()); body->_world = nullptr; } void PhysicsWorld::realRemoveJoint(PhysicsJoint* joint) { - for (auto subjoint : joint->_info->joints) + for (auto subjoint : joint->_info->getJoints()) { _info->removeJoint(subjoint); } @@ -468,18 +468,18 @@ void PhysicsWorld::removeAllBodies() void PhysicsWorld::removeShape(PhysicsShape* shape) { - for (auto cps : shape->_info->shapes) + for (auto cps : shape->_info->getShapes()) { - if (cpSpaceContainsShape(_info->space, cps)) + if (cpSpaceContainsShape(_info->getSpace(), cps)) { - cpSpaceRemoveShape(_info->space, cps); + cpSpaceRemoveShape(_info->getSpace(), cps); } } } void PhysicsWorld::updateBodies() { - if (_info->space->locked_private) + if (_info->getSpace()->locked_private) { return; } @@ -500,7 +500,7 @@ void PhysicsWorld::updateBodies() void PhysicsWorld::updateJoints() { - if (_info->space->locked_private) + if (_info->getSpace()->locked_private) { return; } @@ -533,7 +533,7 @@ void PhysicsWorld::update(float delta) body->update(delta); } - cpSpaceStep(_info->space, delta); + cpSpaceStep(_info->getSpace(), delta); if (_drawNode) { @@ -577,7 +577,7 @@ void PhysicsWorld::debugDraw() void PhysicsWorld::drawWithJoint(DrawNode* node, PhysicsJoint* joint) { - for (auto it = joint->_info->joints.begin(); it != joint->_info->joints.end(); ++it) + for (auto it = joint->_info->getJoints().begin(); it != joint->_info->getJoints().end(); ++it) { cpConstraint *constraint = *it; @@ -638,7 +638,7 @@ void PhysicsWorld::drawWithJoint(DrawNode* node, PhysicsJoint* joint) void PhysicsWorld::drawWithShape(DrawNode* node, PhysicsShape* shape) { - for (auto it = shape->_info->shapes.begin(); it != shape->_info->shapes.end(); ++it) + for (auto it = shape->_info->getShapes().begin(); it != shape->_info->getShapes().end(); ++it) { cpShape *shape = *it; @@ -811,11 +811,11 @@ void PhysicsWorld::setGravity(Point gravity) } _gravity = gravity; - cpSpaceSetGravity(_info->space, PhysicsHelper::point2cpv(gravity)); + cpSpaceSetGravity(_info->getSpace(), PhysicsHelper::point2cpv(gravity)); } -void PhysicsWorld::rayCast(PhysicsRayCastCallback& callback, Point point1, Point point2, void* data) +void PhysicsWorld::rayCast(PhysicsRayCastCallback& callback, const Point& point1, const Point& point2, void* data) { CCASSERT(callback.report != nullptr, "callback.report shouldn't be nullptr"); @@ -824,7 +824,7 @@ void PhysicsWorld::rayCast(PhysicsRayCastCallback& callback, Point point1, Point RayCastCallbackInfo info = { this, &callback, point1, point2, data }; PhysicsWorldCallback::continues = true; - cpSpaceSegmentQuery(this->_info->space, + cpSpaceSegmentQuery(this->_info->getSpace(), PhysicsHelper::point2cpv(point1), PhysicsHelper::point2cpv(point2), CP_ALL_LAYERS, @@ -835,7 +835,7 @@ void PhysicsWorld::rayCast(PhysicsRayCastCallback& callback, Point point1, Point } -void PhysicsWorld::rectQuery(PhysicsRectQueryCallback& callback, Rect rect, void* data) +void PhysicsWorld::rectQuery(PhysicsRectQueryCallback& callback, const Rect& rect, void* data) { CCASSERT(callback.report != nullptr, "callback.report shouldn't be nullptr"); @@ -844,7 +844,7 @@ void PhysicsWorld::rectQuery(PhysicsRectQueryCallback& callback, Rect rect, void RectQueryCallbackInfo info = {this, &callback, data}; PhysicsWorldCallback::continues = true; - cpSpaceBBQuery(this->_info->space, + cpSpaceBBQuery(this->_info->getSpace(), PhysicsHelper::rect2cpbb(rect), CP_ALL_LAYERS, CP_NO_GROUP, @@ -853,10 +853,10 @@ void PhysicsWorld::rectQuery(PhysicsRectQueryCallback& callback, Rect rect, void } } -Array* PhysicsWorld::getShapesAtPoint(Point point) const +Array* PhysicsWorld::getShapes(const Point& point) const { Array* arr = Array::create(); - cpSpaceNearestPointQuery(this->_info->space, + cpSpaceNearestPointQuery(this->_info->getSpace(), PhysicsHelper::point2cpv(point), 0, CP_ALL_LAYERS, @@ -867,16 +867,16 @@ Array* PhysicsWorld::getShapesAtPoint(Point point) const return arr; } -PhysicsShape* PhysicsWorld::getShapeAtPoint(Point point) const +PhysicsShape* PhysicsWorld::getShape(const Point& point) const { - cpShape* shape = cpSpaceNearestPointQueryNearest(this->_info->space, + cpShape* shape = cpSpaceNearestPointQueryNearest(this->_info->getSpace(), PhysicsHelper::point2cpv(point), 0, CP_ALL_LAYERS, CP_NO_GROUP, nullptr); - return shape == nullptr ? nullptr : PhysicsShapeInfo::map.find(shape)->second->shape; + return shape == nullptr ? nullptr : PhysicsShapeInfo::getMap().find(shape)->second->getShape(); } Array* PhysicsWorld::getAllBodies() const @@ -884,7 +884,7 @@ Array* PhysicsWorld::getAllBodies() const return _bodies; } -PhysicsBody* PhysicsWorld::getBodyByTag(int tag) const +PhysicsBody* PhysicsWorld::getBody(int tag) const { for (auto body : *_bodies) { diff --git a/cocos/physics/CCPhysicsWorld.h b/cocos/physics/CCPhysicsWorld.h index 360844db3f..e4cad6e7eb 100644 --- a/cocos/physics/CCPhysicsWorld.h +++ b/cocos/physics/CCPhysicsWorld.h @@ -77,7 +77,7 @@ public: * @param normal the normal vector at the point of intersection * @return true to continue, false to terminate */ - std::function report; + std::function report; }; class PhysicsRectQueryCallback @@ -106,15 +106,15 @@ public: virtual void removeAllJoints(); virtual void removeBody(PhysicsBody* body); - virtual void removeBodyByTag(int tag); + virtual void removeBody(int tag); virtual void removeAllBodies(); - void rayCast(PhysicsRayCastCallback& callback, Point point1, Point point2, void* data); - void rectQuery(PhysicsRectQueryCallback& callback, Rect rect, void* data); - Array* getShapesAtPoint(Point point) const; - PhysicsShape* getShapeAtPoint(Point point) const; + void rayCast(PhysicsRayCastCallback& callback, const Point& point1, const Point& point2, void* data); + void rectQuery(PhysicsRectQueryCallback& callback, const Rect& rect, void* data); + Array* getShapes(const Point& point) const; + PhysicsShape* getShape(const Point& point) const; Array* getAllBodies() const; - PhysicsBody* getBodyByTag(int tag) const; + PhysicsBody* getBody(int tag) const; /** Register a listener to receive contact callbacks*/ //inline void registerContactListener(EventListenerPhysicsContact* delegate) { _listener = delegate; } diff --git a/cocos/physics/chipmunk/CCPhysicsBodyInfo_chipmunk.cpp b/cocos/physics/chipmunk/CCPhysicsBodyInfo_chipmunk.cpp index 42ce5cc7ec..79ad011c17 100644 --- a/cocos/physics/chipmunk/CCPhysicsBodyInfo_chipmunk.cpp +++ b/cocos/physics/chipmunk/CCPhysicsBodyInfo_chipmunk.cpp @@ -27,18 +27,13 @@ NS_CC_BEGIN PhysicsBodyInfo::PhysicsBodyInfo() -: body(nullptr) +: _body(nullptr) { } PhysicsBodyInfo::~PhysicsBodyInfo() { - if (body) cpBodyFree(body); -} - -Clonable* PhysicsBodyInfo::clone() const -{ - return nullptr; + if (_body) cpBodyFree(_body); } NS_CC_END diff --git a/cocos/physics/chipmunk/CCPhysicsBodyInfo_chipmunk.h b/cocos/physics/chipmunk/CCPhysicsBodyInfo_chipmunk.h index 19e43c23fb..dd2d276ac5 100644 --- a/cocos/physics/chipmunk/CCPhysicsBodyInfo_chipmunk.h +++ b/cocos/physics/chipmunk/CCPhysicsBodyInfo_chipmunk.h @@ -34,16 +34,18 @@ NS_CC_BEGIN -class PhysicsBodyInfo : public Clonable +class PhysicsBodyInfo { public: - cpBody* body; + inline cpBody* getBody() const { return _body; } + inline void setBody(cpBody* body) { _body = body; } private: PhysicsBodyInfo(); ~PhysicsBodyInfo(); - Clonable* clone() const override; +private: + cpBody* _body; friend class PhysicsBody; }; diff --git a/cocos/physics/chipmunk/CCPhysicsContactInfo_chipmunk.cpp b/cocos/physics/chipmunk/CCPhysicsContactInfo_chipmunk.cpp index 283a4998ab..150c77a030 100644 --- a/cocos/physics/chipmunk/CCPhysicsContactInfo_chipmunk.cpp +++ b/cocos/physics/chipmunk/CCPhysicsContactInfo_chipmunk.cpp @@ -27,7 +27,7 @@ NS_CC_BEGIN PhysicsContactInfo::PhysicsContactInfo(PhysicsContact* contact) -: contact(contact) +: _contact(contact) { } diff --git a/cocos/physics/chipmunk/CCPhysicsContactInfo_chipmunk.h b/cocos/physics/chipmunk/CCPhysicsContactInfo_chipmunk.h index 7dc6561fec..4b01ad9d53 100644 --- a/cocos/physics/chipmunk/CCPhysicsContactInfo_chipmunk.h +++ b/cocos/physics/chipmunk/CCPhysicsContactInfo_chipmunk.h @@ -36,12 +36,15 @@ class PhysicsContact; class PhysicsContactInfo { public: - PhysicsContact* contact; + inline PhysicsContact* getContact() const { return _contact; } private: PhysicsContactInfo(PhysicsContact* contact); ~PhysicsContactInfo(); +private: + PhysicsContact* _contact; + friend class PhysicsContact; }; diff --git a/cocos/physics/chipmunk/CCPhysicsJointInfo_chipmunk.cpp b/cocos/physics/chipmunk/CCPhysicsJointInfo_chipmunk.cpp index 82053ce4b1..2f8b7817cf 100644 --- a/cocos/physics/chipmunk/CCPhysicsJointInfo_chipmunk.cpp +++ b/cocos/physics/chipmunk/CCPhysicsJointInfo_chipmunk.cpp @@ -27,16 +27,16 @@ #include NS_CC_BEGIN -std::map PhysicsJointInfo::map; +std::map PhysicsJointInfo::_map; PhysicsJointInfo::PhysicsJointInfo(PhysicsJoint* joint) -: joint(joint) +: _joint(joint) { } PhysicsJointInfo::~PhysicsJointInfo() { - for (cpConstraint* joint : joints) + for (cpConstraint* joint : _joints) { cpConstraintFree(joint); } @@ -46,21 +46,21 @@ void PhysicsJointInfo::add(cpConstraint* joint) { if (joint == nullptr) return; - joints.push_back(joint); - map.insert(std::pair(joint, this)); + _joints.push_back(joint); + _map.insert(std::pair(joint, this)); } void PhysicsJointInfo::remove(cpConstraint* joint) { if (joint == nullptr) return; - auto it = std::find(joints.begin(), joints.end(), joint); - if (it != joints.end()) + auto it = std::find(_joints.begin(), _joints.end(), joint); + if (it != _joints.end()) { - joints.erase(it); + _joints.erase(it); - auto mit = map.find(joint); - if (mit != map.end()) map.erase(mit); + auto mit = _map.find(joint); + if (mit != _map.end()) _map.erase(mit); cpConstraintFree(joint); } @@ -68,14 +68,14 @@ void PhysicsJointInfo::remove(cpConstraint* joint) void PhysicsJointInfo::removeAll() { - for (cpConstraint* joint : joints) + for (cpConstraint* joint : _joints) { - auto mit = map.find(joint); - if (mit != map.end()) map.erase(mit); + auto mit = _map.find(joint); + if (mit != _map.end()) _map.erase(mit); cpConstraintFree(joint); } - joints.clear(); + _joints.clear(); } NS_CC_END diff --git a/cocos/physics/chipmunk/CCPhysicsJointInfo_chipmunk.h b/cocos/physics/chipmunk/CCPhysicsJointInfo_chipmunk.h index 8116c48eae..016cd74d12 100644 --- a/cocos/physics/chipmunk/CCPhysicsJointInfo_chipmunk.h +++ b/cocos/physics/chipmunk/CCPhysicsJointInfo_chipmunk.h @@ -43,15 +43,19 @@ public: void remove(cpConstraint* shape); void removeAll(); -public: - std::vector joints; - PhysicsJoint* joint; - static std::map map; + PhysicsJoint* getJoint() const { return _joint; } + std::vector& getJoints() { return _joints; } + static std::map& getMap() { return _map; } private: PhysicsJointInfo(PhysicsJoint* joint); ~PhysicsJointInfo(); +private: + std::vector _joints; + PhysicsJoint* _joint; + static std::map _map; + friend class PhysicsJoint; }; diff --git a/cocos/physics/chipmunk/CCPhysicsShapeInfo_chipmunk.cpp b/cocos/physics/chipmunk/CCPhysicsShapeInfo_chipmunk.cpp index 74abfc75f1..d12a053181 100644 --- a/cocos/physics/chipmunk/CCPhysicsShapeInfo_chipmunk.cpp +++ b/cocos/physics/chipmunk/CCPhysicsShapeInfo_chipmunk.cpp @@ -27,27 +27,27 @@ #include NS_CC_BEGIN -std::map PhysicsShapeInfo::map; -cpBody* PhysicsShapeInfo::shareBody = nullptr; +std::map PhysicsShapeInfo::_map; +cpBody* PhysicsShapeInfo::_sharedBody = nullptr; PhysicsShapeInfo::PhysicsShapeInfo(PhysicsShape* shape) -: shape(shape) -, group(CP_NO_GROUP) +: _shape(shape) +, _group(CP_NO_GROUP) { - if (shareBody == nullptr) + if (_sharedBody == nullptr) { - shareBody = cpBodyNewStatic(); + _sharedBody = cpBodyNewStatic(); } - body = shareBody; + _body = _sharedBody; } PhysicsShapeInfo::~PhysicsShapeInfo() { - for (auto shape : shapes) + for (auto shape : _shapes) { - auto it = map.find(shape); - if (it != map.end()) map.erase(shape); + auto it = _map.find(shape); + if (it != _map.end()) _map.erase(shape); cpShapeFree(shape); } @@ -55,9 +55,9 @@ PhysicsShapeInfo::~PhysicsShapeInfo() void PhysicsShapeInfo::setGroup(cpGroup group) { - this->group = group; + this->_group = group; - for (cpShape* shape : shapes) + for (cpShape* shape : _shapes) { cpShapeSetGroup(shape, group); } @@ -65,12 +65,12 @@ void PhysicsShapeInfo::setGroup(cpGroup group) void PhysicsShapeInfo::setBody(cpBody* body) { - if (this->body != body) + if (this->_body != body) { - this->body = body; - for (cpShape* shape : shapes) + this->_body = body; + for (cpShape* shape : _shapes) { - cpShapeSetBody(shape, body == nullptr ? shareBody : body); + cpShapeSetBody(shape, body == nullptr ? _sharedBody : body); } } } @@ -79,22 +79,22 @@ void PhysicsShapeInfo::add(cpShape* shape) { if (shape == nullptr) return; - cpShapeSetGroup(shape, group); - shapes.push_back(shape); - map.insert(std::pair(shape, this)); + cpShapeSetGroup(shape, _group); + _shapes.push_back(shape); + _map.insert(std::pair(shape, this)); } void PhysicsShapeInfo::remove(cpShape* shape) { if (shape == nullptr) return; - auto it = std::find(shapes.begin(), shapes.end(), shape); - if (it != shapes.end()) + auto it = std::find(_shapes.begin(), _shapes.end(), shape); + if (it != _shapes.end()) { - shapes.erase(it); + _shapes.erase(it); - auto mit = map.find(shape); - if (mit != map.end()) map.erase(mit); + auto mit = _map.find(shape); + if (mit != _map.end()) _map.erase(mit); cpShapeFree(shape); } @@ -102,14 +102,14 @@ void PhysicsShapeInfo::remove(cpShape* shape) void PhysicsShapeInfo::removeAll() { - for (cpShape* shape : shapes) + for (cpShape* shape : _shapes) { - auto mit = map.find(shape); - if (mit != map.end()) map.erase(mit); + auto mit = _map.find(shape); + if (mit != _map.end()) _map.erase(mit); cpShapeFree(shape); } - shapes.clear(); + _shapes.clear(); } NS_CC_END diff --git a/cocos/physics/chipmunk/CCPhysicsShapeInfo_chipmunk.h b/cocos/physics/chipmunk/CCPhysicsShapeInfo_chipmunk.h index 83be655b02..b519fd378a 100644 --- a/cocos/physics/chipmunk/CCPhysicsShapeInfo_chipmunk.h +++ b/cocos/physics/chipmunk/CCPhysicsShapeInfo_chipmunk.h @@ -47,17 +47,25 @@ public: void setBody(cpBody* body); public: - std::vector shapes; - PhysicsShape* shape; - cpBody* body; - cpGroup group; - static std::map map; - static cpBody* shareBody; + PhysicsShape* getShape() const { return _shape; } + std::vector& getShapes() { return _shapes; } + cpBody* getBody() const { return _body; } + cpGroup getGourp() const { return _group; } + static std::map& getMap() { return _map; } + static cpBody* getSharedBody() { return _sharedBody; } private: PhysicsShapeInfo(PhysicsShape* shape); ~PhysicsShapeInfo(); +private: + std::vector _shapes; + PhysicsShape* _shape; + cpBody* _body; + cpGroup _group; + static std::map _map; + static cpBody* _sharedBody; + friend class PhysicsShape; }; diff --git a/cocos/physics/chipmunk/CCPhysicsWorldInfo_chipmunk.cpp b/cocos/physics/chipmunk/CCPhysicsWorldInfo_chipmunk.cpp index fecf2009aa..a12e473ec1 100644 --- a/cocos/physics/chipmunk/CCPhysicsWorldInfo_chipmunk.cpp +++ b/cocos/physics/chipmunk/CCPhysicsWorldInfo_chipmunk.cpp @@ -29,12 +29,12 @@ NS_CC_BEGIN #define PHYSICS_WORLD_INFO_FUNCTION_IMPLEMENTS(name, type) \ void PhysicsWorldInfo::add##name(cp##type* data) \ { \ - if (!cpSpaceContains##type(space, data)) cpSpaceAdd##type(space, data); \ + if (!cpSpaceContains##type(_space, data)) cpSpaceAdd##type(_space, data); \ } \ \ void PhysicsWorldInfo::remove##name(cp##type* data) \ { \ - if (cpSpaceContains##type(space, data)) cpSpaceRemove##type(space, data); \ + if (cpSpaceContains##type(_space, data)) cpSpaceRemove##type(_space, data); \ } \ PHYSICS_WORLD_INFO_FUNCTION_IMPLEMENTS(Shape, Shape) @@ -43,12 +43,12 @@ PHYSICS_WORLD_INFO_FUNCTION_IMPLEMENTS(Joint, Constraint) PhysicsWorldInfo::PhysicsWorldInfo() { - space = cpSpaceNew(); + _space = cpSpaceNew(); } PhysicsWorldInfo::~PhysicsWorldInfo() { - cpSpaceFree(space); + cpSpaceFree(_space); } NS_CC_END diff --git a/cocos/physics/chipmunk/CCPhysicsWorldInfo_chipmunk.h b/cocos/physics/chipmunk/CCPhysicsWorldInfo_chipmunk.h index 5e6c78bc97..b50e1a3240 100644 --- a/cocos/physics/chipmunk/CCPhysicsWorldInfo_chipmunk.h +++ b/cocos/physics/chipmunk/CCPhysicsWorldInfo_chipmunk.h @@ -36,8 +36,7 @@ NS_CC_BEGIN class PhysicsWorldInfo { public: - cpSpace* space; - + cpSpace* getSpace() const { return _space; } void addShape(cpShape* shape); void removeShape(cpShape* shape); void addBody(cpBody* body); @@ -49,6 +48,9 @@ private: PhysicsWorldInfo(); ~PhysicsWorldInfo(); +private: + cpSpace* _space; + friend class PhysicsWorld; }; diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp index 99ce9d0c90..88b17e365e 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp @@ -602,7 +602,7 @@ void PhysicsDemoRayCast::changeModeCallback(Object* sender) } } -bool PhysicsDemoRayCast::anyRay(PhysicsWorld& world, PhysicsRayCastCallback::Info& info, void* data) +bool PhysicsDemoRayCast::anyRay(PhysicsWorld& world, const PhysicsRayCastCallback::Info& info, void* data) { *((Point*)data) = info.contact; return false; @@ -620,7 +620,7 @@ private: PhysicsDemoNearestRayCastCallback::PhysicsDemoNearestRayCastCallback() : _friction(1.0f) { - report = [this](PhysicsWorld& world, PhysicsRayCastCallback::Info& info, void* data)->bool + report = [this](PhysicsWorld& world, const PhysicsRayCastCallback::Info& info, void* data)->bool { if (_friction > info.fraction) { @@ -650,7 +650,7 @@ public: PhysicsDemoMultiRayCastCallback::PhysicsDemoMultiRayCastCallback() : num(0) { - report = [this](PhysicsWorld& world, PhysicsRayCastCallback::Info& info, void* data)->bool + report = [this](PhysicsWorld& world, const PhysicsRayCastCallback::Info& info, void* data)->bool { if (num < MAX_MULTI_RAYCAST_NUM) { @@ -1089,7 +1089,7 @@ void PhysicsDemoSlice::onEnter() addChild(box); } -bool PhysicsDemoSlice::slice(PhysicsWorld &world, PhysicsRayCastCallback::Info &info, void *data) +bool PhysicsDemoSlice::slice(PhysicsWorld &world, const PhysicsRayCastCallback::Info &info, void *data) { if (info.shape->getBody()->getTag() != _sliceTag) { @@ -1150,6 +1150,8 @@ void PhysicsDemoSlice::clipPoly(PhysicsShapePolygon* shape, Point normal, float polyon->setAngularVelocity(body->getAngularVelocity()); polyon->setTag(_sliceTag); addChild(node); + + delete[] points; } void PhysicsDemoSlice::onTouchEnded(Touch *touch, Event *event) @@ -1171,7 +1173,7 @@ std::string PhysicsDemoSlice::subtitle() void PhysicsDemoWater::onEnter() { - + PhysicsDemo::onEnter(); } std::string PhysicsDemoWater::title() diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h index 425dbc56f9..7355045cad 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h @@ -96,7 +96,7 @@ public: void changeModeCallback(Object* sender); - bool anyRay(PhysicsWorld& world, PhysicsRayCastCallback::Info& info, void* data); + bool anyRay(PhysicsWorld& world, const PhysicsRayCastCallback::Info& info, void* data); private: float _angle; @@ -150,7 +150,7 @@ public: std::string title() override; std::string subtitle() override; - bool slice(PhysicsWorld& world, PhysicsRayCastCallback::Info& info, void* data); + bool slice(PhysicsWorld& world, const PhysicsRayCastCallback::Info& info, void* data); void clipPoly(PhysicsShapePolygon* shape, Point normal, float distance); bool onTouchBegan(Touch *touch, Event *event);