123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806 |
- /****************************************************************************
- Copyright (c) 2013-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
-
- 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.
- ****************************************************************************/
- #include "physics/CCPhysicsJoint.h"
- #if CC_USE_PHYSICS
- #include "chipmunk/chipmunk.h"
- #include "physics/CCPhysicsBody.h"
- #include "physics/CCPhysicsWorld.h"
- #include "physics/CCPhysicsHelper.h"
- #include "2d/CCNode.h"
- NS_CC_BEGIN
- PhysicsJoint::PhysicsJoint()
- : _bodyA(nullptr)
- , _bodyB(nullptr)
- , _world(nullptr)
- , _enable(false)
- , _collisionEnable(true)
- , _destroyMark(false)
- , _tag(0)
- , _maxForce(PHYSICS_INFINITY)
- , _initDirty(true)
- {
- }
- PhysicsJoint::~PhysicsJoint()
- {
- // reset the shapes collision group
- setCollisionEnable(true);
- for (cpConstraint* joint : _cpConstraints)
- {
- cpConstraintFree(joint);
- }
- _cpConstraints.clear();
- }
- bool PhysicsJoint::init(cocos2d::PhysicsBody *a, cocos2d::PhysicsBody *b)
- {
- do
- {
- CCASSERT(a != nullptr && b != nullptr, "the body passed in is nil");
- CCASSERT(a != b, "the two bodies are equal");
- _bodyA = a;
- _bodyB = b;
- _bodyA->_joints.push_back(this);
- _bodyB->_joints.push_back(this);
- return true;
- } while (false);
- return false;
- }
- bool PhysicsJoint::initJoint()
- {
- bool ret = !_initDirty;
- while (_initDirty)
- {
- ret = createConstraints();
- CC_BREAK_IF(!ret);
- for (auto subjoint : _cpConstraints)
- {
- cpConstraintSetMaxForce(subjoint, _maxForce);
- cpConstraintSetErrorBias(subjoint, cpfpow(1.0f - 0.15f, 60.0f));
- cpSpaceAddConstraint(_world->_cpSpace, subjoint);
- }
- _initDirty = false;
- ret = true;
- }
- return ret;
- }
- 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)
- {
- _maxForce = force;
- for (auto joint : _cpConstraints)
- {
- cpConstraintSetMaxForce(joint, force);
- }
- }
- PhysicsJointFixed* PhysicsJointFixed::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr)
- {
- auto joint = new (std::nothrow) PhysicsJointFixed();
- if (joint && joint->init(a, b))
- {
- joint->_anchr = anchr;
- return joint;
- }
- CC_SAFE_DELETE(joint);
- return nullptr;
- }
- bool PhysicsJointFixed::createConstraints()
- {
- do
- {
- _bodyA->getNode()->setPosition(_anchr);
- _bodyB->getNode()->setPosition(_anchr);
- // add a pivot joint to fixed two body together
- auto joint = cpPivotJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(),
- PhysicsHelper::point2cpv(_anchr));
- CC_BREAK_IF(joint == nullptr);
- _cpConstraints.push_back(joint);
- // add a gear joint to make two body have the same rotation.
- joint = cpGearJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), 0, 1);
- CC_BREAK_IF(joint == nullptr);
- _cpConstraints.push_back(joint);
- _collisionEnable = false;
- return true;
- } while (false);
- return false;
- }
- PhysicsJointPin* PhysicsJointPin::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& pivot)
- {
- auto joint = new (std::nothrow) PhysicsJointPin();
- if (joint && joint->init(a, b))
- {
- joint->_anchr1 = pivot;
- joint->_useSpecificAnchr = false;
- return joint;
- }
- CC_SAFE_DELETE(joint);
- return nullptr;
- }
- 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()
- {
- do
- {
- cpConstraint* joint = nullptr;
- if (_useSpecificAnchr)
- {
- joint = cpPivotJointNew2(_bodyA->getCPBody(), _bodyB->getCPBody(),
- PhysicsHelper::point2cpv(_anchr1), PhysicsHelper::point2cpv(_anchr2));
- }
- else
- {
- joint = cpPivotJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(),
- PhysicsHelper::point2cpv(_anchr1));
- }
- CC_BREAK_IF(joint == nullptr);
- _cpConstraints.push_back(joint);
- return true;
- } while (false);
- return false;
- }
- PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float min, float max)
- {
- auto joint = new (std::nothrow) PhysicsJointLimit();
- if (joint && joint->init(a, b))
- {
- joint->_anchr1 = anchr1;
- joint->_anchr2 = anchr2;
- joint->_min = min;
- joint->_max = max;
- return joint;
- }
- CC_SAFE_DELETE(joint);
- 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
- {
- auto joint = cpSlideJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(),
- PhysicsHelper::point2cpv(_anchr1),
- PhysicsHelper::point2cpv(_anchr2),
- _min,
- _max);
- CC_BREAK_IF(joint == nullptr);
- _cpConstraints.push_back(joint);
- return true;
- } while (false);
- return false;
- }
- float PhysicsJointLimit::getMin() const
- {
- return PhysicsHelper::cpfloat2float(cpSlideJointGetMin(_cpConstraints.front()));
- }
- void PhysicsJointLimit::setMin(float min)
- {
- cpSlideJointSetMin(_cpConstraints.front(), min);
- }
- float PhysicsJointLimit::getMax() const
- {
- return PhysicsHelper::cpfloat2float(cpSlideJointGetMax(_cpConstraints.front()));
- }
- void PhysicsJointLimit::setMax(float max)
- {
- cpSlideJointSetMax(_cpConstraints.front(), max);
- }
- Vec2 PhysicsJointLimit::getAnchr1() const
- {
- return PhysicsHelper::cpv2point(cpSlideJointGetAnchorA(_cpConstraints.front()));
- }
- void PhysicsJointLimit::setAnchr1(const Vec2& anchr)
- {
- cpSlideJointSetAnchorA(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
- }
- Vec2 PhysicsJointLimit::getAnchr2() const
- {
- return PhysicsHelper::cpv2point(cpSlideJointGetAnchorB(_cpConstraints.front()));
- }
- void PhysicsJointLimit::setAnchr2(const Vec2& anchr)
- {
- cpSlideJointSetAnchorB(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
- }
- PhysicsJointDistance* PhysicsJointDistance::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2)
- {
- auto joint = new (std::nothrow) PhysicsJointDistance();
- if (joint && joint->init(a, b))
- {
- joint->_anchr1 = anchr1;
- joint->_anchr2 = anchr2;
- return joint;
- }
- CC_SAFE_DELETE(joint);
- return nullptr;
- }
- bool PhysicsJointDistance::createConstraints()
- {
- do
- {
- auto joint = cpPinJointNew(_bodyA->getCPBody(),
- _bodyB->getCPBody(),
- PhysicsHelper::point2cpv(_anchr1),
- PhysicsHelper::point2cpv(_anchr2));
- CC_BREAK_IF(joint == nullptr);
- _cpConstraints.push_back(joint);
- return true;
- } while (false);
- return false;
- }
- float PhysicsJointDistance::getDistance() const
- {
- return PhysicsHelper::cpfloat2float(cpPinJointGetDist(_cpConstraints.front()));
- }
- void PhysicsJointDistance::setDistance(float distance)
- {
- cpPinJointSetDist(_cpConstraints.front(), distance);
- }
- PhysicsJointSpring* PhysicsJointSpring::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float stiffness, float damping)
- {
- auto joint = new (std::nothrow) PhysicsJointSpring();
- if (joint && joint->init(a, b))
- {
- joint->_anchr1 = anchr1;
- joint->_anchr2 = anchr2;
- joint->_stiffness = stiffness;
- joint->_damping = damping;
- return joint;
- }
- CC_SAFE_DELETE(joint);
- return nullptr;
- }
- bool PhysicsJointSpring::createConstraints()
- {
- do {
- auto joint = cpDampedSpringNew(_bodyA->getCPBody(),
- _bodyB->getCPBody(),
- PhysicsHelper::point2cpv(_anchr1),
- PhysicsHelper::point2cpv(_anchr2),
- _bodyB->local2World(_anchr1).getDistance(_bodyA->local2World(_anchr2)),
- _stiffness,
- _damping);
- CC_BREAK_IF(joint == nullptr);
- _cpConstraints.push_back(joint);
- return true;
- } while (false);
- return false;
- }
- Vec2 PhysicsJointSpring::getAnchr1() const
- {
- return PhysicsHelper::cpv2point(cpDampedSpringGetAnchorA(_cpConstraints.front()));
- }
- void PhysicsJointSpring::setAnchr1(const Vec2& anchr)
- {
- cpDampedSpringSetAnchorA(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
- }
- Vec2 PhysicsJointSpring::getAnchr2() const
- {
- return PhysicsHelper::cpv2point(cpDampedSpringGetAnchorB(_cpConstraints.front()));
- }
- void PhysicsJointSpring::setAnchr2(const Vec2& anchr)
- {
- cpDampedSpringSetAnchorB(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
- }
- float PhysicsJointSpring::getRestLength() const
- {
- return PhysicsHelper::cpfloat2float(cpDampedSpringGetRestLength(_cpConstraints.front()));
- }
- void PhysicsJointSpring::setRestLength(float restLength)
- {
- cpDampedSpringSetRestLength(_cpConstraints.front(), restLength);
- }
- float PhysicsJointSpring::getStiffness() const
- {
- return PhysicsHelper::cpfloat2float(cpDampedSpringGetStiffness(_cpConstraints.front()));
- }
- void PhysicsJointSpring::setStiffness(float stiffness)
- {
- cpDampedSpringSetStiffness(_cpConstraints.front(), stiffness);
- }
- float PhysicsJointSpring::getDamping() const
- {
- return PhysicsHelper::cpfloat2float(cpDampedSpringGetDamping(_cpConstraints.front()));
- }
- void PhysicsJointSpring::setDamping(float damping)
- {
- cpDampedSpringSetDamping(_cpConstraints.front(), damping);
- }
- PhysicsJointGroove* PhysicsJointGroove::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& grooveA, const Vec2& grooveB, const Vec2& anchr2)
- {
- auto joint = new (std::nothrow) PhysicsJointGroove();
- if (joint && joint->init(a, b))
- {
- joint->_grooveA = grooveA;
- joint->_grooveB = grooveB;
- joint->_anchr2 = anchr2;
- return joint;
- }
- CC_SAFE_DELETE(joint);
- return nullptr;
- }
- bool PhysicsJointGroove::createConstraints()
- {
- do {
- auto joint = cpGrooveJointNew(_bodyA->getCPBody(),
- _bodyB->getCPBody(),
- PhysicsHelper::point2cpv(_grooveA),
- PhysicsHelper::point2cpv(_grooveB),
- PhysicsHelper::point2cpv(_anchr2));
- CC_BREAK_IF(joint == nullptr);
- _cpConstraints.push_back(joint);
- return true;
- } while (false);
- return false;
- }
- Vec2 PhysicsJointGroove::getGrooveA() const
- {
- return PhysicsHelper::cpv2point(cpGrooveJointGetGrooveA(_cpConstraints.front()));
- }
- void PhysicsJointGroove::setGrooveA(const Vec2& grooveA)
- {
- cpGrooveJointSetGrooveA(_cpConstraints.front(), PhysicsHelper::point2cpv(grooveA));
- }
- Vec2 PhysicsJointGroove::getGrooveB() const
- {
- return PhysicsHelper::cpv2point(cpGrooveJointGetGrooveB(_cpConstraints.front()));
- }
- void PhysicsJointGroove::setGrooveB(const Vec2& grooveB)
- {
- cpGrooveJointSetGrooveB(_cpConstraints.front(), PhysicsHelper::point2cpv(grooveB));
- }
- Vec2 PhysicsJointGroove::getAnchr2() const
- {
- return PhysicsHelper::cpv2point(cpGrooveJointGetAnchorB(_cpConstraints.front()));
- }
- void PhysicsJointGroove::setAnchr2(const Vec2& anchr2)
- {
- cpGrooveJointSetAnchorB(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr2));
- }
- PhysicsJointRotarySpring* PhysicsJointRotarySpring::construct(PhysicsBody* a, PhysicsBody* b, float stiffness, float damping)
- {
- auto joint = new (std::nothrow) PhysicsJointRotarySpring();
- if (joint && joint->init(a, b))
- {
- joint->_stiffness = stiffness;
- joint->_damping = damping;
- return joint;
- }
- CC_SAFE_DELETE(joint);
- return nullptr;
- }
- bool PhysicsJointRotarySpring::createConstraints()
- {
- do {
- auto joint = cpDampedRotarySpringNew(_bodyA->getCPBody(),
- _bodyB->getCPBody(),
- _bodyB->getRotation() - _bodyA->getRotation(),
- _stiffness,
- _damping);
- CC_BREAK_IF(joint == nullptr);
- _cpConstraints.push_back(joint);
- return true;
- } while (false);
- return false;
- }
- float PhysicsJointRotarySpring::getRestAngle() const
- {
- return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetRestAngle(_cpConstraints.front()));
- }
- void PhysicsJointRotarySpring::setRestAngle(float restAngle)
- {
- cpDampedRotarySpringSetRestAngle(_cpConstraints.front(), restAngle);
- }
- float PhysicsJointRotarySpring::getStiffness() const
- {
- return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetStiffness(_cpConstraints.front()));
- }
- void PhysicsJointRotarySpring::setStiffness(float stiffness)
- {
- cpDampedRotarySpringSetStiffness(_cpConstraints.front(), stiffness);
- }
- float PhysicsJointRotarySpring::getDamping() const
- {
- return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetDamping(_cpConstraints.front()));
- }
- void PhysicsJointRotarySpring::setDamping(float damping)
- {
- cpDampedRotarySpringSetDamping(_cpConstraints.front(), damping);
- }
- PhysicsJointRotaryLimit* PhysicsJointRotaryLimit::construct(PhysicsBody* a, PhysicsBody* b, float min, float max)
- {
- auto joint = new (std::nothrow) PhysicsJointRotaryLimit();
- if (joint && joint->init(a, b))
- {
- joint->_min = min;
- joint->_max = max;
- return joint;
- }
- CC_SAFE_DELETE(joint);
- return nullptr;
- }
- PhysicsJointRotaryLimit* PhysicsJointRotaryLimit::construct(PhysicsBody* a, PhysicsBody* b)
- {
- return construct(a, b, 0.0f, 0.0f);
- }
- bool PhysicsJointRotaryLimit::createConstraints()
- {
- do
- {
- auto joint = cpRotaryLimitJointNew(_bodyA->getCPBody(),
- _bodyB->getCPBody(),
- _min,
- _max);
- CC_BREAK_IF(joint == nullptr);
- _cpConstraints.push_back(joint);
- return true;
- } while (false);
- return false;
- }
- float PhysicsJointRotaryLimit::getMin() const
- {
- return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMin(_cpConstraints.front()));
- }
- void PhysicsJointRotaryLimit::setMin(float min)
- {
- cpRotaryLimitJointSetMin(_cpConstraints.front(), min);
- }
- float PhysicsJointRotaryLimit::getMax() const
- {
- return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMax(_cpConstraints.front()));
- }
- void PhysicsJointRotaryLimit::setMax(float max)
- {
- cpRotaryLimitJointSetMax(_cpConstraints.front(), max);
- }
- PhysicsJointRatchet* PhysicsJointRatchet::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet)
- {
- auto joint = new (std::nothrow) PhysicsJointRatchet();
- if (joint && joint->init(a, b))
- {
- joint->_phase = phase;
- joint->_ratchet = ratchet;
- return joint;
- }
- CC_SAFE_DELETE(joint);
- return nullptr;
- }
- bool PhysicsJointRatchet::createConstraints()
- {
- do
- {
- auto joint = cpRatchetJointNew(_bodyA->getCPBody(),
- _bodyB->getCPBody(),
- _phase,
- PhysicsHelper::cpfloat2float(_ratchet));
- CC_BREAK_IF(joint == nullptr);
- _cpConstraints.push_back(joint);
- return true;
- } while (false);
- return false;
- }
- float PhysicsJointRatchet::getAngle() const
- {
- return PhysicsHelper::cpfloat2float(cpRatchetJointGetAngle(_cpConstraints.front()));
- }
- void PhysicsJointRatchet::setAngle(float angle)
- {
- cpRatchetJointSetAngle(_cpConstraints.front(), angle);
- }
- float PhysicsJointRatchet::getPhase() const
- {
- return PhysicsHelper::cpfloat2float(cpRatchetJointGetPhase(_cpConstraints.front()));
- }
- void PhysicsJointRatchet::setPhase(float phase)
- {
- cpRatchetJointSetPhase(_cpConstraints.front(), phase);
- }
- float PhysicsJointRatchet::getRatchet() const
- {
- return PhysicsHelper::cpfloat2float(cpRatchetJointGetRatchet(_cpConstraints.front()));
- }
- void PhysicsJointRatchet::setRatchet(float ratchet)
- {
- cpRatchetJointSetRatchet(_cpConstraints.front(), ratchet);
- }
- PhysicsJointGear* PhysicsJointGear::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratio)
- {
- auto joint = new (std::nothrow) PhysicsJointGear();
- if (joint && joint->init(a, b))
- {
- joint->_phase = phase;
- joint->_ratio = ratio;
- return joint;
- }
- CC_SAFE_DELETE(joint);
- return nullptr;
- }
- bool PhysicsJointGear::createConstraints()
- {
- do
- {
- auto joint = cpGearJointNew(_bodyA->getCPBody(),
- _bodyB->getCPBody(),
- _phase,
- _ratio);
- CC_BREAK_IF(joint == nullptr);
- _cpConstraints.push_back(joint);
- return true;
- } while (false);
- return false;
- }
- float PhysicsJointGear::getPhase() const
- {
- return PhysicsHelper::cpfloat2float(cpGearJointGetPhase(_cpConstraints.front()));
- }
- void PhysicsJointGear::setPhase(float phase)
- {
- cpGearJointSetPhase(_cpConstraints.front(), phase);
- }
- float PhysicsJointGear::getRatio() const
- {
- return PhysicsHelper::cpfloat2float(cpGearJointGetRatio(_cpConstraints.front()));
- }
- void PhysicsJointGear::setRatio(float ratio)
- {
- cpGearJointSetRatio(_cpConstraints.front(), ratio);
- }
- PhysicsJointMotor* PhysicsJointMotor::construct(PhysicsBody* a, PhysicsBody* b, float rate)
- {
- auto joint = new (std::nothrow) PhysicsJointMotor();
- if (joint && joint->init(a, b))
- {
- joint->_rate = rate;
- return joint;
- }
- CC_SAFE_DELETE(joint);
- return nullptr;
- }
- bool PhysicsJointMotor::createConstraints()
- {
- do
- {
- auto joint = cpSimpleMotorNew(_bodyA->getCPBody(),
- _bodyB->getCPBody(),
- _rate);
- CC_BREAK_IF(joint == nullptr);
- _cpConstraints.push_back(joint);
- return true;
- } while (false);
- return false;
- }
- float PhysicsJointMotor::getRate() const
- {
- return PhysicsHelper::cpfloat2float(cpSimpleMotorGetRate(_cpConstraints.front()));
- }
- void PhysicsJointMotor::setRate(float rate)
- {
- cpSimpleMotorSetRate(_cpConstraints.front(), rate);
- }
- NS_CC_END
- #endif // CC_USE_PHYSICS
|