using System.Text; using FarseerPhysics.Dynamics; using FarseerPhysics.Factories; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using FarseerPhysics.Dynamics.Joints; namespace FarseerPhysics.SamplesFramework { internal class SimpleDemo1 : PhysicsGameScreen, IDemoScreen { private Border _border; private Body _bodyMain; private Body _bodyTop; private Body _bodyRight; private Body _bodyMainHolder; private Body _bodyTopHolder; private Body _bodyRightHolder; private Sprite _bodySprite; #region IDemoScreen Members public string GetTitle() { return "Body with a single fixture"; } public string GetDetails() { StringBuilder sb = new StringBuilder(); sb.AppendLine("This demo shows a single body with one attached fixture and shape."); sb.AppendLine("A fixture binds a shape to a body and adds material"); sb.AppendLine("properties such as density, friction, and restitution."); sb.AppendLine(string.Empty); sb.AppendLine("GamePad:"); sb.AppendLine(" - Rotate object: left and right triggers"); sb.AppendLine(" - Move object: right thumbstick"); sb.AppendLine(" - Move cursor: left thumbstick"); sb.AppendLine(" - Grab object (beneath cursor): A button"); sb.AppendLine(" - Drag grabbed object: left thumbstick"); sb.AppendLine(" - Exit to menu: Back button"); sb.AppendLine(string.Empty); sb.AppendLine("Keyboard:"); sb.AppendLine(" - Rotate Object: left and right arrows"); sb.AppendLine(" - Move Object: A,S,D,W"); sb.AppendLine(" - Exit to menu: Escape"); sb.AppendLine(string.Empty); sb.AppendLine("Mouse / Touchscreen"); sb.AppendLine(" - Grab object (beneath cursor): Left click"); sb.AppendLine(" - Drag grabbed object: move mouse / finger"); return sb.ToString(); } #endregion public override void LoadContent() { base.LoadContent(); World.Gravity = Vector2.Zero; _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport); _bodyMain = BodyFactory.CreateRectangle(World, 5f, 5f, 1f); _bodyMain.BodyType = BodyType.Dynamic; _bodyMainHolder = BodyFactory.CreateRectangle(World, 5f, 5f, 1f); _bodyMainHolder.BodyType = BodyType.Dynamic; _bodyMainHolder.FixedRotation = true; _bodyMainHolder.CollisionCategories = Category.None; _bodyMainHolder.IsSensor = true; _bodyTop = BodyFactory.CreateRectangle(World, 5f, 5f, 1f); _bodyTop.BodyType = BodyType.Dynamic; _bodyTop.Position = new Vector2(0,-10); _bodyTopHolder = BodyFactory.CreateRectangle(World, 5f, 5f, 1f); _bodyTopHolder.BodyType = BodyType.Dynamic; _bodyTopHolder.CollisionCategories = Category.None; _bodyTopHolder.Position = new Vector2(0, -10); _bodyTopHolder.IsSensor = true; _bodyRight = BodyFactory.CreateRectangle(World, 5f, 5f, 1f); _bodyRight.BodyType = BodyType.Dynamic; _bodyRight.Position = new Vector2(10, 0); _bodyRightHolder = BodyFactory.CreateRectangle(World, 5f, 5f, 1f); _bodyRightHolder.BodyType = BodyType.Dynamic; _bodyRightHolder.CollisionCategories = Category.None; _bodyRightHolder.Position = new Vector2(10, 0); _bodyRightHolder.IsSensor = true; //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. JointFactory.CreateWeldJoint(World, _bodyMainHolder, _bodyRightHolder, _bodyRightHolder.Position - _bodyMainHolder.Position,Vector2.Zero); JointFactory.CreateWeldJoint(World, _bodyMainHolder, _bodyTopHolder, _bodyTopHolder.Position - _bodyMainHolder.Position, Vector2.Zero); //Lock the actual bodies to the holders JointFactory.CreateRevoluteJoint(World, _bodyMainHolder, _bodyMain, Vector2.Zero); JointFactory.CreateRevoluteJoint(World, _bodyRightHolder, _bodyRight, Vector2.Zero); JointFactory.CreateRevoluteJoint(World, _bodyTopHolder, _bodyTop, Vector2.Zero); //Make them rotate the same as each other JointFactory.CreateAngleJoint(World, _bodyMain, _bodyRight); JointFactory.CreateAngleJoint(World, _bodyMain, _bodyTop); SetUserAgent(_bodyMain, 100f, 100f); // create sprite based on body _bodySprite = new Sprite(ScreenManager.Assets.TextureFromShape(_bodyMain.FixtureList[0].Shape, MaterialType.Squares, Color.Orange, 1f)); } public override void Draw(GameTime gameTime) { ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View); ScreenManager.SpriteBatch.Draw(_bodySprite.Texture, ConvertUnits.ToDisplayUnits(_bodyMainHolder.Position), null, Color.Green, _bodyMainHolder.Rotation, _bodySprite.Origin, 1f, SpriteEffects.None, 0f); ScreenManager.SpriteBatch.Draw(_bodySprite.Texture, ConvertUnits.ToDisplayUnits(_bodyRightHolder.Position), null, Color.Green, _bodyRightHolder.Rotation, _bodySprite.Origin, 1f, SpriteEffects.None, 0f); ScreenManager.SpriteBatch.Draw(_bodySprite.Texture, ConvertUnits.ToDisplayUnits(_bodyTopHolder.Position), null, Color.Green, _bodyTopHolder.Rotation, _bodySprite.Origin, 1f, SpriteEffects.None, 0f); ScreenManager.SpriteBatch.Draw(_bodySprite.Texture, ConvertUnits.ToDisplayUnits(_bodyMain.Position), null, Color.Yellow, _bodyMain.Rotation, _bodySprite.Origin, 1f, SpriteEffects.None, 0f); ScreenManager.SpriteBatch.Draw(_bodySprite.Texture, ConvertUnits.ToDisplayUnits(_bodyTop.Position), null, Color.Red, _bodyTop.Rotation, _bodySprite.Origin, 1f, SpriteEffects.None, 0f); ScreenManager.SpriteBatch.Draw(_bodySprite.Texture, ConvertUnits.ToDisplayUnits(_bodyRight.Position), null, Color.Red, _bodyRight.Rotation, _bodySprite.Origin, 1f, SpriteEffects.None, 0f); ScreenManager.SpriteBatch.End(); _border.Draw(); base.Draw(gameTime); } } }