SimpleDemo8.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 SimpleDemo8 : PhysicsGameScreen, IDemoScreen
  9. {
  10. private Border _border;
  11. private Body[] _circle = new Body[6];
  12. private Sprite _circleSprite;
  13. #region IDemoScreen Members
  14. public string GetTitle()
  15. {
  16. return "Restitution";
  17. }
  18. public string GetDetails()
  19. {
  20. StringBuilder sb = new StringBuilder();
  21. sb.AppendLine("This demo shows several bodys with varying restitution.");
  22. sb.AppendLine(string.Empty);
  23. sb.AppendLine("GamePad:");
  24. sb.AppendLine(" - Move cursor: left thumbstick");
  25. sb.AppendLine(" - Grab object (beneath cursor): A button");
  26. sb.AppendLine(" - Drag grabbed object: left thumbstick");
  27. sb.AppendLine(" - Exit to menu: Back button");
  28. sb.AppendLine(string.Empty);
  29. sb.AppendLine("Keyboard:");
  30. sb.AppendLine(" - Exit to menu: Escape");
  31. sb.AppendLine(string.Empty);
  32. sb.AppendLine("Mouse / Touchscreen");
  33. sb.AppendLine(" - Grab object (beneath cursor): Left click");
  34. sb.AppendLine(" - Drag grabbed object: move mouse / finger");
  35. return sb.ToString();
  36. }
  37. #endregion
  38. public override void LoadContent()
  39. {
  40. base.LoadContent();
  41. World.Gravity = new Vector2(0f, 20f);
  42. _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
  43. Vector2 _position = new Vector2(-15f, -8f);
  44. float _restitution = 0f;
  45. for (int i = 0; i < 6; ++i)
  46. {
  47. _circle[i] = BodyFactory.CreateCircle(World, 1.5f, 1f, _position);
  48. _circle[i].BodyType = BodyType.Dynamic;
  49. _circle[i].Restitution = _restitution;
  50. _position.X += 6f;
  51. _restitution += 0.2f;
  52. }
  53. // create sprite based on body
  54. _circleSprite = new Sprite(ScreenManager.Assets.TextureFromShape(_circle[0].FixtureList[0].Shape,
  55. MaterialType.Waves,
  56. Color.Brown, 1f));
  57. }
  58. public override void Draw(GameTime gameTime)
  59. {
  60. ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
  61. for (int i = 0; i < 6; ++i)
  62. {
  63. ScreenManager.SpriteBatch.Draw(_circleSprite.Texture, ConvertUnits.ToDisplayUnits(_circle[i].Position),
  64. null,
  65. Color.White, _circle[i].Rotation, _circleSprite.Origin, 1f,
  66. SpriteEffects.None, 0f);
  67. }
  68. ScreenManager.SpriteBatch.End();
  69. _border.Draw();
  70. base.Draw(gameTime);
  71. }
  72. }
  73. }