issue #2771: refactor some functions and delete PHYSICS_INFINITY macro.

This commit is contained in:
boyu0 2013-11-26 11:38:55 +08:00
parent 09ba28d522
commit df1fca3279
6 changed files with 46 additions and 51 deletions

View File

@ -315,8 +315,8 @@ void PhysicsBody::setDynamic(bool dynamic)
}
// avoid incorrect collion simulation.
cpBodySetMass(_info->getBody(), PHYSICS_INFINITY);
cpBodySetMoment(_info->getBody(), PHYSICS_INFINITY);
cpBodySetMass(_info->getBody(), INFINITY);
cpBodySetMoment(_info->getBody(), INFINITY);
cpBodySetVel(_info->getBody(), cpvzero);
cpBodySetAngVel(_info->getBody(), 0.0f);
}
@ -328,7 +328,7 @@ void PhysicsBody::setRotationEnable(bool enable)
{
if (_rotationEnable != enable)
{
cpBodySetMoment(_info->getBody(), enable ? _moment : PHYSICS_INFINITY);
cpBodySetMoment(_info->getBody(), enable ? _moment : INFINITY);
_rotationEnable = enable;
}
}
@ -454,9 +454,9 @@ void PhysicsBody::setMass(float mass)
_massDefault = false;
// update density
if (_mass == PHYSICS_INFINITY)
if (_mass == INFINITY)
{
_density = PHYSICS_INFINITY;
_density = INFINITY;
}
else
{
@ -478,17 +478,17 @@ void PhysicsBody::setMass(float mass)
void PhysicsBody::addMass(float mass)
{
if (mass == PHYSICS_INFINITY)
if (mass == INFINITY)
{
_mass = PHYSICS_INFINITY;
_mass = INFINITY;
_massDefault = false;
_density = PHYSICS_INFINITY;
_density = INFINITY;
}
else if (mass == -PHYSICS_INFINITY)
else if (mass == -INFINITY)
{
return;
}
else if (_mass != PHYSICS_INFINITY)
else if (_mass != INFINITY)
{
if (_massDefault)
{
@ -524,13 +524,13 @@ void PhysicsBody::addMass(float mass)
void PhysicsBody::addMoment(float moment)
{
if (moment == PHYSICS_INFINITY)
if (moment == INFINITY)
{
// if moment is INFINITY, the moment of the body will become INFINITY
_moment = PHYSICS_INFINITY;
_moment = INFINITY;
_momentDefault = false;
}
else if (moment == -PHYSICS_INFINITY)
else if (moment == -INFINITY)
{
// if moment is -INFINITY, it won't change
return;
@ -538,7 +538,7 @@ void PhysicsBody::addMoment(float moment)
else
{
// if moment of the body is INFINITY is has no effect
if (_moment != PHYSICS_INFINITY)
if (_moment != INFINITY)
{
if (_momentDefault)
{

View File

@ -205,16 +205,16 @@ public:
/**
* @brief set the body mass.
* @note if you need add/subtract mass to body, don't use setMass(getMass() +/- mass), because the mass of body may be equal to PHYSICS_INFINITY, it will cause some unexpected result, please use addMass() instead.
* @note if you need add/subtract mass to body, don't use setMass(getMass() +/- mass), because the mass of body may be equal to INFINITY, it will cause some unexpected result, please use addMass() instead.
*/
void setMass(float mass);
/** get the body mass. */
inline float getMass() const { return _mass; }
/**
* @brief add mass to body.
* if _mass(mass of the body) == PHYSICS_INFINITY, it remains.
* if mass == PHYSICS_INFINITY, _mass will be PHYSICS_INFINITY.
* if mass == -PHYSICS_INFINITY, _mass will not change.
* if _mass(mass of the body) == INFINITY, it remains.
* if mass == INFINITY, _mass will be INFINITY.
* if mass == -INFINITY, _mass will not change.
* if mass + _mass <= 0, _mass will equal to MASS_DEFAULT(1.0)
* other wise, mass = mass + _mass;
*/
@ -222,16 +222,16 @@ public:
/**
* @brief set the body moment of inertia.
* @note if you need add/subtract moment to body, don't use setMoment(getMoment() +/- moment), because the moment of body may be equal to PHYSICS_INFINITY, it will cause some unexpected result, please use addMoment() instead.
* @note if you need add/subtract moment to body, don't use setMoment(getMoment() +/- moment), because the moment of body may be equal to INFINITY, it will cause some unexpected result, please use addMoment() instead.
*/
void setMoment(float moment);
/** get the body moment of inertia. */
inline float getMoment(float moment) const { return _moment; }
/**
* @brief add moment of inertia to body.
* if _moment(moment of the body) == PHYSICS_INFINITY, it remains.
* if moment == PHYSICS_INFINITY, _moment will be PHYSICS_INFINITY.
* if moment == -PHYSICS_INFINITY, _moment will not change.
* if _moment(moment of the body) == INFINITY, it remains.
* if moment == INFINITY, _moment will be INFINITY.
* if moment == -INFINITY, _moment will not change.
* if moment + _moment <= 0, _moment will equal to MASS_DEFAULT(1.0)
* other wise, moment = moment + _moment;
*/

View File

@ -29,7 +29,7 @@
#define CC_PHYSICS_BOX2D 1
#define CC_PHYSICS_CHIPMUNK 2
#define CC_USE_BOX2D
#define CC_USE_CHIPMUNK
#ifdef CC_USE_BOX2D
#define CC_PHYSICS_ENGINE CC_PHYSICS_BOX2D
@ -45,8 +45,6 @@
namespace cocos2d
{
extern const float PHYSICS_INFINITY;
class Point;
typedef Point Vect;

View File

@ -205,9 +205,9 @@ void PhysicsShape::setDensity(float density)
_material.density = density;
if (_material.density == PHYSICS_INFINITY)
if (_material.density == INFINITY)
{
setMass(PHYSICS_INFINITY);
setMass(INFINITY);
}else if (_area > 0)
{
setMass(PhysicsHelper::float2cpfloat(_material.density * _area));
@ -313,7 +313,7 @@ bool PhysicsShapeCircle::init(float radius, const PhysicsMaterial& material/* =
_info->add(shape);
_area = calculateDefaultArea();
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
_mass = material.density == INFINITY ? INFINITY : material.density * _area;
_moment = calculateDefaultMoment();
setMaterial(material);
@ -330,7 +330,7 @@ float PhysicsShapeCircle::calculateArea(float radius)
float PhysicsShapeCircle::calculateMoment(float mass, float radius, const Point& offset)
{
return mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
return mass == INFINITY ? INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForCircle(PhysicsHelper::float2cpfloat(mass),
0,
PhysicsHelper::float2cpfloat(radius),
@ -346,7 +346,7 @@ float PhysicsShapeCircle::calculateDefaultMoment()
{
cpShape* shape = _info->getShapes().front();
return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
return _mass == INFINITY ? INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForCircle(PhysicsHelper::float2cpfloat(_mass),
0,
cpCircleShapeGetRadius(shape),
@ -392,8 +392,8 @@ bool PhysicsShapeEdgeSegment::init(const Point& a, const Point& b, const Physics
_info->add(shape);
_mass = PHYSICS_INFINITY;
_moment = PHYSICS_INFINITY;
_mass = INFINITY;
_moment = INFINITY;
_center = a.getMidpoint(b);
@ -454,7 +454,7 @@ bool PhysicsShapeBox::init(const Size& size, const PhysicsMaterial& material/* =
_offset = offset;
_area = calculateDefaultArea();
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
_mass = material.density == INFINITY ? INFINITY : material.density * _area;
_moment = calculateDefaultMoment();
setMaterial(material);
@ -483,7 +483,7 @@ float PhysicsShapeBox::calculateMoment(float mass, const Size& size, const Point
{-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}
};
return mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
return mass == INFINITY ? INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForPoly(PhysicsHelper::float2cpfloat(mass),
4,
vec,
@ -499,7 +499,7 @@ float PhysicsShapeBox::calculateDefaultArea()
float PhysicsShapeBox::calculateDefaultMoment()
{
cpShape* shape = _info->getShapes().front();
return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
return _mass == INFINITY ? INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero));
}
@ -546,7 +546,7 @@ bool PhysicsShapePolygon::init(const Point* points, int count, const PhysicsMate
_info->add(shape);
_area = calculateDefaultArea();
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
_mass = material.density == INFINITY ? INFINITY : material.density * _area;
_moment = calculateDefaultMoment();
_center = PhysicsHelper::cpv2point(cpCentroidForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts));
@ -572,7 +572,7 @@ float PhysicsShapePolygon::calculateMoment(float mass, const Point* points, int
{
cpVect* vecs = new cpVect[count];
PhysicsHelper::points2cpvs(points, vecs, count);
float moment = mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
float moment = mass == INFINITY ? INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForPoly(mass, count, vecs, PhysicsHelper::point2cpv(offset)));
CC_SAFE_DELETE_ARRAY(vecs);
@ -588,7 +588,7 @@ float PhysicsShapePolygon::calculateDefaultArea()
float PhysicsShapePolygon::calculateDefaultMoment()
{
cpShape* shape = _info->getShapes().front();
return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
return _mass == INFINITY ? INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero));
}
@ -650,8 +650,8 @@ bool PhysicsShapeEdgeBox::init(const Size& size, const PhysicsMaterial& material
CC_BREAK_IF(i < 4);
_offset = offset;
_mass = PHYSICS_INFINITY;
_moment = PHYSICS_INFINITY;
_mass = INFINITY;
_moment = INFINITY;
setMaterial(material);
@ -700,8 +700,8 @@ bool PhysicsShapeEdgePolygon::init(const Point* points, int count, const Physics
CC_BREAK_IF(i < count);
_mass = PHYSICS_INFINITY;
_moment = PHYSICS_INFINITY;
_mass = INFINITY;
_moment = INFINITY;
setMaterial(material);
@ -761,8 +761,8 @@ bool PhysicsShapeEdgeChain::init(const Point* points, int count, const PhysicsMa
CC_SAFE_DELETE_ARRAY(vec);
CC_BREAK_IF(i < count);
_mass = PHYSICS_INFINITY;
_moment = PHYSICS_INFINITY;
_mass = INFINITY;
_moment = INFINITY;
setMaterial(material);

View File

@ -65,9 +65,6 @@ NS_CC_BEGIN
extern const char* PHYSICSCONTACT_EVENT_NAME;
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
const float PHYSICS_INFINITY = INFINITY;
namespace
{
typedef struct RayCastCallbackInfo

View File

@ -384,7 +384,7 @@ bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event)
if (body != nullptr)
{
Node* mouse = Node::create();
mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY));
mouse->setPhysicsBody(PhysicsBody::create(INFINITY, INFINITY));
mouse->getPhysicsBody()->setDynamic(false);
mouse->setPosition(location);
this->addChild(mouse);
@ -444,7 +444,7 @@ void PhysicsDemoLogoSmash::onEnter()
0.95f, PhysicsMaterial(0.01f, 0.0f, 0.0f));
ball->getPhysicsBody()->setMass(1.0);
ball->getPhysicsBody()->setMoment(PHYSICS_INFINITY);
ball->getPhysicsBody()->setMoment(INFINITY);
_ball->addChild(ball);
@ -453,7 +453,7 @@ void PhysicsDemoLogoSmash::onEnter()
}
auto bullet = makeBall(Point(400, 0), 10, PhysicsMaterial(PHYSICS_INFINITY, 0, 0));
auto bullet = makeBall(Point(400, 0), 10, PhysicsMaterial(INFINITY, 0, 0));
bullet->getPhysicsBody()->setVelocity(Point(400, 0));
bullet->setPosition(Point(-1000, VisibleRect::getVisibleRect().size.height/2));
@ -835,7 +835,7 @@ void PhysicsDemoPump::onEnter()
auto body = PhysicsBody::create();
body->setDynamic(false);
PhysicsMaterial staticMaterial(PHYSICS_INFINITY, 0, 0.5f);
PhysicsMaterial staticMaterial(INFINITY, 0, 0.5f);
body->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Point(50, 0), VisibleRect::leftTop() + Point(50, -130), staticMaterial, 2.0f));
body->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Point(190, 0), VisibleRect::leftTop() + Point(100, -50), staticMaterial, 2.0f));
body->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Point(100, -50), VisibleRect::leftTop() + Point(100, -90), staticMaterial, 2.0f));