CCPhysicsDebugNode.cpp 7.7 KB

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