LoopShape.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Microsoft.Xna.Framework;
  23. using System.Diagnostics;
  24. namespace Box2D.XNA
  25. {
  26. /// A loop Shape is a free form sequence of line segments that form a circular list.
  27. /// The loop may cross upon itself, but this is not recommended for smooth collision.
  28. /// The loop has double sided collision, so you can use inside and outside collision.
  29. /// Therefore, you may use any winding order.
  30. public class LoopShape : Shape
  31. {
  32. public LoopShape()
  33. {
  34. ShapeType = ShapeType.Loop;
  35. _radius = Settings.b2_polygonRadius;
  36. _vertices = null;
  37. _count = 0;
  38. }
  39. /// Implement Shape.
  40. public override Shape Clone()
  41. {
  42. var loop = new LoopShape();
  43. loop._count = _count;
  44. loop._radius = _radius;
  45. loop._vertices = (Vector2[])_vertices.Clone();
  46. return loop;
  47. }
  48. /// @see Shape::GetChildCount
  49. public override int GetChildCount()
  50. {
  51. return _count;
  52. }
  53. /// Get a child edge.
  54. public void GetChildEdge(ref EdgeShape edge, int index)
  55. {
  56. Debug.Assert(2 <= _count);
  57. Debug.Assert(0 <= index && index < _count);
  58. edge.ShapeType = ShapeType.Edge;
  59. edge._radius = _radius;
  60. edge._hasVertex0 = true;
  61. edge._hasVertex3 = true;
  62. int i0 = index - 1 >= 0 ? index - 1 : _count - 1;
  63. int i1 = index;
  64. int i2 = index + 1 < _count ? index + 1 : 0;
  65. int i3 = index + 2;
  66. while (i3 >= _count)
  67. {
  68. i3 -= _count;
  69. }
  70. edge._vertex0 = _vertices[i0];
  71. edge._vertex1 = _vertices[i1];
  72. edge._vertex2 = _vertices[i2];
  73. edge._vertex3 = _vertices[i3];
  74. }
  75. /// This always return false.
  76. /// @see Shape::TestPoint
  77. public override bool TestPoint(ref Transform transform, Vector2 p)
  78. {
  79. return false;
  80. }
  81. /// Implement Shape.
  82. public override bool RayCast(out RayCastOutput output, ref RayCastInput input,
  83. ref Transform transform, int childIndex)
  84. {
  85. Debug.Assert(childIndex < _count);
  86. int i1 = childIndex;
  87. int i2 = childIndex + 1;
  88. if (i2 == _count)
  89. {
  90. i2 = 0;
  91. }
  92. s_edgeShape._vertex1 = _vertices[i1];
  93. s_edgeShape._vertex2 = _vertices[i2];
  94. return s_edgeShape.RayCast(out output, ref input, ref transform, 0);
  95. }
  96. /// @see Shape::ComputeAABB
  97. public override void ComputeAABB(out AABB aabb, ref Transform transform, int childIndex)
  98. {
  99. Debug.Assert(childIndex < _count);
  100. aabb = new AABB();
  101. int i1 = childIndex;
  102. int i2 = childIndex + 1;
  103. if (i2 == _count)
  104. {
  105. i2 = 0;
  106. }
  107. Vector2 v1 = MathUtils.Multiply(ref transform, _vertices[i1]);
  108. Vector2 v2 = MathUtils.Multiply(ref transform, _vertices[i2]);
  109. aabb.lowerBound = Vector2.Min(v1, v2);
  110. aabb.upperBound = Vector2.Max(v1, v2);
  111. }
  112. /// Chains have zero mass.
  113. /// @see Shape::ComputeMass
  114. public override void ComputeMass(out MassData massData, float density)
  115. {
  116. massData = new MassData();
  117. massData.mass = 0.0f;
  118. massData.center = Vector2.Zero;
  119. massData.I = 0.0f;
  120. }
  121. /// The vertices. These are not owned/freed by the loop Shape.
  122. public Vector2[] _vertices;
  123. /// The vertex count.
  124. public int _count;
  125. public static EdgeShape s_edgeShape = new EdgeShape();
  126. }
  127. }