2
0

SimpleDemo7.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Text;
  2. using FarseerPhysics.Dynamics;
  3. using FarseerPhysics.Factories;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. namespace FarseerPhysics.SamplesFramework
  7. {
  8. internal class SimpleDemo7 : PhysicsGameScreen, IDemoScreen
  9. {
  10. private Border _border;
  11. private Sprite _obstacle;
  12. private Body[] _obstacles = new Body[4];
  13. private Ragdoll _ragdoll;
  14. #region IDemoScreen Members
  15. public string GetTitle()
  16. {
  17. return "Ragdoll";
  18. }
  19. public string GetDetails()
  20. {
  21. StringBuilder sb = new StringBuilder();
  22. sb.AppendLine("This demo shows how to combine physics objects to create a ragdoll.");
  23. sb.AppendLine(string.Empty);
  24. sb.AppendLine("GamePad:");
  25. sb.AppendLine(" - Rotate ragdoll: left and right triggers");
  26. sb.AppendLine(" - Move ragdoll: right thumbstick");
  27. sb.AppendLine(" - Move cursor: left thumbstick");
  28. sb.AppendLine(" - Grab object (beneath cursor): A button");
  29. sb.AppendLine(" - Drag grabbed object: left thumbstick");
  30. sb.AppendLine(" - Exit to menu: Back button");
  31. sb.AppendLine(string.Empty);
  32. sb.AppendLine("Keyboard:");
  33. sb.AppendLine(" - Rotate ragdoll: left and right arrows");
  34. sb.AppendLine(" - Move ragdoll: A,S,D,W");
  35. sb.AppendLine(" - Exit to menu: Escape");
  36. sb.AppendLine(string.Empty);
  37. sb.AppendLine("Mouse / Touchscreen");
  38. sb.AppendLine(" - Grab object (beneath cursor): Left click");
  39. sb.AppendLine(" - Drag grabbed object: move mouse / finger");
  40. return sb.ToString();
  41. }
  42. #endregion
  43. public override void LoadContent()
  44. {
  45. base.LoadContent();
  46. World.Gravity = new Vector2(0f, 20f);
  47. _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
  48. _ragdoll = new Ragdoll(World, this, Vector2.Zero);
  49. LoadObstacles();
  50. SetUserAgent(_ragdoll.Body, 1000f, 400f);
  51. }
  52. private void LoadObstacles()
  53. {
  54. for (int i = 0; i < 4; i++)
  55. {
  56. _obstacles[i] = BodyFactory.CreateRectangle(World, 5f, 1.5f, 1f);
  57. _obstacles[i].IsStatic = true;
  58. }
  59. _obstacles[0].Position = new Vector2(-9f, 5f);
  60. _obstacles[1].Position = new Vector2(-8f, -7f);
  61. _obstacles[2].Position = new Vector2(9f, 7f);
  62. _obstacles[3].Position = new Vector2(7f, -5f);
  63. // create sprite based on body
  64. _obstacle = new Sprite(ScreenManager.Assets.TextureFromShape(_obstacles[0].FixtureList[0].Shape,
  65. MaterialType.Dots,
  66. Color.SandyBrown, 0.8f));
  67. }
  68. public override void Draw(GameTime gameTime)
  69. {
  70. ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
  71. for (int i = 0; i < 4; ++i)
  72. {
  73. ScreenManager.SpriteBatch.Draw(_obstacle.Texture, ConvertUnits.ToDisplayUnits(_obstacles[i].Position),
  74. null,
  75. Color.White, _obstacles[i].Rotation, _obstacle.Origin, 1f,
  76. SpriteEffects.None, 0f);
  77. }
  78. _ragdoll.Draw();
  79. ScreenManager.SpriteBatch.End();
  80. _border.Draw();
  81. base.Draw(gameTime);
  82. }
  83. }
  84. }