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.

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