GravityController.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using FarseerPhysics.Dynamics;
  4. using Microsoft.Xna.Framework;
  5. namespace FarseerPhysics.Controllers
  6. {
  7. public enum GravityType
  8. {
  9. Linear,
  10. DistanceSquared
  11. }
  12. public class GravityController : Controller
  13. {
  14. public List<Body> Bodies = new List<Body>();
  15. public List<Vector2> Points = new List<Vector2>();
  16. public GravityController(float strength)
  17. : base(ControllerType.GravityController)
  18. {
  19. Strength = strength;
  20. MaxRadius = float.MaxValue;
  21. }
  22. public GravityController(float strength, float maxRadius, float minRadius)
  23. : base(ControllerType.GravityController)
  24. {
  25. MinRadius = minRadius;
  26. MaxRadius = maxRadius;
  27. Strength = strength;
  28. }
  29. public float MinRadius { get; set; }
  30. public float MaxRadius { get; set; }
  31. public float Strength { get; set; }
  32. public GravityType GravityType { get; set; }
  33. public override void Update(float dt)
  34. {
  35. Vector2 f = Vector2.Zero;
  36. foreach (Body body1 in World.BodyList)
  37. {
  38. if (!IsActiveOn(body1))
  39. continue;
  40. foreach (Body body2 in Bodies)
  41. {
  42. if (body1 == body2 || (body1.IsStatic && body2.IsStatic) || !body2.Enabled)
  43. continue;
  44. Vector2 d = body2.WorldCenter - body1.WorldCenter;
  45. float r2 = d.LengthSquared();
  46. if (r2 < Settings.Epsilon)
  47. continue;
  48. float r = d.Length();
  49. if (r >= MaxRadius || r <= MinRadius)
  50. continue;
  51. switch (GravityType)
  52. {
  53. case GravityType.DistanceSquared:
  54. f = Strength / r2 / (float)Math.Sqrt(r2) * body1.Mass * body2.Mass * d;
  55. break;
  56. case GravityType.Linear:
  57. f = Strength / r2 * body1.Mass * body2.Mass * d;
  58. break;
  59. }
  60. body1.ApplyForce(ref f);
  61. Vector2.Negate(ref f, out f);
  62. body2.ApplyForce(ref f);
  63. }
  64. foreach (Vector2 point in Points)
  65. {
  66. Vector2 d = point - body1.Position;
  67. float r2 = d.LengthSquared();
  68. if (r2 < Settings.Epsilon)
  69. continue;
  70. float r = d.Length();
  71. if (r >= MaxRadius || r <= MinRadius)
  72. continue;
  73. switch (GravityType)
  74. {
  75. case GravityType.DistanceSquared:
  76. f = Strength / r2 / (float)Math.Sqrt(r2) * body1.Mass * d;
  77. break;
  78. case GravityType.Linear:
  79. f = Strength / r2 * body1.Mass * d;
  80. break;
  81. }
  82. body1.ApplyForce(ref f);
  83. }
  84. }
  85. }
  86. public void AddBody(Body body)
  87. {
  88. Bodies.Add(body);
  89. }
  90. public void AddPoint(Vector2 point)
  91. {
  92. Points.Add(point);
  93. }
  94. }
  95. }