Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 5th, 2012  |  syntax: C#  |  size: 7.45 KB  |  views: 74  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System.Text;
  2. using FarseerPhysics.Dynamics;
  3. using FarseerPhysics.Factories;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using FarseerPhysics.Dynamics.Joints;
  7.  
  8. namespace FarseerPhysics.SamplesFramework
  9. {
  10.     internal class SimpleDemo1 : PhysicsGameScreen, IDemoScreen
  11.     {
  12.         private Border _border;
  13.         private Body _bodyMain;
  14.         private Body _bodyTop;
  15.         private Body _bodyRight;
  16.         private Body _bodyMainHolder;
  17.         private Body _bodyTopHolder;
  18.         private Body _bodyRightHolder;
  19.         private Sprite _bodySprite;
  20.  
  21.         #region IDemoScreen Members
  22.  
  23.         public string GetTitle()
  24.         {
  25.             return "Body with a single fixture";
  26.         }
  27.  
  28.         public string GetDetails()
  29.         {
  30.             StringBuilder sb = new StringBuilder();
  31.             sb.AppendLine("This demo shows a single body with one attached fixture and shape.");
  32.             sb.AppendLine("A fixture binds a shape to a body and adds material");
  33.             sb.AppendLine("properties such as density, friction, and restitution.");
  34.             sb.AppendLine(string.Empty);
  35.             sb.AppendLine("GamePad:");
  36.             sb.AppendLine("  - Rotate object: left and right triggers");
  37.             sb.AppendLine("  - Move object: right thumbstick");
  38.             sb.AppendLine("  - Move cursor: left thumbstick");
  39.             sb.AppendLine("  - Grab object (beneath cursor): A button");
  40.             sb.AppendLine("  - Drag grabbed object: left thumbstick");
  41.             sb.AppendLine("  - Exit to menu: Back button");
  42.             sb.AppendLine(string.Empty);
  43.             sb.AppendLine("Keyboard:");
  44.             sb.AppendLine("  - Rotate Object: left and right arrows");
  45.             sb.AppendLine("  - Move Object: A,S,D,W");
  46.             sb.AppendLine("  - Exit to menu: Escape");
  47.             sb.AppendLine(string.Empty);
  48.             sb.AppendLine("Mouse / Touchscreen");
  49.             sb.AppendLine("  - Grab object (beneath cursor): Left click");
  50.             sb.AppendLine("  - Drag grabbed object: move mouse / finger");
  51.             return sb.ToString();
  52.         }
  53.  
  54.         #endregion
  55.  
  56.         public override void LoadContent()
  57.         {
  58.             base.LoadContent();
  59.  
  60.             World.Gravity = Vector2.Zero;
  61.  
  62.             _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
  63.  
  64.             _bodyMain = BodyFactory.CreateRectangle(World, 5f, 5f, 1f);
  65.             _bodyMain.BodyType = BodyType.Dynamic;
  66.  
  67.             _bodyMainHolder = BodyFactory.CreateRectangle(World, 5f, 5f, 1f);
  68.             _bodyMainHolder.BodyType = BodyType.Dynamic;
  69.             _bodyMainHolder.FixedRotation = true;
  70.             _bodyMainHolder.CollisionCategories = Category.None;
  71.             _bodyMainHolder.IsSensor = true;
  72.  
  73.             _bodyTop = BodyFactory.CreateRectangle(World, 5f, 5f, 1f);
  74.             _bodyTop.BodyType = BodyType.Dynamic;
  75.             _bodyTop.Position = new Vector2(0,-10);
  76.  
  77.             _bodyTopHolder = BodyFactory.CreateRectangle(World, 5f, 5f, 1f);
  78.             _bodyTopHolder.BodyType = BodyType.Dynamic;
  79.             _bodyTopHolder.CollisionCategories = Category.None;
  80.             _bodyTopHolder.Position = new Vector2(0, -10);
  81.             _bodyTopHolder.IsSensor = true;
  82.  
  83.             _bodyRight = BodyFactory.CreateRectangle(World, 5f, 5f, 1f);
  84.             _bodyRight.BodyType = BodyType.Dynamic;
  85.             _bodyRight.Position = new Vector2(10, 0);
  86.  
  87.             _bodyRightHolder = BodyFactory.CreateRectangle(World, 5f, 5f, 1f);
  88.             _bodyRightHolder.BodyType = BodyType.Dynamic;
  89.             _bodyRightHolder.CollisionCategories = Category.None;
  90.             _bodyRightHolder.Position = new Vector2(10, 0);
  91.             _bodyRightHolder.IsSensor = true;
  92.  
  93.  
  94.             //Weld the holders together so they don't move. Note the main one does not have fixed rotation. If you turn it on, this will not work. I'm not sure why.
  95.             JointFactory.CreateWeldJoint(World, _bodyMainHolder, _bodyRightHolder, _bodyRightHolder.Position - _bodyMainHolder.Position,Vector2.Zero);
  96.             JointFactory.CreateWeldJoint(World, _bodyMainHolder, _bodyTopHolder, _bodyTopHolder.Position - _bodyMainHolder.Position, Vector2.Zero);
  97.  
  98.             //Lock the actual bodies to the holders
  99.             JointFactory.CreateRevoluteJoint(World, _bodyMainHolder, _bodyMain, Vector2.Zero);
  100.             JointFactory.CreateRevoluteJoint(World, _bodyRightHolder, _bodyRight, Vector2.Zero);
  101.             JointFactory.CreateRevoluteJoint(World, _bodyTopHolder, _bodyTop, Vector2.Zero);
  102.  
  103.             //Make them rotate the same as each other
  104.             JointFactory.CreateAngleJoint(World, _bodyMain, _bodyRight);
  105.             JointFactory.CreateAngleJoint(World, _bodyMain, _bodyTop);
  106.            
  107.  
  108.             SetUserAgent(_bodyMain, 100f, 100f);
  109.  
  110.             // create sprite based on body
  111.             _bodySprite = new Sprite(ScreenManager.Assets.TextureFromShape(_bodyMain.FixtureList[0].Shape,
  112.                                                                                 MaterialType.Squares,
  113.                                                                                 Color.Orange, 1f));
  114.         }
  115.  
  116.         public override void Draw(GameTime gameTime)
  117.         {
  118.             ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
  119.  
  120.             ScreenManager.SpriteBatch.Draw(_bodySprite.Texture, ConvertUnits.ToDisplayUnits(_bodyMainHolder.Position),
  121.                                            null,
  122.                                            Color.Green, _bodyMainHolder.Rotation, _bodySprite.Origin, 1f,
  123.                                            SpriteEffects.None, 0f);
  124.             ScreenManager.SpriteBatch.Draw(_bodySprite.Texture, ConvertUnits.ToDisplayUnits(_bodyRightHolder.Position),
  125.                                            null,
  126.                                            Color.Green, _bodyRightHolder.Rotation, _bodySprite.Origin, 1f,
  127.                                            SpriteEffects.None, 0f);
  128.             ScreenManager.SpriteBatch.Draw(_bodySprite.Texture, ConvertUnits.ToDisplayUnits(_bodyTopHolder.Position),
  129.                                            null,
  130.                                            Color.Green, _bodyTopHolder.Rotation, _bodySprite.Origin, 1f,
  131.                                            SpriteEffects.None, 0f);
  132.  
  133.  
  134.             ScreenManager.SpriteBatch.Draw(_bodySprite.Texture, ConvertUnits.ToDisplayUnits(_bodyMain.Position),
  135.                                            null,
  136.                                            Color.Yellow, _bodyMain.Rotation, _bodySprite.Origin, 1f,
  137.                                            SpriteEffects.None, 0f);
  138.             ScreenManager.SpriteBatch.Draw(_bodySprite.Texture, ConvertUnits.ToDisplayUnits(_bodyTop.Position),
  139.                                            null,
  140.                                            Color.Red, _bodyTop.Rotation, _bodySprite.Origin, 1f,
  141.                                            SpriteEffects.None, 0f);
  142.             ScreenManager.SpriteBatch.Draw(_bodySprite.Texture, ConvertUnits.ToDisplayUnits(_bodyRight.Position),
  143.                                            null,
  144.                                            Color.Red, _bodyRight.Rotation, _bodySprite.Origin, 1f,
  145.                                            SpriteEffects.None, 0f);
  146.  
  147.  
  148.            
  149.             ScreenManager.SpriteBatch.End();
  150.             _border.Draw();
  151.             base.Draw(gameTime);
  152.         }
  153.     }
  154. }
clone this paste RAW Paste Data