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.

140 lines
3.4KB

  1. /*
  2. * Copyright (c) 2006-2010 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 "b2EdgeShape.h"
  19. #include <new>
  20. using namespace std;
  21. void b2EdgeShape::Set(const b2Vec2& v1, const b2Vec2& v2)
  22. {
  23. m_vertex1 = v1;
  24. m_vertex2 = v2;
  25. m_hasVertex0 = false;
  26. m_hasVertex3 = false;
  27. }
  28. b2Shape* b2EdgeShape::Clone(b2BlockAllocator* allocator) const
  29. {
  30. void* mem = allocator->Allocate(sizeof(b2EdgeShape));
  31. b2EdgeShape* clone = new (mem) b2EdgeShape;
  32. *clone = *this;
  33. return clone;
  34. }
  35. int32 b2EdgeShape::GetChildCount() const
  36. {
  37. return 1;
  38. }
  39. bool b2EdgeShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const
  40. {
  41. B2_NOT_USED(xf);
  42. B2_NOT_USED(p);
  43. return false;
  44. }
  45. // p = p1 + t * d
  46. // v = v1 + s * e
  47. // p1 + t * d = v1 + s * e
  48. // s * e - t * d = p1 - v1
  49. bool b2EdgeShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  50. const b2Transform& xf, int32 childIndex) const
  51. {
  52. B2_NOT_USED(childIndex);
  53. // Put the ray into the edge's frame of reference.
  54. b2Vec2 p1 = b2MulT(xf.q, input.p1 - xf.p);
  55. b2Vec2 p2 = b2MulT(xf.q, input.p2 - xf.p);
  56. b2Vec2 d = p2 - p1;
  57. b2Vec2 v1 = m_vertex1;
  58. b2Vec2 v2 = m_vertex2;
  59. b2Vec2 e = v2 - v1;
  60. b2Vec2 normal(e.y, -e.x);
  61. normal.Normalize();
  62. // q = p1 + t * d
  63. // dot(normal, q - v1) = 0
  64. // dot(normal, p1 - v1) + t * dot(normal, d) = 0
  65. float32 numerator = b2Dot(normal, v1 - p1);
  66. float32 denominator = b2Dot(normal, d);
  67. if (denominator == 0.0f)
  68. {
  69. return false;
  70. }
  71. float32 t = numerator / denominator;
  72. if (t < 0.0f || input.maxFraction < t)
  73. {
  74. return false;
  75. }
  76. b2Vec2 q = p1 + t * d;
  77. // q = v1 + s * r
  78. // s = dot(q - v1, r) / dot(r, r)
  79. b2Vec2 r = v2 - v1;
  80. float32 rr = b2Dot(r, r);
  81. if (rr == 0.0f)
  82. {
  83. return false;
  84. }
  85. float32 s = b2Dot(q - v1, r) / rr;
  86. if (s < 0.0f || 1.0f < s)
  87. {
  88. return false;
  89. }
  90. output->fraction = t;
  91. if (numerator > 0.0f)
  92. {
  93. output->normal = -normal;
  94. }
  95. else
  96. {
  97. output->normal = normal;
  98. }
  99. return true;
  100. }
  101. void b2EdgeShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
  102. {
  103. B2_NOT_USED(childIndex);
  104. b2Vec2 v1 = b2Mul(xf, m_vertex1);
  105. b2Vec2 v2 = b2Mul(xf, m_vertex2);
  106. b2Vec2 lower = b2Min(v1, v2);
  107. b2Vec2 upper = b2Max(v1, v2);
  108. b2Vec2 r(m_radius, m_radius);
  109. aabb->lowerBound = lower - r;
  110. aabb->upperBound = upper + r;
  111. }
  112. void b2EdgeShape::ComputeMass(b2MassData* massData, float32 density) const
  113. {
  114. B2_NOT_USED(density);
  115. massData->mass = 0.0f;
  116. massData->center = 0.5f * (m_vertex1 + m_vertex2);
  117. massData->I = 0.0f;
  118. }