CCPhysicsJoint.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "physics/CCPhysicsJoint.h"
  22. #if CC_USE_PHYSICS
  23. #include "chipmunk/chipmunk.h"
  24. #include "physics/CCPhysicsBody.h"
  25. #include "physics/CCPhysicsWorld.h"
  26. #include "physics/CCPhysicsHelper.h"
  27. #include "2d/CCNode.h"
  28. NS_CC_BEGIN
  29. PhysicsJoint::PhysicsJoint()
  30. : _bodyA(nullptr)
  31. , _bodyB(nullptr)
  32. , _world(nullptr)
  33. , _enable(false)
  34. , _collisionEnable(true)
  35. , _destroyMark(false)
  36. , _tag(0)
  37. , _maxForce(PHYSICS_INFINITY)
  38. , _initDirty(true)
  39. {
  40. }
  41. PhysicsJoint::~PhysicsJoint()
  42. {
  43. // reset the shapes collision group
  44. setCollisionEnable(true);
  45. for (cpConstraint* joint : _cpConstraints)
  46. {
  47. cpConstraintFree(joint);
  48. }
  49. _cpConstraints.clear();
  50. }
  51. bool PhysicsJoint::init(cocos2d::PhysicsBody *a, cocos2d::PhysicsBody *b)
  52. {
  53. do
  54. {
  55. CCASSERT(a != nullptr && b != nullptr, "the body passed in is nil");
  56. CCASSERT(a != b, "the two bodies are equal");
  57. _bodyA = a;
  58. _bodyB = b;
  59. _bodyA->_joints.push_back(this);
  60. _bodyB->_joints.push_back(this);
  61. return true;
  62. } while (false);
  63. return false;
  64. }
  65. bool PhysicsJoint::initJoint()
  66. {
  67. bool ret = !_initDirty;
  68. while (_initDirty)
  69. {
  70. ret = createConstraints();
  71. CC_BREAK_IF(!ret);
  72. for (auto subjoint : _cpConstraints)
  73. {
  74. cpConstraintSetMaxForce(subjoint, _maxForce);
  75. cpConstraintSetErrorBias(subjoint, cpfpow(1.0f - 0.15f, 60.0f));
  76. cpSpaceAddConstraint(_world->_cpSpace, subjoint);
  77. }
  78. _initDirty = false;
  79. ret = true;
  80. }
  81. return ret;
  82. }
  83. void PhysicsJoint::setEnable(bool enable)
  84. {
  85. if (_enable != enable)
  86. {
  87. _enable = enable;
  88. if (_world)
  89. {
  90. if (enable)
  91. {
  92. _world->addJoint(this);
  93. }
  94. else
  95. {
  96. _world->removeJoint(this, false);
  97. }
  98. }
  99. }
  100. }
  101. void PhysicsJoint::setCollisionEnable(bool enable)
  102. {
  103. if (_collisionEnable != enable)
  104. {
  105. _collisionEnable = enable;
  106. }
  107. }
  108. void PhysicsJoint::removeFormWorld()
  109. {
  110. if (_world)
  111. {
  112. _world->removeJoint(this, false);
  113. }
  114. }
  115. void PhysicsJoint::setMaxForce(float force)
  116. {
  117. _maxForce = force;
  118. for (auto joint : _cpConstraints)
  119. {
  120. cpConstraintSetMaxForce(joint, force);
  121. }
  122. }
  123. PhysicsJointFixed* PhysicsJointFixed::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr)
  124. {
  125. auto joint = new (std::nothrow) PhysicsJointFixed();
  126. if (joint && joint->init(a, b))
  127. {
  128. joint->_anchr = anchr;
  129. return joint;
  130. }
  131. CC_SAFE_DELETE(joint);
  132. return nullptr;
  133. }
  134. bool PhysicsJointFixed::createConstraints()
  135. {
  136. do
  137. {
  138. _bodyA->getNode()->setPosition(_anchr);
  139. _bodyB->getNode()->setPosition(_anchr);
  140. // add a pivot joint to fixed two body together
  141. auto joint = cpPivotJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(),
  142. PhysicsHelper::point2cpv(_anchr));
  143. CC_BREAK_IF(joint == nullptr);
  144. _cpConstraints.push_back(joint);
  145. // add a gear joint to make two body have the same rotation.
  146. joint = cpGearJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), 0, 1);
  147. CC_BREAK_IF(joint == nullptr);
  148. _cpConstraints.push_back(joint);
  149. _collisionEnable = false;
  150. return true;
  151. } while (false);
  152. return false;
  153. }
  154. PhysicsJointPin* PhysicsJointPin::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& pivot)
  155. {
  156. auto joint = new (std::nothrow) PhysicsJointPin();
  157. if (joint && joint->init(a, b))
  158. {
  159. joint->_anchr1 = pivot;
  160. joint->_useSpecificAnchr = false;
  161. return joint;
  162. }
  163. CC_SAFE_DELETE(joint);
  164. return nullptr;
  165. }
  166. PhysicsJointPin* PhysicsJointPin::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2)
  167. {
  168. auto joint = new (std::nothrow) PhysicsJointPin();
  169. if (joint && joint->init(a, b))
  170. {
  171. joint->_anchr1 = anchr1;
  172. joint->_anchr2 = anchr2;
  173. joint->_useSpecificAnchr = true;
  174. return joint;
  175. }
  176. CC_SAFE_DELETE(joint);
  177. return nullptr;
  178. }
  179. bool PhysicsJointPin::createConstraints()
  180. {
  181. do
  182. {
  183. cpConstraint* joint = nullptr;
  184. if (_useSpecificAnchr)
  185. {
  186. joint = cpPivotJointNew2(_bodyA->getCPBody(), _bodyB->getCPBody(),
  187. PhysicsHelper::point2cpv(_anchr1), PhysicsHelper::point2cpv(_anchr2));
  188. }
  189. else
  190. {
  191. joint = cpPivotJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(),
  192. PhysicsHelper::point2cpv(_anchr1));
  193. }
  194. CC_BREAK_IF(joint == nullptr);
  195. _cpConstraints.push_back(joint);
  196. return true;
  197. } while (false);
  198. return false;
  199. }
  200. PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float min, float max)
  201. {
  202. auto joint = new (std::nothrow) PhysicsJointLimit();
  203. if (joint && joint->init(a, b))
  204. {
  205. joint->_anchr1 = anchr1;
  206. joint->_anchr2 = anchr2;
  207. joint->_min = min;
  208. joint->_max = max;
  209. return joint;
  210. }
  211. CC_SAFE_DELETE(joint);
  212. return nullptr;
  213. }
  214. PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2)
  215. {
  216. return construct(a, b, anchr1, anchr2, 0, b->local2World(anchr1).getDistance(a->local2World(anchr2)));
  217. }
  218. bool PhysicsJointLimit::createConstraints()
  219. {
  220. do
  221. {
  222. auto joint = cpSlideJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(),
  223. PhysicsHelper::point2cpv(_anchr1),
  224. PhysicsHelper::point2cpv(_anchr2),
  225. _min,
  226. _max);
  227. CC_BREAK_IF(joint == nullptr);
  228. _cpConstraints.push_back(joint);
  229. return true;
  230. } while (false);
  231. return false;
  232. }
  233. float PhysicsJointLimit::getMin() const
  234. {
  235. return PhysicsHelper::cpfloat2float(cpSlideJointGetMin(_cpConstraints.front()));
  236. }
  237. void PhysicsJointLimit::setMin(float min)
  238. {
  239. cpSlideJointSetMin(_cpConstraints.front(), min);
  240. }
  241. float PhysicsJointLimit::getMax() const
  242. {
  243. return PhysicsHelper::cpfloat2float(cpSlideJointGetMax(_cpConstraints.front()));
  244. }
  245. void PhysicsJointLimit::setMax(float max)
  246. {
  247. cpSlideJointSetMax(_cpConstraints.front(), max);
  248. }
  249. Vec2 PhysicsJointLimit::getAnchr1() const
  250. {
  251. return PhysicsHelper::cpv2point(cpSlideJointGetAnchorA(_cpConstraints.front()));
  252. }
  253. void PhysicsJointLimit::setAnchr1(const Vec2& anchr)
  254. {
  255. cpSlideJointSetAnchorA(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
  256. }
  257. Vec2 PhysicsJointLimit::getAnchr2() const
  258. {
  259. return PhysicsHelper::cpv2point(cpSlideJointGetAnchorB(_cpConstraints.front()));
  260. }
  261. void PhysicsJointLimit::setAnchr2(const Vec2& anchr)
  262. {
  263. cpSlideJointSetAnchorB(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
  264. }
  265. PhysicsJointDistance* PhysicsJointDistance::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2)
  266. {
  267. auto joint = new (std::nothrow) PhysicsJointDistance();
  268. if (joint && joint->init(a, b))
  269. {
  270. joint->_anchr1 = anchr1;
  271. joint->_anchr2 = anchr2;
  272. return joint;
  273. }
  274. CC_SAFE_DELETE(joint);
  275. return nullptr;
  276. }
  277. bool PhysicsJointDistance::createConstraints()
  278. {
  279. do
  280. {
  281. auto joint = cpPinJointNew(_bodyA->getCPBody(),
  282. _bodyB->getCPBody(),
  283. PhysicsHelper::point2cpv(_anchr1),
  284. PhysicsHelper::point2cpv(_anchr2));
  285. CC_BREAK_IF(joint == nullptr);
  286. _cpConstraints.push_back(joint);
  287. return true;
  288. } while (false);
  289. return false;
  290. }
  291. float PhysicsJointDistance::getDistance() const
  292. {
  293. return PhysicsHelper::cpfloat2float(cpPinJointGetDist(_cpConstraints.front()));
  294. }
  295. void PhysicsJointDistance::setDistance(float distance)
  296. {
  297. cpPinJointSetDist(_cpConstraints.front(), distance);
  298. }
  299. PhysicsJointSpring* PhysicsJointSpring::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float stiffness, float damping)
  300. {
  301. auto joint = new (std::nothrow) PhysicsJointSpring();
  302. if (joint && joint->init(a, b))
  303. {
  304. joint->_anchr1 = anchr1;
  305. joint->_anchr2 = anchr2;
  306. joint->_stiffness = stiffness;
  307. joint->_damping = damping;
  308. return joint;
  309. }
  310. CC_SAFE_DELETE(joint);
  311. return nullptr;
  312. }
  313. bool PhysicsJointSpring::createConstraints()
  314. {
  315. do {
  316. auto joint = cpDampedSpringNew(_bodyA->getCPBody(),
  317. _bodyB->getCPBody(),
  318. PhysicsHelper::point2cpv(_anchr1),
  319. PhysicsHelper::point2cpv(_anchr2),
  320. _bodyB->local2World(_anchr1).getDistance(_bodyA->local2World(_anchr2)),
  321. _stiffness,
  322. _damping);
  323. CC_BREAK_IF(joint == nullptr);
  324. _cpConstraints.push_back(joint);
  325. return true;
  326. } while (false);
  327. return false;
  328. }
  329. Vec2 PhysicsJointSpring::getAnchr1() const
  330. {
  331. return PhysicsHelper::cpv2point(cpDampedSpringGetAnchorA(_cpConstraints.front()));
  332. }
  333. void PhysicsJointSpring::setAnchr1(const Vec2& anchr)
  334. {
  335. cpDampedSpringSetAnchorA(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
  336. }
  337. Vec2 PhysicsJointSpring::getAnchr2() const
  338. {
  339. return PhysicsHelper::cpv2point(cpDampedSpringGetAnchorB(_cpConstraints.front()));
  340. }
  341. void PhysicsJointSpring::setAnchr2(const Vec2& anchr)
  342. {
  343. cpDampedSpringSetAnchorB(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
  344. }
  345. float PhysicsJointSpring::getRestLength() const
  346. {
  347. return PhysicsHelper::cpfloat2float(cpDampedSpringGetRestLength(_cpConstraints.front()));
  348. }
  349. void PhysicsJointSpring::setRestLength(float restLength)
  350. {
  351. cpDampedSpringSetRestLength(_cpConstraints.front(), restLength);
  352. }
  353. float PhysicsJointSpring::getStiffness() const
  354. {
  355. return PhysicsHelper::cpfloat2float(cpDampedSpringGetStiffness(_cpConstraints.front()));
  356. }
  357. void PhysicsJointSpring::setStiffness(float stiffness)
  358. {
  359. cpDampedSpringSetStiffness(_cpConstraints.front(), stiffness);
  360. }
  361. float PhysicsJointSpring::getDamping() const
  362. {
  363. return PhysicsHelper::cpfloat2float(cpDampedSpringGetDamping(_cpConstraints.front()));
  364. }
  365. void PhysicsJointSpring::setDamping(float damping)
  366. {
  367. cpDampedSpringSetDamping(_cpConstraints.front(), damping);
  368. }
  369. PhysicsJointGroove* PhysicsJointGroove::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& grooveA, const Vec2& grooveB, const Vec2& anchr2)
  370. {
  371. auto joint = new (std::nothrow) PhysicsJointGroove();
  372. if (joint && joint->init(a, b))
  373. {
  374. joint->_grooveA = grooveA;
  375. joint->_grooveB = grooveB;
  376. joint->_anchr2 = anchr2;
  377. return joint;
  378. }
  379. CC_SAFE_DELETE(joint);
  380. return nullptr;
  381. }
  382. bool PhysicsJointGroove::createConstraints()
  383. {
  384. do {
  385. auto joint = cpGrooveJointNew(_bodyA->getCPBody(),
  386. _bodyB->getCPBody(),
  387. PhysicsHelper::point2cpv(_grooveA),
  388. PhysicsHelper::point2cpv(_grooveB),
  389. PhysicsHelper::point2cpv(_anchr2));
  390. CC_BREAK_IF(joint == nullptr);
  391. _cpConstraints.push_back(joint);
  392. return true;
  393. } while (false);
  394. return false;
  395. }
  396. Vec2 PhysicsJointGroove::getGrooveA() const
  397. {
  398. return PhysicsHelper::cpv2point(cpGrooveJointGetGrooveA(_cpConstraints.front()));
  399. }
  400. void PhysicsJointGroove::setGrooveA(const Vec2& grooveA)
  401. {
  402. cpGrooveJointSetGrooveA(_cpConstraints.front(), PhysicsHelper::point2cpv(grooveA));
  403. }
  404. Vec2 PhysicsJointGroove::getGrooveB() const
  405. {
  406. return PhysicsHelper::cpv2point(cpGrooveJointGetGrooveB(_cpConstraints.front()));
  407. }
  408. void PhysicsJointGroove::setGrooveB(const Vec2& grooveB)
  409. {
  410. cpGrooveJointSetGrooveB(_cpConstraints.front(), PhysicsHelper::point2cpv(grooveB));
  411. }
  412. Vec2 PhysicsJointGroove::getAnchr2() const
  413. {
  414. return PhysicsHelper::cpv2point(cpGrooveJointGetAnchorB(_cpConstraints.front()));
  415. }
  416. void PhysicsJointGroove::setAnchr2(const Vec2& anchr2)
  417. {
  418. cpGrooveJointSetAnchorB(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr2));
  419. }
  420. PhysicsJointRotarySpring* PhysicsJointRotarySpring::construct(PhysicsBody* a, PhysicsBody* b, float stiffness, float damping)
  421. {
  422. auto joint = new (std::nothrow) PhysicsJointRotarySpring();
  423. if (joint && joint->init(a, b))
  424. {
  425. joint->_stiffness = stiffness;
  426. joint->_damping = damping;
  427. return joint;
  428. }
  429. CC_SAFE_DELETE(joint);
  430. return nullptr;
  431. }
  432. bool PhysicsJointRotarySpring::createConstraints()
  433. {
  434. do {
  435. auto joint = cpDampedRotarySpringNew(_bodyA->getCPBody(),
  436. _bodyB->getCPBody(),
  437. _bodyB->getRotation() - _bodyA->getRotation(),
  438. _stiffness,
  439. _damping);
  440. CC_BREAK_IF(joint == nullptr);
  441. _cpConstraints.push_back(joint);
  442. return true;
  443. } while (false);
  444. return false;
  445. }
  446. float PhysicsJointRotarySpring::getRestAngle() const
  447. {
  448. return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetRestAngle(_cpConstraints.front()));
  449. }
  450. void PhysicsJointRotarySpring::setRestAngle(float restAngle)
  451. {
  452. cpDampedRotarySpringSetRestAngle(_cpConstraints.front(), restAngle);
  453. }
  454. float PhysicsJointRotarySpring::getStiffness() const
  455. {
  456. return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetStiffness(_cpConstraints.front()));
  457. }
  458. void PhysicsJointRotarySpring::setStiffness(float stiffness)
  459. {
  460. cpDampedRotarySpringSetStiffness(_cpConstraints.front(), stiffness);
  461. }
  462. float PhysicsJointRotarySpring::getDamping() const
  463. {
  464. return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetDamping(_cpConstraints.front()));
  465. }
  466. void PhysicsJointRotarySpring::setDamping(float damping)
  467. {
  468. cpDampedRotarySpringSetDamping(_cpConstraints.front(), damping);
  469. }
  470. PhysicsJointRotaryLimit* PhysicsJointRotaryLimit::construct(PhysicsBody* a, PhysicsBody* b, float min, float max)
  471. {
  472. auto joint = new (std::nothrow) PhysicsJointRotaryLimit();
  473. if (joint && joint->init(a, b))
  474. {
  475. joint->_min = min;
  476. joint->_max = max;
  477. return joint;
  478. }
  479. CC_SAFE_DELETE(joint);
  480. return nullptr;
  481. }
  482. PhysicsJointRotaryLimit* PhysicsJointRotaryLimit::construct(PhysicsBody* a, PhysicsBody* b)
  483. {
  484. return construct(a, b, 0.0f, 0.0f);
  485. }
  486. bool PhysicsJointRotaryLimit::createConstraints()
  487. {
  488. do
  489. {
  490. auto joint = cpRotaryLimitJointNew(_bodyA->getCPBody(),
  491. _bodyB->getCPBody(),
  492. _min,
  493. _max);
  494. CC_BREAK_IF(joint == nullptr);
  495. _cpConstraints.push_back(joint);
  496. return true;
  497. } while (false);
  498. return false;
  499. }
  500. float PhysicsJointRotaryLimit::getMin() const
  501. {
  502. return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMin(_cpConstraints.front()));
  503. }
  504. void PhysicsJointRotaryLimit::setMin(float min)
  505. {
  506. cpRotaryLimitJointSetMin(_cpConstraints.front(), min);
  507. }
  508. float PhysicsJointRotaryLimit::getMax() const
  509. {
  510. return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMax(_cpConstraints.front()));
  511. }
  512. void PhysicsJointRotaryLimit::setMax(float max)
  513. {
  514. cpRotaryLimitJointSetMax(_cpConstraints.front(), max);
  515. }
  516. PhysicsJointRatchet* PhysicsJointRatchet::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet)
  517. {
  518. auto joint = new (std::nothrow) PhysicsJointRatchet();
  519. if (joint && joint->init(a, b))
  520. {
  521. joint->_phase = phase;
  522. joint->_ratchet = ratchet;
  523. return joint;
  524. }
  525. CC_SAFE_DELETE(joint);
  526. return nullptr;
  527. }
  528. bool PhysicsJointRatchet::createConstraints()
  529. {
  530. do
  531. {
  532. auto joint = cpRatchetJointNew(_bodyA->getCPBody(),
  533. _bodyB->getCPBody(),
  534. _phase,
  535. PhysicsHelper::cpfloat2float(_ratchet));
  536. CC_BREAK_IF(joint == nullptr);
  537. _cpConstraints.push_back(joint);
  538. return true;
  539. } while (false);
  540. return false;
  541. }
  542. float PhysicsJointRatchet::getAngle() const
  543. {
  544. return PhysicsHelper::cpfloat2float(cpRatchetJointGetAngle(_cpConstraints.front()));
  545. }
  546. void PhysicsJointRatchet::setAngle(float angle)
  547. {
  548. cpRatchetJointSetAngle(_cpConstraints.front(), angle);
  549. }
  550. float PhysicsJointRatchet::getPhase() const
  551. {
  552. return PhysicsHelper::cpfloat2float(cpRatchetJointGetPhase(_cpConstraints.front()));
  553. }
  554. void PhysicsJointRatchet::setPhase(float phase)
  555. {
  556. cpRatchetJointSetPhase(_cpConstraints.front(), phase);
  557. }
  558. float PhysicsJointRatchet::getRatchet() const
  559. {
  560. return PhysicsHelper::cpfloat2float(cpRatchetJointGetRatchet(_cpConstraints.front()));
  561. }
  562. void PhysicsJointRatchet::setRatchet(float ratchet)
  563. {
  564. cpRatchetJointSetRatchet(_cpConstraints.front(), ratchet);
  565. }
  566. PhysicsJointGear* PhysicsJointGear::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratio)
  567. {
  568. auto joint = new (std::nothrow) PhysicsJointGear();
  569. if (joint && joint->init(a, b))
  570. {
  571. joint->_phase = phase;
  572. joint->_ratio = ratio;
  573. return joint;
  574. }
  575. CC_SAFE_DELETE(joint);
  576. return nullptr;
  577. }
  578. bool PhysicsJointGear::createConstraints()
  579. {
  580. do
  581. {
  582. auto joint = cpGearJointNew(_bodyA->getCPBody(),
  583. _bodyB->getCPBody(),
  584. _phase,
  585. _ratio);
  586. CC_BREAK_IF(joint == nullptr);
  587. _cpConstraints.push_back(joint);
  588. return true;
  589. } while (false);
  590. return false;
  591. }
  592. float PhysicsJointGear::getPhase() const
  593. {
  594. return PhysicsHelper::cpfloat2float(cpGearJointGetPhase(_cpConstraints.front()));
  595. }
  596. void PhysicsJointGear::setPhase(float phase)
  597. {
  598. cpGearJointSetPhase(_cpConstraints.front(), phase);
  599. }
  600. float PhysicsJointGear::getRatio() const
  601. {
  602. return PhysicsHelper::cpfloat2float(cpGearJointGetRatio(_cpConstraints.front()));
  603. }
  604. void PhysicsJointGear::setRatio(float ratio)
  605. {
  606. cpGearJointSetRatio(_cpConstraints.front(), ratio);
  607. }
  608. PhysicsJointMotor* PhysicsJointMotor::construct(PhysicsBody* a, PhysicsBody* b, float rate)
  609. {
  610. auto joint = new (std::nothrow) PhysicsJointMotor();
  611. if (joint && joint->init(a, b))
  612. {
  613. joint->_rate = rate;
  614. return joint;
  615. }
  616. CC_SAFE_DELETE(joint);
  617. return nullptr;
  618. }
  619. bool PhysicsJointMotor::createConstraints()
  620. {
  621. do
  622. {
  623. auto joint = cpSimpleMotorNew(_bodyA->getCPBody(),
  624. _bodyB->getCPBody(),
  625. _rate);
  626. CC_BREAK_IF(joint == nullptr);
  627. _cpConstraints.push_back(joint);
  628. return true;
  629. } while (false);
  630. return false;
  631. }
  632. float PhysicsJointMotor::getRate() const
  633. {
  634. return PhysicsHelper::cpfloat2float(cpSimpleMotorGetRate(_cpConstraints.front()));
  635. }
  636. void PhysicsJointMotor::setRate(float rate)
  637. {
  638. cpSimpleMotorSetRate(_cpConstraints.front(), rate);
  639. }
  640. NS_CC_END
  641. #endif // CC_USE_PHYSICS