b2d_decompose.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "b2d_decompose.h"
  2. #include "b2Polygon.h"
  3. namespace b2ConvexDecomp {
  4. void add_to_res(Vector< Vector<Vector2> >& res,const b2Polygon& p_poly) {
  5. Vector<Vector2> arr;
  6. for(int i=0;i<p_poly.nVertices;i++) {
  7. arr.push_back(Vector2(p_poly.x[i],p_poly.y[i]));
  8. }
  9. res.push_back(arr);
  10. }
  11. static Vector< Vector<Vector2> > _b2d_decompose(const Vector<Vector2>& p_polygon) {
  12. Vector< Vector<Vector2> > res;
  13. if (p_polygon.size()<3)
  14. return res;
  15. b2Vec2 *polys = memnew_arr(b2Vec2,p_polygon.size());
  16. for(int i=0;i<p_polygon.size();i++)
  17. polys[i]=b2Vec2(p_polygon[i].x,p_polygon[i].y);
  18. b2Polygon *p = new b2Polygon(polys,p_polygon.size());
  19. b2Polygon* decomposed = new b2Polygon[p->nVertices - 2]; //maximum number of polys
  20. memdelete_arr(polys);
  21. int32 nPolys = DecomposeConvex(p, decomposed, p->nVertices - 2);
  22. //int32 extra = 0;
  23. for (int32 i = 0; i < nPolys; ++i) {
  24. // b2FixtureDef* toAdd = &pdarray[i+extra];
  25. // *toAdd = *prototype;
  26. //Hmm, shouldn't have to do all this...
  27. b2Polygon curr = decomposed[i];
  28. //TODO ewjordan: move this triangle handling to a better place so that
  29. //it happens even if this convenience function is not called.
  30. if (curr.nVertices == 3){
  31. //Check here for near-parallel edges, since we can't
  32. //handle this in merge routine
  33. for (int j=0; j<3; ++j){
  34. int32 lower = (j == 0) ? (curr.nVertices - 1) : (j - 1);
  35. int32 middle = j;
  36. int32 upper = (j == curr.nVertices - 1) ? (0) : (j + 1);
  37. float32 dx0 = curr.x[middle] - curr.x[lower]; float32 dy0 = curr.y[middle] - curr.y[lower];
  38. float32 dx1 = curr.x[upper] - curr.x[middle]; float32 dy1 = curr.y[upper] - curr.y[middle];
  39. float32 norm0 = sqrtf(dx0*dx0+dy0*dy0); float32 norm1 = sqrtf(dx1*dx1+dy1*dy1);
  40. if ( !(norm0 > 0.0f && norm1 > 0.0f) ) {
  41. //Identical points, don't do anything!
  42. goto Skip;
  43. }
  44. dx0 /= norm0; dy0 /= norm0;
  45. dx1 /= norm1; dy1 /= norm1;
  46. float32 cross = dx0 * dy1 - dx1 * dy0;
  47. float32 dot = dx0*dx1 + dy0*dy1;
  48. if (fabs(cross) < b2_angularSlop && dot > 0) {
  49. //Angle too close, split the triangle across from this point.
  50. //This is guaranteed to result in two triangles that satify
  51. //the tolerance (one of the angles is 90 degrees)
  52. float32 dx2 = curr.x[lower] - curr.x[upper]; float32 dy2 = curr.y[lower] - curr.y[upper];
  53. float32 norm2 = sqrtf(dx2*dx2+dy2*dy2);
  54. if (norm2 == 0.0f) {
  55. goto Skip;
  56. }
  57. dx2 /= norm2; dy2 /= norm2;
  58. float32 thisArea = curr.GetArea();
  59. float32 thisHeight = 2.0f * thisArea / norm2;
  60. float32 buffer2 = dx2;
  61. dx2 = dy2; dy2 = -buffer2;
  62. //Make two new polygons
  63. //printf("dx2: %f, dy2: %f, thisHeight: %f, middle: %d\n",dx2,dy2,thisHeight,middle);
  64. float32 newX1[3] = { curr.x[middle]+dx2*thisHeight, curr.x[lower], curr.x[middle] };
  65. float32 newY1[3] = { curr.y[middle]+dy2*thisHeight, curr.y[lower], curr.y[middle] };
  66. float32 newX2[3] = { newX1[0], curr.x[middle], curr.x[upper] };
  67. float32 newY2[3] = { newY1[0], curr.y[middle], curr.y[upper] };
  68. b2Polygon p1(newX1,newY1,3);
  69. b2Polygon p2(newX2,newY2,3);
  70. if (p1.IsUsable()){
  71. add_to_res(res,p1);
  72. //++extra;
  73. } else if (B2_POLYGON_REPORT_ERRORS){
  74. printf("Didn't add unusable polygon. Dumping vertices:\n");
  75. p1.print();
  76. }
  77. if (p2.IsUsable()){
  78. add_to_res(res,p2);
  79. //p2.AddTo(pdarray[i+extra]);
  80. //bd->CreateFixture(toAdd);
  81. } else if (B2_POLYGON_REPORT_ERRORS){
  82. printf("Didn't add unusable polygon. Dumping vertices:\n");
  83. p2.print();
  84. }
  85. goto Skip;
  86. }
  87. }
  88. }
  89. if (decomposed[i].IsUsable()){
  90. add_to_res(res,decomposed[i]);
  91. //decomposed[i].AddTo(*toAdd);
  92. //bd->CreateFixture((const b2FixtureDef*)toAdd);
  93. } else if (B2_POLYGON_REPORT_ERRORS){
  94. printf("Didn't add unusable polygon. Dumping vertices:\n");
  95. decomposed[i].print();
  96. }
  97. Skip:
  98. ;
  99. }
  100. // delete[] pdarray;
  101. delete[] decomposed;
  102. delete p;
  103. return res;// pdarray; //needs to be deleted after body is created
  104. }
  105. }
  106. Vector< Vector<Vector2> > b2d_decompose(const Vector<Vector2>& p_polygon) {
  107. return b2ConvexDecomp::_b2d_decompose(p_polygon);
  108. }