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

游戏引擎

开发平台:

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 DYNAMIC_EDGES_H
  19. #define DYNAMIC_EDGES_H
  20. class DynamicEdges : public Test
  21. {
  22. public:
  23. DynamicEdges()
  24. {
  25. {
  26. b2BodyDef bd;
  27. bd.position.Set(0.0f, -10.0f);
  28. b2Body* body = m_world->CreateBody(&bd);
  29. b2PolygonDef sd;
  30. sd.SetAsBox(50.0f, 10.0f);
  31. body->CreateFixture(&sd);
  32. }
  33. {
  34. b2CircleDef sd1;
  35. sd1.radius = 0.5f;
  36. sd1.localPosition.Set(-0.5f, 0.5f);
  37. sd1.density = 2.0f;
  38. b2CircleDef sd2;
  39. sd2.radius = 0.5f;
  40. sd2.localPosition.Set(0.5f, 0.5f);
  41. sd2.density = 0.0f; // massless
  42. for (int i = 0; i < 10; ++i)
  43. {
  44. float32 x = RandomFloat(-0.1f, 0.1f);
  45. b2BodyDef bd;
  46. bd.position.Set(x + 5.0f, 1.05f + 2.5f * i);
  47. bd.angle = RandomFloat(-b2_pi, b2_pi);
  48. b2Body* body = m_world->CreateBody(&bd);
  49. body->CreateFixture(&sd1);
  50. body->CreateFixture(&sd2);
  51. body->SetMassFromShapes();
  52. }
  53. }
  54. {
  55. b2PolygonDef sd1;
  56. sd1.SetAsBox(0.25f, 0.5f);
  57. sd1.density = 2.0f;
  58. b2PolygonDef sd2;
  59. sd2.SetAsBox(0.25f, 0.5f, b2Vec2(0.0f, -0.5f), 0.5f * b2_pi);
  60. sd2.density = 2.0f;
  61. for (int i = 0; i < 10; ++i)
  62. {
  63. float32 x = RandomFloat(-0.1f, 0.1f);
  64. b2BodyDef bd;
  65. bd.position.Set(x - 5.0f, 1.05f + 2.5f * i);
  66. bd.angle = RandomFloat(-b2_pi, b2_pi);
  67. b2Body* body = m_world->CreateBody(&bd);
  68. body->CreateFixture(&sd1);
  69. body->CreateFixture(&sd2);
  70. body->SetMassFromShapes();
  71. }
  72. }
  73. {
  74. b2XForm xf1;
  75. xf1.R.Set(0.3524f * b2_pi);
  76. xf1.position = b2Mul(xf1.R, b2Vec2(1.0f, 0.0f));
  77. b2PolygonDef sd1;
  78. sd1.vertexCount = 3;
  79. sd1.vertices[0] = b2Mul(xf1, b2Vec2(-1.0f, 0.0f));
  80. sd1.vertices[1] = b2Mul(xf1, b2Vec2(1.0f, 0.0f));
  81. sd1.vertices[2] = b2Mul(xf1, b2Vec2(0.0f, 0.5f));
  82. sd1.density = 2.0f;
  83. b2XForm xf2;
  84. xf2.R.Set(-0.3524f * b2_pi);
  85. xf2.position = b2Mul(xf2.R, b2Vec2(-1.0f, 0.0f));
  86. b2PolygonDef sd2;
  87. sd2.vertexCount = 3;
  88. sd2.vertices[0] = b2Mul(xf2, b2Vec2(-1.0f, 0.0f));
  89. sd2.vertices[1] = b2Mul(xf2, b2Vec2(1.0f, 0.0f));
  90. sd2.vertices[2] = b2Mul(xf2, b2Vec2(0.0f, 0.5f));
  91. sd2.density = 2.0f;
  92. for (int32 i = 0; i < 10; ++i)
  93. {
  94. float32 x = RandomFloat(-0.1f, 0.1f);
  95. b2BodyDef bd;
  96. bd.position.Set(x, 2.05f + 2.5f * i);
  97. bd.angle = 0.0f;
  98. b2Body* body = m_world->CreateBody(&bd);
  99. body->CreateFixture(&sd1);
  100. body->CreateFixture(&sd2);
  101. body->SetMassFromShapes();
  102. }
  103. }
  104. {
  105. b2PolygonDef sd_bottom;
  106. sd_bottom.SetAsBox( 1.5f, 0.15f );
  107. sd_bottom.density = 4.0f;
  108. b2PolygonDef sd_left;
  109. sd_left.SetAsBox(0.15f, 2.7f, b2Vec2(-1.45f, 2.35f), 0.2f);
  110. sd_left.density = 4.0f;
  111. b2PolygonDef sd_right;
  112. sd_right.SetAsBox(0.15f, 2.7f, b2Vec2(1.45f, 2.35f), -0.2f);
  113. sd_right.density = 4.0f;
  114. b2BodyDef bd;
  115. bd.position.Set( 0.0f, 2.0f );
  116. b2Body* body = m_world->CreateBody(&bd);
  117. body->CreateFixture(&sd_bottom);
  118. body->CreateFixture(&sd_left);
  119. body->CreateFixture(&sd_right);
  120. body->SetMassFromShapes();
  121. }
  122. {
  123. float32 loop1[] = 
  124. {
  125. 0.063134534f,8.3695248f,
  126. 0.94701801f,9.3165428f,
  127. 0.0f,9.0640047f,
  128. -0.12626907f,10.326695f,
  129. 1.4520943f,11.77879f,
  130. 2.2728432f,10.137292f,
  131. 2.3991123f,11.147444f,
  132. 3.5986685f,10.958041f,
  133. 3.9143411f,7.3593722f,
  134. 4.1668793f,9.4428119f,
  135. 5.4295699f,9.3165428f,
  136. 6.2503189f,8.3063903f,
  137. 6.6922606f,10.137292f,
  138. 4.9876282f,9.8216191f,
  139. 4.7350901f,10.958041f,
  140. 7.2604714f,11.652521f,
  141. 10.732871f,11.147444f,
  142. 10.480333f,10.642368f,
  143. 10.732871f,9.8216191f,
  144. 11.55362f,9.4428119f,
  145. 12.374369f,9.3796773f,
  146. 13.005714f,9.8216191f,
  147. 13.195118f,10.38983f,
  148. 13.005714f,10.768637f,
  149. 12.626907f,10.894906f,
  150. 12.753176f,11.526252f,
  151. 13.573925f,11.715655f,
  152. 14.836616f,11.399982f,
  153. 16.351844f,10.768637f,
  154. 17.867073f,11.399982f,
  155. 17.803939f,10.263561f,
  156. 17.361997f,8.3063903f,
  157. 17.803939f,8.1801212f,
  158. 18.056477f,9.5059464f,
  159. 18.182746f,11.336848f,
  160. 18.561553f,11.210579f,
  161. 18.561553f,9.6322155f,
  162. 18.561553f,7.7381795f,
  163. 18.687822f,5.5284708f,
  164. 19.382302f,5.6547398f,
  165. 19.066629f,8.1801212f,
  166. 19.003495f,10.263561f,
  167. 19.066629f,11.463117f,
  168. 19.887378f,11.841924f,
  169. 20.708127f,11.273713f,
  170. 21.0238f,10.011023f,
  171. 20.708127f,7.2962377f,
  172. 21.086934f,6.2860852f,
  173. 21.150069f,3.7607038f,
  174. 20.392455f,2.5611476f,
  175. 18.624688f,2.5611476f,
  176. 20.771262f,2.1192059f,
  177. 20.771262f,0.22516988f,
  178. 18.624688f,-0.2799064f,
  179. 13.826463f,0.16203534f,
  180. 14.015867f,1.7403987f,
  181. 13.195118f,2.1823404f,
  182. 12.626907f,1.5509951f,
  183. 12.879445f,0.85651522f,
  184. 12.626907f,0.35143895f,
  185. 10.543467f,1.298457f,
  186. 11.490485f,3.9501074f,
  187. 13.889598f,3.6344347f,
  188. 13.889598f,2.9399549f,
  189. 14.584077f,3.8869729f,
  190. 11.932427f,5.2127981f,
  191. 9.7227183f,4.0132419f,
  192. 10.796005f,3.5081657f,
  193. 9.7858528f,3.2556275f,
  194. 10.796005f,2.4980131f,
  195. 7.9549513f,1.7403987f,
  196. 9.6595837f,1.424726f,
  197. 9.217642f,0.66711162f,
  198. 8.270624f,-0.090502792f,
  199. 7.0079333f,0.85651522f,
  200. 6.1240498f,-0.15363733f,
  201. 6.1240498f,3.192493f,
  202. 5.6821081f,2.4348786f,
  203. 4.9876282f,2.1192059f,
  204. 4.1037447f,1.8666678f,
  205. 3.0304576f,1.8666678f,
  206. 2.0834396f,2.245475f,
  207. 1.6414979f,2.6242822f,
  208. 1.3258252f,3.5081657f,
  209. 1.2626907f,0.47770802f,
  210. 0.63134534f,0.035766276f,
  211. 0.063134534f,0.98278429f
  212. };
  213. float32 loop2[] = 
  214. {
  215. 8.270624f,6.1598161f,
  216. 8.270624f,5.3390672f,
  217. 8.7757003f,5.086529f,
  218. 9.4701801f,5.5284708f,
  219. 9.217642f,6.033547f,
  220. 8.7757003f,6.4123542f
  221. };
  222. b2Vec2 b2Loop1[87];
  223. b2Vec2 b2Loop2[6];
  224. for (int32 i = 86; i >= 0; i--) {
  225. b2Loop1[86 - i].Set(loop1[i*2] + 10.0f, loop1[i*2 + 1] + 1.0f);
  226. }
  227. /*for (int32 i = 0; i < 87; i++) {
  228. b2Loop1[i].Set(loop1[i*2] + 10.0f, loop1[i*2 + 1] + 1.0f);
  229. }*/
  230. for (int32 i = 0; i < 6; i++) {
  231. b2Loop2[i].Set(loop2[i*2], loop2[i*2 + 1]);
  232. }
  233. b2BodyDef bd;
  234. bd.position.Set( 0.0f, 0.0f );
  235. b2Body* body = m_world->CreateBody(&bd);
  236. b2CircleDef weight;
  237. weight.filter.maskBits = 0x0000;
  238. weight.density = 4.0f;
  239. weight.radius = 0.5f;
  240. weight.localPosition.Set(8.9f, 5.75f);
  241. body->CreateFixture(&weight);
  242. b2EdgeChainDef edgeDef;
  243. edgeDef.vertexCount = 6;
  244. edgeDef.vertices = b2Loop2;
  245. b2CreateEdgeChain(body, &edgeDef);
  246. body->SetMassFromShapes();
  247. body = m_world->CreateBody(&bd);
  248. weight.radius = 5.0f;
  249. weight.localPosition.Set(20.5f, 7.0f);
  250. body->CreateFixture(&weight);
  251. edgeDef.vertexCount = 87;
  252. edgeDef.vertices = b2Loop1;
  253. b2CreateEdgeChain(body, &edgeDef);
  254. body->SetMassFromShapes();
  255. }
  256. }
  257. static Test* Create()
  258. {
  259. return new DynamicEdges;
  260. }
  261. };
  262. #endif