2013-09-09 10:29:02 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
2013-09-09 10:40:31 +08:00
|
|
|
****************************************************************************/
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
#include "CCPhysicsJoint.h"
|
|
|
|
#ifdef CC_USE_PHYSICS
|
2013-09-17 17:39:08 +08:00
|
|
|
|
|
|
|
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
|
|
|
#include "chipmunk.h"
|
|
|
|
#elif (CC_PHYSICS_ENGINE == CCPHYSICS_BOX2D)
|
|
|
|
#include "Box2D.h"
|
|
|
|
#endif
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
#include "CCPhysicsBody.h"
|
2013-11-07 14:17:57 +08:00
|
|
|
#include "CCPhysicsWorld.h"
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-31 18:18:02 +08:00
|
|
|
#include "chipmunk/CCPhysicsJointInfo_chipmunk.h"
|
|
|
|
#include "box2d/CCPhysicsJointInfo_box2d.h"
|
|
|
|
#include "chipmunk/CCPhysicsBodyInfo_chipmunk.h"
|
|
|
|
#include "box2d/CCPhysicsBodyInfo_box2d.h"
|
|
|
|
#include "chipmunk/CCPhysicsShapeInfo_chipmunk.h"
|
|
|
|
#include "box2d/CCPhysicsShapeInfo_box2d.h"
|
|
|
|
#include "chipmunk/CCPhysicsHelper_chipmunk.h"
|
|
|
|
#include "box2d/CCPhysicsHelper_box2d.h"
|
2013-10-28 16:17:19 +08:00
|
|
|
#include "CCNode.h"
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
PhysicsJoint::PhysicsJoint()
|
|
|
|
: _bodyA(nullptr)
|
|
|
|
, _bodyB(nullptr)
|
2013-11-07 14:17:57 +08:00
|
|
|
, _world(nullptr)
|
2013-09-16 21:22:22 +08:00
|
|
|
, _info(nullptr)
|
2013-09-29 09:39:20 +08:00
|
|
|
, _enable(false)
|
2013-10-17 10:57:48 +08:00
|
|
|
, _collisionEnable(true)
|
2013-11-07 14:17:57 +08:00
|
|
|
, _destoryMark(false)
|
2013-09-29 09:39:20 +08:00
|
|
|
, _tag(0)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJoint::~PhysicsJoint()
|
|
|
|
{
|
2013-10-17 10:57:48 +08:00
|
|
|
// reset the shapes collision group
|
|
|
|
setCollisionEnable(true);
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
CC_SAFE_DELETE(_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJoint::init(cocos2d::PhysicsBody *a, cocos2d::PhysicsBody *b)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-10-17 10:57:48 +08:00
|
|
|
CC_BREAK_IF(!(_info = new PhysicsJointInfo(this)));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-23 17:28:23 +08:00
|
|
|
if (a != nullptr)
|
|
|
|
{
|
|
|
|
_bodyA = a;
|
|
|
|
_bodyA->_joints.push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b != nullptr)
|
|
|
|
{
|
|
|
|
_bodyB = b;
|
|
|
|
_bodyB->_joints.push_back(this);
|
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-29 09:39:20 +08:00
|
|
|
void PhysicsJoint::setEnable(bool enable)
|
|
|
|
{
|
|
|
|
if (_enable != enable)
|
|
|
|
{
|
|
|
|
_enable = enable;
|
|
|
|
|
2013-11-07 14:17:57 +08:00
|
|
|
if (_world != nullptr)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2013-11-07 14:17:57 +08:00
|
|
|
if (enable)
|
|
|
|
{
|
2013-11-07 15:12:13 +08:00
|
|
|
_world->addJointOrDelay(this);
|
2013-11-07 14:17:57 +08:00
|
|
|
}else
|
|
|
|
{
|
2013-11-07 15:12:13 +08:00
|
|
|
_world->removeJointOrDelay(this);
|
2013-11-07 14:17:57 +08:00
|
|
|
}
|
2013-09-29 09:39:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-23 10:11:08 +08:00
|
|
|
PhysicsJointPin::PhysicsJointPin()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointPin::~PhysicsJointPin()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-09-17 14:31:43 +08:00
|
|
|
PhysicsJointFixed::PhysicsJointFixed()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointFixed::~PhysicsJointFixed()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointSliding::PhysicsJointSliding()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointSliding::~PhysicsJointSliding()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointLimit::PhysicsJointLimit()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointLimit::~PhysicsJointLimit()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
PhysicsJointDistance::PhysicsJointDistance()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointDistance::~PhysicsJointDistance()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
2013-11-01 16:26:03 +08:00
|
|
|
PhysicsBodyInfo* PhysicsJoint::getBodyInfo(PhysicsBody* body) const
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
return body->_info;
|
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
Node* PhysicsJoint::getBodyNode(PhysicsBody* body) const
|
2013-10-28 16:17:19 +08:00
|
|
|
{
|
2013-11-01 16:26:03 +08:00
|
|
|
return body->_node;
|
2013-10-28 16:17:19 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-17 10:57:48 +08:00
|
|
|
void PhysicsJoint::setCollisionEnable(bool enable)
|
|
|
|
{
|
|
|
|
if (_collisionEnable != enable)
|
|
|
|
{
|
|
|
|
_collisionEnable = enable;
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-11-07 14:17:57 +08:00
|
|
|
void PhysicsJoint::removeFormWorld()
|
|
|
|
{
|
|
|
|
if (_world)
|
|
|
|
{
|
|
|
|
_world->removeJoint(this, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJoint::destroy(PhysicsJoint* joint)
|
|
|
|
{
|
|
|
|
if (joint!= nullptr)
|
|
|
|
{
|
|
|
|
// remove the joint and delete it.
|
|
|
|
if (joint->_world != nullptr)
|
|
|
|
{
|
|
|
|
joint->_world->removeJoint(joint, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (joint->_bodyA != nullptr)
|
|
|
|
{
|
|
|
|
joint->_bodyA->removeJoint(joint);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (joint->_bodyB != nullptr)
|
|
|
|
{
|
|
|
|
joint->_bodyB->removeJoint(joint);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete joint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-07 17:46:05 +08:00
|
|
|
PhysicsJointFixed* PhysicsJointFixed::construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsJointFixed* joint = new PhysicsJointFixed();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b, anchr))
|
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointFixed::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
getBodyNode(a)->setPosition(anchr);
|
|
|
|
getBodyNode(b)->setPosition(anchr);
|
2013-10-28 16:17:19 +08:00
|
|
|
|
2013-10-17 10:57:48 +08:00
|
|
|
// add a pivot joint to fixed two body together
|
2013-11-05 20:02:58 +08:00
|
|
|
cpConstraint* joint = cpPivotJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(),
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsHelper::point2cpv(anchr));
|
2013-10-28 11:08:41 +08:00
|
|
|
CC_BREAK_IF(joint == nullptr);
|
2013-10-17 10:57:48 +08:00
|
|
|
_info->add(joint);
|
|
|
|
|
|
|
|
// add a gear joint to make two body have the same rotation.
|
2013-11-05 20:02:58 +08:00
|
|
|
joint = cpGearJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(), 0, 1);
|
2013-10-28 11:08:41 +08:00
|
|
|
CC_BREAK_IF(joint == nullptr);
|
2013-10-17 10:57:48 +08:00
|
|
|
_info->add(joint);
|
|
|
|
|
|
|
|
setCollisionEnable(false);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-07 17:46:05 +08:00
|
|
|
PhysicsJointPin* PhysicsJointPin::construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsJointPin* joint = new PhysicsJointPin();
|
|
|
|
|
2013-10-17 10:57:48 +08:00
|
|
|
if (joint && joint->init(a, b, anchr))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-17 10:57:48 +08:00
|
|
|
bool PhysicsJointPin::init(PhysicsBody *a, PhysicsBody *b, const Point& anchr)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
2013-11-05 20:02:58 +08:00
|
|
|
cpConstraint* joint = cpPivotJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(),
|
2013-10-17 10:57:48 +08:00
|
|
|
PhysicsHelper::point2cpv(anchr));
|
|
|
|
|
2013-10-28 11:08:41 +08:00
|
|
|
CC_BREAK_IF(joint == nullptr);
|
2013-10-17 10:57:48 +08:00
|
|
|
|
|
|
|
_info->add(joint);
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-28 16:17:19 +08:00
|
|
|
void PhysicsJointPin::setMaxForce(float force)
|
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
_info->getJoints().front()->maxForce = PhysicsHelper::float2cpfloat(force);
|
2013-10-28 16:17:19 +08:00
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
float PhysicsJointPin::getMaxForce() const
|
2013-10-28 16:17:19 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(_info->getJoints().front()->maxForce);
|
2013-10-28 16:17:19 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 17:46:05 +08:00
|
|
|
PhysicsJointSliding* PhysicsJointSliding::construct(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
cpConstraint* joint = cpGrooveJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(),
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsHelper::point2cpv(grooveA),
|
|
|
|
PhysicsHelper::point2cpv(grooveB),
|
|
|
|
PhysicsHelper::point2cpv(anchr));
|
|
|
|
|
2013-10-28 11:08:41 +08:00
|
|
|
CC_BREAK_IF(joint == nullptr);
|
2013-10-17 10:57:48 +08:00
|
|
|
|
|
|
|
_info->add(joint);
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-07 17:46:05 +08:00
|
|
|
PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsJointLimit* joint = new PhysicsJointLimit();
|
|
|
|
|
2013-10-17 10:57:48 +08:00
|
|
|
if (joint && joint->init(a, b, anchr1, anchr2))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-17 10:57:48 +08:00
|
|
|
bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
cpConstraint* joint = cpSlideJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(),
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsHelper::point2cpv(anchr1),
|
|
|
|
PhysicsHelper::point2cpv(anchr2),
|
2013-10-17 10:57:48 +08:00
|
|
|
0,
|
|
|
|
PhysicsHelper::float2cpfloat(anchr1.getDistance(anchr2)));
|
|
|
|
|
2013-10-28 11:08:41 +08:00
|
|
|
CC_BREAK_IF(joint == nullptr);
|
2013-10-17 10:57:48 +08:00
|
|
|
|
|
|
|
_info->add(joint);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
float PhysicsJointLimit::getMin() const
|
2013-10-17 10:57:48 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpSlideJointGetMin(_info->getJoints().front()));
|
2013-10-17 10:57:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointLimit::setMin(float min)
|
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
cpSlideJointSetMin(_info->getJoints().front(), PhysicsHelper::float2cpfloat(min));
|
2013-10-17 10:57:48 +08:00
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
float PhysicsJointLimit::getMax() const
|
2013-10-17 10:57:48 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpSlideJointGetMax(_info->getJoints().front()));
|
2013-10-17 10:57:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointLimit::setMax(float max)
|
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
cpSlideJointSetMax(_info->getJoints().front(), PhysicsHelper::float2cpfloat(max));
|
2013-10-17 10:57:48 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 17:46:05 +08:00
|
|
|
PhysicsJointDistance* PhysicsJointDistance::construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2)
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
|
|
|
PhysicsJointDistance* joint = new PhysicsJointDistance();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b, anchr1, anchr2))
|
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointDistance::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
cpConstraint* joint = cpPinJointNew(getBodyInfo(a)->getBody(),
|
|
|
|
getBodyInfo(b)->getBody(),
|
2013-10-29 17:31:35 +08:00
|
|
|
PhysicsHelper::point2cpv(anchr1), PhysicsHelper::point2cpv(anchr2));
|
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
|
|
|
|
_info->add(joint);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
#endif // CC_USE_PHYSICS
|