SimpleDemo1.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 SimpleDemo1 : PhysicsGameScreen, IDemoScreen
  9. {
  10. private Border _border;
  11. private Body _rectangle;
  12. private Sprite _rectangleSprite;
  13. #region IDemoScreen Members
  14. public string GetTitle()
  15. {
  16. return "Body with a single fixture";
  17. }
  18. public string GetDetails()
  19. {
  20. StringBuilder sb = new StringBuilder();
  21. sb.AppendLine("This demo shows a single body with one attached fixture and shape.");
  22. sb.AppendLine("A fixture binds a shape to a body and adds material");
  23. sb.AppendLine("properties such as density, friction, and restitution.");
  24. sb.AppendLine(string.Empty);
  25. sb.AppendLine("GamePad:");
  26. sb.AppendLine(" - Rotate object: left and right triggers");
  27. sb.AppendLine(" - Move object: right thumbstick");
  28. sb.AppendLine(" - Move cursor: left thumbstick");
  29. sb.AppendLine(" - Grab object (beneath cursor): A button");
  30. sb.AppendLine(" - Drag grabbed object: left thumbstick");
  31. sb.AppendLine(" - Exit to menu: Back button");
  32. sb.AppendLine(string.Empty);
  33. sb.AppendLine("Keyboard:");
  34. sb.AppendLine(" - Rotate Object: left and right arrows");
  35. sb.AppendLine(" - Move Object: A,S,D,W");
  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 = Vector2.Zero;
  48. _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
  49. _rectangle = BodyFactory.CreateRectangle(World, 5f, 5f, 1f);
  50. _rectangle.BodyType = BodyType.Dynamic;
  51. SetUserAgent(_rectangle, 100f, 100f);
  52. // create sprite based on body
  53. _rectangleSprite = new Sprite(ScreenManager.Assets.TextureFromShape(_rectangle.FixtureList[0].Shape,
  54. MaterialType.Squares,
  55. Color.Orange, 1f));
  56. }
  57. public override void Draw(GameTime gameTime)
  58. {
  59. ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
  60. ScreenManager.SpriteBatch.Draw(_rectangleSprite.Texture, ConvertUnits.ToDisplayUnits(_rectangle.Position),
  61. null,
  62. Color.White, _rectangle.Rotation, _rectangleSprite.Origin, 1f,
  63. SpriteEffects.None, 0f);
  64. ScreenManager.SpriteBatch.End();
  65. _border.Draw();
  66. base.Draw(gameTime);
  67. }
  68. }
  69. }