issue #2771: delete PhysicsSlidingJoint and PhysicsSpringJoint. add joint tests, fix bugs

This commit is contained in:
boyu0 2013-11-18 02:16:20 +08:00
parent 02e5d44955
commit 0710cfa526
4 changed files with 37 additions and 84 deletions

View File

@ -130,7 +130,7 @@ float PhysicsContactPreSolve::getElasticity() const
return static_cast<cpArbiter*>(_contactInfo)->e;
}
float PhysicsContactPreSolve::getFriciton() const
float PhysicsContactPreSolve::getFriction() const
{
return static_cast<cpArbiter*>(_contactInfo)->u;
}
@ -177,7 +177,7 @@ float PhysicsContactPostSolve::getElasticity() const
return static_cast<cpArbiter*>(_contactInfo)->e;
}
float PhysicsContactPostSolve::getFriciton() const
float PhysicsContactPostSolve::getFriction() const
{
return static_cast<cpArbiter*>(_contactInfo)->u;
}

View File

@ -130,16 +130,6 @@ PhysicsJointFixed::~PhysicsJointFixed()
}
PhysicsJointSliding::PhysicsJointSliding()
{
}
PhysicsJointSliding::~PhysicsJointSliding()
{
}
PhysicsJointLimit::PhysicsJointLimit()
{
@ -296,41 +286,6 @@ float PhysicsJointPin::getMaxForce() const
return PhysicsHelper::cpfloat2float(_info->getJoints().front()->maxForce);
}
PhysicsJointSliding* PhysicsJointSliding::construct(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr)
{
PhysicsJointSliding* joint = new PhysicsJointSliding();
if (joint && joint->init(a, b, grooveA, grooveB, anchr))
{
return joint;
}
CC_SAFE_DELETE(joint);
return nullptr;
}
bool PhysicsJointSliding::init(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr)
{
do
{
CC_BREAK_IF(!PhysicsJoint::init(a, b));
cpConstraint* joint = cpGrooveJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(),
PhysicsHelper::point2cpv(grooveA),
PhysicsHelper::point2cpv(grooveB),
PhysicsHelper::point2cpv(anchr));
CC_BREAK_IF(joint == nullptr);
_info->add(joint);
return true;
} while (false);
return false;
}
PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2)
{
PhysicsJointLimit* joint = new PhysicsJointLimit();
@ -354,7 +309,7 @@ bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1
PhysicsHelper::point2cpv(anchr1),
PhysicsHelper::point2cpv(anchr2),
0,
PhysicsHelper::float2cpfloat(anchr1.getDistance(anchr2)));
PhysicsHelper::float2cpfloat(_bodyA->local2World(anchr1).getDistance(_bodyB->local2World(anchr2))));
CC_BREAK_IF(joint == nullptr);
@ -407,7 +362,8 @@ bool PhysicsJointDistance::init(PhysicsBody* a, PhysicsBody* b, const Point& anc
cpConstraint* joint = cpPinJointNew(getBodyInfo(a)->getBody(),
getBodyInfo(b)->getBody(),
PhysicsHelper::point2cpv(anchr1), PhysicsHelper::point2cpv(anchr2));
PhysicsHelper::point2cpv(anchr1),
PhysicsHelper::point2cpv(anchr2));
CC_BREAK_IF(joint == nullptr);

View File

@ -100,45 +100,13 @@ protected:
virtual ~PhysicsJointFixed();
};
/*
* @brief A sliding joint allows the two bodies to slide along a chosen axis.
*/
class PhysicsJointSliding : public PhysicsJoint
{
public:
static PhysicsJointSliding* construct(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr);
protected:
bool init(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr);
protected:
PhysicsJointSliding();
virtual ~PhysicsJointSliding();
};
/*
* @brief A spring joint connects the two bodies with a spring whose length is the initial distance between the two bodies.
*/
class PhysicsJointSpring : public PhysicsJoint
{
public:
PhysicsJointSpring* construct();
protected:
bool init();
protected:
PhysicsJointSpring();
virtual ~PhysicsJointSpring();
};
/*
* @brief A limit joint imposes a maximum distance between the two bodies, as if they were connected by a rope.
*/
class PhysicsJointLimit : public PhysicsJoint
{
public:
PhysicsJointLimit* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);
static PhysicsJointLimit* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);
float getMin() const;
void setMin(float min);
@ -174,7 +142,6 @@ protected:
class PhysicsJointDistance : public PhysicsJoint
{
public:
static PhysicsJointDistance* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);

View File

@ -405,7 +405,6 @@ bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event)
if (shape != nullptr)
{
Node* mouse = Node::create();
mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY));
mouse->getPhysicsBody()->setDynamic(false);
@ -759,6 +758,37 @@ void PhysicsDemoJoints::onEnter()
this->addChild(sp2);
break;
}
case 2:
{
auto sp1 = makeBall(offset - Point(30, 0), 10);
sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
PhysicsJointDistance* joint = PhysicsJointDistance::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO);
_scene->getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
case 3:
{
auto sp1 = makeBall(offset - Point(30, 0), 10);
sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
PhysicsJointLimit* joint = PhysicsJointLimit::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO);
joint->setMin(30.0f);
joint->setMax(60.0f);
_scene->getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
default:
break;
}