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.

361 lines
9.6KB

  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. #include "b2PolygonShape.h"
  19. b2Shape* b2PolygonShape::Clone(b2BlockAllocator* allocator) const
  20. {
  21. void* mem = allocator->Allocate(sizeof(b2PolygonShape));
  22. b2PolygonShape* clone = new (mem) b2PolygonShape;
  23. *clone = *this;
  24. return clone;
  25. }
  26. void b2PolygonShape::SetAsBox(float32 hx, float32 hy)
  27. {
  28. m_vertexCount = 4;
  29. m_vertices[0].Set(-hx, -hy);
  30. m_vertices[1].Set( hx, -hy);
  31. m_vertices[2].Set( hx, hy);
  32. m_vertices[3].Set(-hx, hy);
  33. m_normals[0].Set(0.0f, -1.0f);
  34. m_normals[1].Set(1.0f, 0.0f);
  35. m_normals[2].Set(0.0f, 1.0f);
  36. m_normals[3].Set(-1.0f, 0.0f);
  37. m_centroid.SetZero();
  38. }
  39. void b2PolygonShape::SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle)
  40. {
  41. m_vertexCount = 4;
  42. m_vertices[0].Set(-hx, -hy);
  43. m_vertices[1].Set( hx, -hy);
  44. m_vertices[2].Set( hx, hy);
  45. m_vertices[3].Set(-hx, hy);
  46. m_normals[0].Set(0.0f, -1.0f);
  47. m_normals[1].Set(1.0f, 0.0f);
  48. m_normals[2].Set(0.0f, 1.0f);
  49. m_normals[3].Set(-1.0f, 0.0f);
  50. m_centroid = center;
  51. b2Transform xf;
  52. xf.p = center;
  53. xf.q.Set(angle);
  54. // Transform vertices and normals.
  55. for (int32 i = 0; i < m_vertexCount; ++i)
  56. {
  57. m_vertices[i] = b2Mul(xf, m_vertices[i]);
  58. m_normals[i] = b2Mul(xf.q, m_normals[i]);
  59. }
  60. }
  61. int32 b2PolygonShape::GetChildCount() const
  62. {
  63. return 1;
  64. }
  65. static b2Vec2 ComputeCentroid(const b2Vec2* vs, int32 count)
  66. {
  67. b2Assert(count >= 3);
  68. b2Vec2 c; c.Set(0.0f, 0.0f);
  69. float32 area = 0.0f;
  70. // pRef is the reference point for forming triangles.
  71. // It's location doesn't change the result (except for rounding error).
  72. b2Vec2 pRef(0.0f, 0.0f);
  73. #if 0
  74. // This code would put the reference point inside the polygon.
  75. for (int32 i = 0; i < count; ++i)
  76. {
  77. pRef += vs[i];
  78. }
  79. pRef *= 1.0f / count;
  80. #endif
  81. const float32 inv3 = 1.0f / 3.0f;
  82. for (int32 i = 0; i < count; ++i)
  83. {
  84. // Triangle vertices.
  85. b2Vec2 p1 = pRef;
  86. b2Vec2 p2 = vs[i];
  87. b2Vec2 p3 = i + 1 < count ? vs[i+1] : vs[0];
  88. b2Vec2 e1 = p2 - p1;
  89. b2Vec2 e2 = p3 - p1;
  90. float32 D = b2Cross(e1, e2);
  91. float32 triangleArea = 0.5f * D;
  92. area += triangleArea;
  93. // Area weighted centroid
  94. c += triangleArea * inv3 * (p1 + p2 + p3);
  95. }
  96. // Centroid
  97. b2Assert(area > b2_epsilon);
  98. c *= 1.0f / area;
  99. return c;
  100. }
  101. void b2PolygonShape::Set(const b2Vec2* vertices, int32 count)
  102. {
  103. b2Assert(3 <= count && count <= b2_maxPolygonVertices);
  104. m_vertexCount = count;
  105. // Copy vertices.
  106. for (int32 i = 0; i < m_vertexCount; ++i)
  107. {
  108. m_vertices[i] = vertices[i];
  109. }
  110. // Compute normals. Ensure the edges have non-zero length.
  111. for (int32 i = 0; i < m_vertexCount; ++i)
  112. {
  113. int32 i1 = i;
  114. int32 i2 = i + 1 < m_vertexCount ? i + 1 : 0;
  115. b2Vec2 edge = m_vertices[i2] - m_vertices[i1];
  116. b2Assert(edge.LengthSquared() > b2_epsilon * b2_epsilon);
  117. m_normals[i] = b2Cross(edge, 1.0f);
  118. m_normals[i].Normalize();
  119. }
  120. #ifdef _DEBUG
  121. // Ensure the polygon is convex and the interior
  122. // is to the left of each edge.
  123. for (int32 i = 0; i < m_vertexCount; ++i)
  124. {
  125. int32 i1 = i;
  126. int32 i2 = i + 1 < m_vertexCount ? i + 1 : 0;
  127. b2Vec2 edge = m_vertices[i2] - m_vertices[i1];
  128. for (int32 j = 0; j < m_vertexCount; ++j)
  129. {
  130. // Don't check vertices on the current edge.
  131. if (j == i1 || j == i2)
  132. {
  133. continue;
  134. }
  135. b2Vec2 r = m_vertices[j] - m_vertices[i1];
  136. // If this crashes, your polygon is non-convex, has colinear edges,
  137. // or the winding order is wrong.
  138. float32 s = b2Cross(edge, r);
  139. b2Assert(s > 0.0f && "ERROR: Please ensure your polygon is convex and has a CCW winding order");
  140. }
  141. }
  142. #endif
  143. // Compute the polygon centroid.
  144. m_centroid = ComputeCentroid(m_vertices, m_vertexCount);
  145. }
  146. bool b2PolygonShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const
  147. {
  148. b2Vec2 pLocal = b2MulT(xf.q, p - xf.p);
  149. for (int32 i = 0; i < m_vertexCount; ++i)
  150. {
  151. float32 dot = b2Dot(m_normals[i], pLocal - m_vertices[i]);
  152. if (dot > 0.0f)
  153. {
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. bool b2PolygonShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  160. const b2Transform& xf, int32 childIndex) const
  161. {
  162. B2_NOT_USED(childIndex);
  163. // Put the ray into the polygon's frame of reference.
  164. b2Vec2 p1 = b2MulT(xf.q, input.p1 - xf.p);
  165. b2Vec2 p2 = b2MulT(xf.q, input.p2 - xf.p);
  166. b2Vec2 d = p2 - p1;
  167. float32 lower = 0.0f, upper = input.maxFraction;
  168. int32 index = -1;
  169. for (int32 i = 0; i < m_vertexCount; ++i)
  170. {
  171. // p = p1 + a * d
  172. // dot(normal, p - v) = 0
  173. // dot(normal, p1 - v) + a * dot(normal, d) = 0
  174. float32 numerator = b2Dot(m_normals[i], m_vertices[i] - p1);
  175. float32 denominator = b2Dot(m_normals[i], d);
  176. if (denominator == 0.0f)
  177. {
  178. if (numerator < 0.0f)
  179. {
  180. return false;
  181. }
  182. }
  183. else
  184. {
  185. // Note: we want this predicate without division:
  186. // lower < numerator / denominator, where denominator < 0
  187. // Since denominator < 0, we have to flip the inequality:
  188. // lower < numerator / denominator <==> denominator * lower > numerator.
  189. if (denominator < 0.0f && numerator < lower * denominator)
  190. {
  191. // Increase lower.
  192. // The segment enters this half-space.
  193. lower = numerator / denominator;
  194. index = i;
  195. }
  196. else if (denominator > 0.0f && numerator < upper * denominator)
  197. {
  198. // Decrease upper.
  199. // The segment exits this half-space.
  200. upper = numerator / denominator;
  201. }
  202. }
  203. // The use of epsilon here causes the assert on lower to trip
  204. // in some cases. Apparently the use of epsilon was to make edge
  205. // shapes work, but now those are handled separately.
  206. //if (upper < lower - b2_epsilon)
  207. if (upper < lower)
  208. {
  209. return false;
  210. }
  211. }
  212. b2Assert(0.0f <= lower && lower <= input.maxFraction);
  213. if (index >= 0)
  214. {
  215. output->fraction = lower;
  216. output->normal = b2Mul(xf.q, m_normals[index]);
  217. return true;
  218. }
  219. return false;
  220. }
  221. void b2PolygonShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
  222. {
  223. B2_NOT_USED(childIndex);
  224. b2Vec2 lower = b2Mul(xf, m_vertices[0]);
  225. b2Vec2 upper = lower;
  226. for (int32 i = 1; i < m_vertexCount; ++i)
  227. {
  228. b2Vec2 v = b2Mul(xf, m_vertices[i]);
  229. lower = b2Min(lower, v);
  230. upper = b2Max(upper, v);
  231. }
  232. b2Vec2 r(m_radius, m_radius);
  233. aabb->lowerBound = lower - r;
  234. aabb->upperBound = upper + r;
  235. }
  236. void b2PolygonShape::ComputeMass(b2MassData* massData, float32 density) const
  237. {
  238. // Polygon mass, centroid, and inertia.
  239. // Let rho be the polygon density in mass per unit area.
  240. // Then:
  241. // mass = rho * int(dA)
  242. // centroid.x = (1/mass) * rho * int(x * dA)
  243. // centroid.y = (1/mass) * rho * int(y * dA)
  244. // I = rho * int((x*x + y*y) * dA)
  245. //
  246. // We can compute these integrals by summing all the integrals
  247. // for each triangle of the polygon. To evaluate the integral
  248. // for a single triangle, we make a change of variables to
  249. // the (u,v) coordinates of the triangle:
  250. // x = x0 + e1x * u + e2x * v
  251. // y = y0 + e1y * u + e2y * v
  252. // where 0 <= u && 0 <= v && u + v <= 1.
  253. //
  254. // We integrate u from [0,1-v] and then v from [0,1].
  255. // We also need to use the Jacobian of the transformation:
  256. // D = cross(e1, e2)
  257. //
  258. // Simplification: triangle centroid = (1/3) * (p1 + p2 + p3)
  259. //
  260. // The rest of the derivation is handled by computer algebra.
  261. b2Assert(m_vertexCount >= 3);
  262. b2Vec2 center; center.Set(0.0f, 0.0f);
  263. float32 area = 0.0f;
  264. float32 I = 0.0f;
  265. // s is the reference point for forming triangles.
  266. // It's location doesn't change the result (except for rounding error).
  267. b2Vec2 s(0.0f, 0.0f);
  268. // This code would put the reference point inside the polygon.
  269. for (int32 i = 0; i < m_vertexCount; ++i)
  270. {
  271. s += m_vertices[i];
  272. }
  273. s *= 1.0f / m_vertexCount;
  274. const float32 k_inv3 = 1.0f / 3.0f;
  275. for (int32 i = 0; i < m_vertexCount; ++i)
  276. {
  277. // Triangle vertices.
  278. b2Vec2 e1 = m_vertices[i] - s;
  279. b2Vec2 e2 = i + 1 < m_vertexCount ? m_vertices[i+1] - s : m_vertices[0] - s;
  280. float32 D = b2Cross(e1, e2);
  281. float32 triangleArea = 0.5f * D;
  282. area += triangleArea;
  283. // Area weighted centroid
  284. center += triangleArea * k_inv3 * (e1 + e2);
  285. float32 ex1 = e1.x, ey1 = e1.y;
  286. float32 ex2 = e2.x, ey2 = e2.y;
  287. float32 intx2 = ex1*ex1 + ex2*ex1 + ex2*ex2;
  288. float32 inty2 = ey1*ey1 + ey2*ey1 + ey2*ey2;
  289. I += (0.25f * k_inv3 * D) * (intx2 + inty2);
  290. }
  291. // Total mass
  292. massData->mass = density * area;
  293. // Center of mass
  294. b2Assert(area > b2_epsilon);
  295. center *= 1.0f / area;
  296. massData->center = center + s;
  297. // Inertia tensor relative to the local origin (point s).
  298. massData->I = density * I;
  299. // Shift to center of mass then to original body origin.
  300. massData->I += massData->mass * (b2Dot(massData->center, massData->center) - b2Dot(center, center));
  301. }