SimpleDemo9.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using FarseerPhysics.Dynamics;
  4. using FarseerPhysics.Factories;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace FarseerPhysics.SamplesFramework
  8. {
  9. internal class SimpleDemo9 : PhysicsGameScreen, IDemoScreen
  10. {
  11. private Border _border;
  12. private List<Body> _ramps;
  13. private Body[] _rectangle = new Body[5];
  14. private Sprite _rectangleSprite;
  15. #region IDemoScreen Members
  16. public string GetTitle()
  17. {
  18. return "Friction";
  19. }
  20. public string GetDetails()
  21. {
  22. StringBuilder sb = new StringBuilder();
  23. sb.AppendLine("This demo shows several bodys with varying friction.");
  24. sb.AppendLine(string.Empty);
  25. sb.AppendLine("GamePad:");
  26. sb.AppendLine(" - Move cursor: left thumbstick");
  27. sb.AppendLine(" - Grab object (beneath cursor): A button");
  28. sb.AppendLine(" - Drag grabbed object: left thumbstick");
  29. sb.AppendLine(" - Exit to menu: Back button");
  30. sb.AppendLine(string.Empty);
  31. sb.AppendLine("Keyboard:");
  32. sb.AppendLine(" - Exit to menu: Escape");
  33. sb.AppendLine(string.Empty);
  34. sb.AppendLine("Mouse / Touchscreen");
  35. sb.AppendLine(" - Grab object (beneath cursor): Left click");
  36. sb.AppendLine(" - Drag grabbed object: move mouse / finger");
  37. return sb.ToString();
  38. }
  39. #endregion
  40. public override void LoadContent()
  41. {
  42. base.LoadContent();
  43. World.Gravity = new Vector2(0f, 20f);
  44. _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
  45. _ramps = new List<Body>();
  46. _ramps.Add(BodyFactory.CreateEdge(World, new Vector2(-20f, -11.2f), new Vector2(10f, -3.8f)));
  47. _ramps.Add(BodyFactory.CreateEdge(World, new Vector2(12f, -5.6f), new Vector2(12f, -3.2f)));
  48. _ramps.Add(BodyFactory.CreateEdge(World, new Vector2(-10f, 4.4f), new Vector2(20f, -1.4f)));
  49. _ramps.Add(BodyFactory.CreateEdge(World, new Vector2(-12f, 2.6f), new Vector2(-12f, 5f)));
  50. _ramps.Add(BodyFactory.CreateEdge(World, new Vector2(-20f, 6.8f), new Vector2(10f, 11.5f)));
  51. float[] friction = new[] { 0.75f, 0.45f, 0.28f, 0.17f, 0.0f };
  52. for (int i = 0; i < 5; ++i)
  53. {
  54. _rectangle[i] = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
  55. _rectangle[i].BodyType = BodyType.Dynamic;
  56. _rectangle[i].Position = new Vector2(-18f + 5.2f * i, -13.0f + 1.282f * i);
  57. _rectangle[i].Friction = friction[i];
  58. }
  59. // create sprite based on body
  60. _rectangleSprite = new Sprite(ScreenManager.Assets.TextureFromShape(_rectangle[0].FixtureList[0].Shape,
  61. MaterialType.Squares,
  62. Color.ForestGreen, 0.8f));
  63. }
  64. public override void Draw(GameTime gameTime)
  65. {
  66. ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
  67. for (int i = 0; i < 5; ++i)
  68. {
  69. ScreenManager.SpriteBatch.Draw(_rectangleSprite.Texture,
  70. ConvertUnits.ToDisplayUnits(_rectangle[i].Position), null,
  71. Color.White, _rectangle[i].Rotation, _rectangleSprite.Origin, 1f,
  72. SpriteEffects.None, 0f);
  73. }
  74. ScreenManager.SpriteBatch.End();
  75. ScreenManager.LineBatch.Begin(Camera.SimProjection, Camera.SimView);
  76. for (int i = 0; i < _ramps.Count; ++i)
  77. {
  78. ScreenManager.LineBatch.DrawLineShape(_ramps[i].FixtureList[0].Shape, Color.DarkGreen);
  79. }
  80. ScreenManager.LineBatch.End();
  81. _border.Draw();
  82. base.Draw(gameTime);
  83. }
  84. }
  85. }