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

游戏引擎

开发平台:

Visual C++

  1. /*
  2. * Copyright (c) 2006-2010 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 CHARACTER_COLLISION_H
  19. #define CHARACTER_COLLISION_H
  20. /// This is a test of typical character collision scenarios. This does not
  21. /// show how you should implement a character in your application.
  22. class CharacterCollision : public Test
  23. {
  24. public:
  25. CharacterCollision()
  26. {
  27. // Ground body
  28. {
  29. b2BodyDef bd;
  30. b2Body* ground = m_world->CreateBody(&bd);
  31. b2PolygonShape shape;
  32. shape.SetAsEdge(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
  33. ground->CreateFixture(&shape, 0.0f);
  34. }
  35. // Collinear edges
  36. {
  37. b2BodyDef bd;
  38. b2Body* ground = m_world->CreateBody(&bd);
  39. b2PolygonShape shape;
  40. shape.m_radius = 0.0f;
  41. shape.SetAsEdge(b2Vec2(-8.0f, 1.0f), b2Vec2(-6.0f, 1.0f));
  42. ground->CreateFixture(&shape, 0.0f);
  43. shape.SetAsEdge(b2Vec2(-6.0f, 1.0f), b2Vec2(-4.0f, 1.0f));
  44. ground->CreateFixture(&shape, 0.0f);
  45. shape.SetAsEdge(b2Vec2(-4.0f, 1.0f), b2Vec2(-2.0f, 1.0f));
  46. ground->CreateFixture(&shape, 0.0f);
  47. }
  48. // Square tiles
  49. {
  50. b2BodyDef bd;
  51. b2Body* ground = m_world->CreateBody(&bd);
  52. b2PolygonShape shape;
  53. shape.SetAsBox(1.0f, 1.0f, b2Vec2(4.0f, 3.0f), 0.0f);
  54. ground->CreateFixture(&shape, 0.0f);
  55. shape.SetAsBox(1.0f, 1.0f, b2Vec2(6.0f, 3.0f), 0.0f);
  56. ground->CreateFixture(&shape, 0.0f);
  57. shape.SetAsBox(1.0f, 1.0f, b2Vec2(8.0f, 3.0f), 0.0f);
  58. ground->CreateFixture(&shape, 0.0f);
  59. }
  60. // Square made from edges notice how the edges are shrunk to account
  61. // for the polygon radius. This makes it so the square character does
  62. // not get snagged. However, ray casts can now go through the cracks.
  63. {
  64. b2BodyDef bd;
  65. b2Body* ground = m_world->CreateBody(&bd);
  66. b2PolygonShape shape;
  67. float32 d = 2.0f * b2_polygonRadius;
  68. shape.SetAsEdge(b2Vec2(-1.0f + d, 3.0f), b2Vec2(1.0f - d, 3.0f));
  69. ground->CreateFixture(&shape, 0.0f);
  70. shape.SetAsEdge(b2Vec2(1.0f, 3.0f + d), b2Vec2(1.0f, 5.0f - d));
  71. ground->CreateFixture(&shape, 0.0f);
  72. shape.SetAsEdge(b2Vec2(1.0f - d, 5.0f), b2Vec2(-1.0f + d, 5.0f));
  73. ground->CreateFixture(&shape, 0.0f);
  74. shape.SetAsEdge(b2Vec2(-1.0f, 5.0f - d), b2Vec2(-1.0f, 3.0f + d));
  75. ground->CreateFixture(&shape, 0.0f);
  76. }
  77. // Square character
  78. {
  79. b2BodyDef bd;
  80. bd.position.Set(-3.0f, 5.0f);
  81. bd.type = b2_dynamicBody;
  82. bd.fixedRotation = true;
  83. bd.allowSleep = false;
  84. b2Body* body = m_world->CreateBody(&bd);
  85. b2PolygonShape shape;
  86. shape.SetAsBox(0.5f, 0.5f);
  87. b2FixtureDef fd;
  88. fd.shape = &shape;
  89. fd.density = 20.0f;
  90. body->CreateFixture(&fd);
  91. }
  92. // Hexagon character
  93. {
  94. b2BodyDef bd;
  95. bd.position.Set(-5.0f, 5.0f);
  96. bd.type = b2_dynamicBody;
  97. bd.fixedRotation = true;
  98. bd.allowSleep = false;
  99. b2Body* body = m_world->CreateBody(&bd);
  100. float32 angle = 0.0f;
  101. float32 delta = b2_pi / 3.0f;
  102. b2Vec2 vertices[6];
  103. for (int32 i = 0; i < 6; ++i)
  104. {
  105. vertices[i].Set(0.5f * cosf(angle), 0.5f * sinf(angle));
  106. angle += delta;
  107. }
  108. b2PolygonShape shape;
  109. shape.Set(vertices, 6);
  110. b2FixtureDef fd;
  111. fd.shape = &shape;
  112. fd.density = 20.0f;
  113. body->CreateFixture(&fd);
  114. }
  115. // Circle character
  116. {
  117. b2BodyDef bd;
  118. bd.position.Set(3.0f, 5.0f);
  119. bd.type = b2_dynamicBody;
  120. bd.fixedRotation = true;
  121. bd.allowSleep = false;
  122. b2Body* body = m_world->CreateBody(&bd);
  123. b2CircleShape shape;
  124. shape.m_radius = 0.5f;
  125. b2FixtureDef fd;
  126. fd.shape = &shape;
  127. fd.density = 20.0f;
  128. body->CreateFixture(&fd);
  129. }
  130. }
  131. void Step(Settings* settings)
  132. {
  133. Test::Step(settings);
  134. m_debugDraw.DrawString(5, m_textLine, "This tests various character collision shapes");
  135. m_textLine += 15;
  136. }
  137. static Test* Create()
  138. {
  139. return new CharacterCollision;
  140. }
  141. };
  142. #endif