Advertisement
tylerf001

SceneItem.cs Farseer Test

Apr 9th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using FarseerPhysics.Common;
  6. using FarseerPhysics.Common.Decomposition;
  7. using FarseerPhysics.Common.PolygonManipulation;
  8. using FarseerPhysics.Dynamics;
  9. using FarseerPhysics.Factories;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12.  
  13. namespace FarseerSimple
  14. {
  15.     public class SceneItem
  16.     {
  17.         public Vector2 Position;
  18.         public Texture2D Texture;
  19.         public Body FarseerBody;
  20.  
  21.         public float PositionX {
  22.             get {
  23.                 return Position.X;
  24.             }
  25.             set {
  26.                 Position.X = value;
  27.             }
  28.         }
  29.  
  30.         public float PositionY {
  31.             get {
  32.                 return Position.Y;
  33.             }
  34.             set {
  35.                 Position.Y = value;
  36.             }
  37.         }
  38.  
  39.  
  40.         public void CreateFarseerObject(World world) {
  41.             SceneItem item = this;
  42.             Texture2D polygonTexture = item.Texture;
  43.  
  44.             //Create an array to hold the data from the texture
  45.             uint[] data = new uint[polygonTexture.Width * polygonTexture.Height];
  46.  
  47.             //Transfer the texture data to the array
  48.             polygonTexture.GetData(data);
  49.  
  50.             //Find the vertices that makes up the outline of the shape in the texture
  51.             Vertices textureVertices = PolygonTools.CreatePolygon(data, polygonTexture.Width, false);
  52.  
  53.             //The tool return vertices as they were found in the texture.
  54.             //We need to find the real center (centroid) of the vertices for 2 reasons:
  55.  
  56.             //1. To translate the vertices so the polygon is centered around the centroid.
  57.             //Vector2 centroid = -textureVertices.GetCentroid();
  58.             //textureVertices.Translate(ref centroid);
  59.  
  60.             //2. To draw the texture the correct place.
  61.             //SceneItem.Pivot = -centroid;
  62.             //Offset = -centroid;
  63.  
  64.             //We simplify the vertices found in the texture.
  65.             textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 1f);
  66.  
  67.             //Since it is a concave polygon, we need to partition it into several smaller convex polygons
  68.             List<Vertices> list = BayazitDecomposer.ConvexPartition(textureVertices);
  69.            
  70.             //Create a single body with multiple fixtures
  71.             FarseerBody = BodyFactory.CreateCompoundPolygon(world, list, 1f, item.Position, item);
  72.             //Body = BodyFactory.CreateCompoundPolygon(IceFarseerManager.StaticWorld, list, 1f, BodyType.Dynamic);
  73.             //Body = BodyFactory.CreateCircle(IceFarseerManager.Instance.World, polygonTexture.Width * 0.5f, 1.0f, SceneItem.Position, SceneItem);
  74.             FarseerBody.BodyType = BodyType.Static;
  75.  
  76.             FarseerBody.IgnoreGravity = true;
  77.             FarseerBody.Enabled = true;
  78.             FarseerBody.Awake = true;
  79.             FarseerBody.CollidesWith = Category.All;
  80.             FarseerBody.CollisionCategories = Category.All;
  81.             FarseerBody.OnCollision += new OnCollisionEventHandler(Game1.Body_OnCollision);
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement