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
|
|
|
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "physics/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"
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
#include "CCPhysicsBody.h"
|
|
|
|
#include "CCPhysicsWorld.h"
|
|
|
|
#include "CCPhysicsHelper.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/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-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);
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
for (auto constraint : _cpConstraints)
|
|
|
|
{
|
|
|
|
cpConstraintFree(constraint);
|
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
_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
|
|
|
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)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
for (auto joint : _cpConstraints)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
|
|
|
joint->maxForce = PhysicsHelper::float2cpfloat(force);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJoint::getMaxForce() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(_cpConstraints.front()->maxForce);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
PhysicsJointFixed* PhysicsJointFixed::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsJointFixed* joint = new (std::nothrow) PhysicsJointFixed();
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
if (joint && joint->init(a, b, anchr))
|
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool PhysicsJointFixed::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
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
|
2015-01-06 10:29:07 +08:00
|
|
|
auto constraint = cpPivotJointNew(a->getCPBody(), b->getCPBody(),
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsHelper::point2cpv(anchr));
|
2015-01-06 10:29:07 +08:00
|
|
|
CC_BREAK_IF(constraint == nullptr);
|
|
|
|
_cpConstraints.push_back(constraint);
|
2013-10-17 10:57:48 +08:00
|
|
|
|
|
|
|
// add a gear joint to make two body have the same rotation.
|
2015-01-06 10:29:07 +08:00
|
|
|
constraint = cpGearJointNew(a->getCPBody(), b->getCPBody(), 0, 1);
|
|
|
|
CC_BREAK_IF(constraint == nullptr);
|
|
|
|
_cpConstraints.push_back(constraint);
|
2013-10-17 10:57:48 +08:00
|
|
|
|
|
|
|
setCollisionEnable(false);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
PhysicsJointPin* PhysicsJointPin::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsJointPin* joint = new (std::nothrow) PhysicsJointPin();
|
2013-09-16 21:22:22 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool PhysicsJointPin::init(PhysicsBody *a, PhysicsBody *b, const Vec2& anchr)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
2015-01-06 10:29:07 +08:00
|
|
|
auto constraint = cpPivotJointNew(a->getCPBody(), b->getCPBody(),
|
2013-10-17 10:57:48 +08:00
|
|
|
PhysicsHelper::point2cpv(anchr));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
CC_BREAK_IF(constraint == nullptr);
|
2013-10-17 10:57:48 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
_cpConstraints.push_back(constraint);
|
2013-10-17 10:57:48 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float min, float max)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsJointLimit* joint = new (std::nothrow) PhysicsJointLimit();
|
2013-09-16 21:22:22 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2)
|
2013-12-03 12:47:03 +08:00
|
|
|
{
|
|
|
|
return construct(a, b, anchr1, anchr2, 0, b->local2World(anchr1).getDistance(a->local2World(anchr2)));
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float min, float max)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
auto constraint = cpSlideJointNew(a->getCPBody(), b->getCPBody(),
|
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
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
CC_BREAK_IF(constraint == nullptr);
|
2013-10-17 10:57:48 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
_cpConstraints.push_back(constraint);
|
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
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpSlideJointGetMin(_cpConstraints.front()));
|
2013-10-17 10:57:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointLimit::setMin(float min)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpSlideJointSetMin(_cpConstraints.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
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpSlideJointGetMax(_cpConstraints.front()));
|
2013-10-17 10:57:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointLimit::setMax(float max)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpSlideJointSetMax(_cpConstraints.front(), PhysicsHelper::float2cpfloat(max));
|
2013-10-17 10:57:48 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsJointLimit::getAnchr1() const
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpv2point(cpSlideJointGetAnchr1(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PhysicsJointLimit::setAnchr1(const Vec2& anchr)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpSlideJointSetAnchr1(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsJointLimit::getAnchr2() const
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpv2point(cpSlideJointGetAnchr2(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PhysicsJointLimit::setAnchr2(const Vec2& anchr)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpSlideJointSetAnchr1(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
PhysicsJointDistance* PhysicsJointDistance::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2)
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsJointDistance* joint = new (std::nothrow) PhysicsJointDistance();
|
2013-10-29 17:31:35 +08:00
|
|
|
|
|
|
|
if (joint && joint->init(a, b, anchr1, anchr2))
|
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool PhysicsJointDistance::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2)
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
auto constraint = cpPinJointNew(a->getCPBody(),
|
|
|
|
b->getCPBody(),
|
2013-11-18 02:16:20 +08:00
|
|
|
PhysicsHelper::point2cpv(anchr1),
|
|
|
|
PhysicsHelper::point2cpv(anchr2));
|
2013-10-29 17:31:35 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
CC_BREAK_IF(constraint == nullptr);
|
2013-10-29 17:31:35 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
_cpConstraints.push_back(constraint);
|
2013-10-29 17:31:35 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
float PhysicsJointDistance::getDistance() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpPinJointGetDist(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointDistance::setDistance(float distance)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpPinJointSetDist(_cpConstraints.front(), PhysicsHelper::float2cpfloat(distance));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
PhysicsJointSpring* PhysicsJointSpring::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float stiffness, float damping)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsJointSpring* joint = new (std::nothrow) PhysicsJointSpring();
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
if (joint && joint->init(a, b, anchr1, anchr2, stiffness, damping))
|
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool PhysicsJointSpring::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float stiffness, float damping)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
|
|
|
do {
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
auto constraint = cpDampedSpringNew(a->getCPBody(),
|
|
|
|
b->getCPBody(),
|
2013-12-02 18:29:04 +08:00
|
|
|
PhysicsHelper::point2cpv(anchr1),
|
|
|
|
PhysicsHelper::point2cpv(anchr2),
|
|
|
|
PhysicsHelper::float2cpfloat(_bodyB->local2World(anchr1).getDistance(_bodyA->local2World(anchr2))),
|
|
|
|
PhysicsHelper::float2cpfloat(stiffness),
|
|
|
|
PhysicsHelper::float2cpfloat(damping));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
CC_BREAK_IF(constraint == nullptr);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
_cpConstraints.push_back(constraint);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsJointSpring::getAnchr1() const
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpv2point(cpDampedSpringGetAnchr1(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PhysicsJointSpring::setAnchr1(const Vec2& anchr)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpDampedSpringSetAnchr1(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsJointSpring::getAnchr2() const
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpv2point(cpDampedSpringGetAnchr2(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PhysicsJointSpring::setAnchr2(const Vec2& anchr)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpDampedSpringSetAnchr1(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointSpring::getRestLength() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedSpringGetRestLength(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setRestLength(float restLength)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpDampedSpringSetRestLength(_cpConstraints.front(), PhysicsHelper::float2cpfloat(restLength));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointSpring::getStiffness() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedSpringGetStiffness(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setStiffness(float stiffness)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpDampedSpringSetStiffness(_cpConstraints.front(), PhysicsHelper::float2cpfloat(stiffness));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointSpring::getDamping() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedSpringGetDamping(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setDamping(float damping)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpDampedSpringSetDamping(_cpConstraints.front(), PhysicsHelper::float2cpfloat(damping));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
PhysicsJointGroove* PhysicsJointGroove::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& grooveA, const Vec2& grooveB, const Vec2& anchr2)
|
2013-12-03 12:47:03 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsJointGroove* joint = new (std::nothrow) PhysicsJointGroove();
|
2013-12-03 12:47:03 +08:00
|
|
|
|
|
|
|
if (joint && joint->init(a, b, grooveA, grooveB, anchr2))
|
|
|
|
{
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool PhysicsJointGroove::init(PhysicsBody* a, PhysicsBody* b, const Vec2& grooveA, const Vec2& grooveB, const Vec2& anchr2)
|
2013-12-03 12:47:03 +08:00
|
|
|
{
|
|
|
|
do {
|
|
|
|
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
auto constraint = cpGrooveJointNew(a->getCPBody(),
|
|
|
|
b->getCPBody(),
|
2013-12-03 12:47:03 +08:00
|
|
|
PhysicsHelper::point2cpv(grooveA),
|
|
|
|
PhysicsHelper::point2cpv(grooveB),
|
|
|
|
PhysicsHelper::point2cpv(anchr2));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
CC_BREAK_IF(constraint == nullptr);
|
2013-12-03 12:47:03 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
_cpConstraints.push_back(constraint);
|
2013-12-03 12:47:03 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsJointGroove::getGrooveA() const
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpv2point(cpGrooveJointGetGrooveA(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PhysicsJointGroove::setGrooveA(const Vec2& grooveA)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpGrooveJointSetGrooveA(_cpConstraints.front(), PhysicsHelper::point2cpv(grooveA));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsJointGroove::getGrooveB() const
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpv2point(cpGrooveJointGetGrooveB(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PhysicsJointGroove::setGrooveB(const Vec2& grooveB)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpGrooveJointSetGrooveB(_cpConstraints.front(), PhysicsHelper::point2cpv(grooveB));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsJointGroove::getAnchr2() const
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpv2point(cpGrooveJointGetAnchr2(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PhysicsJointGroove::setAnchr2(const Vec2& anchr2)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpGrooveJointSetAnchr2(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr2));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointRotarySpring* PhysicsJointRotarySpring::construct(PhysicsBody* a, PhysicsBody* b, float stiffness, float damping)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsJointRotarySpring* joint = new (std::nothrow) PhysicsJointRotarySpring();
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
auto constraint = cpDampedRotarySpringNew(a->getCPBody(),
|
|
|
|
b->getCPBody(),
|
2013-12-02 18:29:04 +08:00
|
|
|
PhysicsHelper::float2cpfloat(_bodyB->getRotation() - _bodyA->getRotation()),
|
|
|
|
PhysicsHelper::float2cpfloat(stiffness),
|
|
|
|
PhysicsHelper::float2cpfloat(damping));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
CC_BREAK_IF(constraint == nullptr);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
_cpConstraints.push_back(constraint);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotarySpring::getRestAngle() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetRestAngle(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotarySpring::setRestAngle(float restAngle)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpDampedRotarySpringSetRestAngle(_cpConstraints.front(), PhysicsHelper::float2cpfloat(restAngle));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotarySpring::getStiffness() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetStiffness(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotarySpring::setStiffness(float stiffness)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpDampedRotarySpringSetStiffness(_cpConstraints.front(), PhysicsHelper::float2cpfloat(stiffness));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotarySpring::getDamping() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetDamping(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotarySpring::setDamping(float damping)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpDampedRotarySpringSetDamping(_cpConstraints.front(), PhysicsHelper::float2cpfloat(damping));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsJointRotaryLimit* joint = new (std::nothrow) PhysicsJointRotaryLimit();
|
2013-12-02 18:29:04 +08:00
|
|
|
|
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));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
auto constraint = cpRotaryLimitJointNew(a->getCPBody(),
|
|
|
|
b->getCPBody(),
|
2013-12-03 12:47:03 +08:00
|
|
|
PhysicsHelper::float2cpfloat(min),
|
|
|
|
PhysicsHelper::float2cpfloat(max));
|
2013-12-02 18:29:04 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
CC_BREAK_IF(constraint == nullptr);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
_cpConstraints.push_back(constraint);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotaryLimit::getMin() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMin(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotaryLimit::setMin(float min)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpRotaryLimitJointSetMin(_cpConstraints.front(), PhysicsHelper::float2cpfloat(min));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotaryLimit::getMax() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMax(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotaryLimit::setMax(float max)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpRotaryLimitJointSetMax(_cpConstraints.front(), PhysicsHelper::float2cpfloat(max));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointRatchet* PhysicsJointRatchet::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsJointRatchet* joint = new (std::nothrow) PhysicsJointRatchet();
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
auto constraint = cpRatchetJointNew(a->getCPBody(),
|
|
|
|
b->getCPBody(),
|
2013-12-02 18:29:04 +08:00
|
|
|
PhysicsHelper::float2cpfloat(phase),
|
|
|
|
PhysicsHelper::cpfloat2float(ratchet));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
CC_BREAK_IF(constraint == nullptr);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
_cpConstraints.push_back(constraint);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRatchet::getAngle() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpRatchetJointGetAngle(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRatchet::setAngle(float angle)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpRatchetJointSetAngle(_cpConstraints.front(), PhysicsHelper::float2cpfloat(angle));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRatchet::getPhase() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpRatchetJointGetPhase(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRatchet::setPhase(float phase)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpRatchetJointSetPhase(_cpConstraints.front(), PhysicsHelper::float2cpfloat(phase));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRatchet::getRatchet() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpRatchetJointGetRatchet(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRatchet::setRatchet(float ratchet)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpRatchetJointSetRatchet(_cpConstraints.front(), PhysicsHelper::float2cpfloat(ratchet));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointGear* PhysicsJointGear::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsJointGear* joint = new (std::nothrow) PhysicsJointGear();
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
auto constraint = cpGearJointNew(a->getCPBody(),
|
|
|
|
b->getCPBody(),
|
2013-12-02 18:29:04 +08:00
|
|
|
PhysicsHelper::float2cpfloat(phase),
|
2013-12-03 12:47:03 +08:00
|
|
|
PhysicsHelper::float2cpfloat(ratio));
|
2013-12-02 18:29:04 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
CC_BREAK_IF(constraint == nullptr);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
_cpConstraints.push_back(constraint);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointGear::getPhase() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpGearJointGetPhase(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointGear::setPhase(float phase)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpGearJointSetPhase(_cpConstraints.front(), PhysicsHelper::float2cpfloat(phase));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointGear::getRatio() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpGearJointGetRatio(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointGear::setRatio(float ratio)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpGearJointSetRatio(_cpConstraints.front(), PhysicsHelper::float2cpfloat(ratio));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointMotor* PhysicsJointMotor::construct(PhysicsBody* a, PhysicsBody* b, float rate)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsJointMotor* joint = new (std::nothrow) PhysicsJointMotor();
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
auto constraint = cpSimpleMotorNew(a->getCPBody(),
|
|
|
|
b->getCPBody(),
|
2013-12-02 18:29:04 +08:00
|
|
|
PhysicsHelper::float2cpfloat(rate));
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
CC_BREAK_IF(constraint == nullptr);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
_cpConstraints.push_back(constraint);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointMotor::getRate() const
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpSimpleMotorGetRate(_cpConstraints.front()));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointMotor::setRate(float rate)
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpSimpleMotorSetRate(_cpConstraints.front(), PhysicsHelper::float2cpfloat(rate));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
NS_CC_END
|
|
|
|
#endif // CC_USE_PHYSICS
|