issue #2771: change some coding style

This commit is contained in:
boyu0 2013-11-05 20:02:58 +08:00
parent 8c62bf813f
commit af129e25f0
24 changed files with 325 additions and 286 deletions

View File

@ -96,6 +96,16 @@ bool Point::operator!=(const Point& right)
return this->x != right.x || this->y != right.y; 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 Point Point::operator*(float a) const
{ {
return Point(this->x * a, this->y * a); return Point(this->x * a, this->y * a);

View File

@ -123,6 +123,16 @@ public:
* @lua NA * @lua NA
*/ */
bool operator!=(const Point& right); 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 * @js NA
* @lua NA * @lua NA

View File

@ -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(); PhysicsBody* body = new PhysicsBody();
if (body && body->init()) if (body && body->init())
@ -171,7 +171,7 @@ PhysicsBody* PhysicsBody::createCircle(float radius, PhysicsMaterial material, P
return nullptr; 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(); PhysicsBody* body = new PhysicsBody();
if (body && body->init()) if (body && body->init())
@ -185,7 +185,7 @@ PhysicsBody* PhysicsBody::createBox(Size size, PhysicsMaterial material, Point o
return nullptr; 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(); PhysicsBody* body = new PhysicsBody();
if (body && body->init()) if (body && body->init())
@ -199,7 +199,7 @@ PhysicsBody* PhysicsBody::createPolygon(Point* points, int count, PhysicsMateria
return nullptr; 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(); PhysicsBody* body = new PhysicsBody();
if (body && body->init()) if (body && body->init())
@ -214,7 +214,7 @@ PhysicsBody* PhysicsBody::createEdgeSegment(Point a, Point b, PhysicsMaterial ma
return nullptr; 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(); PhysicsBody* body = new PhysicsBody();
if (body && body->init()) if (body && body->init())
@ -230,7 +230,7 @@ PhysicsBody* PhysicsBody::createEdgeBox(Size size, PhysicsMaterial material, flo
return nullptr; 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(); PhysicsBody* body = new PhysicsBody();
if (body && body->init()) if (body && body->init())
@ -246,7 +246,7 @@ PhysicsBody* PhysicsBody::createEdgePolygon(Point* points, int count, PhysicsMat
return nullptr; 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(); PhysicsBody* body = new PhysicsBody();
if (body && body->init()) if (body && body->init())
@ -272,9 +272,9 @@ bool PhysicsBody::init()
CC_BREAK_IF(_shapes == nullptr); CC_BREAK_IF(_shapes == nullptr);
_shapes->retain(); _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; return true;
} while (false); } while (false);
@ -289,19 +289,19 @@ void PhysicsBody::setDynamic(bool dynamic)
_dynamic = dynamic; _dynamic = dynamic;
if (dynamic) if (dynamic)
{ {
cpBodySetMass(_info->body, _mass); cpBodySetMass(_info->getBody(), _mass);
if (_world != nullptr) if (_world != nullptr)
{ {
cpSpaceAddBody(_world->_info->space, _info->body); cpSpaceAddBody(_world->_info->getSpace(), _info->getBody());
} }
}else }else
{ {
cpBodySetMass(_info->body, PHYSICS_INFINITY); cpBodySetMass(_info->getBody(), PHYSICS_INFINITY);
if (_world != nullptr) 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) if (_rotationEnable != enable)
{ {
cpBodySetMoment(_info->body, enable ? _moment : PHYSICS_INFINITY); cpBodySetMoment(_info->getBody(), enable ? _moment : PHYSICS_INFINITY);
_rotationEnable = enable; _rotationEnable = enable;
} }
} }
@ -338,23 +338,23 @@ void PhysicsBody::setGravityEnable(bool enable)
void PhysicsBody::setPosition(Point position) void PhysicsBody::setPosition(Point position)
{ {
cpBodySetPos(_info->body, PhysicsHelper::point2cpv(position)); cpBodySetPos(_info->getBody(), PhysicsHelper::point2cpv(position));
} }
void PhysicsBody::setRotation(float rotation) void PhysicsBody::setRotation(float rotation)
{ {
cpBodySetAngle(_info->body, PhysicsHelper::float2cpfloat(rotation)); cpBodySetAngle(_info->getBody(), PhysicsHelper::float2cpfloat(rotation));
} }
Point PhysicsBody::getPosition() const Point PhysicsBody::getPosition() const
{ {
cpVect vec = cpBodyGetPos(_info->body); cpVect vec = cpBodyGetPos(_info->getBody());
return PhysicsHelper::cpv2point(vec); return PhysicsHelper::cpv2point(vec);
} }
float PhysicsBody::getRotation() const 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) PhysicsShape* PhysicsBody::addShape(PhysicsShape* shape)
@ -388,29 +388,29 @@ PhysicsShape* PhysicsBody::addShape(PhysicsShape* shape)
return shape; return shape;
} }
void PhysicsBody::applyForce(Point force) void PhysicsBody::applyForce(const Vect& force)
{ {
applyForce(force, Point::ZERO); 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()); 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) void PhysicsBody::applyTorque(float torque)
{ {
cpBodySetTorque(_info->body, PhysicsHelper::float2cpfloat(torque)); cpBodySetTorque(_info->getBody(), PhysicsHelper::float2cpfloat(torque));
} }
void PhysicsBody::setMass(float mass) 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) 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) void PhysicsBody::addMoment(float moment)
@ -522,58 +522,58 @@ void PhysicsBody::addMoment(float moment)
if (_rotationEnable) 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() 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) void PhysicsBody::setAngularVelocity(float velocity)
{ {
cpBodySetAngVel(_info->body, PhysicsHelper::float2cpfloat(velocity)); cpBodySetAngVel(_info->getBody(), PhysicsHelper::float2cpfloat(velocity));
} }
float PhysicsBody::getAngularVelocity() float PhysicsBody::getAngularVelocity()
{ {
return PhysicsHelper::cpfloat2float(cpBodyGetAngVel(_info->body)); return PhysicsHelper::cpfloat2float(cpBodyGetAngVel(_info->getBody()));
} }
void PhysicsBody::setVelocityLimit(float limit) void PhysicsBody::setVelocityLimit(float limit)
{ {
cpBodySetVelLimit(_info->body, PhysicsHelper::float2cpfloat(limit)); cpBodySetVelLimit(_info->getBody(), PhysicsHelper::float2cpfloat(limit));
} }
float PhysicsBody::getVelocityLimit() float PhysicsBody::getVelocityLimit()
{ {
return PhysicsHelper::cpfloat2float(cpBodyGetVelLimit(_info->body)); return PhysicsHelper::cpfloat2float(cpBodyGetVelLimit(_info->getBody()));
} }
void PhysicsBody::setAngularVelocityLimit(float limit) void PhysicsBody::setAngularVelocityLimit(float limit)
{ {
cpBodySetVelLimit(_info->body, PhysicsHelper::float2cpfloat(limit)); cpBodySetVelLimit(_info->getBody(), PhysicsHelper::float2cpfloat(limit));
} }
float PhysicsBody::getAngularVelocityLimit() float PhysicsBody::getAngularVelocityLimit()
{ {
return PhysicsHelper::cpfloat2float(cpBodyGetAngVelLimit(_info->body)); return PhysicsHelper::cpfloat2float(cpBodyGetAngVelLimit(_info->getBody()));
} }
void PhysicsBody::setMoment(float moment) void PhysicsBody::setMoment(float moment)
@ -583,11 +583,11 @@ void PhysicsBody::setMoment(float moment)
if (_rotationEnable) 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) for (auto child : *_shapes)
{ {
@ -601,7 +601,7 @@ PhysicsShape* PhysicsBody::getShapeByTag(int tag) const
return nullptr; return nullptr;
} }
void PhysicsBody::removeShapeByTag(int tag) void PhysicsBody::removeShape(int tag)
{ {
for (auto child : *_shapes) for (auto child : *_shapes)
{ {
@ -685,7 +685,7 @@ void PhysicsBody::setEnable(bool enable)
bool PhysicsBody::isResting() const bool PhysicsBody::isResting() const
{ {
return cpBodyIsSleeping(_info->body) == cpTrue; return cpBodyIsSleeping(_info->getBody()) == cpTrue;
} }
void PhysicsBody::update(float delta) void PhysicsBody::update(float delta)
@ -693,9 +693,9 @@ void PhysicsBody::update(float delta)
// damping compute // damping compute
if (_dynamic) if (_dynamic)
{ {
_info->body->v.x *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f); _info->getBody()->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->getBody()->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()->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) 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) 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) #elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D)

View File

@ -59,62 +59,62 @@ public:
/** /**
* @brief Create a body contains a circle shape. * @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. * @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. * @brief Create a body contains a polygon shape.
* points is an array of Point structs defining a convex hull with a clockwise winding. * 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. * @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. * @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. * @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. * @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); virtual PhysicsShape* addShape(PhysicsShape* shape);
/** /**
* @brief Applies a immediate force to body. * @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. * @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. * @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. * @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. * @brief Applies a torque force to body.
*/ */
virtual void applyTorque(float torque); virtual void applyTorque(float torque);
virtual void setVelocity(Point velocity); virtual void setVelocity(const Vect& velocity);
virtual Point getVelocity(); virtual Point getVelocity();
virtual void setAngularVelocity(float velocity); virtual void setAngularVelocity(float velocity);
virtual Point getVelocityAtLocalPoint(Point point); virtual Point getVelocityAtLocalPoint(const Point& point);
virtual Point getVelocityAtWorldPoint(Point point); virtual Point getVelocityAtWorldPoint(const Point& point);
virtual float getAngularVelocity(); virtual float getAngularVelocity();
virtual void setVelocityLimit(float limit); virtual void setVelocityLimit(float limit);
virtual float getVelocityLimit(); virtual float getVelocityLimit();
@ -129,12 +129,12 @@ public:
* @brief get the first body shapes. * @brief get the first body shapes.
*/ */
inline PhysicsShape* getShape() const { return _shapes->count() >= 1 ? dynamic_cast<PhysicsShape*>(_shapes->getObjectAtIndex(0)) : nullptr; } inline PhysicsShape* getShape() const { return _shapes->count() >= 1 ? dynamic_cast<PhysicsShape*>(_shapes->getObjectAtIndex(0)) : nullptr; }
PhysicsShape* getShapeByTag(int tag) const; PhysicsShape* getShape(int tag) const;
/* /*
* @brief remove a shape from body * @brief remove a shape from body
*/ */
void removeShape(PhysicsShape* shape); void removeShape(PhysicsShape* shape);
void removeShapeByTag(int tag); void removeShape(int tag);
/* /*
* @brief remove all shapes * @brief remove all shapes
*/ */

View File

@ -150,9 +150,9 @@ void PhysicsContactPreSolve::setFriction(float friction)
static_cast<cpArbiter*>(_contactInfo)->u = friction; static_cast<cpArbiter*>(_contactInfo)->u = friction;
} }
void PhysicsContactPreSolve::setSurfaceVelocity(Point surfaceVelocity) void PhysicsContactPreSolve::setSurfaceVelocity(const Vect& velocity)
{ {
static_cast<cpArbiter*>(_contactInfo)->surface_vr = PhysicsHelper::point2cpv(surfaceVelocity); static_cast<cpArbiter*>(_contactInfo)->surface_vr = PhysicsHelper::point2cpv(velocity);
} }
void PhysicsContactPreSolve::ignore() void PhysicsContactPreSolve::ignore()

View File

@ -138,7 +138,7 @@ public:
Point getSurfaceVelocity() const; Point getSurfaceVelocity() const;
void setElasticity(float elasticity); void setElasticity(float elasticity);
void setFriction(float friction); void setFriction(float friction);
void setSurfaceVelocity(Point surfaceVelocity); void setSurfaceVelocity(const Vect& velocity);
void ignore(); void ignore();
private: private:

View File

@ -197,13 +197,13 @@ bool PhysicsJointFixed::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr)
getBodyNode(b)->setPosition(anchr); getBodyNode(b)->setPosition(anchr);
// add a pivot joint to fixed two body together // 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)); PhysicsHelper::point2cpv(anchr));
CC_BREAK_IF(joint == nullptr); CC_BREAK_IF(joint == nullptr);
_info->add(joint); _info->add(joint);
// add a gear joint to make two body have the same rotation. // 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); CC_BREAK_IF(joint == nullptr);
_info->add(joint); _info->add(joint);
@ -233,7 +233,7 @@ bool PhysicsJointPin::init(PhysicsBody *a, PhysicsBody *b, const Point& anchr)
do do
{ {
CC_BREAK_IF(!PhysicsJoint::init(a, b)); 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)); PhysicsHelper::point2cpv(anchr));
CC_BREAK_IF(joint == nullptr); CC_BREAK_IF(joint == nullptr);
@ -248,12 +248,12 @@ bool PhysicsJointPin::init(PhysicsBody *a, PhysicsBody *b, const Point& anchr)
void PhysicsJointPin::setMaxForce(float force) void PhysicsJointPin::setMaxForce(float force)
{ {
_info->joints.front()->maxForce = PhysicsHelper::float2cpfloat(force); _info->getJoints().front()->maxForce = PhysicsHelper::float2cpfloat(force);
} }
float PhysicsJointPin::getMaxForce() const 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) 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)); 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(grooveA),
PhysicsHelper::point2cpv(grooveB), PhysicsHelper::point2cpv(grooveB),
PhysicsHelper::point2cpv(anchr)); PhysicsHelper::point2cpv(anchr));
@ -310,7 +310,7 @@ bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1
{ {
CC_BREAK_IF(!PhysicsJoint::init(a, b)); 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(anchr1),
PhysicsHelper::point2cpv(anchr2), PhysicsHelper::point2cpv(anchr2),
0, 0,
@ -328,22 +328,22 @@ bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1
float PhysicsJointLimit::getMin() const float PhysicsJointLimit::getMin() const
{ {
return PhysicsHelper::cpfloat2float(cpSlideJointGetMin(_info->joints.front())); return PhysicsHelper::cpfloat2float(cpSlideJointGetMin(_info->getJoints().front()));
} }
void PhysicsJointLimit::setMin(float min) void PhysicsJointLimit::setMin(float min)
{ {
cpSlideJointSetMin(_info->joints.front(), PhysicsHelper::float2cpfloat(min)); cpSlideJointSetMin(_info->getJoints().front(), PhysicsHelper::float2cpfloat(min));
} }
float PhysicsJointLimit::getMax() const float PhysicsJointLimit::getMax() const
{ {
return PhysicsHelper::cpfloat2float(cpSlideJointGetMax(_info->joints.front())); return PhysicsHelper::cpfloat2float(cpSlideJointGetMax(_info->getJoints().front()));
} }
void PhysicsJointLimit::setMax(float max) 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) 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)); CC_BREAK_IF(!PhysicsJoint::init(a, b));
cpConstraint* joint = cpPinJointNew(getBodyInfo(a)->body, cpConstraint* joint = cpPinJointNew(getBodyInfo(a)->getBody(),
getBodyInfo(b)->body, getBodyInfo(b)->getBody(),
PhysicsHelper::point2cpv(anchr1), PhysicsHelper::point2cpv(anchr2)); PhysicsHelper::point2cpv(anchr1), PhysicsHelper::point2cpv(anchr2));
CC_BREAK_IF(joint == nullptr); CC_BREAK_IF(joint == nullptr);

View File

@ -47,6 +47,9 @@ namespace cocos2d
{ {
extern const float PHYSICS_INFINITY; extern const float PHYSICS_INFINITY;
class Point;
typedef Point Vect;
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) #if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
static const int PHYSICS_CONTACT_POINT_MAX = 4; static const int PHYSICS_CONTACT_POINT_MAX = 4;
#else #else

View File

@ -105,7 +105,7 @@ void PhysicsShape::setMoment(float moment)
_moment = moment; _moment = moment;
} }
void PhysicsShape::setMaterial(PhysicsMaterial material) void PhysicsShape::setMaterial(const PhysicsMaterial& material)
{ {
setDensity(material.density); setDensity(material.density);
setRestitution(material.restitution); setRestitution(material.restitution);
@ -216,7 +216,7 @@ void PhysicsShape::setRestitution(float restitution)
{ {
_material.restitution = restitution; _material.restitution = restitution;
for (cpShape* shape : _info->shapes) for (cpShape* shape : _info->getShapes())
{ {
cpShapeSetElasticity(shape, PhysicsHelper::float2cpfloat(restitution)); cpShapeSetElasticity(shape, PhysicsHelper::float2cpfloat(restitution));
} }
@ -226,14 +226,14 @@ void PhysicsShape::setFriction(float friction)
{ {
_material.friction = friction; _material.friction = friction;
for (cpShape* shape : _info->shapes) for (cpShape* shape : _info->getShapes())
{ {
cpShapeSetFriction(shape, PhysicsHelper::float2cpfloat(friction)); 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]; cpVect* cpvs = new cpVect[count];
cpRecenterPoly(count, PhysicsHelper::points2cpvs(points, cpvs, count)); cpRecenterPoly(count, PhysicsHelper::points2cpvs(points, cpvs, count));
@ -251,7 +251,7 @@ Point* PhysicsShape::recenterPoints(Point* points, int count, Point center)
return points; return points;
} }
Point PhysicsShape::getPolyonCenter(Point* points, int count) Point PhysicsShape::getPolyonCenter(const Point* points, int count)
{ {
cpVect* cpvs = new cpVect[count]; cpVect* cpvs = new cpVect[count];
cpVect center = cpCentroidForPoly(count, PhysicsHelper::points2cpvs(points, cpvs, count)); cpVect center = cpCentroidForPoly(count, PhysicsHelper::points2cpvs(points, cpvs, count));
@ -280,14 +280,14 @@ void PhysicsShape::setBody(PhysicsBody *body)
_body = nullptr; _body = nullptr;
}else }else
{ {
_info->setBody(body->_info->body); _info->setBody(body->_info->getBody());
//_info->setGroup(body->_info->group); //_info->setGroup(body->_info->group);
_body = body; _body = body;
} }
} }
// PhysicsShapeCircle // 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(); PhysicsShapeCircle* shape = new PhysicsShapeCircle();
if (shape && shape->init(radius, material, offset)) if (shape && shape->init(radius, material, offset))
@ -300,13 +300,13 @@ PhysicsShapeCircle* PhysicsShapeCircle::create(float radius, PhysicsMaterial mat
return nullptr; 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 do
{ {
CC_BREAK_IF(!PhysicsShape::init(Type::CIRCLE)); 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); CC_BREAK_IF(shape == nullptr);
@ -328,7 +328,7 @@ float PhysicsShapeCircle::calculateArea(float radius)
return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, 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 return mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForCircle(PhysicsHelper::float2cpfloat(mass), : PhysicsHelper::cpfloat2float(cpMomentForCircle(PhysicsHelper::float2cpfloat(mass),
@ -339,12 +339,12 @@ float PhysicsShapeCircle::calculateMoment(float mass, float radius, Point offset
float PhysicsShapeCircle::calculateDefaultArea() float PhysicsShapeCircle::calculateDefaultArea()
{ {
return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, cpCircleShapeGetRadius(_info->shapes.front()))); return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, cpCircleShapeGetRadius(_info->getShapes().front())));
} }
float PhysicsShapeCircle::calculateDefaultMoment() float PhysicsShapeCircle::calculateDefaultMoment()
{ {
cpShape* shape = _info->shapes.front(); cpShape* shape = _info->getShapes().front();
return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForCircle(PhysicsHelper::float2cpfloat(_mass), : PhysicsHelper::cpfloat2float(cpMomentForCircle(PhysicsHelper::float2cpfloat(_mass),
@ -355,16 +355,16 @@ float PhysicsShapeCircle::calculateDefaultMoment()
float PhysicsShapeCircle::getRadius() const float PhysicsShapeCircle::getRadius() const
{ {
return PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(_info->shapes.front())); return PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(_info->getShapes().front()));
} }
Point PhysicsShapeCircle::getOffset() Point PhysicsShapeCircle::getOffset()
{ {
return PhysicsHelper::cpv2point(cpCircleShapeGetOffset(_info->shapes.front())); return PhysicsHelper::cpv2point(cpCircleShapeGetOffset(_info->getShapes().front()));
} }
// PhysicsShapeEdgeSegment // 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(); PhysicsShapeEdgeSegment* shape = new PhysicsShapeEdgeSegment();
if (shape && shape->init(a, b, material, border)) if (shape && shape->init(a, b, material, border))
@ -377,13 +377,13 @@ PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(Point a, Point b, Physi
return nullptr; 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 do
{ {
CC_BREAK_IF(!PhysicsShape::init(Type::EDGESEGMENT)); CC_BREAK_IF(!PhysicsShape::init(Type::EDGESEGMENT));
cpShape* shape = cpSegmentShapeNew(_info->shareBody, cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(),
PhysicsHelper::point2cpv(a), PhysicsHelper::point2cpv(a),
PhysicsHelper::point2cpv(b), PhysicsHelper::point2cpv(b),
PhysicsHelper::float2cpfloat(border)); PhysicsHelper::float2cpfloat(border));
@ -407,12 +407,12 @@ bool PhysicsShapeEdgeSegment::init(Point a, Point b, PhysicsMaterial material/*
Point PhysicsShapeEdgeSegment::getPointA() const Point PhysicsShapeEdgeSegment::getPointA() const
{ {
return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->shapes.front()))->ta); return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->getShapes().front()))->ta);
} }
Point PhysicsShapeEdgeSegment::getPointB() const Point PhysicsShapeEdgeSegment::getPointB() const
{ {
return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->shapes.front()))->tb); return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->getShapes().front()))->tb);
} }
Point PhysicsShapeEdgeSegment::getCenter() Point PhysicsShapeEdgeSegment::getCenter()
@ -421,7 +421,7 @@ Point PhysicsShapeEdgeSegment::getCenter()
} }
// PhysicsShapeBox // 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(); PhysicsShapeBox* shape = new PhysicsShapeBox();
if (shape && shape->init(size, material, offset)) if (shape && shape->init(size, material, offset))
@ -434,7 +434,7 @@ PhysicsShapeBox* PhysicsShapeBox::create(Size size, PhysicsMaterial material/* =
return nullptr; 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 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} {-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); CC_BREAK_IF(shape == nullptr);
@ -465,7 +465,7 @@ bool PhysicsShapeBox::init(Size size, PhysicsMaterial material/* = MaterialDefau
return false; return false;
} }
float PhysicsShapeBox::calculateArea(Size size) float PhysicsShapeBox::calculateArea(const Size& size)
{ {
cpVect wh = PhysicsHelper::size2cpv(size); cpVect wh = PhysicsHelper::size2cpv(size);
cpVect vec[4] = cpVect vec[4] =
@ -475,7 +475,7 @@ float PhysicsShapeBox::calculateArea(Size size)
return PhysicsHelper::cpfloat2float(cpAreaForPoly(4, vec)); 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 wh = PhysicsHelper::size2cpv(size);
cpVect vec[4] = cpVect vec[4] =
@ -492,20 +492,20 @@ float PhysicsShapeBox::calculateMoment(float mass, Size size, Point offset)
float PhysicsShapeBox::calculateDefaultArea() float PhysicsShapeBox::calculateDefaultArea()
{ {
cpShape* shape = _info->shapes.front(); cpShape* shape = _info->getShapes().front();
return PhysicsHelper::cpfloat2float(cpAreaForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts)); return PhysicsHelper::cpfloat2float(cpAreaForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts));
} }
float PhysicsShapeBox::calculateDefaultMoment() float PhysicsShapeBox::calculateDefaultMoment()
{ {
cpShape* shape = _info->shapes.front(); cpShape* shape = _info->getShapes().front();
return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero)); : PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero));
} }
Point* PhysicsShapeBox::getPoints(Point* points) const 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 PhysicsHelper::cpvs2points(((cpPolyShape*)shape)->verts, points, ((cpPolyShape*)shape)->numVerts);
return points; return points;
@ -513,13 +513,13 @@ Point* PhysicsShapeBox::getPoints(Point* points) const
Size PhysicsShapeBox::getSize() 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)), return PhysicsHelper::cpv2size(cpv(cpvdist(cpPolyShapeGetVert(shape, 0), cpPolyShapeGetVert(shape, 1)),
cpvdist(cpPolyShapeGetVert(shape, 1), cpPolyShapeGetVert(shape, 2)))); cpvdist(cpPolyShapeGetVert(shape, 1), cpPolyShapeGetVert(shape, 2))));
} }
// PhysicsShapePolygon // 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(); PhysicsShapePolygon* shape = new PhysicsShapePolygon();
if (shape && shape->init(points, count, material, offset)) if (shape && shape->init(points, count, material, offset))
@ -532,7 +532,7 @@ PhysicsShapePolygon* PhysicsShapePolygon::create(Point* points, int count, Physi
return nullptr; 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 do
{ {
@ -540,7 +540,7 @@ bool PhysicsShapePolygon::init(Point* points, int count, PhysicsMaterial materia
cpVect* vecs = new cpVect[count]; cpVect* vecs = new cpVect[count];
PhysicsHelper::points2cpvs(points, vecs, 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_SAFE_DELETE(vecs);
CC_BREAK_IF(shape == nullptr); CC_BREAK_IF(shape == nullptr);
@ -560,7 +560,7 @@ bool PhysicsShapePolygon::init(Point* points, int count, PhysicsMaterial materia
return false; return false;
} }
float PhysicsShapePolygon::calculateArea(Point* points, int count) float PhysicsShapePolygon::calculateArea(const Point* points, int count)
{ {
cpVect* vecs = new cpVect[count]; cpVect* vecs = new cpVect[count];
PhysicsHelper::points2cpvs(points, vecs, count); PhysicsHelper::points2cpvs(points, vecs, count);
@ -570,7 +570,7 @@ float PhysicsShapePolygon::calculateArea(Point* points, int count)
return area; 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]; cpVect* vecs = new cpVect[count];
PhysicsHelper::points2cpvs(points, vecs, count); PhysicsHelper::points2cpvs(points, vecs, count);
@ -583,31 +583,31 @@ float PhysicsShapePolygon::calculateMoment(float mass, Point* points, int count,
float PhysicsShapePolygon::calculateDefaultArea() float PhysicsShapePolygon::calculateDefaultArea()
{ {
cpShape* shape = _info->shapes.front(); cpShape* shape = _info->getShapes().front();
return PhysicsHelper::cpfloat2float(cpAreaForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts)); return PhysicsHelper::cpfloat2float(cpAreaForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts));
} }
float PhysicsShapePolygon::calculateDefaultMoment() float PhysicsShapePolygon::calculateDefaultMoment()
{ {
cpShape* shape = _info->shapes.front(); cpShape* shape = _info->getShapes().front();
return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero)); : PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero));
} }
Point PhysicsShapePolygon::getPoint(int i) const 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 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); return PhysicsHelper::cpvs2points(((cpPolyShape*)shape)->verts, points, ((cpPolyShape*)shape)->numVerts);
} }
int PhysicsShapePolygon::getPointsCount() const int PhysicsShapePolygon::getPointsCount() const
{ {
return ((cpPolyShape*)_info->shapes.front())->numVerts; return ((cpPolyShape*)_info->getShapes().front())->numVerts;
} }
Point PhysicsShapePolygon::getCenter() Point PhysicsShapePolygon::getCenter()
@ -616,7 +616,7 @@ Point PhysicsShapePolygon::getCenter()
} }
// PhysicsShapeEdgeBox // 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(); PhysicsShapeEdgeBox* shape = new PhysicsShapeEdgeBox();
if (shape && shape->init(size, material, border, offset)) if (shape && shape->init(size, material, border, offset))
@ -629,7 +629,7 @@ PhysicsShapeEdgeBox* PhysicsShapeEdgeBox::create(Size size, PhysicsMaterial mate
return nullptr; 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 do
{ {
@ -644,7 +644,7 @@ bool PhysicsShapeEdgeBox::init(Size size, PhysicsMaterial material/* = MaterialD
int i = 0; int i = 0;
for (; i < 4; ++i) 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)); PhysicsHelper::float2cpfloat(border));
CC_BREAK_IF(shape == nullptr); CC_BREAK_IF(shape == nullptr);
_info->add(shape); _info->add(shape);
@ -664,7 +664,7 @@ bool PhysicsShapeEdgeBox::init(Size size, PhysicsMaterial material/* = MaterialD
} }
// PhysicsShapeEdgeBox // 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(); PhysicsShapeEdgePolygon* shape = new PhysicsShapeEdgePolygon();
if (shape && shape->init(points, count, material, border)) if (shape && shape->init(points, count, material, border))
@ -677,7 +677,7 @@ PhysicsShapeEdgePolygon* PhysicsShapeEdgePolygon::create(Point* points, int coun
return nullptr; 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; cpVect* vec = nullptr;
do do
@ -691,7 +691,7 @@ bool PhysicsShapeEdgePolygon::init(Point* points, int count, PhysicsMaterial mat
int i = 0; int i = 0;
for (; i < count; ++i) 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)); PhysicsHelper::float2cpfloat(border));
CC_BREAK_IF(shape == nullptr); CC_BREAK_IF(shape == nullptr);
cpShapeSetElasticity(shape, 1.0f); cpShapeSetElasticity(shape, 1.0f);
@ -722,11 +722,11 @@ Point PhysicsShapeEdgePolygon::getCenter()
int PhysicsShapeEdgePolygon::getPointsCount() const int PhysicsShapeEdgePolygon::getPointsCount() const
{ {
return _info->shapes.size() + 1; return _info->getShapes().size() + 1;
} }
// PhysicsShapeEdgeChain // 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(); PhysicsShapeEdgeChain* shape = new PhysicsShapeEdgeChain();
if (shape && shape->init(points, count, material, border)) if (shape && shape->init(points, count, material, border))
@ -739,7 +739,7 @@ PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(Point* points, int count, P
return nullptr; 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; cpVect* vec = nullptr;
do do
@ -753,7 +753,7 @@ bool PhysicsShapeEdgeChain::init(Point* points, int count, PhysicsMaterial mater
int i = 0; int i = 0;
for (; i < count - 1; ++i) 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)); PhysicsHelper::float2cpfloat(border));
CC_BREAK_IF(shape == nullptr); CC_BREAK_IF(shape == nullptr);
cpShapeSetElasticity(shape, 1.0f); cpShapeSetElasticity(shape, 1.0f);
@ -783,23 +783,23 @@ Point PhysicsShapeEdgeChain::getCenter()
int PhysicsShapeEdgeChain::getPointsCount() const int PhysicsShapeEdgeChain::getPointsCount() const
{ {
return _info->shapes.size() + 1; return _info->getShapes().size() + 1;
} }
void PhysicsShape::setGroup(int group) void PhysicsShape::setGroup(int group)
{ {
if (group < 0) if (group < 0)
{ {
for (auto shape : _info->shapes) for (auto shape : _info->getShapes())
{ {
cpShapeSetGroup(shape, (cpGroup)group); 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))) if (cpShapePointQuery(shape, PhysicsHelper::point2cpv(point)))
{ {

View File

@ -92,16 +92,16 @@ public:
void setDensity(float density); void setDensity(float density);
void setRestitution(float restitution); void setRestitution(float restitution);
void setFriction(float friction); void setFriction(float friction);
void setMaterial(PhysicsMaterial material); void setMaterial(const PhysicsMaterial& material);
virtual float calculateDefaultMoment() { return 0; } virtual float calculateDefaultMoment() { return 0; }
virtual float calculateDefaultArea() { return 0; } virtual float calculateDefaultArea() { return 0; }
virtual Point getOffset() { return Point::ZERO; } virtual Point getOffset() { return Point::ZERO; }
virtual Point getCenter() { return getOffset(); } 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* recenterPoints(Point* points, int count, const Point& center = Point::ZERO);
static Point getPolyonCenter(Point* points, int count); static Point getPolyonCenter(const Point* points, int count);
inline void setCategoryBitmask(int bitmask) { _categoryBitmask = bitmask; } inline void setCategoryBitmask(int bitmask) { _categoryBitmask = bitmask; }
inline int getCategoryBitmask() const { return _categoryBitmask; } inline int getCategoryBitmask() const { return _categoryBitmask; }
@ -150,9 +150,9 @@ protected:
class PhysicsShapeCircle : public PhysicsShape class PhysicsShapeCircle : public PhysicsShape
{ {
public: 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 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 calculateDefaultArea() override;
float calculateDefaultMoment() override; float calculateDefaultMoment() override;
@ -160,7 +160,7 @@ public:
float getRadius() const; float getRadius() const;
Point getOffset() override; Point getOffset() override;
protected: 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: protected:
PhysicsShapeCircle(); PhysicsShapeCircle();
@ -171,9 +171,9 @@ protected:
class PhysicsShapeBox : public PhysicsShape class PhysicsShapeBox : public PhysicsShape
{ {
public: public:
static PhysicsShapeBox* create(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, 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(Size size); static float calculateArea(const Size& size);
static float calculateMoment(float mass, Size size, Point offset = Point(0, 0)); static float calculateMoment(float mass, const Size& size, const Point& offset = Point::ZERO);
float calculateDefaultArea() override; float calculateDefaultArea() override;
float calculateDefaultMoment() override; float calculateDefaultMoment() override;
@ -183,7 +183,7 @@ public:
Point getOffset() override { return _offset; } Point getOffset() override { return _offset; }
protected: 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: protected:
PhysicsShapeBox(); PhysicsShapeBox();
@ -197,9 +197,9 @@ protected:
class PhysicsShapePolygon : public PhysicsShape class PhysicsShapePolygon : public PhysicsShape
{ {
public: public:
static PhysicsShapePolygon* create(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, 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(Point* points, int count); static float calculateArea(const Point* points, int count);
static float calculateMoment(float mass, Point* points, int count, Point offset = Point(0, 0)); static float calculateMoment(float mass, const Point* points, int count, const Point& offset = Point::ZERO);
float calculateDefaultArea() override; float calculateDefaultArea() override;
float calculateDefaultMoment() override; float calculateDefaultMoment() override;
@ -209,7 +209,7 @@ public:
int getPointsCount() const; int getPointsCount() const;
Point getCenter() override; Point getCenter() override;
protected: 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: protected:
PhysicsShapePolygon(); PhysicsShapePolygon();
@ -223,14 +223,14 @@ protected:
class PhysicsShapeEdgeSegment : public PhysicsShape class PhysicsShapeEdgeSegment : public PhysicsShape
{ {
public: 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 getPointA() const;
Point getPointB() const; Point getPointB() const;
Point getCenter() override; Point getCenter() override;
protected: 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: protected:
PhysicsShapeEdgeSegment(); PhysicsShapeEdgeSegment();
@ -246,13 +246,13 @@ protected:
class PhysicsShapeEdgeBox : public PhysicsShape class PhysicsShapeEdgeBox : public PhysicsShape
{ {
public: 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 getOffset() override { return _offset; }
Point* getPoints(Point* points) const; Point* getPoints(const Point* points) const;
int getPointsCount() const; int getPointsCount() const;
protected: 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: protected:
PhysicsShapeEdgeBox(); PhysicsShapeEdgeBox();
@ -268,13 +268,13 @@ protected:
class PhysicsShapeEdgePolygon : public PhysicsShape class PhysicsShapeEdgePolygon : public PhysicsShape
{ {
public: 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 getCenter() override;
Point* getPoints(Point* points) const; Point* getPoints(Point* points) const;
int getPointsCount() const; int getPointsCount() const;
protected: 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: protected:
PhysicsShapeEdgePolygon(); PhysicsShapeEdgePolygon();
@ -290,13 +290,13 @@ protected:
class PhysicsShapeEdgeChain : public PhysicsShape class PhysicsShapeEdgeChain : public PhysicsShape
{ {
public: 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 getCenter() override;
Point* getPoints(Point* points) const; Point* getPoints(Point* points) const;
int getPointsCount() const; int getPointsCount() const;
protected: 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: protected:
PhysicsShapeEdgeChain(); PhysicsShapeEdgeChain();

View File

@ -106,11 +106,11 @@ int PhysicsWorldCallback::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSp
{ {
CP_ARBITER_GET_SHAPES(arb, a, b); CP_ARBITER_GET_SHAPES(arb, a, b);
auto ita = PhysicsShapeInfo::map.find(a); auto ita = PhysicsShapeInfo::getMap().find(a);
auto itb = PhysicsShapeInfo::map.find(b); auto itb = PhysicsShapeInfo::getMap().find(b);
CC_ASSERT(ita != PhysicsShapeInfo::map.end() && itb != PhysicsShapeInfo::map.end()); 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; arb->data = contact;
contact->_contactInfo = arb; contact->_contactInfo = arb;
@ -143,12 +143,12 @@ void PhysicsWorldCallback::rayCastCallbackFunc(cpShape *shape, cpFloat t, cpVect
return; return;
} }
auto it = PhysicsShapeInfo::map.find(shape); auto it = PhysicsShapeInfo::getMap().find(shape);
CC_ASSERT(it != PhysicsShapeInfo::map.end()); CC_ASSERT(it != PhysicsShapeInfo::getMap().end());
PhysicsRayCastCallback::Info callbackInfo = PhysicsRayCastCallback::Info callbackInfo =
{ {
it->second->shape, it->second->getShape(),
info->p1, info->p1,
info->p2, info->p2,
Point(info->p1.x+(info->p2.x-info->p1.x)*t, info->p1.y+(info->p2.y-info->p1.y)*t), 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) 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) if (!PhysicsWorldCallback::continues)
{ {
@ -171,17 +171,17 @@ void PhysicsWorldCallback::rectQueryCallbackFunc(cpShape *shape, RectQueryCallba
} }
PhysicsWorldCallback::continues = info->callback->report(*info->world, PhysicsWorldCallback::continues = info->callback->report(*info->world,
*it->second->shape, *it->second->getShape(),
info->data); info->data);
} }
void PhysicsWorldCallback::nearestPointQueryFunc(cpShape *shape, cpFloat distance, cpVect point, Array *arr) 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) bool PhysicsWorld::init(Scene& scene)
@ -202,9 +202,9 @@ bool PhysicsWorld::init(Scene& scene)
_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, (cpCollisionBeginFunc)PhysicsWorldCallback::collisionBeginCallbackFunc,
(cpCollisionPreSolveFunc)PhysicsWorldCallback::collisionPreSolveCallbackFunc, (cpCollisionPreSolveFunc)PhysicsWorldCallback::collisionPreSolveCallbackFunc,
(cpCollisionPostSolveFunc)PhysicsWorldCallback::collisionPostSolveCallbackFunc, (cpCollisionPostSolveFunc)PhysicsWorldCallback::collisionPostSolveCallbackFunc,
@ -225,7 +225,7 @@ void PhysicsWorld::delayTestAddBody(PhysicsBody* body)
return; return;
} }
if (_info->space->locked_private) if (_info->getSpace()->locked_private)
{ {
if (_delayAddBodies->getIndexOfObject(body) == UINT_MAX) if (_delayAddBodies->getIndexOfObject(body) == UINT_MAX)
{ {
@ -246,7 +246,7 @@ void PhysicsWorld::delayTestRemoveBody(PhysicsBody* body)
return; return;
} }
if (_info->space->locked_private) if (_info->getSpace()->locked_private)
{ {
if (_delayRemoveBodies->getIndexOfObject(body) == UINT_MAX) if (_delayRemoveBodies->getIndexOfObject(body) == UINT_MAX)
{ {
@ -268,7 +268,7 @@ void PhysicsWorld::delayTestAddJoint(PhysicsJoint* joint)
return; return;
} }
if (_info->space->locked_private) if (_info->getSpace()->locked_private)
{ {
if (std::find(_delayAddJoints.begin(), _delayAddJoints.end(), joint) == _delayAddJoints.end()) if (std::find(_delayAddJoints.begin(), _delayAddJoints.end(), joint) == _delayAddJoints.end())
{ {
@ -290,7 +290,7 @@ void PhysicsWorld::delayTestRemoveJoint(PhysicsJoint* joint)
return; return;
} }
if (_info->space->locked_private) if (_info->getSpace()->locked_private)
{ {
if (std::find(_delayRemoveJoints.begin(), _delayRemoveJoints.end(), joint) == _delayRemoveJoints.end()) if (std::find(_delayRemoveJoints.begin(), _delayRemoveJoints.end(), joint) == _delayRemoveJoints.end())
{ {
@ -338,7 +338,7 @@ void PhysicsWorld::removeAllJoints()
PhysicsShape* PhysicsWorld::addShape(PhysicsShape* shape) PhysicsShape* PhysicsWorld::addShape(PhysicsShape* shape)
{ {
for (auto cps : shape->_info->shapes) for (auto cps : shape->_info->getShapes())
{ {
_info->addShape(cps); _info->addShape(cps);
} }
@ -348,7 +348,7 @@ PhysicsShape* PhysicsWorld::addShape(PhysicsShape* shape)
void PhysicsWorld::realAddJoint(PhysicsJoint *joint) void PhysicsWorld::realAddJoint(PhysicsJoint *joint)
{ {
for (auto subjoint : joint->_info->joints) for (auto subjoint : joint->_info->getJoints())
{ {
_info->addJoint(subjoint); _info->addJoint(subjoint);
} }
@ -374,7 +374,7 @@ void PhysicsWorld::realAddBody(PhysicsBody* body)
// add body to space // add body to space
if (body->isDynamic()) if (body->isDynamic())
{ {
_info->addBody(body->_info->body); _info->addBody(body->_info->getBody());
} }
// add shapes to space // add shapes to space
@ -401,7 +401,7 @@ void PhysicsWorld::removeBody(PhysicsBody* body)
_bodies->removeObject(body); _bodies->removeObject(body);
} }
void PhysicsWorld::removeBodyByTag(int tag) void PhysicsWorld::removeBody(int tag)
{ {
for (Object* obj : *_bodies) for (Object* obj : *_bodies)
{ {
@ -442,14 +442,14 @@ void PhysicsWorld::realRemoveBody(PhysicsBody* body)
} }
// remove body // remove body
_info->removeBody(body->_info->body); _info->removeBody(body->_info->getBody());
body->_world = nullptr; body->_world = nullptr;
} }
void PhysicsWorld::realRemoveJoint(PhysicsJoint* joint) void PhysicsWorld::realRemoveJoint(PhysicsJoint* joint)
{ {
for (auto subjoint : joint->_info->joints) for (auto subjoint : joint->_info->getJoints())
{ {
_info->removeJoint(subjoint); _info->removeJoint(subjoint);
} }
@ -468,18 +468,18 @@ void PhysicsWorld::removeAllBodies()
void PhysicsWorld::removeShape(PhysicsShape* shape) 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() void PhysicsWorld::updateBodies()
{ {
if (_info->space->locked_private) if (_info->getSpace()->locked_private)
{ {
return; return;
} }
@ -500,7 +500,7 @@ void PhysicsWorld::updateBodies()
void PhysicsWorld::updateJoints() void PhysicsWorld::updateJoints()
{ {
if (_info->space->locked_private) if (_info->getSpace()->locked_private)
{ {
return; return;
} }
@ -533,7 +533,7 @@ void PhysicsWorld::update(float delta)
body->update(delta); body->update(delta);
} }
cpSpaceStep(_info->space, delta); cpSpaceStep(_info->getSpace(), delta);
if (_drawNode) if (_drawNode)
{ {
@ -577,7 +577,7 @@ void PhysicsWorld::debugDraw()
void PhysicsWorld::drawWithJoint(DrawNode* node, PhysicsJoint* joint) 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; cpConstraint *constraint = *it;
@ -638,7 +638,7 @@ void PhysicsWorld::drawWithJoint(DrawNode* node, PhysicsJoint* joint)
void PhysicsWorld::drawWithShape(DrawNode* node, PhysicsShape* shape) 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; cpShape *shape = *it;
@ -811,11 +811,11 @@ void PhysicsWorld::setGravity(Point gravity)
} }
_gravity = 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"); 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 }; RayCastCallbackInfo info = { this, &callback, point1, point2, data };
PhysicsWorldCallback::continues = true; PhysicsWorldCallback::continues = true;
cpSpaceSegmentQuery(this->_info->space, cpSpaceSegmentQuery(this->_info->getSpace(),
PhysicsHelper::point2cpv(point1), PhysicsHelper::point2cpv(point1),
PhysicsHelper::point2cpv(point2), PhysicsHelper::point2cpv(point2),
CP_ALL_LAYERS, 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"); 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}; RectQueryCallbackInfo info = {this, &callback, data};
PhysicsWorldCallback::continues = true; PhysicsWorldCallback::continues = true;
cpSpaceBBQuery(this->_info->space, cpSpaceBBQuery(this->_info->getSpace(),
PhysicsHelper::rect2cpbb(rect), PhysicsHelper::rect2cpbb(rect),
CP_ALL_LAYERS, CP_ALL_LAYERS,
CP_NO_GROUP, 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(); Array* arr = Array::create();
cpSpaceNearestPointQuery(this->_info->space, cpSpaceNearestPointQuery(this->_info->getSpace(),
PhysicsHelper::point2cpv(point), PhysicsHelper::point2cpv(point),
0, 0,
CP_ALL_LAYERS, CP_ALL_LAYERS,
@ -867,16 +867,16 @@ Array* PhysicsWorld::getShapesAtPoint(Point point) const
return arr; 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), PhysicsHelper::point2cpv(point),
0, 0,
CP_ALL_LAYERS, CP_ALL_LAYERS,
CP_NO_GROUP, CP_NO_GROUP,
nullptr); 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 Array* PhysicsWorld::getAllBodies() const
@ -884,7 +884,7 @@ Array* PhysicsWorld::getAllBodies() const
return _bodies; return _bodies;
} }
PhysicsBody* PhysicsWorld::getBodyByTag(int tag) const PhysicsBody* PhysicsWorld::getBody(int tag) const
{ {
for (auto body : *_bodies) for (auto body : *_bodies)
{ {

View File

@ -77,7 +77,7 @@ public:
* @param normal the normal vector at the point of intersection * @param normal the normal vector at the point of intersection
* @return true to continue, false to terminate * @return true to continue, false to terminate
*/ */
std::function<bool(PhysicsWorld& world, Info& info, void* data)> report; std::function<bool(PhysicsWorld& world, const Info& info, void* data)> report;
}; };
class PhysicsRectQueryCallback class PhysicsRectQueryCallback
@ -106,15 +106,15 @@ public:
virtual void removeAllJoints(); virtual void removeAllJoints();
virtual void removeBody(PhysicsBody* body); virtual void removeBody(PhysicsBody* body);
virtual void removeBodyByTag(int tag); virtual void removeBody(int tag);
virtual void removeAllBodies(); virtual void removeAllBodies();
void rayCast(PhysicsRayCastCallback& callback, Point point1, Point point2, void* data); void rayCast(PhysicsRayCastCallback& callback, const Point& point1, const Point& point2, void* data);
void rectQuery(PhysicsRectQueryCallback& callback, Rect rect, void* data); void rectQuery(PhysicsRectQueryCallback& callback, const Rect& rect, void* data);
Array* getShapesAtPoint(Point point) const; Array* getShapes(const Point& point) const;
PhysicsShape* getShapeAtPoint(Point point) const; PhysicsShape* getShape(const Point& point) const;
Array* getAllBodies() const; Array* getAllBodies() const;
PhysicsBody* getBodyByTag(int tag) const; PhysicsBody* getBody(int tag) const;
/** Register a listener to receive contact callbacks*/ /** Register a listener to receive contact callbacks*/
//inline void registerContactListener(EventListenerPhysicsContact* delegate) { _listener = delegate; } //inline void registerContactListener(EventListenerPhysicsContact* delegate) { _listener = delegate; }

View File

@ -27,18 +27,13 @@
NS_CC_BEGIN NS_CC_BEGIN
PhysicsBodyInfo::PhysicsBodyInfo() PhysicsBodyInfo::PhysicsBodyInfo()
: body(nullptr) : _body(nullptr)
{ {
} }
PhysicsBodyInfo::~PhysicsBodyInfo() PhysicsBodyInfo::~PhysicsBodyInfo()
{ {
if (body) cpBodyFree(body); if (_body) cpBodyFree(_body);
}
Clonable* PhysicsBodyInfo::clone() const
{
return nullptr;
} }
NS_CC_END NS_CC_END

View File

@ -34,16 +34,18 @@
NS_CC_BEGIN NS_CC_BEGIN
class PhysicsBodyInfo : public Clonable class PhysicsBodyInfo
{ {
public: public:
cpBody* body; inline cpBody* getBody() const { return _body; }
inline void setBody(cpBody* body) { _body = body; }
private: private:
PhysicsBodyInfo(); PhysicsBodyInfo();
~PhysicsBodyInfo(); ~PhysicsBodyInfo();
Clonable* clone() const override; private:
cpBody* _body;
friend class PhysicsBody; friend class PhysicsBody;
}; };

View File

@ -27,7 +27,7 @@
NS_CC_BEGIN NS_CC_BEGIN
PhysicsContactInfo::PhysicsContactInfo(PhysicsContact* contact) PhysicsContactInfo::PhysicsContactInfo(PhysicsContact* contact)
: contact(contact) : _contact(contact)
{ {
} }

View File

@ -36,12 +36,15 @@ class PhysicsContact;
class PhysicsContactInfo class PhysicsContactInfo
{ {
public: public:
PhysicsContact* contact; inline PhysicsContact* getContact() const { return _contact; }
private: private:
PhysicsContactInfo(PhysicsContact* contact); PhysicsContactInfo(PhysicsContact* contact);
~PhysicsContactInfo(); ~PhysicsContactInfo();
private:
PhysicsContact* _contact;
friend class PhysicsContact; friend class PhysicsContact;
}; };

View File

@ -27,16 +27,16 @@
#include <algorithm> #include <algorithm>
NS_CC_BEGIN NS_CC_BEGIN
std::map<cpConstraint*, PhysicsJointInfo*> PhysicsJointInfo::map; std::map<cpConstraint*, PhysicsJointInfo*> PhysicsJointInfo::_map;
PhysicsJointInfo::PhysicsJointInfo(PhysicsJoint* joint) PhysicsJointInfo::PhysicsJointInfo(PhysicsJoint* joint)
: joint(joint) : _joint(joint)
{ {
} }
PhysicsJointInfo::~PhysicsJointInfo() PhysicsJointInfo::~PhysicsJointInfo()
{ {
for (cpConstraint* joint : joints) for (cpConstraint* joint : _joints)
{ {
cpConstraintFree(joint); cpConstraintFree(joint);
} }
@ -46,21 +46,21 @@ void PhysicsJointInfo::add(cpConstraint* joint)
{ {
if (joint == nullptr) return; if (joint == nullptr) return;
joints.push_back(joint); _joints.push_back(joint);
map.insert(std::pair<cpConstraint*, PhysicsJointInfo*>(joint, this)); _map.insert(std::pair<cpConstraint*, PhysicsJointInfo*>(joint, this));
} }
void PhysicsJointInfo::remove(cpConstraint* joint) void PhysicsJointInfo::remove(cpConstraint* joint)
{ {
if (joint == nullptr) return; if (joint == nullptr) return;
auto it = std::find(joints.begin(), joints.end(), joint); auto it = std::find(_joints.begin(), _joints.end(), joint);
if (it != joints.end()) if (it != _joints.end())
{ {
joints.erase(it); _joints.erase(it);
auto mit = map.find(joint); auto mit = _map.find(joint);
if (mit != map.end()) map.erase(mit); if (mit != _map.end()) _map.erase(mit);
cpConstraintFree(joint); cpConstraintFree(joint);
} }
@ -68,14 +68,14 @@ void PhysicsJointInfo::remove(cpConstraint* joint)
void PhysicsJointInfo::removeAll() void PhysicsJointInfo::removeAll()
{ {
for (cpConstraint* joint : joints) for (cpConstraint* joint : _joints)
{ {
auto mit = map.find(joint); auto mit = _map.find(joint);
if (mit != map.end()) map.erase(mit); if (mit != _map.end()) _map.erase(mit);
cpConstraintFree(joint); cpConstraintFree(joint);
} }
joints.clear(); _joints.clear();
} }
NS_CC_END NS_CC_END

View File

@ -43,15 +43,19 @@ public:
void remove(cpConstraint* shape); void remove(cpConstraint* shape);
void removeAll(); void removeAll();
public: PhysicsJoint* getJoint() const { return _joint; }
std::vector<cpConstraint*> joints; std::vector<cpConstraint*>& getJoints() { return _joints; }
PhysicsJoint* joint; static std::map<cpConstraint*, PhysicsJointInfo*>& getMap() { return _map; }
static std::map<cpConstraint*, PhysicsJointInfo*> map;
private: private:
PhysicsJointInfo(PhysicsJoint* joint); PhysicsJointInfo(PhysicsJoint* joint);
~PhysicsJointInfo(); ~PhysicsJointInfo();
private:
std::vector<cpConstraint*> _joints;
PhysicsJoint* _joint;
static std::map<cpConstraint*, PhysicsJointInfo*> _map;
friend class PhysicsJoint; friend class PhysicsJoint;
}; };

View File

@ -27,27 +27,27 @@
#include <algorithm> #include <algorithm>
NS_CC_BEGIN NS_CC_BEGIN
std::map<cpShape*, PhysicsShapeInfo*> PhysicsShapeInfo::map; std::map<cpShape*, PhysicsShapeInfo*> PhysicsShapeInfo::_map;
cpBody* PhysicsShapeInfo::shareBody = nullptr; cpBody* PhysicsShapeInfo::_sharedBody = nullptr;
PhysicsShapeInfo::PhysicsShapeInfo(PhysicsShape* shape) PhysicsShapeInfo::PhysicsShapeInfo(PhysicsShape* shape)
: shape(shape) : _shape(shape)
, group(CP_NO_GROUP) , _group(CP_NO_GROUP)
{ {
if (shareBody == nullptr) if (_sharedBody == nullptr)
{ {
shareBody = cpBodyNewStatic(); _sharedBody = cpBodyNewStatic();
} }
body = shareBody; _body = _sharedBody;
} }
PhysicsShapeInfo::~PhysicsShapeInfo() PhysicsShapeInfo::~PhysicsShapeInfo()
{ {
for (auto shape : shapes) for (auto shape : _shapes)
{ {
auto it = map.find(shape); auto it = _map.find(shape);
if (it != map.end()) map.erase(shape); if (it != _map.end()) _map.erase(shape);
cpShapeFree(shape); cpShapeFree(shape);
} }
@ -55,9 +55,9 @@ PhysicsShapeInfo::~PhysicsShapeInfo()
void PhysicsShapeInfo::setGroup(cpGroup group) void PhysicsShapeInfo::setGroup(cpGroup group)
{ {
this->group = group; this->_group = group;
for (cpShape* shape : shapes) for (cpShape* shape : _shapes)
{ {
cpShapeSetGroup(shape, group); cpShapeSetGroup(shape, group);
} }
@ -65,12 +65,12 @@ void PhysicsShapeInfo::setGroup(cpGroup group)
void PhysicsShapeInfo::setBody(cpBody* body) void PhysicsShapeInfo::setBody(cpBody* body)
{ {
if (this->body != body) if (this->_body != body)
{ {
this->body = body; this->_body = body;
for (cpShape* shape : shapes) 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; if (shape == nullptr) return;
cpShapeSetGroup(shape, group); cpShapeSetGroup(shape, _group);
shapes.push_back(shape); _shapes.push_back(shape);
map.insert(std::pair<cpShape*, PhysicsShapeInfo*>(shape, this)); _map.insert(std::pair<cpShape*, PhysicsShapeInfo*>(shape, this));
} }
void PhysicsShapeInfo::remove(cpShape* shape) void PhysicsShapeInfo::remove(cpShape* shape)
{ {
if (shape == nullptr) return; if (shape == nullptr) return;
auto it = std::find(shapes.begin(), shapes.end(), shape); auto it = std::find(_shapes.begin(), _shapes.end(), shape);
if (it != shapes.end()) if (it != _shapes.end())
{ {
shapes.erase(it); _shapes.erase(it);
auto mit = map.find(shape); auto mit = _map.find(shape);
if (mit != map.end()) map.erase(mit); if (mit != _map.end()) _map.erase(mit);
cpShapeFree(shape); cpShapeFree(shape);
} }
@ -102,14 +102,14 @@ void PhysicsShapeInfo::remove(cpShape* shape)
void PhysicsShapeInfo::removeAll() void PhysicsShapeInfo::removeAll()
{ {
for (cpShape* shape : shapes) for (cpShape* shape : _shapes)
{ {
auto mit = map.find(shape); auto mit = _map.find(shape);
if (mit != map.end()) map.erase(mit); if (mit != _map.end()) _map.erase(mit);
cpShapeFree(shape); cpShapeFree(shape);
} }
shapes.clear(); _shapes.clear();
} }
NS_CC_END NS_CC_END

View File

@ -47,17 +47,25 @@ public:
void setBody(cpBody* body); void setBody(cpBody* body);
public: public:
std::vector<cpShape*> shapes; PhysicsShape* getShape() const { return _shape; }
PhysicsShape* shape; std::vector<cpShape*>& getShapes() { return _shapes; }
cpBody* body; cpBody* getBody() const { return _body; }
cpGroup group; cpGroup getGourp() const { return _group; }
static std::map<cpShape*, PhysicsShapeInfo*> map; static std::map<cpShape*, PhysicsShapeInfo*>& getMap() { return _map; }
static cpBody* shareBody; static cpBody* getSharedBody() { return _sharedBody; }
private: private:
PhysicsShapeInfo(PhysicsShape* shape); PhysicsShapeInfo(PhysicsShape* shape);
~PhysicsShapeInfo(); ~PhysicsShapeInfo();
private:
std::vector<cpShape*> _shapes;
PhysicsShape* _shape;
cpBody* _body;
cpGroup _group;
static std::map<cpShape*, PhysicsShapeInfo*> _map;
static cpBody* _sharedBody;
friend class PhysicsShape; friend class PhysicsShape;
}; };

View File

@ -29,12 +29,12 @@ NS_CC_BEGIN
#define PHYSICS_WORLD_INFO_FUNCTION_IMPLEMENTS(name, type) \ #define PHYSICS_WORLD_INFO_FUNCTION_IMPLEMENTS(name, type) \
void PhysicsWorldInfo::add##name(cp##type* data) \ 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) \ 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) PHYSICS_WORLD_INFO_FUNCTION_IMPLEMENTS(Shape, Shape)
@ -43,12 +43,12 @@ PHYSICS_WORLD_INFO_FUNCTION_IMPLEMENTS(Joint, Constraint)
PhysicsWorldInfo::PhysicsWorldInfo() PhysicsWorldInfo::PhysicsWorldInfo()
{ {
space = cpSpaceNew(); _space = cpSpaceNew();
} }
PhysicsWorldInfo::~PhysicsWorldInfo() PhysicsWorldInfo::~PhysicsWorldInfo()
{ {
cpSpaceFree(space); cpSpaceFree(_space);
} }
NS_CC_END NS_CC_END

View File

@ -36,8 +36,7 @@ NS_CC_BEGIN
class PhysicsWorldInfo class PhysicsWorldInfo
{ {
public: public:
cpSpace* space; cpSpace* getSpace() const { return _space; }
void addShape(cpShape* shape); void addShape(cpShape* shape);
void removeShape(cpShape* shape); void removeShape(cpShape* shape);
void addBody(cpBody* body); void addBody(cpBody* body);
@ -49,6 +48,9 @@ private:
PhysicsWorldInfo(); PhysicsWorldInfo();
~PhysicsWorldInfo(); ~PhysicsWorldInfo();
private:
cpSpace* _space;
friend class PhysicsWorld; friend class PhysicsWorld;
}; };

View File

@ -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; *((Point*)data) = info.contact;
return false; return false;
@ -620,7 +620,7 @@ private:
PhysicsDemoNearestRayCastCallback::PhysicsDemoNearestRayCastCallback() PhysicsDemoNearestRayCastCallback::PhysicsDemoNearestRayCastCallback()
: _friction(1.0f) : _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) if (_friction > info.fraction)
{ {
@ -650,7 +650,7 @@ public:
PhysicsDemoMultiRayCastCallback::PhysicsDemoMultiRayCastCallback() PhysicsDemoMultiRayCastCallback::PhysicsDemoMultiRayCastCallback()
: num(0) : 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) if (num < MAX_MULTI_RAYCAST_NUM)
{ {
@ -1089,7 +1089,7 @@ void PhysicsDemoSlice::onEnter()
addChild(box); 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) if (info.shape->getBody()->getTag() != _sliceTag)
{ {
@ -1150,6 +1150,8 @@ void PhysicsDemoSlice::clipPoly(PhysicsShapePolygon* shape, Point normal, float
polyon->setAngularVelocity(body->getAngularVelocity()); polyon->setAngularVelocity(body->getAngularVelocity());
polyon->setTag(_sliceTag); polyon->setTag(_sliceTag);
addChild(node); addChild(node);
delete[] points;
} }
void PhysicsDemoSlice::onTouchEnded(Touch *touch, Event *event) void PhysicsDemoSlice::onTouchEnded(Touch *touch, Event *event)
@ -1171,7 +1173,7 @@ std::string PhysicsDemoSlice::subtitle()
void PhysicsDemoWater::onEnter() void PhysicsDemoWater::onEnter()
{ {
PhysicsDemo::onEnter();
} }
std::string PhysicsDemoWater::title() std::string PhysicsDemoWater::title()

View File

@ -96,7 +96,7 @@ public:
void changeModeCallback(Object* sender); void changeModeCallback(Object* sender);
bool anyRay(PhysicsWorld& world, PhysicsRayCastCallback::Info& info, void* data); bool anyRay(PhysicsWorld& world, const PhysicsRayCastCallback::Info& info, void* data);
private: private:
float _angle; float _angle;
@ -150,7 +150,7 @@ public:
std::string title() override; std::string title() override;
std::string subtitle() 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); void clipPoly(PhysicsShapePolygon* shape, Point normal, float distance);
bool onTouchBegan(Touch *touch, Event *event); bool onTouchBegan(Touch *touch, Event *event);