b2_circle_shape.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #include "box2d/b2_circle_shape.h"
  19. #include "box2d/b2_block_allocator.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. // Collision Detection in Interactive 3D Environments by Gino van den Bergen
  39. // From Section 3.1.2
  40. // x = s + a * r
  41. // norm(x) = radius
  42. bool b2CircleShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  43. const b2Transform& transform, int32 childIndex) const
  44. {
  45. B2_NOT_USED(childIndex);
  46. b2Vec2 position = transform.p + b2Mul(transform.q, m_p);
  47. b2Vec2 s = input.p1 - position;
  48. float b = b2Dot(s, s) - m_radius * m_radius;
  49. // Solve quadratic equation.
  50. b2Vec2 r = input.p2 - input.p1;
  51. float c = b2Dot(s, r);
  52. float rr = b2Dot(r, r);
  53. float sigma = c * c - rr * b;
  54. // Check for negative discriminant and short segment.
  55. if (sigma < 0.0f || rr < b2_epsilon)
  56. {
  57. return false;
  58. }
  59. // Find the point of intersection of the line with the circle.
  60. float a = -(c + b2Sqrt(sigma));
  61. // Is the intersection point on the segment?
  62. if (0.0f <= a && a <= input.maxFraction * rr)
  63. {
  64. a /= rr;
  65. output->fraction = a;
  66. output->normal = s + a * r;
  67. output->normal.Normalize();
  68. return true;
  69. }
  70. return false;
  71. }
  72. void b2CircleShape::ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const
  73. {
  74. B2_NOT_USED(childIndex);
  75. b2Vec2 p = transform.p + b2Mul(transform.q, m_p);
  76. aabb->lowerBound.Set(p.x - m_radius, p.y - m_radius);
  77. aabb->upperBound.Set(p.x + m_radius, p.y + m_radius);
  78. }
  79. void b2CircleShape::ComputeMass(b2MassData* massData, float density) const
  80. {
  81. massData->mass = density * b2_pi * m_radius * m_radius;
  82. massData->center = m_p;
  83. // inertia about the local origin
  84. massData->I = massData->mass * (0.5f * m_radius * m_radius + b2Dot(m_p, m_p));
  85. }