2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "physics/PhysicsJoint.h"
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_PHYSICS
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "chipmunk/chipmunk.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
# include "physics/PhysicsBody.h"
|
|
|
|
# include "physics/PhysicsWorld.h"
|
|
|
|
# include "physics/PhysicsHelper.h"
|
|
|
|
# include "2d/Node.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
template <typename T>
|
|
|
|
class Optional
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
Optional() {}
|
2021-12-25 10:04:45 +08:00
|
|
|
Optional(T d) : _isSet(true), _data(d) {}
|
|
|
|
Optional(const Optional& t) : _isSet(t._isSet), _data(t._data) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// bool isNull() const { return !_isSet; }
|
|
|
|
// bool isDefineded() const { return _isSet; }
|
|
|
|
// bool isEmpty() const { return !_isSet; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
T get() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_isSet, "data should be set!");
|
2021-12-25 10:04:45 +08:00
|
|
|
return _data;
|
|
|
|
}
|
|
|
|
void set(T d)
|
|
|
|
{
|
|
|
|
_isSet = true;
|
|
|
|
_data = d;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool _isSet = false;
|
|
|
|
T _data;
|
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
class WriteCache
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
Optional<Vec2> _grooveA;
|
|
|
|
Optional<Vec2> _grooveB;
|
|
|
|
Optional<Vec2> _anchr1;
|
|
|
|
Optional<Vec2> _anchr2;
|
2019-11-23 20:27:39 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# 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
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
# define AX_PJOINT_CACHE_READ(field) \
|
2021-12-25 10:04:45 +08:00
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (UNLIKELY(_initDirty)) \
|
|
|
|
{ \
|
|
|
|
return _writeCache->field.get(); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
# define AX_PJOINT_CACHE_WRITE2(field, method, arg, convertedArg) \
|
2021-12-25 10:04:45 +08:00
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (UNLIKELY(_initDirty)) \
|
|
|
|
{ \
|
|
|
|
_writeCache->field.set(arg); \
|
|
|
|
delay([this, arg]() { method(_cpConstraints.front(), convertedArg); }); \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
method(_cpConstraints.front(), convertedArg); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
# define AX_PJOINT_CACHE_WRITE(field, method, arg) AX_PJOINT_CACHE_WRITE2(field, method, arg, arg)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
PhysicsJoint::PhysicsJoint()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _bodyA(nullptr)
|
|
|
|
, _bodyB(nullptr)
|
|
|
|
, _world(nullptr)
|
|
|
|
, _enable(false)
|
|
|
|
, _collisionEnable(true)
|
|
|
|
, _destroyMark(false)
|
|
|
|
, _tag(0)
|
|
|
|
, _maxForce(PHYSICS_INFINITY)
|
|
|
|
, _initDirty(true)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_writeCache = new WriteCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJoint::~PhysicsJoint()
|
|
|
|
{
|
|
|
|
// reset the shapes collision group
|
|
|
|
setCollisionEnable(true);
|
|
|
|
|
|
|
|
for (cpConstraint* joint : _cpConstraints)
|
|
|
|
{
|
|
|
|
cpConstraintFree(joint);
|
|
|
|
}
|
|
|
|
_cpConstraints.clear();
|
|
|
|
|
|
|
|
delete _writeCache;
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
bool PhysicsJoint::init(ax::PhysicsBody* a, ax::PhysicsBody* b)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(a != nullptr && b != nullptr, "the body passed in is nil");
|
|
|
|
AXASSERT(a != b, "the two bodies are equal");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_bodyA = a;
|
|
|
|
_bodyB = b;
|
2022-08-08 13:18:33 +08:00
|
|
|
_bodyA->_joints.emplace_back(this);
|
|
|
|
_bodyB->_joints.emplace_back(this);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJoint::initJoint()
|
|
|
|
{
|
|
|
|
bool ret = !_initDirty;
|
|
|
|
while (_initDirty)
|
|
|
|
{
|
|
|
|
ret = createConstraints();
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(!ret);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& subjoint : _cpConstraints)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
cpConstraintSetMaxForce(subjoint, _maxForce);
|
|
|
|
cpConstraintSetErrorBias(subjoint, cpfpow(1.0f - 0.15f, 60.0f));
|
|
|
|
cpSpaceAddConstraint(_world->_cpSpace, subjoint);
|
|
|
|
}
|
|
|
|
_initDirty = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
ret = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJoint::flushDelayTasks()
|
|
|
|
{
|
|
|
|
for (const auto& tsk : _delayTasks)
|
|
|
|
{
|
|
|
|
tsk();
|
|
|
|
}
|
|
|
|
_delayTasks.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJoint::setEnable(bool enable)
|
|
|
|
{
|
|
|
|
if (_enable != enable)
|
|
|
|
{
|
|
|
|
_enable = enable;
|
|
|
|
|
|
|
|
if (_world)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
_world->addJoint(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_world->removeJoint(this, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJoint::setCollisionEnable(bool enable)
|
|
|
|
{
|
|
|
|
if (_collisionEnable != enable)
|
|
|
|
{
|
|
|
|
_collisionEnable = enable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJoint::removeFormWorld()
|
|
|
|
{
|
|
|
|
if (_world)
|
|
|
|
{
|
|
|
|
_world->removeJoint(this, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJoint::setMaxForce(float force)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_initDirty)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
delay([this, force]() {
|
|
|
|
_maxForce = force;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& joint : _cpConstraints)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
cpConstraintSetMaxForce(joint, force);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_maxForce = force;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& joint : _cpConstraints)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
cpConstraintSetMaxForce(joint, force);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointFixed* PhysicsJointFixed::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto joint = new PhysicsJointFixed();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (joint->init(a, b))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
joint->_anchr = anchr;
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointFixed::createConstraints()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
_bodyA->getNode()->setPosition(_anchr);
|
|
|
|
_bodyB->getNode()->setPosition(_anchr);
|
|
|
|
|
|
|
|
// add a pivot joint to fixed two body together
|
2021-12-25 10:04:45 +08:00
|
|
|
auto joint = cpPivotJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), PhysicsHelper::vec22cpv(_anchr));
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(joint == nullptr);
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpConstraints.emplace_back(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// add a gear joint to make two body have the same rotation.
|
|
|
|
joint = cpGearJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), 0, 1);
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(joint == nullptr);
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpConstraints.emplace_back(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_collisionEnable = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointPin* PhysicsJointPin::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& pivot)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto joint = new PhysicsJointPin();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (joint->init(a, b))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
joint->_anchr1 = pivot;
|
2019-11-23 20:27:39 +08:00
|
|
|
joint->_useSpecificAnchr = false;
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointPin* PhysicsJointPin::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto joint = new PhysicsJointPin();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (joint->init(a, b))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
joint->_anchr1 = anchr1;
|
|
|
|
joint->_anchr2 = anchr2;
|
2019-11-23 20:27:39 +08:00
|
|
|
joint->_useSpecificAnchr = true;
|
|
|
|
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointPin::createConstraints()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
cpConstraint* joint = nullptr;
|
|
|
|
if (_useSpecificAnchr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
joint = cpPivotJointNew2(_bodyA->getCPBody(), _bodyB->getCPBody(), PhysicsHelper::vec22cpv(_anchr1),
|
|
|
|
PhysicsHelper::vec22cpv(_anchr2));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
joint = cpPivotJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), PhysicsHelper::vec22cpv(_anchr1));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(joint == nullptr);
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpConstraints.emplace_back(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a,
|
|
|
|
PhysicsBody* b,
|
|
|
|
const Vec2& anchr1,
|
|
|
|
const Vec2& anchr2,
|
|
|
|
float min,
|
|
|
|
float max)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto joint = new PhysicsJointLimit();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (joint->init(a, b))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
joint->_anchr1 = anchr1;
|
|
|
|
joint->_anchr2 = anchr2;
|
2021-12-25 10:04:45 +08:00
|
|
|
joint->_min = min;
|
|
|
|
joint->_max = max;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2)
|
|
|
|
{
|
|
|
|
return construct(a, b, anchr1, anchr2, 0, b->local2World(anchr1).getDistance(a->local2World(anchr2)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointLimit::createConstraints()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto joint = cpSlideJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), PhysicsHelper::vec22cpv(_anchr1),
|
|
|
|
PhysicsHelper::vec22cpv(_anchr2), _min, _max);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(joint == nullptr);
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpConstraints.emplace_back(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointLimit::getMin() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_min);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpSlideJointGetMin(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointLimit::setMin(float min)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_min, cpSlideJointSetMin, min);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointLimit::getMax() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_max);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpSlideJointGetMax(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointLimit::setMax(float max)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_max, cpSlideJointSetMax, max);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 PhysicsJointLimit::getAnchr1() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_anchr1);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpSlideJointGetAnchorA(_cpConstraints.front()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointLimit::setAnchr1(const Vec2& anchr)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE2(_anchr1, cpSlideJointSetAnchorA, anchr, PhysicsHelper::vec22cpv(anchr));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 PhysicsJointLimit::getAnchr2() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_anchr2);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpSlideJointGetAnchorB(_cpConstraints.front()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointLimit::setAnchr2(const Vec2& anchr)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE2(_anchr2, cpSlideJointSetAnchorB, anchr, PhysicsHelper::vec22cpv(anchr));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsJointDistance* PhysicsJointDistance::construct(PhysicsBody* a,
|
|
|
|
PhysicsBody* b,
|
|
|
|
const Vec2& anchr1,
|
|
|
|
const Vec2& anchr2)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto joint = new PhysicsJointDistance();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (joint->init(a, b))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
joint->_anchr1 = anchr1;
|
|
|
|
joint->_anchr2 = anchr2;
|
|
|
|
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointDistance::createConstraints()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto joint = cpPinJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), PhysicsHelper::vec22cpv(_anchr1),
|
|
|
|
PhysicsHelper::vec22cpv(_anchr2));
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(joint == nullptr);
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpConstraints.emplace_back(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointDistance::getDistance() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_distance);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpPinJointGetDist(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointDistance::setDistance(float distance)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_distance, cpPinJointSetDist, distance);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsJointSpring* PhysicsJointSpring::construct(PhysicsBody* a,
|
|
|
|
PhysicsBody* b,
|
|
|
|
const Vec2& anchr1,
|
|
|
|
const Vec2& anchr2,
|
|
|
|
float stiffness,
|
|
|
|
float damping)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto joint = new PhysicsJointSpring();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (joint->init(a, b))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
joint->_anchr1 = anchr1;
|
|
|
|
joint->_anchr2 = anchr2;
|
2019-11-23 20:27:39 +08:00
|
|
|
joint->_stiffness = stiffness;
|
2021-12-25 10:04:45 +08:00
|
|
|
joint->_damping = damping;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointSpring::createConstraints()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
auto joint = cpDampedSpringNew(_bodyA->getCPBody(), _bodyB->getCPBody(), PhysicsHelper::vec22cpv(_anchr1),
|
|
|
|
PhysicsHelper::vec22cpv(_anchr2),
|
|
|
|
_bodyB->local2World(_anchr1).getDistance(_bodyA->local2World(_anchr2)),
|
|
|
|
_stiffness, _damping);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(joint == nullptr);
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpConstraints.emplace_back(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 PhysicsJointSpring::getAnchr1() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_anchr1);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpDampedSpringGetAnchorA(_cpConstraints.front()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setAnchr1(const Vec2& anchr)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE2(_anchr1, cpDampedSpringSetAnchorA, anchr, PhysicsHelper::vec22cpv(anchr));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 PhysicsJointSpring::getAnchr2() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_anchr2);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpDampedSpringGetAnchorB(_cpConstraints.front()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setAnchr2(const Vec2& anchr)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE2(_anchr2, cpDampedSpringSetAnchorB, anchr, PhysicsHelper::vec22cpv(anchr));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointSpring::getRestLength() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_restLength);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedSpringGetRestLength(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setRestLength(float restLength)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_restLength, cpDampedSpringSetRestLength, restLength);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointSpring::getStiffness() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_stiffness);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedSpringGetStiffness(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setStiffness(float stiffness)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_stiffness, cpDampedSpringSetStiffness, stiffness);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointSpring::getDamping() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_damping);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedSpringGetDamping(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointSpring::setDamping(float damping)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_damping, cpDampedSpringSetDamping, damping);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsJointGroove* PhysicsJointGroove::construct(PhysicsBody* a,
|
|
|
|
PhysicsBody* b,
|
|
|
|
const Vec2& grooveA,
|
|
|
|
const Vec2& grooveB,
|
|
|
|
const Vec2& anchr2)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto joint = new PhysicsJointGroove();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (joint->init(a, b))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
joint->_grooveA = grooveA;
|
|
|
|
joint->_grooveB = grooveB;
|
2021-12-25 10:04:45 +08:00
|
|
|
joint->_anchr2 = anchr2;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointGroove::createConstraints()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
auto joint = cpGrooveJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), PhysicsHelper::vec22cpv(_grooveA),
|
|
|
|
PhysicsHelper::vec22cpv(_grooveB), PhysicsHelper::vec22cpv(_anchr2));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(joint == nullptr);
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpConstraints.emplace_back(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 PhysicsJointGroove::getGrooveA() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_grooveA);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpGrooveJointGetGrooveA(_cpConstraints.front()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointGroove::setGrooveA(const Vec2& grooveA)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE2(_grooveA, cpGrooveJointSetGrooveA, grooveA, PhysicsHelper::vec22cpv(grooveA));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 PhysicsJointGroove::getGrooveB() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_grooveB);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpGrooveJointGetGrooveB(_cpConstraints.front()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointGroove::setGrooveB(const Vec2& grooveB)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE2(_grooveB, cpGrooveJointSetGrooveB, grooveB, PhysicsHelper::vec22cpv(grooveB));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 PhysicsJointGroove::getAnchr2() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_anchr2);
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpGrooveJointGetAnchorB(_cpConstraints.front()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointGroove::setAnchr2(const Vec2& anchr2)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE2(_anchr2, cpGrooveJointSetAnchorB, anchr2, PhysicsHelper::vec22cpv(anchr2));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsJointRotarySpring* PhysicsJointRotarySpring::construct(PhysicsBody* a,
|
|
|
|
PhysicsBody* b,
|
|
|
|
float stiffness,
|
|
|
|
float damping)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto joint = new PhysicsJointRotarySpring();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (joint->init(a, b))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
joint->_stiffness = stiffness;
|
2021-12-25 10:04:45 +08:00
|
|
|
joint->_damping = damping;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointRotarySpring::createConstraints()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
auto joint = cpDampedRotarySpringNew(_bodyA->getCPBody(), _bodyB->getCPBody(),
|
|
|
|
_bodyB->getRotation() - _bodyA->getRotation(), _stiffness, _damping);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(joint == nullptr);
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpConstraints.emplace_back(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotarySpring::getRestAngle() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_restAngle);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetRestAngle(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotarySpring::setRestAngle(float restAngle)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_restAngle, cpDampedRotarySpringSetRestAngle, restAngle);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotarySpring::getStiffness() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_stiffness);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetStiffness(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotarySpring::setStiffness(float stiffness)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_stiffness, cpDampedRotarySpringSetStiffness, stiffness);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotarySpring::getDamping() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_damping);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetDamping(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotarySpring::setDamping(float damping)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_damping, cpDampedRotarySpringSetDamping, damping);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointRotaryLimit* PhysicsJointRotaryLimit::construct(PhysicsBody* a, PhysicsBody* b, float min, float max)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto joint = new PhysicsJointRotaryLimit();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (joint->init(a, b))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
joint->_min = min;
|
|
|
|
joint->_max = max;
|
|
|
|
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointRotaryLimit* PhysicsJointRotaryLimit::construct(PhysicsBody* a, PhysicsBody* b)
|
|
|
|
{
|
|
|
|
return construct(a, b, 0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointRotaryLimit::createConstraints()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto joint = cpRotaryLimitJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), _min, _max);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(joint == nullptr);
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpConstraints.emplace_back(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotaryLimit::getMin() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_min);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMin(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotaryLimit::setMin(float min)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_min, cpRotaryLimitJointSetMin, min);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRotaryLimit::getMax() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_max);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMax(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRotaryLimit::setMax(float max)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_max, cpRotaryLimitJointSetMax, max);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointRatchet* PhysicsJointRatchet::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto joint = new PhysicsJointRatchet();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (joint->init(a, b))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
joint->_phase = phase;
|
2019-11-23 20:27:39 +08:00
|
|
|
joint->_ratchet = ratchet;
|
|
|
|
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointRatchet::createConstraints()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto joint =
|
|
|
|
cpRatchetJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), _phase, PhysicsHelper::cpfloat2float(_ratchet));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(joint == nullptr);
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpConstraints.emplace_back(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRatchet::getAngle() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_angle);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpRatchetJointGetAngle(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRatchet::setAngle(float angle)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_angle, cpRatchetJointSetAngle, angle);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRatchet::getPhase() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_phase);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpRatchetJointGetPhase(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRatchet::setPhase(float phase)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_phase, cpRatchetJointSetPhase, phase);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointRatchet::getRatchet() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_ratchet);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpRatchetJointGetRatchet(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointRatchet::setRatchet(float ratchet)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_ratchet, cpRatchetJointSetRatchet, ratchet);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointGear* PhysicsJointGear::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratio)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto joint = new PhysicsJointGear();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (joint->init(a, b))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
joint->_phase = phase;
|
|
|
|
joint->_ratio = ratio;
|
|
|
|
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointGear::createConstraints()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto joint = cpGearJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), _phase, _ratio);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(joint == nullptr);
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpConstraints.emplace_back(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointGear::getPhase() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_phase);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpGearJointGetPhase(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointGear::setPhase(float phase)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_phase, cpGearJointSetPhase, phase);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointGear::getRatio() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_ratio);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpGearJointGetRatio(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointGear::setRatio(float ratio)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_ratio, cpGearJointSetRatio, ratio);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsJointMotor* PhysicsJointMotor::construct(PhysicsBody* a, PhysicsBody* b, float rate)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto joint = new PhysicsJointMotor();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (joint->init(a, b))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
joint->_rate = rate;
|
|
|
|
|
|
|
|
return joint;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsJointMotor::createConstraints()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto joint = cpSimpleMotorNew(_bodyA->getCPBody(), _bodyB->getCPBody(), _rate);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(joint == nullptr);
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpConstraints.emplace_back(joint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsJointMotor::getRate() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_READ(_rate);
|
2019-11-23 20:27:39 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpSimpleMotorGetRate(_cpConstraints.front()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsJointMotor::setRate(float rate)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PJOINT_CACHE_WRITE(_rate, cpSimpleMotorSetRate, rate);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2022-07-16 10:43:05 +08:00
|
|
|
#endif // AX_USE_PHYSICS
|