b2CircleShape.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2006-2009 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/b2CircleShape.h>
  20. #include <new>
  21. b2Shape* b2CircleShape::Clone(b2BlockAllocator* allocator) const
  22. {
  23. void* mem = allocator->Allocate(sizeof(b2CircleShape));
  24. b2CircleShape* clone = new (mem) b2CircleShape;
  25. *clone = *this;
  26. return clone;
  27. }
  28. int32 b2CircleShape::GetChildCount() const
  29. {
  30. return 1;
  31. }
  32. bool b2CircleShape::TestPoint(const b2Transform& transform, const b2Vec2& p) const
  33. {
  34. b2Vec2 center = transform.p + b2Mul(transform.q, m_p);
  35. b2Vec2 d = p - center;
  36. return b2Dot(d, d) <= m_radius * m_radius;
  37. }
  38. void b2CircleShape::ComputeDistance(const b2Transform& transform, const b2Vec2& p, float32* distance, b2Vec2* normal, int32 childIndex) const
  39. {
  40. B2_NOT_USED(childIndex);
  41. b2Vec2 center = transform.p + b2Mul(transform.q, m_p);
  42. b2Vec2 d = p - center;
  43. float32 d1 = d.Length();
  44. *distance = d1 - m_radius;
  45. *normal = 1 / d1 * d;
  46. }
  47. // Collision Detection in Interactive 3D Environments by Gino van den Bergen
  48. // From Section 3.1.2
  49. // x = s + a * r
  50. // norm(x) = radius
  51. bool b2CircleShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  52. const b2Transform& transform, int32 childIndex) const
  53. {
  54. B2_NOT_USED(childIndex);
  55. b2Vec2 position = transform.p + b2Mul(transform.q, m_p);
  56. b2Vec2 s = input.p1 - position;
  57. float32 b = b2Dot(s, s) - m_radius * m_radius;
  58. // Solve quadratic equation.
  59. b2Vec2 r = input.p2 - input.p1;
  60. float32 c = b2Dot(s, r);
  61. float32 rr = b2Dot(r, r);
  62. float32 sigma = c * c - rr * b;
  63. // Check for negative discriminant and short segment.
  64. if (sigma < 0.0f || rr < b2_epsilon)
  65. {
  66. return false;
  67. }
  68. // Find the point of intersection of the line with the circle.
  69. float32 a = -(c + b2Sqrt(sigma));
  70. // Is the intersection point on the segment?
  71. if (0.0f <= a && a <= input.maxFraction * rr)
  72. {
  73. a /= rr;
  74. output->fraction = a;
  75. output->normal = s + a * r;
  76. output->normal.Normalize();
  77. return true;
  78. }
  79. return false;
  80. }
  81. void b2CircleShape::ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const
  82. {
  83. B2_NOT_USED(childIndex);
  84. b2Vec2 p = transform.p + b2Mul(transform.q, m_p);
  85. aabb->lowerBound.Set(p.x - m_radius, p.y - m_radius);
  86. aabb->upperBound.Set(p.x + m_radius, p.y + m_radius);
  87. }
  88. void b2CircleShape::ComputeMass(b2MassData* massData, float32 density) const
  89. {
  90. massData->mass = density * b2_pi * m_radius * m_radius;
  91. massData->center = m_p;
  92. // inertia about the local origin
  93. massData->I = massData->mass * (0.5f * m_radius * m_radius + b2Dot(m_p, m_p));
  94. }