CCPhysicsShape.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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/CCPhysicsShape.h"
  22. #if CC_USE_PHYSICS
  23. #include <climits>
  24. #include <cmath>
  25. #include <unordered_map>
  26. #include "chipmunk/chipmunk.h"
  27. #include "chipmunk/chipmunk_unsafe.h"
  28. #include "physics/CCPhysicsBody.h"
  29. #include "physics/CCPhysicsWorld.h"
  30. #include "physics/CCPhysicsHelper.h"
  31. NS_CC_BEGIN
  32. extern const float PHYSICS_INFINITY;
  33. static cpBody* s_sharedBody = nullptr;
  34. PhysicsShape::PhysicsShape()
  35. : _body(nullptr)
  36. , _type(Type::UNKNOWN)
  37. , _area(0.0f)
  38. , _mass(0.0f)
  39. , _moment(0.0f)
  40. , _sensor(false)
  41. , _scaleX(1.0f)
  42. , _scaleY(1.0f)
  43. , _newScaleX(1.0f)
  44. , _newScaleY(1.0f)
  45. , _tag(0)
  46. , _categoryBitmask(UINT_MAX)
  47. , _collisionBitmask(UINT_MAX)
  48. , _contactTestBitmask(0)
  49. , _group(0)
  50. {
  51. if (s_sharedBody == nullptr)
  52. {
  53. s_sharedBody = cpBodyNewStatic();
  54. }
  55. }
  56. PhysicsShape::~PhysicsShape()
  57. {
  58. for (auto shape : _cpShapes)
  59. {
  60. cpShapeFree(shape);
  61. }
  62. }
  63. void PhysicsShape::setMass(float mass)
  64. {
  65. if (mass < 0)
  66. {
  67. return;
  68. }
  69. if (_body)
  70. {
  71. _body->addMass(-_mass);
  72. _body->addMass(mass);
  73. };
  74. _mass = mass;
  75. }
  76. void PhysicsShape::setMoment(float moment)
  77. {
  78. if (moment < 0)
  79. {
  80. return;
  81. }
  82. if (_body)
  83. {
  84. _body->addMoment(-_moment);
  85. _body->addMoment(moment);
  86. };
  87. _moment = moment;
  88. }
  89. void PhysicsShape::setMaterial(const PhysicsMaterial& material)
  90. {
  91. setDensity(material.density);
  92. setRestitution(material.restitution);
  93. setFriction(material.friction);
  94. }
  95. void PhysicsShape::setScale(float scaleX, float scaleY)
  96. {
  97. if (std::abs(_scaleX - scaleX) > FLT_EPSILON || std::abs(_scaleY - scaleY) > FLT_EPSILON)
  98. {
  99. if (_type == Type::CIRCLE && scaleX != scaleY)
  100. {
  101. CCLOG("PhysicsShapeCircle WARNING: CANNOT support setScale with different x and y");
  102. return;
  103. }
  104. _newScaleX = scaleX;
  105. _newScaleY = scaleY;
  106. updateScale();
  107. // re-calculate area and mass
  108. _area = calculateArea();
  109. _mass = _material.density * _area;
  110. _moment = calculateDefaultMoment();
  111. }
  112. }
  113. void PhysicsShape::updateScale()
  114. {
  115. _scaleX = _newScaleX;
  116. _scaleY = _newScaleY;
  117. }
  118. void PhysicsShape::addShape(cpShape* shape)
  119. {
  120. if (shape)
  121. {
  122. cpShapeSetUserData(shape, this);
  123. cpShapeSetFilter(shape, cpShapeFilterNew(_group, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
  124. _cpShapes.push_back(shape);
  125. }
  126. }
  127. PhysicsShapeCircle::PhysicsShapeCircle()
  128. {
  129. }
  130. PhysicsShapeCircle::~PhysicsShapeCircle()
  131. {
  132. }
  133. PhysicsShapeBox::PhysicsShapeBox()
  134. {
  135. }
  136. PhysicsShapeBox::~PhysicsShapeBox()
  137. {
  138. }
  139. PhysicsShapePolygon::PhysicsShapePolygon()
  140. {
  141. }
  142. PhysicsShapePolygon::~PhysicsShapePolygon()
  143. {
  144. }
  145. PhysicsShapeEdgeBox::PhysicsShapeEdgeBox()
  146. {
  147. }
  148. PhysicsShapeEdgeBox::~PhysicsShapeEdgeBox()
  149. {
  150. }
  151. PhysicsShapeEdgeChain::PhysicsShapeEdgeChain()
  152. {
  153. }
  154. PhysicsShapeEdgeChain::~PhysicsShapeEdgeChain()
  155. {
  156. }
  157. PhysicsShapeEdgePolygon::PhysicsShapeEdgePolygon()
  158. {
  159. }
  160. PhysicsShapeEdgePolygon::~PhysicsShapeEdgePolygon()
  161. {
  162. }
  163. PhysicsShapeEdgeSegment::PhysicsShapeEdgeSegment()
  164. {
  165. }
  166. PhysicsShapeEdgeSegment::~PhysicsShapeEdgeSegment()
  167. {
  168. }
  169. void PhysicsShape::setDensity(float density)
  170. {
  171. if (density < 0)
  172. {
  173. return;
  174. }
  175. _material.density = density;
  176. if (_material.density == PHYSICS_INFINITY)
  177. {
  178. setMass(PHYSICS_INFINITY);
  179. }else if (_area > 0)
  180. {
  181. setMass(_material.density * _area);
  182. }
  183. }
  184. void PhysicsShape::setRestitution(float restitution)
  185. {
  186. _material.restitution = restitution;
  187. for (cpShape* shape : _cpShapes)
  188. {
  189. cpShapeSetElasticity(shape, restitution);
  190. }
  191. }
  192. void PhysicsShape::setFriction(float friction)
  193. {
  194. _material.friction = friction;
  195. for (cpShape* shape : _cpShapes)
  196. {
  197. cpShapeSetFriction(shape, friction);
  198. }
  199. }
  200. void PhysicsShape::setSensor(bool sensor)
  201. {
  202. if (sensor != _sensor)
  203. {
  204. for (cpShape* shape : _cpShapes)
  205. {
  206. cpShapeSetSensor(shape, sensor);
  207. }
  208. _sensor = sensor;
  209. }
  210. }
  211. void PhysicsShape::recenterPoints(Vec2* points, int count, const Vec2& center)
  212. {
  213. cpVect* cpvs = new cpVect[count];
  214. cpVect centroid = cpCentroidForPoly(count, cpvs);
  215. for(int i=0; i<count; i++){
  216. cpvs[i] = cpvsub(cpvs[i], centroid);
  217. }
  218. PhysicsHelper::cpvs2points(cpvs, points, count);
  219. delete[] cpvs;
  220. if (center != Vec2::ZERO)
  221. {
  222. for (int i = 0; i < count; ++i)
  223. {
  224. points[i] += center;
  225. }
  226. }
  227. }
  228. Vec2 PhysicsShape::getPolygonCenter(const Vec2* points, int count)
  229. {
  230. cpVect* cpvs = new (std::nothrow) cpVect[count];
  231. cpVect center = cpCentroidForPoly(count, PhysicsHelper::points2cpvs(points, cpvs, count));
  232. delete[] cpvs;
  233. return PhysicsHelper::cpv2point(center);
  234. }
  235. Vec2 PhysicsShape::getPolyonCenter(const Vec2* points, int count)
  236. {
  237. return getPolygonCenter(points, count);
  238. }
  239. void PhysicsShape::setBody(PhysicsBody *body)
  240. {
  241. // already added
  242. if (body && _body == body)
  243. {
  244. return;
  245. }
  246. if (_body)
  247. {
  248. _body->removeShape(this);
  249. }
  250. for (auto shape : _cpShapes)
  251. {
  252. cpShapeSetBody(shape, body == nullptr ? s_sharedBody : body->_cpBody);
  253. }
  254. _body = body;
  255. }
  256. // PhysicsShapeCircle
  257. PhysicsShapeCircle* PhysicsShapeCircle::create(float radius, const PhysicsMaterial& material/* = MaterialDefault*/, const Vec2& offset/* = Vec2(0, 0)*/)
  258. {
  259. PhysicsShapeCircle* shape = new (std::nothrow) PhysicsShapeCircle();
  260. if (shape && shape->init(radius, material, offset))
  261. {
  262. shape->autorelease();
  263. return shape;
  264. }
  265. CC_SAFE_DELETE(shape);
  266. return nullptr;
  267. }
  268. bool PhysicsShapeCircle::init(float radius, const PhysicsMaterial& material/* = MaterialDefault*/, const Vec2& offset /*= Vec2(0, 0)*/)
  269. {
  270. do
  271. {
  272. _type = Type::CIRCLE;
  273. auto shape = cpCircleShapeNew(s_sharedBody, radius, PhysicsHelper::point2cpv(offset));
  274. CC_BREAK_IF(shape == nullptr);
  275. cpShapeSetUserData(shape, this);
  276. addShape(shape);
  277. _area = calculateArea();
  278. _mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
  279. _moment = calculateDefaultMoment();
  280. setMaterial(material);
  281. return true;
  282. } while (false);
  283. return false;
  284. }
  285. float PhysicsShapeCircle::calculateArea(float radius)
  286. {
  287. return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, radius));
  288. }
  289. float PhysicsShapeCircle::calculateMoment(float mass, float radius, const Vec2& offset)
  290. {
  291. return mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
  292. : PhysicsHelper::cpfloat2float(cpMomentForCircle(mass,
  293. 0,
  294. radius,
  295. PhysicsHelper::point2cpv(offset)));
  296. }
  297. float PhysicsShapeCircle::calculateArea()
  298. {
  299. return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, cpCircleShapeGetRadius(_cpShapes.front())));
  300. }
  301. float PhysicsShapeCircle::calculateDefaultMoment()
  302. {
  303. auto shape = _cpShapes.front();
  304. return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
  305. : PhysicsHelper::cpfloat2float(cpMomentForCircle(_mass,
  306. 0,
  307. cpCircleShapeGetRadius(shape),
  308. cpCircleShapeGetOffset(shape)));
  309. }
  310. float PhysicsShapeCircle::getRadius() const
  311. {
  312. return PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(_cpShapes.front()));
  313. }
  314. Vec2 PhysicsShapeCircle::getOffset()
  315. {
  316. return PhysicsHelper::cpv2point(cpCircleShapeGetOffset(_cpShapes.front()));
  317. }
  318. void PhysicsShapeCircle::updateScale()
  319. {
  320. cpFloat factor = std::abs(_newScaleX / _scaleX);
  321. cpShape* shape = _cpShapes.front();
  322. cpVect v = cpCircleShapeGetOffset(shape);
  323. v = cpvmult(v, factor);
  324. cpCircleShapeSetOffset(shape, v);
  325. cpCircleShapeSetRadius(shape, cpCircleShapeGetRadius(shape) * factor);
  326. PhysicsShape::updateScale();
  327. }
  328. // PhysicsShapeEdgeSegment
  329. PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(const Vec2& a, const Vec2& b, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/)
  330. {
  331. PhysicsShapeEdgeSegment* shape = new (std::nothrow) PhysicsShapeEdgeSegment();
  332. if (shape && shape->init(a, b, material, border))
  333. {
  334. shape->autorelease();
  335. return shape;
  336. }
  337. CC_SAFE_DELETE(shape);
  338. return nullptr;
  339. }
  340. bool PhysicsShapeEdgeSegment::init(const Vec2& a, const Vec2& b, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/)
  341. {
  342. do
  343. {
  344. _type = Type::EDGESEGMENT;
  345. auto shape = cpSegmentShapeNew(s_sharedBody,
  346. PhysicsHelper::point2cpv(a),
  347. PhysicsHelper::point2cpv(b),
  348. border);
  349. CC_BREAK_IF(shape == nullptr);
  350. cpShapeSetUserData(shape, this);
  351. addShape(shape);
  352. _mass = PHYSICS_INFINITY;
  353. _moment = PHYSICS_INFINITY;
  354. setMaterial(material);
  355. return true;
  356. } while (false);
  357. return false;
  358. }
  359. Vec2 PhysicsShapeEdgeSegment::getPointA() const
  360. {
  361. return PhysicsHelper::cpv2point(cpSegmentShapeGetA(_cpShapes.front()));
  362. }
  363. Vec2 PhysicsShapeEdgeSegment::getPointB() const
  364. {
  365. return PhysicsHelper::cpv2point(cpSegmentShapeGetB(_cpShapes.front()));
  366. }
  367. Vec2 PhysicsShapeEdgeSegment::getCenter()
  368. {
  369. auto a = PhysicsHelper::cpv2point(cpSegmentShapeGetA(_cpShapes.front()));
  370. auto b = PhysicsHelper::cpv2point(cpSegmentShapeGetB(_cpShapes.front()));
  371. return ( a + b ) / 2;
  372. }
  373. void PhysicsShapeEdgeSegment::updateScale()
  374. {
  375. cpFloat factorX = _newScaleX / _scaleX;
  376. cpFloat factorY = _newScaleY / _scaleY;
  377. cpShape* shape = _cpShapes.front();
  378. cpVect a = cpSegmentShapeGetA(shape);
  379. a.x *= factorX;
  380. a.y *= factorY;
  381. cpVect b = cpSegmentShapeGetB(shape);
  382. b.x *= factorX;
  383. b.y *= factorY;
  384. cpSegmentShapeSetEndpoints(shape, a, b);
  385. PhysicsShape::updateScale();
  386. }
  387. // PhysicsShapeBox
  388. PhysicsShapeBox* PhysicsShapeBox::create(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, const Vec2& offset/* = Vec2(0, 0)*/, float radius/* = 0.0f*/)
  389. {
  390. PhysicsShapeBox* shape = new (std::nothrow) PhysicsShapeBox();
  391. if (shape && shape->init(size, material, offset, radius))
  392. {
  393. shape->autorelease();
  394. return shape;
  395. }
  396. CC_SAFE_DELETE(shape);
  397. return nullptr;
  398. }
  399. bool PhysicsShapeBox::init(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, const Vec2& offset /*= Vec2(0, 0)*/, float radius/* = 0.0f*/)
  400. {
  401. do
  402. {
  403. _type = Type::BOX;
  404. auto wh = PhysicsHelper::size2cpv(size);
  405. cpVect vec[4] =
  406. {
  407. {-wh.x/2.0f, -wh.y/2.0f}, {-wh.x/2.0f, wh.y/2.0f}, {wh.x/2.0f, wh.y/2.0f}, {wh.x/2.0f, -wh.y/2.0f}
  408. };
  409. cpTransform transform = cpTransformTranslate(PhysicsHelper::point2cpv(offset));
  410. auto shape = cpPolyShapeNew(s_sharedBody, 4, vec, transform, radius);
  411. CC_BREAK_IF(shape == nullptr);
  412. cpShapeSetUserData(shape, this);
  413. addShape(shape);
  414. _area = calculateArea();
  415. _mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
  416. _moment = calculateDefaultMoment();
  417. setMaterial(material);
  418. return true;
  419. } while (false);
  420. return false;
  421. }
  422. Size PhysicsShapeBox::getSize() const
  423. {
  424. cpShape* shape = _cpShapes.front();
  425. return PhysicsHelper::cpv2size(cpv(cpvdist(cpPolyShapeGetVert(shape, 1), cpPolyShapeGetVert(shape, 2)),
  426. cpvdist(cpPolyShapeGetVert(shape, 0), cpPolyShapeGetVert(shape, 1))));
  427. }
  428. // PhysicsShapePolygon
  429. PhysicsShapePolygon* PhysicsShapePolygon::create(const Vec2* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, const Vec2& offset/* = Vec2(0, 0)*/, float radius/* = 0.0f*/)
  430. {
  431. PhysicsShapePolygon* shape = new (std::nothrow) PhysicsShapePolygon();
  432. if (shape && shape->init(points, count, material, offset, radius))
  433. {
  434. shape->autorelease();
  435. return shape;
  436. }
  437. CC_SAFE_DELETE(shape);
  438. return nullptr;
  439. }
  440. bool PhysicsShapePolygon::init(const Vec2* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, const Vec2& offset/* = Vec2(0, 0)*/, float radius/* = 0.0f*/)
  441. {
  442. do
  443. {
  444. _type = Type::POLYGON;
  445. auto vecs = new (std::nothrow) cpVect[count];
  446. PhysicsHelper::points2cpvs(points, vecs, count); //count = cpConvexHull((int)count, vecs, nullptr, nullptr, 0);
  447. cpTransform transform = cpTransformTranslate(PhysicsHelper::point2cpv(offset));
  448. auto shape = cpPolyShapeNew(s_sharedBody, count, vecs, transform, radius);
  449. CC_SAFE_DELETE_ARRAY(vecs);
  450. CC_BREAK_IF(shape == nullptr);
  451. cpShapeSetUserData(shape, this);
  452. addShape(shape);
  453. _area = calculateArea();
  454. _mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
  455. _moment = calculateDefaultMoment();
  456. setMaterial(material);
  457. return true;
  458. } while (false);
  459. return false;
  460. }
  461. float PhysicsShapePolygon::calculateArea(const Vec2* points, int count)
  462. {
  463. cpVect* vecs = new (std::nothrow) cpVect[count];
  464. PhysicsHelper::points2cpvs(points, vecs, count);
  465. float area = PhysicsHelper::cpfloat2float(cpAreaForPoly(count, vecs, 0.0f));
  466. CC_SAFE_DELETE_ARRAY(vecs);
  467. return area;
  468. }
  469. float PhysicsShapePolygon::calculateMoment(float mass, const Vec2* points, int count, const Vec2& offset, float radius)
  470. {
  471. cpVect* vecs = new (std::nothrow) cpVect[count];
  472. PhysicsHelper::points2cpvs(points, vecs, count);
  473. float moment = mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
  474. : PhysicsHelper::cpfloat2float(cpMomentForPoly(mass, count, vecs, PhysicsHelper::point2cpv(offset), radius));
  475. CC_SAFE_DELETE_ARRAY(vecs);
  476. return moment;
  477. }
  478. float PhysicsShapePolygon::calculateArea()
  479. {
  480. auto shape = _cpShapes.front();
  481. int count = cpPolyShapeGetCount(shape);
  482. cpVect* vecs = new cpVect[count];
  483. for(int i=0;i<count;++i)
  484. vecs[i] = cpPolyShapeGetVert(shape, i);
  485. float area = PhysicsHelper::cpfloat2float(cpAreaForPoly(count, vecs, cpPolyShapeGetRadius(shape)));
  486. CC_SAFE_DELETE_ARRAY(vecs);
  487. return area;
  488. }
  489. float PhysicsShapePolygon::calculateDefaultMoment()
  490. {
  491. if(_mass == PHYSICS_INFINITY)
  492. {
  493. return PHYSICS_INFINITY;
  494. }
  495. else
  496. {
  497. auto shape = _cpShapes.front();
  498. int count = cpPolyShapeGetCount(shape);
  499. cpVect* vecs = new cpVect[count];
  500. for(int i=0;i<count;++i)
  501. vecs[i] = cpPolyShapeGetVert(shape, i);
  502. float moment = PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, count, vecs, cpvzero, cpPolyShapeGetRadius(shape)));
  503. CC_SAFE_DELETE_ARRAY(vecs);
  504. return moment;
  505. }
  506. }
  507. Vec2 PhysicsShapePolygon::getPoint(int i) const
  508. {
  509. return PhysicsHelper::cpv2point(cpPolyShapeGetVert(_cpShapes.front(), i));
  510. }
  511. void PhysicsShapePolygon::getPoints(Vec2* outPoints) const
  512. {
  513. auto shape = _cpShapes.front();
  514. int count = cpPolyShapeGetCount(shape);
  515. cpVect* vecs = new cpVect[count];
  516. for(int i=0;i<count;++i)
  517. vecs[i] = cpPolyShapeGetVert(shape, i);
  518. PhysicsHelper::cpvs2points(vecs, outPoints, count);
  519. CC_SAFE_DELETE_ARRAY(vecs);
  520. }
  521. int PhysicsShapePolygon::getPointsCount() const
  522. {
  523. return cpPolyShapeGetCount(_cpShapes.front());
  524. }
  525. Vec2 PhysicsShapePolygon::getCenter()
  526. {
  527. auto shape = _cpShapes.front();
  528. int count = cpPolyShapeGetCount(shape);
  529. cpVect* vecs = new cpVect[count];
  530. for(int i=0;i<count;++i)
  531. vecs[i] = cpPolyShapeGetVert(shape, i);
  532. Vec2 center = PhysicsHelper::cpv2point(cpCentroidForPoly(count, vecs));
  533. CC_SAFE_DELETE_ARRAY(vecs);
  534. return center;
  535. }
  536. void PhysicsShapePolygon::updateScale()
  537. {
  538. cpFloat factorX = _newScaleX / _scaleX;
  539. cpFloat factorY = _newScaleY / _scaleY;
  540. auto shape = _cpShapes.front();
  541. int count = cpPolyShapeGetCount(shape);
  542. cpVect* vects = new cpVect[count];
  543. for(int i=0;i<count;++i)
  544. vects[i] = cpPolyShapeGetVert(shape, i);
  545. for (int i = 0; i < count; ++i)
  546. {
  547. vects[i].x *= factorX;
  548. vects[i].y *= factorY;
  549. }
  550. // convert hole to clockwise
  551. if (factorX * factorY < 0)
  552. {
  553. for (int i = 0; i < count / 2; ++i)
  554. {
  555. cpVect v = vects[i];
  556. vects[i] = vects[count - i - 1];
  557. vects[count - i - 1] = v;
  558. }
  559. }
  560. cpPolyShapeSetVertsRaw(shape, count, vects);
  561. CC_SAFE_DELETE_ARRAY(vects);
  562. PhysicsShape::updateScale();
  563. }
  564. // PhysicsShapeEdgeBox
  565. PhysicsShapeEdgeBox* PhysicsShapeEdgeBox::create(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/, const Vec2& offset/* = Vec2(0, 0)*/)
  566. {
  567. PhysicsShapeEdgeBox* shape = new (std::nothrow) PhysicsShapeEdgeBox();
  568. if (shape && shape->init(size, material, border, offset))
  569. {
  570. shape->autorelease();
  571. return shape;
  572. }
  573. CC_SAFE_DELETE(shape);
  574. return nullptr;
  575. }
  576. bool PhysicsShapeEdgeBox::init(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/, const Vec2& offset/*= Vec2(0, 0)*/)
  577. {
  578. do
  579. {
  580. _type = Type::EDGEBOX;
  581. cpVect vec[4] = {};
  582. vec[0] = PhysicsHelper::point2cpv(Vec2(-size.width/2+offset.x, -size.height/2+offset.y));
  583. vec[1] = PhysicsHelper::point2cpv(Vec2(+size.width/2+offset.x, -size.height/2+offset.y));
  584. vec[2] = PhysicsHelper::point2cpv(Vec2(+size.width/2+offset.x, +size.height/2+offset.y));
  585. vec[3] = PhysicsHelper::point2cpv(Vec2(-size.width/2+offset.x, +size.height/2+offset.y));
  586. int i = 0;
  587. for (; i < 4; ++i)
  588. {
  589. auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[(i + 1) % 4], border);
  590. CC_BREAK_IF(shape == nullptr);
  591. cpShapeSetUserData(shape, this);
  592. addShape(shape);
  593. }
  594. CC_BREAK_IF(i < 4);
  595. _mass = PHYSICS_INFINITY;
  596. _moment = PHYSICS_INFINITY;
  597. setMaterial(material);
  598. return true;
  599. } while (false);
  600. return false;
  601. }
  602. // PhysicsShapeEdgeBox
  603. PhysicsShapeEdgePolygon* PhysicsShapeEdgePolygon::create(const Vec2* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/)
  604. {
  605. PhysicsShapeEdgePolygon* shape = new (std::nothrow) PhysicsShapeEdgePolygon();
  606. if (shape && shape->init(points, count, material, border))
  607. {
  608. shape->autorelease();
  609. return shape;
  610. }
  611. CC_SAFE_DELETE(shape);
  612. return nullptr;
  613. }
  614. bool PhysicsShapeEdgePolygon::init(const Vec2* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/)
  615. {
  616. cpVect* vec = nullptr;
  617. do
  618. {
  619. _type = Type::EDGEPOLYGON;
  620. vec = new (std::nothrow) cpVect[count];
  621. PhysicsHelper::points2cpvs(points, vec, count);
  622. int i = 0;
  623. for (; i < count; ++i)
  624. {
  625. auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[(i + 1) % count], border);
  626. CC_BREAK_IF(shape == nullptr);
  627. cpShapeSetUserData(shape, this);
  628. cpShapeSetElasticity(shape, 1.0f);
  629. cpShapeSetFriction(shape, 1.0f);
  630. addShape(shape);
  631. }
  632. CC_SAFE_DELETE_ARRAY(vec);
  633. CC_BREAK_IF(i < count);
  634. _mass = PHYSICS_INFINITY;
  635. _moment = PHYSICS_INFINITY;
  636. setMaterial(material);
  637. return true;
  638. } while (false);
  639. CC_SAFE_DELETE_ARRAY(vec);
  640. return false;
  641. }
  642. Vec2 PhysicsShapeEdgePolygon::getCenter()
  643. {
  644. int count = (int)_cpShapes.size();
  645. cpVect* points = new (std::nothrow) cpVect[count];
  646. int i = 0;
  647. for(auto shape : _cpShapes)
  648. {
  649. points[i++] = cpSegmentShapeGetA(shape);
  650. }
  651. Vec2 center = PhysicsHelper::cpv2point(cpCentroidForPoly(count, points));
  652. delete[] points;
  653. return center;
  654. }
  655. void PhysicsShapeEdgePolygon::getPoints(cocos2d::Vec2 *outPoints) const
  656. {
  657. int i = 0;
  658. for(auto shape : _cpShapes)
  659. {
  660. outPoints[i++] = PhysicsHelper::cpv2point(cpSegmentShapeGetA(shape));
  661. }
  662. }
  663. int PhysicsShapeEdgePolygon::getPointsCount() const
  664. {
  665. return static_cast<int>(_cpShapes.size());
  666. }
  667. // PhysicsShapeEdgeChain
  668. PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(const Vec2* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/)
  669. {
  670. PhysicsShapeEdgeChain* shape = new (std::nothrow) PhysicsShapeEdgeChain();
  671. if (shape && shape->init(points, count, material, border))
  672. {
  673. shape->autorelease();
  674. return shape;
  675. }
  676. CC_SAFE_DELETE(shape);
  677. return nullptr;
  678. }
  679. void PhysicsShapeEdgePolygon::updateScale()
  680. {
  681. cpFloat factorX = _newScaleX / _scaleX;
  682. cpFloat factorY = _newScaleY / _scaleY;
  683. for (auto shape : _cpShapes)
  684. {
  685. cpVect a = cpSegmentShapeGetA(shape);
  686. a.x *= factorX;
  687. a.y *= factorY;
  688. cpVect b = cpSegmentShapeGetB(shape);
  689. b.x *= factorX;
  690. b.y *= factorY;
  691. cpSegmentShapeSetEndpoints(shape, a, b);
  692. }
  693. PhysicsShape::updateScale();
  694. }
  695. bool PhysicsShapeEdgeChain::init(const Vec2* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/)
  696. {
  697. cpVect* vec = nullptr;
  698. do
  699. {
  700. _type = Type::EDGECHAIN;
  701. vec = new (std::nothrow) cpVect[count];
  702. PhysicsHelper::points2cpvs(points, vec, count);
  703. int i = 0;
  704. for (; i < count - 1; ++i)
  705. {
  706. auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[i + 1], border);
  707. CC_BREAK_IF(shape == nullptr);
  708. cpShapeSetUserData(shape, this);
  709. cpShapeSetElasticity(shape, 1.0f);
  710. cpShapeSetFriction(shape, 1.0f);
  711. addShape(shape);
  712. }
  713. CC_SAFE_DELETE_ARRAY(vec);
  714. CC_BREAK_IF(i < count - 1);
  715. _mass = PHYSICS_INFINITY;
  716. _moment = PHYSICS_INFINITY;
  717. setMaterial(material);
  718. return true;
  719. } while (false);
  720. CC_SAFE_DELETE_ARRAY(vec);
  721. return false;
  722. }
  723. Vec2 PhysicsShapeEdgeChain::getCenter()
  724. {
  725. int count = (int)_cpShapes.size() + 1;
  726. cpVect* points = new (std::nothrow) cpVect[count];
  727. int i = 0;
  728. for(auto shape : _cpShapes)
  729. {
  730. points[i++] = cpSegmentShapeGetA(shape);
  731. }
  732. points[i++] = cpSegmentShapeGetB(_cpShapes.back());
  733. Vec2 center = PhysicsHelper::cpv2point(cpCentroidForPoly(count, points));
  734. delete[] points;
  735. return center;
  736. }
  737. void PhysicsShapeEdgeChain::getPoints(Vec2* outPoints) const
  738. {
  739. int i = 0;
  740. for(auto shape : _cpShapes)
  741. {
  742. outPoints[i++] = PhysicsHelper::cpv2point(cpSegmentShapeGetA(shape));
  743. }
  744. outPoints[i++] = PhysicsHelper::cpv2point(cpSegmentShapeGetB(_cpShapes.back()));
  745. }
  746. int PhysicsShapeEdgeChain::getPointsCount() const
  747. {
  748. return static_cast<int>(_cpShapes.size() + 1);
  749. }
  750. void PhysicsShapeEdgeChain::updateScale()
  751. {
  752. cpFloat factorX = _newScaleX / _scaleX;
  753. cpFloat factorY = _newScaleY / _scaleY;
  754. for (auto shape : _cpShapes)
  755. {
  756. cpVect a = cpSegmentShapeGetA(shape);
  757. a.x *= factorX;
  758. a.y *= factorY;
  759. cpVect b = cpSegmentShapeGetB(shape);
  760. b.x *= factorX;
  761. b.y *= factorY;
  762. cpSegmentShapeSetEndpoints(shape, a, b);
  763. }
  764. PhysicsShape::updateScale();
  765. }
  766. void PhysicsShape::setGroup(int group)
  767. {
  768. if (group < 0)
  769. {
  770. for (auto shape : _cpShapes)
  771. {
  772. cpShapeSetFilter(shape, cpShapeFilterNew(group, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
  773. }
  774. }
  775. _group = group;
  776. }
  777. bool PhysicsShape::containsPoint(const Vec2& point) const
  778. {
  779. for (auto shape : _cpShapes)
  780. {
  781. if (cpShapePointQuery(shape, PhysicsHelper::point2cpv(point), nullptr) < 0)
  782. {
  783. return true;
  784. }
  785. }
  786. return false;
  787. }
  788. NS_CC_END
  789. #endif // CC_USE_PHYSICS