The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

441 lines
12KB

  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  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 RAY_CAST_H
  19. #define RAY_CAST_H
  20. // This test demonstrates how to use the world ray-cast feature.
  21. // NOTE: we are intentionally filtering one of the polygons, therefore
  22. // the ray will always miss one type of polygon.
  23. // This callback finds the closest hit. Polygon 0 is filtered.
  24. class RayCastClosestCallback : public b2RayCastCallback
  25. {
  26. public:
  27. RayCastClosestCallback()
  28. {
  29. m_hit = false;
  30. }
  31. float32 ReportFixture( b2Fixture* fixture, const b2Vec2& point,
  32. const b2Vec2& normal, float32 fraction)
  33. {
  34. b2Body* body = fixture->GetBody();
  35. void* userData = body->GetUserData();
  36. if (userData)
  37. {
  38. int32 index = *(int32*)userData;
  39. if (index == 0)
  40. {
  41. // filter
  42. return -1.0f;
  43. }
  44. }
  45. m_hit = true;
  46. m_point = point;
  47. m_normal = normal;
  48. return fraction;
  49. }
  50. bool m_hit;
  51. b2Vec2 m_point;
  52. b2Vec2 m_normal;
  53. };
  54. // This callback finds any hit. Polygon 0 is filtered.
  55. class RayCastAnyCallback : public b2RayCastCallback
  56. {
  57. public:
  58. RayCastAnyCallback()
  59. {
  60. m_hit = false;
  61. }
  62. float32 ReportFixture( b2Fixture* fixture, const b2Vec2& point,
  63. const b2Vec2& normal, float32 fraction)
  64. {
  65. b2Body* body = fixture->GetBody();
  66. void* userData = body->GetUserData();
  67. if (userData)
  68. {
  69. int32 index = *(int32*)userData;
  70. if (index == 0)
  71. {
  72. // filter
  73. return -1.0f;
  74. }
  75. }
  76. m_hit = true;
  77. m_point = point;
  78. m_normal = normal;
  79. return 0.0f;
  80. }
  81. bool m_hit;
  82. b2Vec2 m_point;
  83. b2Vec2 m_normal;
  84. };
  85. // This ray cast collects multiple hits along the ray. Polygon 0 is filtered.
  86. class RayCastMultipleCallback : public b2RayCastCallback
  87. {
  88. public:
  89. enum
  90. {
  91. e_maxCount = 3
  92. };
  93. RayCastMultipleCallback()
  94. {
  95. m_count = 0;
  96. }
  97. float32 ReportFixture( b2Fixture* fixture, const b2Vec2& point,
  98. const b2Vec2& normal, float32 fraction)
  99. {
  100. b2Body* body = fixture->GetBody();
  101. void* userData = body->GetUserData();
  102. if (userData)
  103. {
  104. int32 index = *(int32*)userData;
  105. if (index == 0)
  106. {
  107. // filter
  108. return -1.0f;
  109. }
  110. }
  111. b2Assert(m_count < e_maxCount);
  112. m_points[m_count] = point;
  113. m_normals[m_count] = normal;
  114. ++m_count;
  115. if (m_count == e_maxCount)
  116. {
  117. return 0.0f;
  118. }
  119. return 1.0f;
  120. }
  121. b2Vec2 m_points[e_maxCount];
  122. b2Vec2 m_normals[e_maxCount];
  123. int32 m_count;
  124. };
  125. class RayCast : public Test
  126. {
  127. public:
  128. enum
  129. {
  130. e_maxBodies = 256
  131. };
  132. enum Mode
  133. {
  134. e_closest,
  135. e_any,
  136. e_multiple
  137. };
  138. RayCast()
  139. {
  140. // Ground body
  141. {
  142. b2BodyDef bd;
  143. b2Body* ground = m_world->CreateBody(&bd);
  144. b2EdgeShape shape;
  145. shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
  146. ground->CreateFixture(&shape, 0.0f);
  147. }
  148. {
  149. b2Vec2 vertices[3];
  150. vertices[0].Set(-0.5f, 0.0f);
  151. vertices[1].Set(0.5f, 0.0f);
  152. vertices[2].Set(0.0f, 1.5f);
  153. m_polygons[0].Set(vertices, 3);
  154. }
  155. {
  156. b2Vec2 vertices[3];
  157. vertices[0].Set(-0.1f, 0.0f);
  158. vertices[1].Set(0.1f, 0.0f);
  159. vertices[2].Set(0.0f, 1.5f);
  160. m_polygons[1].Set(vertices, 3);
  161. }
  162. {
  163. float32 w = 1.0f;
  164. float32 b = w / (2.0f + b2Sqrt(2.0f));
  165. float32 s = b2Sqrt(2.0f) * b;
  166. b2Vec2 vertices[8];
  167. vertices[0].Set(0.5f * s, 0.0f);
  168. vertices[1].Set(0.5f * w, b);
  169. vertices[2].Set(0.5f * w, b + s);
  170. vertices[3].Set(0.5f * s, w);
  171. vertices[4].Set(-0.5f * s, w);
  172. vertices[5].Set(-0.5f * w, b + s);
  173. vertices[6].Set(-0.5f * w, b);
  174. vertices[7].Set(-0.5f * s, 0.0f);
  175. m_polygons[2].Set(vertices, 8);
  176. }
  177. {
  178. m_polygons[3].SetAsBox(0.5f, 0.5f);
  179. }
  180. {
  181. m_circle.m_radius = 0.5f;
  182. }
  183. m_bodyIndex = 0;
  184. memset(m_bodies, 0, sizeof(m_bodies));
  185. m_angle = 0.0f;
  186. m_mode = e_closest;
  187. }
  188. void Create(int32 index)
  189. {
  190. if (m_bodies[m_bodyIndex] != NULL)
  191. {
  192. m_world->DestroyBody(m_bodies[m_bodyIndex]);
  193. m_bodies[m_bodyIndex] = NULL;
  194. }
  195. b2BodyDef bd;
  196. float32 x = RandomFloat(-10.0f, 10.0f);
  197. float32 y = RandomFloat(0.0f, 20.0f);
  198. bd.position.Set(x, y);
  199. bd.angle = RandomFloat(-b2_pi, b2_pi);
  200. m_userData[m_bodyIndex] = index;
  201. bd.userData = m_userData + m_bodyIndex;
  202. if (index == 4)
  203. {
  204. bd.angularDamping = 0.02f;
  205. }
  206. m_bodies[m_bodyIndex] = m_world->CreateBody(&bd);
  207. if (index < 4)
  208. {
  209. b2FixtureDef fd;
  210. fd.shape = m_polygons + index;
  211. fd.friction = 0.3f;
  212. m_bodies[m_bodyIndex]->CreateFixture(&fd);
  213. }
  214. else
  215. {
  216. b2FixtureDef fd;
  217. fd.shape = &m_circle;
  218. fd.friction = 0.3f;
  219. m_bodies[m_bodyIndex]->CreateFixture(&fd);
  220. }
  221. m_bodyIndex = (m_bodyIndex + 1) % e_maxBodies;
  222. }
  223. void DestroyBody()
  224. {
  225. for (int32 i = 0; i < e_maxBodies; ++i)
  226. {
  227. if (m_bodies[i] != NULL)
  228. {
  229. m_world->DestroyBody(m_bodies[i]);
  230. m_bodies[i] = NULL;
  231. return;
  232. }
  233. }
  234. }
  235. void Keyboard(unsigned char key)
  236. {
  237. switch (key)
  238. {
  239. case '1':
  240. case '2':
  241. case '3':
  242. case '4':
  243. case '5':
  244. Create(key - '1');
  245. break;
  246. case 'd':
  247. DestroyBody();
  248. break;
  249. case 'm':
  250. if (m_mode == e_closest)
  251. {
  252. m_mode = e_any;
  253. }
  254. else if (m_mode == e_any)
  255. {
  256. m_mode = e_multiple;
  257. }
  258. else if (m_mode == e_multiple)
  259. {
  260. m_mode = e_closest;
  261. }
  262. }
  263. }
  264. void Step(Settings* settings)
  265. {
  266. bool advanceRay = settings->pause == 0 || settings->singleStep;
  267. Test::Step(settings);
  268. m_debugDraw.DrawString(5, m_textLine, "Press 1-5 to drop stuff, m to change the mode");
  269. m_textLine += 15;
  270. m_debugDraw.DrawString(5, m_textLine, "Mode = %d", m_mode);
  271. m_textLine += 15;
  272. float32 L = 11.0f;
  273. b2Vec2 point1(0.0f, 10.0f);
  274. b2Vec2 d(L * cosf(m_angle), L * sinf(m_angle));
  275. b2Vec2 point2 = point1 + d;
  276. if (m_mode == e_closest)
  277. {
  278. RayCastClosestCallback callback;
  279. m_world->RayCast(&callback, point1, point2);
  280. if (callback.m_hit)
  281. {
  282. m_debugDraw.DrawPoint(callback.m_point, 5.0f, b2Color(0.4f, 0.9f, 0.4f));
  283. m_debugDraw.DrawSegment(point1, callback.m_point, b2Color(0.8f, 0.8f, 0.8f));
  284. b2Vec2 head = callback.m_point + 0.5f * callback.m_normal;
  285. m_debugDraw.DrawSegment(callback.m_point, head, b2Color(0.9f, 0.9f, 0.4f));
  286. }
  287. else
  288. {
  289. m_debugDraw.DrawSegment(point1, point2, b2Color(0.8f, 0.8f, 0.8f));
  290. }
  291. }
  292. else if (m_mode == e_any)
  293. {
  294. RayCastAnyCallback callback;
  295. m_world->RayCast(&callback, point1, point2);
  296. if (callback.m_hit)
  297. {
  298. m_debugDraw.DrawPoint(callback.m_point, 5.0f, b2Color(0.4f, 0.9f, 0.4f));
  299. m_debugDraw.DrawSegment(point1, callback.m_point, b2Color(0.8f, 0.8f, 0.8f));
  300. b2Vec2 head = callback.m_point + 0.5f * callback.m_normal;
  301. m_debugDraw.DrawSegment(callback.m_point, head, b2Color(0.9f, 0.9f, 0.4f));
  302. }
  303. else
  304. {
  305. m_debugDraw.DrawSegment(point1, point2, b2Color(0.8f, 0.8f, 0.8f));
  306. }
  307. }
  308. else if (m_mode == e_multiple)
  309. {
  310. RayCastMultipleCallback callback;
  311. m_world->RayCast(&callback, point1, point2);
  312. m_debugDraw.DrawSegment(point1, point2, b2Color(0.8f, 0.8f, 0.8f));
  313. for (int32 i = 0; i < callback.m_count; ++i)
  314. {
  315. b2Vec2 p = callback.m_points[i];
  316. b2Vec2 n = callback.m_normals[i];
  317. m_debugDraw.DrawPoint(p, 5.0f, b2Color(0.4f, 0.9f, 0.4f));
  318. m_debugDraw.DrawSegment(point1, p, b2Color(0.8f, 0.8f, 0.8f));
  319. b2Vec2 head = p + 0.5f * n;
  320. m_debugDraw.DrawSegment(p, head, b2Color(0.9f, 0.9f, 0.4f));
  321. }
  322. }
  323. if (advanceRay)
  324. {
  325. m_angle += 0.25f * b2_pi / 180.0f;
  326. }
  327. #if 0
  328. // This case was failing.
  329. {
  330. b2Vec2 vertices[4];
  331. //vertices[0].Set(-22.875f, -3.0f);
  332. //vertices[1].Set(22.875f, -3.0f);
  333. //vertices[2].Set(22.875f, 3.0f);
  334. //vertices[3].Set(-22.875f, 3.0f);
  335. b2PolygonShape shape;
  336. //shape.Set(vertices, 4);
  337. shape.SetAsBox(22.875f, 3.0f);
  338. b2RayCastInput input;
  339. input.p1.Set(10.2725f,1.71372f);
  340. input.p2.Set(10.2353f,2.21807f);
  341. //input.maxFraction = 0.567623f;
  342. input.maxFraction = 0.56762173f;
  343. b2Transform xf;
  344. xf.SetIdentity();
  345. xf.position.Set(23.0f, 5.0f);
  346. b2RayCastOutput output;
  347. bool hit;
  348. hit = shape.RayCast(&output, input, xf);
  349. hit = false;
  350. b2Color color(1.0f, 1.0f, 1.0f);
  351. b2Vec2 vs[4];
  352. for (int32 i = 0; i < 4; ++i)
  353. {
  354. vs[i] = b2Mul(xf, shape.m_vertices[i]);
  355. }
  356. m_debugDraw.DrawPolygon(vs, 4, color);
  357. m_debugDraw.DrawSegment(input.p1, input.p2, color);
  358. }
  359. #endif
  360. }
  361. static Test* Create()
  362. {
  363. return new RayCast;
  364. }
  365. int32 m_bodyIndex;
  366. b2Body* m_bodies[e_maxBodies];
  367. int32 m_userData[e_maxBodies];
  368. b2PolygonShape m_polygons[4];
  369. b2CircleShape m_circle;
  370. float32 m_angle;
  371. Mode m_mode;
  372. };
  373. #endif