CollisionProcessing.h
上传用户:gb3593
上传日期:2022-01-07
资源大小:3028k
文件大小:5k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty.  In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef COLLISION_PROCESSING_H
  19. #define COLLISION_PROCESSING_H
  20. #include <algorithm>
  21. // This test shows collision processing and tests
  22. // deferred body destruction.
  23. class CollisionProcessing : public Test
  24. {
  25. public:
  26. CollisionProcessing()
  27. {
  28. // Ground body
  29. {
  30. b2PolygonShape shape;
  31. shape.SetAsEdge(b2Vec2(-50.0f, 0.0f), b2Vec2(50.0f, 0.0f));
  32. b2FixtureDef sd;
  33. sd.shape = &shape;;
  34. b2BodyDef bd;
  35. b2Body* ground = m_world->CreateBody(&bd);
  36. ground->CreateFixture(&sd);
  37. }
  38. float32 xLo = -5.0f, xHi = 5.0f;
  39. float32 yLo = 2.0f, yHi = 35.0f;
  40. // Small triangle
  41. b2Vec2 vertices[3];
  42. vertices[0].Set(-1.0f, 0.0f);
  43. vertices[1].Set(1.0f, 0.0f);
  44. vertices[2].Set(0.0f, 2.0f);
  45. b2PolygonShape polygon;
  46. polygon.Set(vertices, 3);
  47. b2FixtureDef triangleShapeDef;
  48. triangleShapeDef.shape = &polygon;
  49. triangleShapeDef.density = 1.0f;
  50. b2BodyDef triangleBodyDef;
  51. triangleBodyDef.type = b2_dynamicBody;
  52. triangleBodyDef.position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi));
  53. b2Body* body1 = m_world->CreateBody(&triangleBodyDef);
  54. body1->CreateFixture(&triangleShapeDef);
  55. // Large triangle (recycle definitions)
  56. vertices[0] *= 2.0f;
  57. vertices[1] *= 2.0f;
  58. vertices[2] *= 2.0f;
  59. polygon.Set(vertices, 3);
  60. triangleBodyDef.position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi));
  61. b2Body* body2 = m_world->CreateBody(&triangleBodyDef);
  62. body2->CreateFixture(&triangleShapeDef);
  63. // Small box
  64. polygon.SetAsBox(1.0f, 0.5f);
  65. b2FixtureDef boxShapeDef;
  66. boxShapeDef.shape = &polygon;
  67. boxShapeDef.density = 1.0f;
  68. b2BodyDef boxBodyDef;
  69. boxBodyDef.type = b2_dynamicBody;
  70. boxBodyDef.position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi));
  71. b2Body* body3 = m_world->CreateBody(&boxBodyDef);
  72. body3->CreateFixture(&boxShapeDef);
  73. // Large box (recycle definitions)
  74. polygon.SetAsBox(2.0f, 1.0f);
  75. boxBodyDef.position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi));
  76. b2Body* body4 = m_world->CreateBody(&boxBodyDef);
  77. body4->CreateFixture(&boxShapeDef);
  78. // Small circle
  79. b2CircleShape circle;
  80. circle.m_radius = 1.0f;
  81. b2FixtureDef circleShapeDef;
  82. circleShapeDef.shape = &circle;
  83. circleShapeDef.density = 1.0f;
  84. b2BodyDef circleBodyDef;
  85. circleBodyDef.type = b2_dynamicBody;
  86. circleBodyDef.position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi));
  87. b2Body* body5 = m_world->CreateBody(&circleBodyDef);
  88. body5->CreateFixture(&circleShapeDef);
  89. // Large circle
  90. circle.m_radius *= 2.0f;
  91. circleBodyDef.position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi));
  92. b2Body* body6 = m_world->CreateBody(&circleBodyDef);
  93. body6->CreateFixture(&circleShapeDef);
  94. }
  95. void Step(Settings* settings)
  96. {
  97. Test::Step(settings);
  98. // We are going to destroy some bodies according to contact
  99. // points. We must buffer the bodies that should be destroyed
  100. // because they may belong to multiple contact points.
  101. const int32 k_maxNuke = 6;
  102. b2Body* nuke[k_maxNuke];
  103. int32 nukeCount = 0;
  104. // Traverse the contact results. Destroy bodies that
  105. // are touching heavier bodies.
  106. for (int32 i = 0; i < m_pointCount; ++i)
  107. {
  108. ContactPoint* point = m_points + i;
  109. b2Body* body1 = point->fixtureA->GetBody();
  110. b2Body* body2 = point->fixtureB->GetBody();
  111. float32 mass1 = body1->GetMass();
  112. float32 mass2 = body2->GetMass();
  113. if (mass1 > 0.0f && mass2 > 0.0f)
  114. {
  115. if (mass2 > mass1)
  116. {
  117. nuke[nukeCount++] = body1;
  118. }
  119. else
  120. {
  121. nuke[nukeCount++] = body2;
  122. }
  123. if (nukeCount == k_maxNuke)
  124. {
  125. break;
  126. }
  127. }
  128. }
  129. // Sort the nuke array to group duplicates.
  130. std::sort(nuke, nuke + nukeCount);
  131. // Destroy the bodies, skipping duplicates.
  132. int32 i = 0;
  133. while (i < nukeCount)
  134. {
  135. b2Body* b = nuke[i++];
  136. while (i < nukeCount && nuke[i] == b)
  137. {
  138. ++i;
  139. }
  140. if (b != m_bomb)
  141. {
  142. m_world->DestroyBody(b);
  143. }
  144. }
  145. }
  146. static Test* Create()
  147. {
  148. return new CollisionProcessing;
  149. }
  150. };
  151. #endif