CircleShape.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Box2D.XNA port of Box2D:
  3. * Copyright (c) 2009 Brandon Furtwangler, Nathan Furtwangler
  4. *
  5. * Original source Box2D:
  6. * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
  7. *
  8. * This software is provided 'as-is', without any express or implied
  9. * warranty. In no event will the authors be held liable for any damages
  10. * arising from the use of this software.
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. * 1. The origin of this software must not be misrepresented; you must not
  15. * claim that you wrote the original software. If you use this software
  16. * in a product, an acknowledgment in the product documentation would be
  17. * appreciated but is not required.
  18. * 2. Altered source versions must be plainly marked as such, and must not be
  19. * misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source distribution.
  21. */
  22. using System;
  23. using System.Diagnostics;
  24. using Microsoft.Xna.Framework;
  25. namespace Box2D.XNA
  26. {
  27. public class CircleShape : Shape
  28. {
  29. public CircleShape()
  30. {
  31. ShapeType = ShapeType.Circle;
  32. _radius = 0.0f;
  33. _p = Vector2.Zero;
  34. }
  35. /// Implement Shape.
  36. public override Shape Clone()
  37. {
  38. CircleShape shape = new CircleShape();
  39. shape.ShapeType = ShapeType;
  40. shape._radius = _radius;
  41. shape._p = _p;
  42. return shape;
  43. }
  44. /// @see b2Shape::GetChildCount
  45. public override int GetChildCount()
  46. {
  47. return 1;
  48. }
  49. /// @see Shape.TestPoint
  50. public override bool TestPoint(ref Transform transform, Vector2 p)
  51. {
  52. Vector2 center = transform.Position + MathUtils.Multiply(ref transform.R, _p);
  53. Vector2 d = p - center;
  54. return Vector2.Dot(d, d) <= _radius * _radius;
  55. }
  56. // Collision Detection in Interactive 3D Environments by Gino van den Bergen
  57. // From Section 3.1.2
  58. // x = s + a * r
  59. // norm(x) = radius
  60. public override bool RayCast(out RayCastOutput output, ref RayCastInput input, ref Transform transform, int childIndex)
  61. {
  62. output = new RayCastOutput();
  63. Vector2 position = transform.Position + MathUtils.Multiply(ref transform.R, _p);
  64. Vector2 s = input.p1 - position;
  65. float b = Vector2.Dot(s, s) - _radius * _radius;
  66. // Solve quadratic equation.
  67. Vector2 r = input.p2 - input.p1;
  68. float c = Vector2.Dot(s, r);
  69. float rr = Vector2.Dot(r, r);
  70. float sigma = c * c - rr * b;
  71. // Check for negative discriminant and short segment.
  72. if (sigma < 0.0f || rr < Settings.b2_epsilon)
  73. {
  74. return false;
  75. }
  76. // Find the point of intersection of the line with the circle.
  77. float a = -(c + (float)Math.Sqrt((double)sigma));
  78. // Is the intersection point on the segment?
  79. if (0.0f <= a && a <= input.maxFraction * rr)
  80. {
  81. a /= rr;
  82. output.fraction = a;
  83. Vector2 norm = (s + a * r);
  84. norm.Normalize();
  85. output.normal = norm;
  86. return true;
  87. }
  88. return false;
  89. }
  90. /// @see Shape.ComputeAABB
  91. public override void ComputeAABB(out AABB aabb, ref Transform transform, int childIndex)
  92. {
  93. Vector2 p = transform.Position + MathUtils.Multiply(ref transform.R, _p);
  94. aabb.lowerBound = new Vector2(p.X - _radius, p.Y - _radius);
  95. aabb.upperBound = new Vector2(p.X + _radius, p.Y + _radius);
  96. }
  97. /// @see Shape.ComputeMass
  98. public override void ComputeMass(out MassData massData, float density)
  99. {
  100. massData.mass = density * Settings.b2_pi * _radius * _radius;
  101. massData.center = _p;
  102. // inertia about the local origin
  103. massData.I = massData.mass * (0.5f * _radius * _radius + Vector2.Dot(_p, _p));
  104. }
  105. /// Get the vertex count.
  106. public int GetVertexCount() { return 1; }
  107. /// Get a vertex by index. Used by b2Distance.
  108. public Vector2 GetVertex(int index)
  109. {
  110. Debug.Assert(index == 0);
  111. return _p;
  112. }
  113. /// Position
  114. public Vector2 _p;
  115. }
  116. }