CollisionFiltering.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_FILTERING_H
  19. #define COLLISION_FILTERING_H
  20. // This is a test of collision filtering.
  21. // There is a triangle, a box, and a circle.
  22. // There are 6 shapes. 3 large and 3 small.
  23. // The 3 small ones always collide.
  24. // The 3 large ones never collide.
  25. // The boxes don't collide with triangles (except if both are small).
  26. const int16 k_smallGroup = 1;
  27. const int16 k_largeGroup = -1;
  28. const uint16 k_defaultCategory = 0x0001;
  29. const uint16 k_triangleCategory = 0x0002;
  30. const uint16 k_boxCategory = 0x0004;
  31. const uint16 k_circleCategory = 0x0008;
  32. const uint16 k_triangleMask = 0xFFFF;
  33. const uint16 k_boxMask = 0xFFFF ^ k_triangleCategory;
  34. const uint16 k_circleMask = 0xFFFF;
  35. class CollisionFiltering : public Test
  36. {
  37. public:
  38. CollisionFiltering()
  39. {
  40. // Ground body
  41. {
  42. b2PolygonShape shape;
  43. shape.SetAsEdge(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
  44. b2FixtureDef sd;
  45. sd.shape = &shape;
  46. sd.friction = 0.3f;
  47. b2BodyDef bd;
  48. b2Body* ground = m_world->CreateBody(&bd);
  49. ground->CreateFixture(&sd);
  50. }
  51. // Small triangle
  52. b2Vec2 vertices[3];
  53. vertices[0].Set(-1.0f, 0.0f);
  54. vertices[1].Set(1.0f, 0.0f);
  55. vertices[2].Set(0.0f, 2.0f);
  56. b2PolygonShape polygon;
  57. polygon.Set(vertices, 3);
  58. b2FixtureDef triangleShapeDef;
  59. triangleShapeDef.shape = &polygon;
  60. triangleShapeDef.density = 1.0f;
  61. triangleShapeDef.filter.groupIndex = k_smallGroup;
  62. triangleShapeDef.filter.categoryBits = k_triangleCategory;
  63. triangleShapeDef.filter.maskBits = k_triangleMask;
  64. b2BodyDef triangleBodyDef;
  65. triangleBodyDef.type = b2_dynamicBody;
  66. triangleBodyDef.position.Set(-5.0f, 2.0f);
  67. b2Body* body1 = m_world->CreateBody(&triangleBodyDef);
  68. body1->CreateFixture(&triangleShapeDef);
  69. // Large triangle (recycle definitions)
  70. vertices[0] *= 2.0f;
  71. vertices[1] *= 2.0f;
  72. vertices[2] *= 2.0f;
  73. polygon.Set(vertices, 3);
  74. triangleShapeDef.filter.groupIndex = k_largeGroup;
  75. triangleBodyDef.position.Set(-5.0f, 6.0f);
  76. triangleBodyDef.fixedRotation = true; // look at me!
  77. b2Body* body2 = m_world->CreateBody(&triangleBodyDef);
  78. body2->CreateFixture(&triangleShapeDef);
  79. {
  80. b2BodyDef bd;
  81. bd.type = b2_dynamicBody;
  82. bd.position.Set(-5.0f, 10.0f);
  83. b2Body* body = m_world->CreateBody(&bd);
  84. b2PolygonShape p;
  85. p.SetAsBox(0.5f, 1.0f);
  86. body->CreateFixture(&p, 1.0f);
  87. b2PrismaticJointDef jd;
  88. jd.bodyA = body2;
  89. jd.bodyB = body;
  90. jd.enableLimit = true;
  91. jd.localAnchorA.Set(0.0f, 4.0f);
  92. jd.localAnchorB.SetZero();
  93. jd.localAxis1.Set(0.0f, 1.0f);
  94. jd.lowerTranslation = -1.0f;
  95. jd.upperTranslation = 1.0f;
  96. m_world->CreateJoint(&jd);
  97. }
  98. // Small box
  99. polygon.SetAsBox(1.0f, 0.5f);
  100. b2FixtureDef boxShapeDef;
  101. boxShapeDef.shape = &polygon;
  102. boxShapeDef.density = 1.0f;
  103. boxShapeDef.restitution = 0.1f;
  104. boxShapeDef.filter.groupIndex = k_smallGroup;
  105. boxShapeDef.filter.categoryBits = k_boxCategory;
  106. boxShapeDef.filter.maskBits = k_boxMask;
  107. b2BodyDef boxBodyDef;
  108. boxBodyDef.type = b2_dynamicBody;
  109. boxBodyDef.position.Set(0.0f, 2.0f);
  110. b2Body* body3 = m_world->CreateBody(&boxBodyDef);
  111. body3->CreateFixture(&boxShapeDef);
  112. // Large box (recycle definitions)
  113. polygon.SetAsBox(2.0f, 1.0f);
  114. boxShapeDef.filter.groupIndex = k_largeGroup;
  115. boxBodyDef.position.Set(0.0f, 6.0f);
  116. b2Body* body4 = m_world->CreateBody(&boxBodyDef);
  117. body4->CreateFixture(&boxShapeDef);
  118. // Small circle
  119. b2CircleShape circle;
  120. circle.m_radius = 1.0f;
  121. b2FixtureDef circleShapeDef;
  122. circleShapeDef.shape = &circle;
  123. circleShapeDef.density = 1.0f;
  124. circleShapeDef.filter.groupIndex = k_smallGroup;
  125. circleShapeDef.filter.categoryBits = k_circleCategory;
  126. circleShapeDef.filter.maskBits = k_circleMask;
  127. b2BodyDef circleBodyDef;
  128. circleBodyDef.type = b2_dynamicBody;
  129. circleBodyDef.position.Set(5.0f, 2.0f);
  130. b2Body* body5 = m_world->CreateBody(&circleBodyDef);
  131. body5->CreateFixture(&circleShapeDef);
  132. // Large circle
  133. circle.m_radius *= 2.0f;
  134. circleShapeDef.filter.groupIndex = k_largeGroup;
  135. circleBodyDef.position.Set(5.0f, 6.0f);
  136. b2Body* body6 = m_world->CreateBody(&circleBodyDef);
  137. body6->CreateFixture(&circleShapeDef);
  138. }
  139. static Test* Create()
  140. {
  141. return new CollisionFiltering;
  142. }
  143. };
  144. #endif