123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System.Collections.Generic;
- using System.Text;
- using FarseerPhysics.Common;
- using FarseerPhysics.Common.Decomposition;
- using FarseerPhysics.Common.PolygonManipulation;
- using FarseerPhysics.Dynamics;
- using FarseerPhysics.Factories;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace FarseerPhysics.SamplesFramework
- {
- internal class AdvancedDemo1 : PhysicsGameScreen, IDemoScreen
- {
- private Border _border;
- private Body _compound;
- private Vector2 _origin;
- private Texture2D _polygonTexture;
- private float _scale;
- #region IDemoScreen Members
- public string GetTitle()
- {
- return "Texture to vertices";
- }
- public string GetDetails()
- {
- StringBuilder sb = new StringBuilder();
- sb.AppendLine("TODO: Add sample description!");
- sb.AppendLine(string.Empty);
- sb.AppendLine("GamePad:");
- 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(" - 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);
- //load texture that will represent the physics body
- _polygonTexture = ScreenManager.Content.Load<Texture2D>("Samples/object");
- //Create an array to hold the data from the texture
- uint[] data = new uint[_polygonTexture.Width * _polygonTexture.Height];
- //Transfer the texture data to the array
- _polygonTexture.GetData(data);
- //Find the vertices that makes up the outline of the shape in the texture
- Vertices textureVertices = PolygonTools.CreatePolygon(data, _polygonTexture.Width, false);
- //The tool return vertices as they were found in the texture.
- //We need to find the real center (centroid) of the vertices for 2 reasons:
- //1. To translate the vertices so the polygon is centered around the centroid.
- Vector2 centroid = -textureVertices.GetCentroid();
- textureVertices.Translate(ref centroid);
- //2. To draw the texture the correct place.
- _origin = -centroid;
- //We simplify the vertices found in the texture.
- textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 4f);
- //Since it is a concave polygon, we need to partition it into several smaller convex polygons
- List<Vertices> list = BayazitDecomposer.ConvexPartition(textureVertices);
- //Adjust the scale of the object for WP7's lower resolution
- #if WINDOWS_PHONE
- _scale = 0.6f;
- #else
- _scale = 1f;
- #endif
- //scale the vertices from graphics space to sim space
- Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * _scale;
- foreach (Vertices vertices in list)
- {
- vertices.Scale(ref vertScale);
- }
- //Create a single body with multiple fixtures
- _compound = BodyFactory.CreateCompoundPolygon(World, list, 1f, BodyType.Dynamic);
- _compound.BodyType = BodyType.Dynamic;
- }
- public override void Draw(GameTime gameTime)
- {
- ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
- ScreenManager.SpriteBatch.Draw(_polygonTexture, ConvertUnits.ToDisplayUnits(_compound.Position),
- null, Color.Tomato, _compound.Rotation, _origin, _scale, SpriteEffects.None,
- 0f);
- ScreenManager.SpriteBatch.End();
- _border.Draw();
- base.Draw(gameTime);
- }
- }
- }
|