2
0

SimpleDemo2.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using FarseerPhysics.Common;
  4. using FarseerPhysics.Dynamics;
  5. using FarseerPhysics.Factories;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. namespace FarseerPhysics.SamplesFramework
  9. {
  10. internal class SimpleDemo2 : PhysicsGameScreen, IDemoScreen
  11. {
  12. private Border _border;
  13. private Sprite _rectangleSprite;
  14. private Body _rectangles;
  15. private Vector2 _offset;
  16. #region IDemoScreen Members
  17. public string GetTitle()
  18. {
  19. return "Body with two fixtures";
  20. }
  21. public string GetDetails()
  22. {
  23. StringBuilder sb = new StringBuilder();
  24. sb.AppendLine("This demo shows a single body with two attached fixtures and shapes.");
  25. sb.AppendLine("A fixture binds a shape to a body and adds material");
  26. sb.AppendLine("properties such as density, friction, and restitution.");
  27. sb.AppendLine(string.Empty);
  28. sb.AppendLine("GamePad:");
  29. sb.AppendLine(" - Rotate object: left and right triggers");
  30. sb.AppendLine(" - Move object: right thumbstick");
  31. sb.AppendLine(" - Move cursor: left thumbstick");
  32. sb.AppendLine(" - Grab object (beneath cursor): A button");
  33. sb.AppendLine(" - Drag grabbed object: left thumbstick");
  34. sb.AppendLine(" - Exit to menu: Back button");
  35. sb.AppendLine(string.Empty);
  36. sb.AppendLine("Keyboard:");
  37. sb.AppendLine(" - Rotate Object: left and right arrows");
  38. sb.AppendLine(" - Move Object: A,S,D,W");
  39. sb.AppendLine(" - Exit to menu: Escape");
  40. sb.AppendLine(string.Empty);
  41. sb.AppendLine("Mouse / Touchscreen");
  42. sb.AppendLine(" - Grab object (beneath cursor): Left click");
  43. sb.AppendLine(" - Drag grabbed object: move mouse / finger");
  44. return sb.ToString();
  45. }
  46. #endregion
  47. public override void LoadContent()
  48. {
  49. base.LoadContent();
  50. World.Gravity = Vector2.Zero;
  51. _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
  52. Vertices rect1 = PolygonTools.CreateRectangle(2f, 2f);
  53. Vertices rect2 = PolygonTools.CreateRectangle(2f, 2f);
  54. Vector2 trans = new Vector2(-2f, 0f);
  55. rect1.Translate(ref trans);
  56. trans = new Vector2(2f, 0f);
  57. rect2.Translate(ref trans);
  58. List<Vertices> vertices = new List<Vertices>(2);
  59. vertices.Add(rect1);
  60. vertices.Add(rect2);
  61. _rectangles = BodyFactory.CreateCompoundPolygon(World, vertices, 1f);
  62. _rectangles.BodyType = BodyType.Dynamic;
  63. SetUserAgent(_rectangles, 200f, 200f);
  64. // create sprite based on rectangle fixture
  65. _rectangleSprite = new Sprite(ScreenManager.Assets.TextureFromVertices(rect1, MaterialType.Squares,
  66. Color.Orange, 1f));
  67. _offset = new Vector2(ConvertUnits.ToDisplayUnits(2f), 0f);
  68. }
  69. public override void Draw(GameTime gameTime)
  70. {
  71. ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
  72. // draw first rectangle
  73. ScreenManager.SpriteBatch.Draw(_rectangleSprite.Texture,
  74. ConvertUnits.ToDisplayUnits(_rectangles.Position), null,
  75. Color.White, _rectangles.Rotation,
  76. _rectangleSprite.Origin + _offset, 1f, SpriteEffects.None, 0f);
  77. // draw second rectangle
  78. ScreenManager.SpriteBatch.Draw(_rectangleSprite.Texture,
  79. ConvertUnits.ToDisplayUnits(_rectangles.Position), null,
  80. Color.White, _rectangles.Rotation,
  81. _rectangleSprite.Origin - _offset, 1f, SpriteEffects.None, 0f);
  82. ScreenManager.SpriteBatch.End();
  83. _border.Draw();
  84. base.Draw(gameTime);
  85. }
  86. }
  87. }