b2EdgeShape.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2006-2010 Erin Catto http://www.box2d.org
  3. * Copyright (c) 2013 Google, Inc.
  4. *
  5. * This software is provided 'as-is', without any express or implied
  6. * warranty. In no event will the authors be held liable for any damages
  7. * arising from the use of this software.
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. * 1. The origin of this software must not be misrepresented; you must not
  12. * claim that you wrote the original software. If you use this software
  13. * in a product, an acknowledgment in the product documentation would be
  14. * appreciated but is not required.
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. * misrepresented as being the original software.
  17. * 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #include <Box2D/Collision/Shapes/b2EdgeShape.h>
  20. #include <new>
  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. void b2EdgeShape::ComputeDistance(const b2Transform& xf, const b2Vec2& p, float32* distance, b2Vec2* normal, int32 childIndex) const
  46. {
  47. B2_NOT_USED(childIndex);
  48. b2Vec2 v1 = b2Mul(xf, m_vertex1);
  49. b2Vec2 v2 = b2Mul(xf, m_vertex2);
  50. b2Vec2 d = p - v1;
  51. b2Vec2 s = v2 - v1;
  52. float32 ds = b2Dot(d, s);
  53. if (ds > 0)
  54. {
  55. float32 s2 = b2Dot(s, s);
  56. if (ds > s2)
  57. {
  58. d = p - v2;
  59. }
  60. else
  61. {
  62. d -= ds / s2 * s;
  63. }
  64. }
  65. float32 d1 = d.Length();
  66. *distance = d1;
  67. *normal = d1 > 0 ? 1 / d1 * d : b2Vec2_zero;
  68. }
  69. // p = p1 + t * d
  70. // v = v1 + s * e
  71. // p1 + t * d = v1 + s * e
  72. // s * e - t * d = p1 - v1
  73. bool b2EdgeShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  74. const b2Transform& xf, int32 childIndex) const
  75. {
  76. B2_NOT_USED(childIndex);
  77. // Put the ray into the edge's frame of reference.
  78. b2Vec2 p1 = b2MulT(xf.q, input.p1 - xf.p);
  79. b2Vec2 p2 = b2MulT(xf.q, input.p2 - xf.p);
  80. b2Vec2 d = p2 - p1;
  81. b2Vec2 v1 = m_vertex1;
  82. b2Vec2 v2 = m_vertex2;
  83. b2Vec2 e = v2 - v1;
  84. b2Vec2 normal(e.y, -e.x);
  85. normal.Normalize();
  86. // q = p1 + t * d
  87. // dot(normal, q - v1) = 0
  88. // dot(normal, p1 - v1) + t * dot(normal, d) = 0
  89. float32 numerator = b2Dot(normal, v1 - p1);
  90. float32 denominator = b2Dot(normal, d);
  91. if (denominator == 0.0f)
  92. {
  93. return false;
  94. }
  95. float32 t = numerator / denominator;
  96. if (t < 0.0f || input.maxFraction < t)
  97. {
  98. return false;
  99. }
  100. b2Vec2 q = p1 + t * d;
  101. // q = v1 + s * r
  102. // s = dot(q - v1, r) / dot(r, r)
  103. b2Vec2 r = v2 - v1;
  104. float32 rr = b2Dot(r, r);
  105. if (rr == 0.0f)
  106. {
  107. return false;
  108. }
  109. float32 s = b2Dot(q - v1, r) / rr;
  110. if (s < 0.0f || 1.0f < s)
  111. {
  112. return false;
  113. }
  114. output->fraction = t;
  115. if (numerator > 0.0f)
  116. {
  117. output->normal = -b2Mul(xf.q, normal);
  118. }
  119. else
  120. {
  121. output->normal = b2Mul(xf.q, normal);
  122. }
  123. return true;
  124. }
  125. void b2EdgeShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
  126. {
  127. B2_NOT_USED(childIndex);
  128. b2Vec2 v1 = b2Mul(xf, m_vertex1);
  129. b2Vec2 v2 = b2Mul(xf, m_vertex2);
  130. b2Vec2 lower = b2Min(v1, v2);
  131. b2Vec2 upper = b2Max(v1, v2);
  132. b2Vec2 r(m_radius, m_radius);
  133. aabb->lowerBound = lower - r;
  134. aabb->upperBound = upper + r;
  135. }
  136. void b2EdgeShape::ComputeMass(b2MassData* massData, float32 density) const
  137. {
  138. B2_NOT_USED(density);
  139. massData->mass = 0.0f;
  140. massData->center = 0.5f * (m_vertex1 + m_vertex2);
  141. massData->I = 0.0f;
  142. }