b2EdgeShape.cpp 3.2 KB

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