2
0

AdvancedDemo5.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using FarseerPhysics.Collision;
  4. using FarseerPhysics.Common;
  5. using FarseerPhysics.Common.Decomposition;
  6. using FarseerPhysics.Common.PolygonManipulation;
  7. using FarseerPhysics.Dynamics;
  8. using Microsoft.Xna.Framework;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11. namespace FarseerPhysics.SamplesFramework
  12. {
  13. internal class AdvancedDemo5 : PhysicsGameScreen, IDemoScreen
  14. {
  15. private Border _border;
  16. #region IDemoScreen Members
  17. public string GetTitle()
  18. {
  19. return "Breakable bodies and explosions";
  20. }
  21. public string GetDetails()
  22. {
  23. StringBuilder sb = new StringBuilder();
  24. sb.AppendLine("TODO: Add sample description!");
  25. sb.AppendLine(string.Empty);
  26. sb.AppendLine("GamePad:");
  27. sb.AppendLine(" - Explode (at cursor): B button");
  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(" - Exit to menu: Escape");
  35. sb.AppendLine(string.Empty);
  36. sb.AppendLine("Mouse / Touchscreen");
  37. sb.AppendLine(" - Explode (at cursor): Right click");
  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. DebugView.AppendFlags(DebugViewFlags.Shape);
  47. World.Gravity = Vector2.Zero;
  48. _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
  49. Texture2D alphabet = ScreenManager.Content.Load<Texture2D>("Samples/alphabet");
  50. uint[] data = new uint[alphabet.Width * alphabet.Height];
  51. alphabet.GetData(data);
  52. List<Vertices> list = PolygonTools.CreatePolygon(data, alphabet.Width, 3.5f, 20, true, true);
  53. float yOffset = -5f;
  54. float xOffset = -14f;
  55. for (int i = 0; i < list.Count; i++)
  56. {
  57. if (i == 9)
  58. {
  59. yOffset = 0f;
  60. xOffset = -14f;
  61. }
  62. if (i == 18)
  63. {
  64. yOffset = 5f;
  65. xOffset = -12.25f;
  66. }
  67. Vertices polygon = list[i];
  68. Vector2 centroid = -polygon.GetCentroid();
  69. polygon.Translate(ref centroid);
  70. polygon = SimplifyTools.CollinearSimplify(polygon);
  71. polygon = SimplifyTools.ReduceByDistance(polygon, 4);
  72. List<Vertices> triangulated = BayazitDecomposer.ConvexPartition(polygon);
  73. #if WINDOWS_PHONE
  74. const float scale = 0.6f;
  75. #else
  76. const float scale = 1f;
  77. #endif
  78. Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * scale;
  79. foreach (Vertices vertices in triangulated)
  80. {
  81. vertices.Scale(ref vertScale);
  82. }
  83. BreakableBody breakableBody = new BreakableBody(triangulated, World, 1);
  84. breakableBody.MainBody.Position = new Vector2(xOffset, yOffset);
  85. breakableBody.Strength = 100;
  86. World.AddBreakableBody(breakableBody);
  87. xOffset += 3.5f;
  88. }
  89. }
  90. public override void HandleInput(InputHelper input, GameTime gameTime)
  91. {
  92. if (input.IsNewMouseButtonPress(MouseButtons.RightButton) ||
  93. input.IsNewButtonPress(Buttons.B))
  94. {
  95. Vector2 cursorPos = Camera.ConvertScreenToWorld(input.Cursor);
  96. Vector2 min = cursorPos - new Vector2(10, 10);
  97. Vector2 max = cursorPos + new Vector2(10, 10);
  98. AABB aabb = new AABB(ref min, ref max);
  99. World.QueryAABB(fixture =>
  100. {
  101. Vector2 fv = fixture.Body.Position - cursorPos;
  102. fv.Normalize();
  103. fv *= 40;
  104. fixture.Body.ApplyLinearImpulse(ref fv);
  105. return true;
  106. }, ref aabb);
  107. }
  108. base.HandleInput(input, gameTime);
  109. }
  110. public override void Draw(GameTime gameTime)
  111. {
  112. _border.Draw();
  113. base.Draw(gameTime);
  114. }
  115. public override void UnloadContent()
  116. {
  117. DebugView.RemoveFlags(DebugViewFlags.Shape);
  118. base.UnloadContent();
  119. }
  120. }
  121. }