2013-09-09 10:29:02 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013 Chukong Technologies Inc.
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
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"
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-09-17 17:39:08 +08:00
|
|
|
#include "chipmunk.h"
|
|
|
|
|
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 "chipmunk/CCPhysicsBodyInfo_chipmunk.h"
|
|
|
|
#include "chipmunk/CCPhysicsShapeInfo_chipmunk.h"
|
|
|
|
#include "chipmunk/CCPhysicsHelper_chipmunk.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-11-21 14:39:37 +08:00
|
|
|
CCASSERT(a != nullptr && b != nullptr, "the body passed in is nil");
|
|
|
|
CCASSERT(a != b, "the two bodies are equal");
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-11-21 14:39:37 +08:00
|
|
|
CC_BREAK_IF(!(_info = new PhysicsJointInfo(this)));
|
2013-10-23 17:28:23 +08:00
|
|
|
|
2013-11-21 14:39:37 +08:00
|
|
|
_bodyA = a;
|
|
|
|
_bodyA->_joints.push_back(this);
|
|
|
|
_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-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-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-12-02 18:29:04 +08:00
|
|
|
void PhysicsJoint::setMaxForce(float force)
|
|
|
|
{
|
|
|
|
for (cpConstraint* joint : _info->getJoints())
|
|
|
|
{
|
|
|
|
joint->maxForce = PhysicsHelper::float2cpfloat(force);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJoint::getMaxForce() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(_info->getJoints().front()->maxForce);
|
|
|
|
}
|
|
|
|
|
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-12-03 12:47:03 +08:00
|
|
|
PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2, float min, float max)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsJointLimit* joint = new PhysicsJointLimit();
|
|
|
|
|
2013-12-03 12:47:03 +08:00
|
|
|
if (joint && joint->init(a, b, anchr1, anchr2, min, max))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-03 12:47:03 +08:00
|
|
|
PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2)
|
|
|
|
{
|
|
|
|
return construct(a, b, anchr1, anchr2, 0, b->local2World(anchr1).getDistance(a->local2World(anchr2)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2, float min, float max)
|
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-12-03 12:47:03 +08:00
|
|
|
PhysicsHelper::float2cpfloat(min),
|
|
|
|
PhysicsHelper::float2cpfloat(max));
|
2013-10-17 10:57:48 +08:00
|
|
|
|
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-12-02 18:29:04 +08:00
|
|
|
Point PhysicsJointLimit::getAnchr1() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpv2point(cpSlideJointGetAnchr1(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointLimit::setAnchr1(const Point& anchr)
|
|
|
|
{
|
|
|
|
cpSlideJointSetAnchr1(_info->getJoints().front(), PhysicsHelper::point2cpv(anchr));
|
|
|
|
}
|
|
|
|
|
|
|
|
Point PhysicsJointLimit::getAnchr2() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpv2point(cpSlideJointGetAnchr2(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointLimit::setAnchr2(const Point& anchr)
|
|
|
|
{
|
|
|
|
cpSlideJointSetAnchr1(_info->getJoints().front(), PhysicsHelper::point2cpv(anchr));
|
|
|
|
}
|
|
|
|
|
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-11-18 02:16:20 +08:00
|
|
|
PhysicsHelper::point2cpv(anchr1),
|
|
|
|
PhysicsHelper::point2cpv(anchr2));
|
2013-10-29 17:31:35 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
|
|
|
|
_info->add(joint);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
float PhysicsJointDistance::getDistance() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpPinJointGetDist(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointDistance::setDistance(float distance)
|
|
|
|
{
|
|
|
|
cpPinJointSetDist(_info->getJoints().front(), PhysicsHelper::float2cpfloat(distance));
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointSpring* PhysicsJointSpring::construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2, float stiffness, float damping)
|
|
|
|
{
|
|
|
|
PhysicsJointSpring* joint = new PhysicsJointSpring();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b, anchr1, anchr2, stiffness, damping))
|
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointSpring::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2, float stiffness, float damping)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
|
|
|
cpConstraint* joint = cpDampedSpringNew(getBodyInfo(a)->getBody(),
|
|
|
|
getBodyInfo(b)->getBody(),
|
|
|
|
PhysicsHelper::point2cpv(anchr1),
|
|
|
|
PhysicsHelper::point2cpv(anchr2),
|
|
|
|
PhysicsHelper::float2cpfloat(_bodyB->local2World(anchr1).getDistance(_bodyA->local2World(anchr2))),
|
|
|
|
PhysicsHelper::float2cpfloat(stiffness),
|
|
|
|
PhysicsHelper::float2cpfloat(damping));
|
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
|
|
|
|
_info->add(joint);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Point PhysicsJointSpring::getAnchr1() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpv2point(cpDampedSpringGetAnchr1(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setAnchr1(const Point& anchr)
|
|
|
|
{
|
|
|
|
cpDampedSpringSetAnchr1(_info->getJoints().front(), PhysicsHelper::point2cpv(anchr));
|
|
|
|
}
|
|
|
|
|
|
|
|
Point PhysicsJointSpring::getAnchr2() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpv2point(cpDampedSpringGetAnchr2(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setAnchr2(const Point& anchr)
|
|
|
|
{
|
|
|
|
cpDampedSpringSetAnchr1(_info->getJoints().front(), PhysicsHelper::point2cpv(anchr));
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointSpring::getRestLength() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedSpringGetRestLength(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setRestLength(float restLength)
|
|
|
|
{
|
|
|
|
cpDampedSpringSetRestLength(_info->getJoints().front(), PhysicsHelper::float2cpfloat(restLength));
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointSpring::getStiffness() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedSpringGetStiffness(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setStiffness(float stiffness)
|
|
|
|
{
|
|
|
|
cpDampedSpringSetStiffness(_info->getJoints().front(), PhysicsHelper::float2cpfloat(stiffness));
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointSpring::getDamping() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedSpringGetDamping(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setDamping(float damping)
|
|
|
|
{
|
|
|
|
cpDampedSpringSetDamping(_info->getJoints().front(), PhysicsHelper::float2cpfloat(damping));
|
|
|
|
}
|
|
|
|
|
2013-12-03 12:47:03 +08:00
|
|
|
PhysicsJointGroove* PhysicsJointGroove::construct(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr2)
|
|
|
|
{
|
|
|
|
PhysicsJointGroove* joint = new PhysicsJointGroove();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b, grooveA, grooveB, anchr2))
|
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointGroove::init(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr2)
|
|
|
|
{
|
|
|
|
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(anchr2));
|
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
|
|
|
|
_info->add(joint);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
Point PhysicsJointGroove::getGrooveA() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpv2point(cpGrooveJointGetGrooveA(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointGroove::setGrooveA(const Point& grooveA)
|
|
|
|
{
|
|
|
|
cpGrooveJointSetGrooveA(_info->getJoints().front(), PhysicsHelper::point2cpv(grooveA));
|
|
|
|
}
|
|
|
|
|
|
|
|
Point PhysicsJointGroove::getGrooveB() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpv2point(cpGrooveJointGetGrooveB(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointGroove::setGrooveB(const Point& grooveB)
|
|
|
|
{
|
|
|
|
cpGrooveJointSetGrooveB(_info->getJoints().front(), PhysicsHelper::point2cpv(grooveB));
|
|
|
|
}
|
|
|
|
|
|
|
|
Point PhysicsJointGroove::getAnchr2() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpv2point(cpGrooveJointGetAnchr2(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointGroove::setAnchr2(const Point& anchr2)
|
|
|
|
{
|
|
|
|
cpGrooveJointSetAnchr2(_info->getJoints().front(), PhysicsHelper::point2cpv(anchr2));
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointRotarySpring* PhysicsJointRotarySpring::construct(PhysicsBody* a, PhysicsBody* b, float stiffness, float damping)
|
|
|
|
{
|
|
|
|
PhysicsJointRotarySpring* joint = new PhysicsJointRotarySpring();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b, stiffness, damping))
|
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointRotarySpring::init(PhysicsBody* a, PhysicsBody* b, float stiffness, float damping)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
|
|
|
cpConstraint* joint = cpDampedRotarySpringNew(getBodyInfo(a)->getBody(),
|
|
|
|
getBodyInfo(b)->getBody(),
|
|
|
|
PhysicsHelper::float2cpfloat(_bodyB->getRotation() - _bodyA->getRotation()),
|
|
|
|
PhysicsHelper::float2cpfloat(stiffness),
|
|
|
|
PhysicsHelper::float2cpfloat(damping));
|
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
|
|
|
|
_info->add(joint);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotarySpring::getRestAngle() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetRestAngle(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotarySpring::setRestAngle(float restAngle)
|
|
|
|
{
|
|
|
|
cpDampedRotarySpringSetRestAngle(_info->getJoints().front(), PhysicsHelper::float2cpfloat(restAngle));
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotarySpring::getStiffness() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetStiffness(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotarySpring::setStiffness(float stiffness)
|
|
|
|
{
|
|
|
|
cpDampedRotarySpringSetStiffness(_info->getJoints().front(), PhysicsHelper::float2cpfloat(stiffness));
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotarySpring::getDamping() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetDamping(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotarySpring::setDamping(float damping)
|
|
|
|
{
|
|
|
|
cpDampedRotarySpringSetDamping(_info->getJoints().front(), PhysicsHelper::float2cpfloat(damping));
|
|
|
|
}
|
|
|
|
|
2013-12-03 12:47:03 +08:00
|
|
|
PhysicsJointRotaryLimit* PhysicsJointRotaryLimit::construct(PhysicsBody* a, PhysicsBody* b, float min, float max)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
|
|
|
PhysicsJointRotaryLimit* joint = new PhysicsJointRotaryLimit();
|
|
|
|
|
2013-12-03 12:47:03 +08:00
|
|
|
if (joint && joint->init(a, b, min, max))
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-03 12:47:03 +08:00
|
|
|
PhysicsJointRotaryLimit* PhysicsJointRotaryLimit::construct(PhysicsBody* a, PhysicsBody* b)
|
|
|
|
{
|
|
|
|
return construct(a, b, 0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointRotaryLimit::init(PhysicsBody* a, PhysicsBody* b, float min, float max)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
|
|
|
cpConstraint* joint = cpRotaryLimitJointNew(getBodyInfo(a)->getBody(),
|
|
|
|
getBodyInfo(b)->getBody(),
|
2013-12-03 12:47:03 +08:00
|
|
|
PhysicsHelper::float2cpfloat(min),
|
|
|
|
PhysicsHelper::float2cpfloat(max));
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
|
|
|
|
_info->add(joint);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotaryLimit::getMin() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMin(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotaryLimit::setMin(float min)
|
|
|
|
{
|
|
|
|
cpRotaryLimitJointSetMin(_info->getJoints().front(), PhysicsHelper::float2cpfloat(min));
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotaryLimit::getMax() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMax(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotaryLimit::setMax(float max)
|
|
|
|
{
|
|
|
|
cpRotaryLimitJointSetMax(_info->getJoints().front(), PhysicsHelper::float2cpfloat(max));
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointRatchet* PhysicsJointRatchet::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet)
|
|
|
|
{
|
|
|
|
PhysicsJointRatchet* joint = new PhysicsJointRatchet();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b, phase, ratchet))
|
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointRatchet::init(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
|
|
|
cpConstraint* joint = cpRatchetJointNew(getBodyInfo(a)->getBody(),
|
|
|
|
getBodyInfo(b)->getBody(),
|
|
|
|
PhysicsHelper::float2cpfloat(phase),
|
|
|
|
PhysicsHelper::cpfloat2float(ratchet));
|
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
|
|
|
|
_info->add(joint);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRatchet::getAngle() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpRatchetJointGetAngle(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRatchet::setAngle(float angle)
|
|
|
|
{
|
|
|
|
cpRatchetJointSetAngle(_info->getJoints().front(), PhysicsHelper::float2cpfloat(angle));
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRatchet::getPhase() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpRatchetJointGetPhase(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRatchet::setPhase(float phase)
|
|
|
|
{
|
|
|
|
cpRatchetJointSetPhase(_info->getJoints().front(), PhysicsHelper::float2cpfloat(phase));
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRatchet::getRatchet() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpRatchetJointGetRatchet(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRatchet::setRatchet(float ratchet)
|
|
|
|
{
|
|
|
|
cpRatchetJointSetRatchet(_info->getJoints().front(), PhysicsHelper::float2cpfloat(ratchet));
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointGear* PhysicsJointGear::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet)
|
|
|
|
{
|
|
|
|
PhysicsJointGear* joint = new PhysicsJointGear();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b, phase, ratchet))
|
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointGear::init(PhysicsBody* a, PhysicsBody* b, float phase, float ratio)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
|
|
|
cpConstraint* joint = cpGearJointNew(getBodyInfo(a)->getBody(),
|
|
|
|
getBodyInfo(b)->getBody(),
|
|
|
|
PhysicsHelper::float2cpfloat(phase),
|
2013-12-03 12:47:03 +08:00
|
|
|
PhysicsHelper::float2cpfloat(ratio));
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
|
|
|
|
_info->add(joint);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointGear::getPhase() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpGearJointGetPhase(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointGear::setPhase(float phase)
|
|
|
|
{
|
|
|
|
cpGearJointSetPhase(_info->getJoints().front(), PhysicsHelper::float2cpfloat(phase));
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointGear::getRatio() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpGearJointGetRatio(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointGear::setRatio(float ratio)
|
|
|
|
{
|
|
|
|
cpGearJointSetRatio(_info->getJoints().front(), PhysicsHelper::float2cpfloat(ratio));
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointMotor* PhysicsJointMotor::construct(PhysicsBody* a, PhysicsBody* b, float rate)
|
|
|
|
{
|
|
|
|
PhysicsJointMotor* joint = new PhysicsJointMotor();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b, rate))
|
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointMotor::init(PhysicsBody* a, PhysicsBody* b, float rate)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
|
|
|
cpConstraint* joint = cpSimpleMotorNew(getBodyInfo(a)->getBody(),
|
|
|
|
getBodyInfo(b)->getBody(),
|
|
|
|
PhysicsHelper::float2cpfloat(rate));
|
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
|
|
|
|
_info->add(joint);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointMotor::getRate() const
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpSimpleMotorGetRate(_info->getJoints().front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointMotor::setRate(float rate)
|
|
|
|
{
|
|
|
|
cpSimpleMotorSetRate(_info->getJoints().front(), PhysicsHelper::float2cpfloat(rate));
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
NS_CC_END
|
|
|
|
#endif // CC_USE_PHYSICS
|