AdvancedDemo2.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using FarseerPhysics.Collision.Shapes;
  4. using FarseerPhysics.Common;
  5. using FarseerPhysics.Dynamics;
  6. using FarseerPhysics.Factories;
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. namespace FarseerPhysics.SamplesFramework
  10. {
  11. internal class AdvancedDemo2 : PhysicsGameScreen, IDemoScreen
  12. {
  13. private Border _border;
  14. private List<Body> _bridgeBodies;
  15. private Sprite _bridgeBox;
  16. private List<Body> _softBodies;
  17. private Sprite _softBodyBox;
  18. private Sprite _softBodyCircle;
  19. #region IDemoScreen Members
  20. public string GetTitle()
  21. {
  22. return "Path generator";
  23. }
  24. public string GetDetails()
  25. {
  26. StringBuilder sb = new StringBuilder();
  27. sb.AppendLine("TODO: Add sample description!");
  28. sb.AppendLine(string.Empty);
  29. sb.AppendLine("GamePad:");
  30. sb.AppendLine(" - Move cursor: left thumbstick");
  31. sb.AppendLine(" - Grab object (beneath cursor): A button");
  32. sb.AppendLine(" - Drag grabbed object: left thumbstick");
  33. sb.AppendLine(" - Exit to menu: Back button");
  34. sb.AppendLine(string.Empty);
  35. sb.AppendLine("Keyboard:");
  36. sb.AppendLine(" - Exit to menu: Escape");
  37. sb.AppendLine(string.Empty);
  38. sb.AppendLine("Mouse / Touchscreen");
  39. sb.AppendLine(" - Grab object (beneath cursor): Left click");
  40. sb.AppendLine(" - Drag grabbed object: move mouse / finger");
  41. return sb.ToString();
  42. }
  43. #endregion
  44. public override void LoadContent()
  45. {
  46. base.LoadContent();
  47. World.Gravity = new Vector2(0, 9.82f);
  48. _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
  49. /* Bridge */
  50. //We make a path using 2 points.
  51. Path bridgePath = new Path();
  52. bridgePath.Add(new Vector2(-15, 5));
  53. bridgePath.Add(new Vector2(15, 5));
  54. bridgePath.Closed = false;
  55. Vertices box = PolygonTools.CreateRectangle(0.125f, 0.5f);
  56. PolygonShape shape = new PolygonShape(box, 20);
  57. _bridgeBodies = PathManager.EvenlyDistributeShapesAlongPath(World, bridgePath, shape,
  58. BodyType.Dynamic, 29);
  59. _bridgeBox =
  60. new Sprite(ScreenManager.Assets.TextureFromShape(shape, MaterialType.Dots, Color.SandyBrown, 1f));
  61. //Attach the first and last fixtures to the world
  62. JointFactory.CreateFixedRevoluteJoint(World, _bridgeBodies[0], new Vector2(0f, -0.5f),
  63. _bridgeBodies[0].Position);
  64. JointFactory.CreateFixedRevoluteJoint(World, _bridgeBodies[_bridgeBodies.Count - 1], new Vector2(0, 0.5f),
  65. _bridgeBodies[_bridgeBodies.Count - 1].Position);
  66. PathManager.AttachBodiesWithRevoluteJoint(World, _bridgeBodies, new Vector2(0f, -0.5f),
  67. new Vector2(0f, 0.5f),
  68. false, true);
  69. /* Soft body */
  70. //We make a rectangular path.
  71. Path rectanglePath = new Path();
  72. rectanglePath.Add(new Vector2(-6, -11));
  73. rectanglePath.Add(new Vector2(-6, 1));
  74. rectanglePath.Add(new Vector2(6, 1));
  75. rectanglePath.Add(new Vector2(6, -11));
  76. rectanglePath.Closed = true;
  77. //Creating two shapes. A circle to form the circle and a rectangle to stabilize the soft body.
  78. List<Shape> shapes = new List<Shape>(2);
  79. shapes.Add(new PolygonShape(PolygonTools.CreateRectangle(0.5f, 0.5f, new Vector2(-0.1f, 0f), 0f), 1f));
  80. shapes.Add(new CircleShape(0.5f, 1f));
  81. //We distribute the shapes in the rectangular path.
  82. _softBodies = PathManager.EvenlyDistributeShapesAlongPath(World, rectanglePath, shapes,
  83. BodyType.Dynamic, 30);
  84. _softBodyBox =
  85. new Sprite(ScreenManager.Assets.TextureFromShape(shapes[0], MaterialType.Blank, Color.Silver * 0.8f, 1f));
  86. _softBodyBox.Origin += new Vector2(ConvertUnits.ToDisplayUnits(0.1f), 0f);
  87. _softBodyCircle =
  88. new Sprite(ScreenManager.Assets.TextureFromShape(shapes[1], MaterialType.Waves, Color.Silver, 1f));
  89. //Attach the bodies together with revolute joints. The rectangular form will converge to a circular form.
  90. PathManager.AttachBodiesWithRevoluteJoint(World, _softBodies, new Vector2(0f, -0.5f), new Vector2(0f, 0.5f),
  91. true, true);
  92. }
  93. public override void Draw(GameTime gameTime)
  94. {
  95. ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
  96. for (int i = 0; i < _softBodies.Count; ++i)
  97. {
  98. ScreenManager.SpriteBatch.Draw(_softBodyBox.Texture,
  99. ConvertUnits.ToDisplayUnits(_softBodies[i].Position), null,
  100. Color.White, _softBodies[i].Rotation, _softBodyBox.Origin, 1f,
  101. SpriteEffects.None, 0f);
  102. }
  103. for (int i = 0; i < _softBodies.Count; ++i)
  104. {
  105. ScreenManager.SpriteBatch.Draw(_softBodyCircle.Texture,
  106. ConvertUnits.ToDisplayUnits(_softBodies[i].Position), null,
  107. Color.White, _softBodies[i].Rotation, _softBodyCircle.Origin, 1f,
  108. SpriteEffects.None, 0f);
  109. }
  110. for (int i = 0; i < _bridgeBodies.Count; ++i)
  111. {
  112. ScreenManager.SpriteBatch.Draw(_bridgeBox.Texture,
  113. ConvertUnits.ToDisplayUnits(_bridgeBodies[i].Position), null,
  114. Color.White, _bridgeBodies[i].Rotation, _bridgeBox.Origin, 1f,
  115. SpriteEffects.None, 0f);
  116. }
  117. ScreenManager.SpriteBatch.End();
  118. _border.Draw();
  119. base.Draw(gameTime);
  120. }
  121. }
  122. }