CCPhysicsJoint.cpp 19 KB

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