2
0

SimpleDemo5.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Text;
  2. using FarseerPhysics.Dynamics;
  3. using Microsoft.Xna.Framework;
  4. namespace FarseerPhysics.SamplesFramework
  5. {
  6. internal class SimpleDemo5 : PhysicsGameScreen, IDemoScreen
  7. {
  8. private Agent _agent;
  9. private Border _border;
  10. private Objects _circles;
  11. private Objects _gears;
  12. private Objects _rectangles;
  13. private Objects _stars;
  14. #region IDemoScreen Members
  15. public string GetTitle()
  16. {
  17. return "Collision Categories";
  18. }
  19. public string GetDetails()
  20. {
  21. StringBuilder sb = new StringBuilder();
  22. sb.AppendLine("This demo shows how to setup complex collision scenerios.");
  23. sb.AppendLine("In this demo:");
  24. sb.AppendLine(" - Circles and rectangles are set to only collide with themselves.");
  25. sb.AppendLine(" - Stars are set to collide with gears.");
  26. sb.AppendLine(" - Gears are set to collide with stars.");
  27. sb.AppendLine(" - The agent is set to collide with everything but stars");
  28. sb.AppendLine(string.Empty);
  29. sb.AppendLine("GamePad:");
  30. sb.AppendLine(" - Rotate agent: left and right triggers");
  31. sb.AppendLine(" - Move agent: right thumbstick");
  32. sb.AppendLine(" - Move cursor: left thumbstick");
  33. sb.AppendLine(" - Grab object (beneath cursor): A button");
  34. sb.AppendLine(" - Drag grabbed object: left thumbstick");
  35. sb.AppendLine(" - Exit to menu: Back button");
  36. sb.AppendLine(string.Empty);
  37. sb.AppendLine("Keyboard:");
  38. sb.AppendLine(" - Rotate agent: left and right arrows");
  39. sb.AppendLine(" - Move agent: A,S,D,W");
  40. sb.AppendLine(" - Exit to menu: Escape");
  41. sb.AppendLine(string.Empty);
  42. sb.AppendLine("Mouse / Touchscreen");
  43. sb.AppendLine(" - Grab object (beneath cursor): Left click");
  44. sb.AppendLine(" - Drag grabbed object: move mouse / finger");
  45. return sb.ToString();
  46. }
  47. #endregion
  48. public override void LoadContent()
  49. {
  50. base.LoadContent();
  51. World.Gravity = Vector2.Zero;
  52. _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
  53. //Cat1=Circles, Cat2=Rectangles, Cat3=Gears, Cat4=Stars
  54. _agent = new Agent(World, this, Vector2.Zero);
  55. //Collide with all but stars
  56. _agent.CollisionCategories = Category.All & ~Category.Cat4;
  57. _agent.CollidesWith = Category.All & ~Category.Cat4;
  58. Vector2 startPosition = new Vector2(-20f, -11f);
  59. Vector2 endPosition = new Vector2(20, -11f);
  60. _circles = new Objects(World, this, startPosition, endPosition, 15, 0.6f, ObjectType.Circle);
  61. //Collide with itself only
  62. _circles.CollisionCategories = Category.Cat1;
  63. _circles.CollidesWith = Category.Cat1;
  64. startPosition = new Vector2(-20, 11f);
  65. endPosition = new Vector2(20, 11f);
  66. _rectangles = new Objects(World, this, startPosition, endPosition, 15, 1.2f, ObjectType.Rectangle);
  67. //Collides with itself only
  68. _rectangles.CollisionCategories = Category.Cat2;
  69. _rectangles.CollidesWith = Category.Cat2;
  70. startPosition = new Vector2(-20, 7);
  71. endPosition = new Vector2(-20, -7);
  72. _gears = new Objects(World, this, startPosition, endPosition, 5, 0.6f, ObjectType.Gear);
  73. //Collides with stars
  74. _gears.CollisionCategories = Category.Cat3;
  75. _gears.CollidesWith = Category.Cat3 | Category.Cat4;
  76. startPosition = new Vector2(20, 7);
  77. endPosition = new Vector2(20, -7);
  78. _stars = new Objects(World, this, startPosition, endPosition, 5, 0.6f, ObjectType.Star);
  79. //Collides with gears
  80. _stars.CollisionCategories = Category.Cat4;
  81. _stars.CollidesWith = Category.Cat3 | Category.Cat4;
  82. SetUserAgent(_agent.Body, 1000f, 400f);
  83. }
  84. public override void Draw(GameTime gameTime)
  85. {
  86. ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
  87. _agent.Draw();
  88. _circles.Draw();
  89. _rectangles.Draw();
  90. _stars.Draw();
  91. _gears.Draw();
  92. ScreenManager.SpriteBatch.End();
  93. _border.Draw();
  94. base.Draw(gameTime);
  95. }
  96. }
  97. }