2013-09-09 10:29:02 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
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
|
2016-03-01 05:53:00 +08:00
|
|
|
#include "chipmunk/chipmunk.h"
|
2013-09-17 17:39:08 +08:00
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "physics/CCPhysicsBody.h"
|
|
|
|
#include "physics/CCPhysicsWorld.h"
|
|
|
|
#include "physics/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
|
|
|
|
|
2018-10-19 18:11:50 +08:00
|
|
|
template<typename T>
|
|
|
|
class Optional {
|
|
|
|
|
|
|
|
public:
|
|
|
|
Optional() {}
|
|
|
|
Optional(T d) :_isSet(true), _data(d) {}
|
|
|
|
Optional(const Optional &t) : _isSet(t._isSet), _data(t._data) {}
|
|
|
|
|
|
|
|
//bool isNull() const { return !_isSet; }
|
|
|
|
//bool isDefineded() const { return _isSet; }
|
|
|
|
//bool isEmpty() const { return !_isSet; }
|
|
|
|
|
|
|
|
T get() const { CCASSERT(_isSet, "data should be set!"); return _data; }
|
|
|
|
void set(T d) { _isSet = true; _data = d; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool _isSet = false;
|
|
|
|
T _data;
|
|
|
|
};
|
|
|
|
|
|
|
|
class WriteCache {
|
|
|
|
public:
|
|
|
|
Optional<Vec2> _grooveA;
|
|
|
|
Optional<Vec2> _grooveB;
|
|
|
|
Optional<Vec2> _anchr1;
|
|
|
|
Optional<Vec2> _anchr2;
|
|
|
|
Optional<float> _min;
|
|
|
|
Optional<float> _max;
|
|
|
|
Optional<float> _distance;
|
|
|
|
Optional<float> _restLength;
|
|
|
|
Optional<float> _restAngle;
|
|
|
|
Optional<float> _stiffness;
|
|
|
|
Optional<float> _damping;
|
|
|
|
Optional<float> _angle;
|
|
|
|
Optional<float> _phase;
|
|
|
|
Optional<float> _ratchet;
|
|
|
|
Optional<float> _ratio;
|
|
|
|
Optional<float> _rate;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if (defined(__GNUC__) && __GNUC__ >= 4) || defined(__clang__)
|
|
|
|
#define LIKELY(x) (__builtin_expect((x), 1))
|
|
|
|
#define UNLIKELY(x) (__builtin_expect((x), 0))
|
|
|
|
#else
|
|
|
|
#define LIKELY(x) (x)
|
|
|
|
#define UNLIKELY(x) (x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define CC_PJOINT_CACHE_READ(field) \
|
|
|
|
do { \
|
|
|
|
if(UNLIKELY(_initDirty)) \
|
|
|
|
{ \
|
|
|
|
return _writeCache->field.get(); \
|
|
|
|
} \
|
|
|
|
}while(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define CC_PJOINT_CACHE_WRITE2(field, method, arg, convertedArg) \
|
|
|
|
do { \
|
|
|
|
if(UNLIKELY(_initDirty)) \
|
|
|
|
{ \
|
|
|
|
_writeCache->field.set(arg); \
|
|
|
|
delay([this, arg]() { \
|
|
|
|
method(_cpConstraints.front(), convertedArg); \
|
|
|
|
}); \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
method(_cpConstraints.front(), convertedArg); \
|
|
|
|
} \
|
|
|
|
}while(0)
|
|
|
|
|
|
|
|
#define CC_PJOINT_CACHE_WRITE(field, method, arg) \
|
|
|
|
CC_PJOINT_CACHE_WRITE2(field, method, arg, arg)
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
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)
|
2016-06-28 22:11:22 +08:00
|
|
|
, _destroyMark(false)
|
2013-09-29 09:39:20 +08:00
|
|
|
, _tag(0)
|
2015-01-07 17:08:04 +08:00
|
|
|
, _maxForce(PHYSICS_INFINITY)
|
2015-01-21 17:59:53 +08:00
|
|
|
, _initDirty(true)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
_writeCache = new WriteCache();
|
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-07 17:08:04 +08:00
|
|
|
|
|
|
|
for (cpConstraint* joint : _cpConstraints)
|
2015-01-06 10:29:07 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
cpConstraintFree(joint);
|
2015-01-06 10:29:07 +08:00
|
|
|
}
|
2015-01-07 17:08:04 +08:00
|
|
|
_cpConstraints.clear();
|
2018-10-19 18:11:50 +08:00
|
|
|
|
|
|
|
delete _writeCache;
|
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");
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-11-21 14:39:37 +08:00
|
|
|
_bodyA = a;
|
|
|
|
_bodyB = b;
|
2015-01-07 17:08:04 +08:00
|
|
|
_bodyA->_joints.push_back(this);
|
2013-11-21 14:39:37 +08:00
|
|
|
_bodyB->_joints.push_back(this);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
bool PhysicsJoint::initJoint()
|
|
|
|
{
|
|
|
|
bool ret = !_initDirty;
|
|
|
|
while (_initDirty)
|
|
|
|
{
|
|
|
|
ret = createConstraints();
|
|
|
|
CC_BREAK_IF(!ret);
|
|
|
|
|
|
|
|
for (auto subjoint : _cpConstraints)
|
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpConstraintSetMaxForce(subjoint, _maxForce);
|
|
|
|
cpConstraintSetErrorBias(subjoint, cpfpow(1.0f - 0.15f, 60.0f));
|
2015-01-07 17:08:04 +08:00
|
|
|
cpSpaceAddConstraint(_world->_cpSpace, subjoint);
|
|
|
|
}
|
|
|
|
_initDirty = false;
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:11:50 +08:00
|
|
|
void PhysicsJoint::flushDelayTasks()
|
|
|
|
{
|
2019-04-28 09:20:22 +08:00
|
|
|
for (const auto& tsk : _delayTasks)
|
2018-10-19 18:11:50 +08:00
|
|
|
{
|
|
|
|
tsk();
|
|
|
|
}
|
|
|
|
_delayTasks.clear();
|
|
|
|
}
|
|
|
|
|
2013-09-29 09:39:20 +08:00
|
|
|
void PhysicsJoint::setEnable(bool enable)
|
|
|
|
{
|
|
|
|
if (_enable != enable)
|
|
|
|
{
|
|
|
|
_enable = enable;
|
2015-01-07 17:08:04 +08:00
|
|
|
|
|
|
|
if (_world)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2013-11-07 14:17:57 +08:00
|
|
|
if (enable)
|
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
_world->addJoint(this);
|
|
|
|
}
|
|
|
|
else
|
2013-11-07 14:17:57 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
_world->removeJoint(this, false);
|
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-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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
void PhysicsJoint::setMaxForce(float force)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
if(_initDirty)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
delay([this, force]() {
|
|
|
|
_maxForce = force;
|
|
|
|
for (auto joint : _cpConstraints)
|
|
|
|
{
|
|
|
|
cpConstraintSetMaxForce(joint, force);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_maxForce = force;
|
|
|
|
for (auto joint : _cpConstraints)
|
|
|
|
{
|
|
|
|
cpConstraintSetMaxForce(joint, force);
|
|
|
|
}
|
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
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = new (std::nothrow) PhysicsJointFixed();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
joint->_anchr = anchr;
|
2013-09-16 21:22:22 +08:00
|
|
|
return joint;
|
|
|
|
}
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
bool PhysicsJointFixed::createConstraints()
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
_bodyA->getNode()->setPosition(_anchr);
|
|
|
|
_bodyB->getNode()->setPosition(_anchr);
|
|
|
|
|
2013-10-17 10:57:48 +08:00
|
|
|
// add a pivot joint to fixed two body together
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = cpPivotJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(),
|
2021-10-24 14:09:59 +08:00
|
|
|
PhysicsHelper::vec22cpv(_anchr));
|
2015-01-07 17:08:04 +08:00
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
_cpConstraints.push_back(joint);
|
|
|
|
|
2013-10-17 10:57:48 +08:00
|
|
|
// add a gear joint to make two body have the same rotation.
|
2015-01-07 17:08:04 +08:00
|
|
|
joint = cpGearJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), 0, 1);
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
_cpConstraints.push_back(joint);
|
|
|
|
|
|
|
|
_collisionEnable = false;
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
PhysicsJointPin* PhysicsJointPin::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& pivot)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = new (std::nothrow) PhysicsJointPin();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
joint->_anchr1 = pivot;
|
|
|
|
joint->_useSpecificAnchr = false;
|
2013-09-16 21:22:22 +08:00
|
|
|
return joint;
|
|
|
|
}
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
PhysicsJointPin* PhysicsJointPin::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2)
|
|
|
|
{
|
|
|
|
auto joint = new (std::nothrow) PhysicsJointPin();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b))
|
|
|
|
{
|
|
|
|
joint->_anchr1 = anchr1;
|
|
|
|
joint->_anchr2 = anchr2;
|
|
|
|
joint->_useSpecificAnchr = true;
|
|
|
|
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointPin::createConstraints()
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
cpConstraint* joint = nullptr;
|
|
|
|
if (_useSpecificAnchr)
|
|
|
|
{
|
|
|
|
joint = cpPivotJointNew2(_bodyA->getCPBody(), _bodyB->getCPBody(),
|
2021-10-24 14:09:59 +08:00
|
|
|
PhysicsHelper::vec22cpv(_anchr1), PhysicsHelper::vec22cpv(_anchr2));
|
2015-01-07 17:08:04 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
joint = cpPivotJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(),
|
2021-10-24 14:09:59 +08:00
|
|
|
PhysicsHelper::vec22cpv(_anchr1));
|
2015-01-07 17:08:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
_cpConstraints.push_back(joint);
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
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
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = new (std::nothrow) PhysicsJointLimit();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
joint->_anchr1 = anchr1;
|
|
|
|
joint->_anchr2 = anchr2;
|
|
|
|
joint->_min = min;
|
|
|
|
joint->_max = max;
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return joint;
|
|
|
|
}
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
bool PhysicsJointLimit::createConstraints()
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = cpSlideJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(),
|
2021-10-24 14:09:59 +08:00
|
|
|
PhysicsHelper::vec22cpv(_anchr1),
|
|
|
|
PhysicsHelper::vec22cpv(_anchr2),
|
2015-09-18 11:48:43 +08:00
|
|
|
_min,
|
|
|
|
_max);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
_cpConstraints.push_back(joint);
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
float PhysicsJointLimit::getMin() const
|
2013-10-17 10:57:48 +08:00
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_min);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_min, cpSlideJointSetMin, 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
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_max);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_max, cpSlideJointSetMax, 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
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_anchr1);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpSlideJointGetAnchorA(_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
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE2(_anchr1, cpSlideJointSetAnchorA, anchr, PhysicsHelper::vec22cpv(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
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_anchr2);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpSlideJointGetAnchorB(_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
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE2(_anchr2, cpSlideJointSetAnchorB, anchr, PhysicsHelper::vec22cpv(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
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = new (std::nothrow) PhysicsJointDistance();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b))
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
joint->_anchr1 = anchr1;
|
|
|
|
joint->_anchr2 = anchr2;
|
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
return joint;
|
|
|
|
}
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
bool PhysicsJointDistance::createConstraints()
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = cpPinJointNew(_bodyA->getCPBody(),
|
|
|
|
_bodyB->getCPBody(),
|
2021-10-24 14:09:59 +08:00
|
|
|
PhysicsHelper::vec22cpv(_anchr1),
|
|
|
|
PhysicsHelper::vec22cpv(_anchr2));
|
2015-01-07 17:08:04 +08:00
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
_cpConstraints.push_back(joint);
|
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
float PhysicsJointDistance::getDistance() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_distance);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_distance, cpPinJointSetDist, 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
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = new (std::nothrow) PhysicsJointSpring();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b))
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
joint->_anchr1 = anchr1;
|
|
|
|
joint->_anchr2 = anchr2;
|
|
|
|
joint->_stiffness = stiffness;
|
|
|
|
joint->_damping = damping;
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return joint;
|
|
|
|
}
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
bool PhysicsJointSpring::createConstraints()
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
|
|
|
do {
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = cpDampedSpringNew(_bodyA->getCPBody(),
|
|
|
|
_bodyB->getCPBody(),
|
2021-10-24 14:09:59 +08:00
|
|
|
PhysicsHelper::vec22cpv(_anchr1),
|
|
|
|
PhysicsHelper::vec22cpv(_anchr2),
|
2015-09-18 11:48:43 +08:00
|
|
|
_bodyB->local2World(_anchr1).getDistance(_bodyA->local2World(_anchr2)),
|
|
|
|
_stiffness,
|
|
|
|
_damping);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
_cpConstraints.push_back(joint);
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsJointSpring::getAnchr1() const
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_anchr1);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpDampedSpringGetAnchorA(_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
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE2(_anchr1, cpDampedSpringSetAnchorA, anchr, PhysicsHelper::vec22cpv(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
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_anchr2);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpDampedSpringGetAnchorB(_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
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE2(_anchr2, cpDampedSpringSetAnchorB, anchr, PhysicsHelper::vec22cpv(anchr));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointSpring::getRestLength() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_restLength);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_restLength, cpDampedSpringSetRestLength, restLength);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointSpring::getStiffness() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_stiffness);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_stiffness, cpDampedSpringSetStiffness, stiffness);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointSpring::getDamping() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_damping);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_damping, cpDampedSpringSetDamping, 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
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = new (std::nothrow) PhysicsJointGroove();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b))
|
2013-12-03 12:47:03 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
joint->_grooveA = grooveA;
|
|
|
|
joint->_grooveB = grooveB;
|
|
|
|
joint->_anchr2 = anchr2;
|
|
|
|
|
2013-12-03 12:47:03 +08:00
|
|
|
return joint;
|
|
|
|
}
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-03 12:47:03 +08:00
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
bool PhysicsJointGroove::createConstraints()
|
2013-12-03 12:47:03 +08:00
|
|
|
{
|
|
|
|
do {
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = cpGrooveJointNew(_bodyA->getCPBody(),
|
|
|
|
_bodyB->getCPBody(),
|
2021-10-24 14:09:59 +08:00
|
|
|
PhysicsHelper::vec22cpv(_grooveA),
|
|
|
|
PhysicsHelper::vec22cpv(_grooveB),
|
|
|
|
PhysicsHelper::vec22cpv(_anchr2));
|
2015-01-07 17:08:04 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
_cpConstraints.push_back(joint);
|
|
|
|
|
2013-12-03 12:47:03 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-03 12:47:03 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsJointGroove::getGrooveA() const
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_grooveA);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(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
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE2(_grooveA, cpGrooveJointSetGrooveA, grooveA, PhysicsHelper::vec22cpv(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
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_grooveB);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(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
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE2(_grooveB, cpGrooveJointSetGrooveB, grooveB, PhysicsHelper::vec22cpv(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
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_anchr2);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpGrooveJointGetAnchorB(_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
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE2(_anchr2, cpGrooveJointSetAnchorB, anchr2, PhysicsHelper::vec22cpv(anchr2));
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointRotarySpring* PhysicsJointRotarySpring::construct(PhysicsBody* a, PhysicsBody* b, float stiffness, float damping)
|
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = new (std::nothrow) PhysicsJointRotarySpring();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b))
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
joint->_stiffness = stiffness;
|
|
|
|
joint->_damping = damping;
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return joint;
|
|
|
|
}
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
bool PhysicsJointRotarySpring::createConstraints()
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
|
|
|
do {
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = cpDampedRotarySpringNew(_bodyA->getCPBody(),
|
|
|
|
_bodyB->getCPBody(),
|
2015-09-18 11:48:43 +08:00
|
|
|
_bodyB->getRotation() - _bodyA->getRotation(),
|
|
|
|
_stiffness,
|
|
|
|
_damping);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
_cpConstraints.push_back(joint);
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotarySpring::getRestAngle() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_restAngle);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_restAngle, cpDampedRotarySpringSetRestAngle, restAngle);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotarySpring::getStiffness() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_stiffness);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_stiffness, cpDampedRotarySpringSetStiffness, stiffness);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotarySpring::getDamping() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_damping);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_damping, cpDampedRotarySpringSetDamping, 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
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = new (std::nothrow) PhysicsJointRotaryLimit();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b))
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
joint->_min = min;
|
|
|
|
joint->_max = max;
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return joint;
|
|
|
|
}
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
bool PhysicsJointRotaryLimit::createConstraints()
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = cpRotaryLimitJointNew(_bodyA->getCPBody(),
|
|
|
|
_bodyB->getCPBody(),
|
2015-09-18 11:48:43 +08:00
|
|
|
_min,
|
|
|
|
_max);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
_cpConstraints.push_back(joint);
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotaryLimit::getMin() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_min);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_min, cpRotaryLimitJointSetMin, min);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotaryLimit::getMax() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_max);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_max, cpRotaryLimitJointSetMax, max);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointRatchet* PhysicsJointRatchet::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet)
|
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = new (std::nothrow) PhysicsJointRatchet();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b))
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
joint->_phase = phase;
|
|
|
|
joint->_ratchet = ratchet;
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return joint;
|
|
|
|
}
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
bool PhysicsJointRatchet::createConstraints()
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = cpRatchetJointNew(_bodyA->getCPBody(),
|
|
|
|
_bodyB->getCPBody(),
|
2015-09-18 11:48:43 +08:00
|
|
|
_phase,
|
2015-01-07 17:08:04 +08:00
|
|
|
PhysicsHelper::cpfloat2float(_ratchet));
|
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
_cpConstraints.push_back(joint);
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRatchet::getAngle() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_angle);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_angle, cpRatchetJointSetAngle, angle);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRatchet::getPhase() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_phase);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_phase, cpRatchetJointSetPhase, phase);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRatchet::getRatchet() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_ratchet);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_ratchet, cpRatchetJointSetRatchet, ratchet);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
PhysicsJointGear* PhysicsJointGear::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratio)
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = new (std::nothrow) PhysicsJointGear();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b))
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
joint->_phase = phase;
|
|
|
|
joint->_ratio = ratio;
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return joint;
|
|
|
|
}
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
bool PhysicsJointGear::createConstraints()
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = cpGearJointNew(_bodyA->getCPBody(),
|
|
|
|
_bodyB->getCPBody(),
|
2015-09-18 11:48:43 +08:00
|
|
|
_phase,
|
|
|
|
_ratio);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
_cpConstraints.push_back(joint);
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointGear::getPhase() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_phase);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_phase, cpGearJointSetPhase, phase);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointGear::getRatio() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_ratio);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_ratio, cpGearJointSetRatio, ratio);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointMotor* PhysicsJointMotor::construct(PhysicsBody* a, PhysicsBody* b, float rate)
|
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = new (std::nothrow) PhysicsJointMotor();
|
|
|
|
|
|
|
|
if (joint && joint->init(a, b))
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
joint->_rate = rate;
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return joint;
|
|
|
|
}
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
CC_SAFE_DELETE(joint);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
bool PhysicsJointMotor::createConstraints()
|
2013-12-02 18:29:04 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
auto joint = cpSimpleMotorNew(_bodyA->getCPBody(),
|
|
|
|
_bodyB->getCPBody(),
|
2015-09-18 11:48:43 +08:00
|
|
|
_rate);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(joint == nullptr);
|
|
|
|
_cpConstraints.push_back(joint);
|
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointMotor::getRate() const
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_READ(_rate);
|
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)
|
|
|
|
{
|
2018-10-19 18:11:50 +08:00
|
|
|
CC_PJOINT_CACHE_WRITE(_rate, cpSimpleMotorSetRate, rate);
|
2013-12-02 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
NS_CC_END
|
|
|
|
#endif // CC_USE_PHYSICS
|