CCPhysicsDebugNode.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
  2. * Copyright (c) 2012 cocos2d-x.org
  3. * Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. *
  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. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #include "CCPhysicsDebugNode.h"
  24. #if CC_ENABLE_CHIPMUNK_INTEGRATION
  25. #include "chipmunk/chipmunk_private.h"
  26. #endif
  27. #include "base/ccTypes.h"
  28. #include "math/CCGeometry.h"
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <math.h>
  32. #include <limits.h>
  33. #include <string.h>
  34. NS_CC_EXT_BEGIN
  35. #if CC_ENABLE_CHIPMUNK_INTEGRATION
  36. /*
  37. IMPORTANT - READ ME!
  38. This file sets pokes around in the private API a lot to provide efficient
  39. debug rendering given nothing more than reference to a Chipmunk space.
  40. It is not recommended to write rendering code like this in your own games
  41. as the private API may change with little or no warning.
  42. */
  43. static Color4F ColorForBody(cpBody *body)
  44. {
  45. if (CP_BODY_TYPE_STATIC == cpBodyGetType(body) || cpBodyIsSleeping(body))
  46. {
  47. return Color4F(0.5f, 0.5f, 0.5f ,0.5f);
  48. }
  49. else if (body->sleeping.idleTime > cpBodyGetSpace(body)->sleepTimeThreshold)
  50. {
  51. return Color4F(0.33f, 0.33f, 0.33f, 0.5f);
  52. }
  53. else
  54. {
  55. return Color4F(1.0f, 0.0f, 0.0f, 0.5f);
  56. }
  57. }
  58. static Vec2 cpVert2Point(const cpVect &vert)
  59. {
  60. return Vec2(vert.x, vert.y);
  61. }
  62. static void DrawShape(cpShape *shape, DrawNode *renderer)
  63. {
  64. cpBody *body = cpShapeGetBody(shape);
  65. Color4F color = ColorForBody(body);
  66. switch (shape->CP_PRIVATE(klass)->type)
  67. {
  68. case CP_CIRCLE_SHAPE:
  69. {
  70. cpCircleShape *circle = (cpCircleShape *)shape;
  71. cpVect center = circle->tc;
  72. cpFloat radius = circle->r;
  73. renderer->drawDot(cpVert2Point(center), cpfmax(radius, 1.0), color);
  74. renderer->drawSegment(cpVert2Point(center), cpVert2Point(cpvadd(center, cpvmult(cpBodyGetRotation(body), radius))), 1.0, color);
  75. }
  76. break;
  77. case CP_SEGMENT_SHAPE:
  78. {
  79. cpSegmentShape *seg = (cpSegmentShape *)shape;
  80. renderer->drawSegment(cpVert2Point(seg->ta), cpVert2Point(seg->tb), cpfmax(seg->r, 2.0), color);
  81. }
  82. break;
  83. case CP_POLY_SHAPE:
  84. {
  85. cpPolyShape* poly = (cpPolyShape*)shape;
  86. Color4F line = color;
  87. line.a = cpflerp(color.a, 1.0, 0.5);
  88. int num = poly->count;
  89. Vec2* pPoints = new (std::nothrow) Vec2[num];
  90. for(int i=0;i<num;++i)
  91. pPoints[i] = cpVert2Point(poly->planes[i].v0);
  92. renderer->drawPolygon(pPoints, num, color, 1.0, line);
  93. CC_SAFE_DELETE_ARRAY(pPoints);
  94. }
  95. break;
  96. default:
  97. cpAssertHard(false, "Bad assertion in DrawShape()");
  98. }
  99. }
  100. static Color4F CONSTRAINT_COLOR(0, 1, 0, 0.5);
  101. static void DrawConstraint(cpConstraint *constraint, DrawNode *renderer)
  102. {
  103. cpBody *body_a = cpConstraintGetBodyA(constraint);
  104. cpBody *body_b = cpConstraintGetBodyB(constraint);
  105. if(cpConstraintIsPinJoint(constraint))
  106. {
  107. cpVect a = cpvadd(cpBodyGetPosition(body_a), cpvrotate(cpPinJointGetAnchorA(constraint), cpBodyGetRotation(body_a)));
  108. cpVect b = cpvadd(cpBodyGetPosition(body_b), cpvrotate(cpPinJointGetAnchorB(constraint), cpBodyGetRotation(body_b)));
  109. renderer->drawDot(cpVert2Point(a), 3.0, CONSTRAINT_COLOR);
  110. renderer->drawDot(cpVert2Point(b), 3.0, CONSTRAINT_COLOR);
  111. renderer->drawSegment(cpVert2Point(a), cpVert2Point(b), 1.0, CONSTRAINT_COLOR);
  112. }
  113. else if(cpConstraintIsSlideJoint(constraint))
  114. {
  115. cpVect a = cpvadd(cpBodyGetPosition(body_a), cpvrotate(cpSlideJointGetAnchorA(constraint), cpBodyGetRotation(body_a)));
  116. cpVect b = cpvadd(cpBodyGetPosition(body_b), cpvrotate(cpSlideJointGetAnchorB(constraint), cpBodyGetRotation(body_b)));
  117. renderer->drawDot(cpVert2Point(a), 3.0, CONSTRAINT_COLOR);
  118. renderer->drawDot(cpVert2Point(b), 3.0, CONSTRAINT_COLOR);
  119. renderer->drawSegment(cpVert2Point(a), cpVert2Point(b), 1.0, CONSTRAINT_COLOR);
  120. }
  121. else if(cpConstraintIsPivotJoint(constraint))
  122. {
  123. cpVect a = cpvadd(cpBodyGetPosition(body_a), cpvrotate(cpPivotJointGetAnchorA(constraint), cpBodyGetRotation(body_a)));
  124. cpVect b = cpvadd(cpBodyGetPosition(body_b), cpvrotate(cpPivotJointGetAnchorB(constraint), cpBodyGetRotation(body_b)));
  125. renderer->drawDot(cpVert2Point(a), 3.0, CONSTRAINT_COLOR);
  126. renderer->drawDot(cpVert2Point(b), 3.0, CONSTRAINT_COLOR);
  127. }
  128. else if(cpConstraintIsGrooveJoint(constraint))
  129. {
  130. cpVect a = cpvadd(cpBodyGetPosition(body_a), cpvrotate(cpGrooveJointGetGrooveA(constraint), cpBodyGetRotation(body_a)));
  131. cpVect b = cpvadd(cpBodyGetPosition(body_a), cpvrotate(cpGrooveJointGetGrooveB(constraint), cpBodyGetRotation(body_a)));
  132. cpVect c = cpvadd(cpBodyGetPosition(body_b), cpvrotate(cpGrooveJointGetAnchorB(constraint), cpBodyGetRotation(body_b)));
  133. renderer->drawDot(cpVert2Point(c), 3.0, CONSTRAINT_COLOR);
  134. renderer->drawSegment(cpVert2Point(a), cpVert2Point(b), 1.0, CONSTRAINT_COLOR);
  135. }
  136. else if(cpConstraintIsDampedSpring(constraint))
  137. {
  138. // TODO: uninplemented
  139. }
  140. else
  141. {
  142. // printf("Cannot draw constraint\n");
  143. }
  144. }
  145. #endif // #if CC_ENABLE_CHIPMUNK_INTEGRATION
  146. // implementation of PhysicsDebugNode
  147. void PhysicsDebugNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  148. {
  149. if (! _spacePtr)
  150. {
  151. return;
  152. }
  153. #if CC_ENABLE_CHIPMUNK_INTEGRATION
  154. // clear the shapes information before draw current shapes.
  155. DrawNode::clear();
  156. cpSpaceEachShape(_spacePtr, (cpSpaceShapeIteratorFunc)DrawShape, this);
  157. cpSpaceEachConstraint(_spacePtr, (cpSpaceConstraintIteratorFunc)DrawConstraint, this);
  158. DrawNode::draw(renderer, transform, flags);
  159. #endif
  160. }
  161. PhysicsDebugNode::PhysicsDebugNode()
  162. : _spacePtr(nullptr)
  163. {}
  164. PhysicsDebugNode* PhysicsDebugNode::create(cpSpace *space)
  165. {
  166. PhysicsDebugNode *node = new (std::nothrow) PhysicsDebugNode();
  167. if (node)
  168. {
  169. node->init();
  170. #if CC_ENABLE_CHIPMUNK_INTEGRATION
  171. node->_spacePtr = space;
  172. #else
  173. CCASSERT(false, "CC_ENABLE_CHIPMUNK_INTEGRATION was not enabled!");
  174. #endif
  175. node->autorelease();
  176. }
  177. else
  178. {
  179. CC_SAFE_DELETE(node);
  180. }
  181. return node;
  182. }
  183. PhysicsDebugNode::~PhysicsDebugNode()
  184. {
  185. }
  186. cpSpace* PhysicsDebugNode::getSpace() const
  187. {
  188. #if CC_ENABLE_CHIPMUNK_INTEGRATION
  189. return _spacePtr;
  190. #else
  191. CCASSERT(false, "Can't call chipmunk methods when Chipmunk is disabled");
  192. return nullptr;
  193. #endif
  194. }
  195. void PhysicsDebugNode::setSpace(cpSpace *space)
  196. {
  197. #if CC_ENABLE_CHIPMUNK_INTEGRATION
  198. _spacePtr = space;
  199. #else
  200. CCASSERT(false, "Can't call chipmunk methods when Chipmunk is disabled");
  201. #endif
  202. }
  203. NS_CC_EXT_END