Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- 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 FarseerSimple
- {
- public class SceneItem
- {
- public Vector2 Position;
- public Texture2D Texture;
- public Body FarseerBody;
- public float PositionX {
- get {
- return Position.X;
- }
- set {
- Position.X = value;
- }
- }
- public float PositionY {
- get {
- return Position.Y;
- }
- set {
- Position.Y = value;
- }
- }
- public void CreateFarseerObject(World world) {
- SceneItem item = this;
- Texture2D polygonTexture = item.Texture;
- //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.
- //SceneItem.Pivot = -centroid;
- //Offset = -centroid;
- //We simplify the vertices found in the texture.
- textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 1f);
- //Since it is a concave polygon, we need to partition it into several smaller convex polygons
- List<Vertices> list = BayazitDecomposer.ConvexPartition(textureVertices);
- //Create a single body with multiple fixtures
- FarseerBody = BodyFactory.CreateCompoundPolygon(world, list, 1f, item.Position, item);
- //Body = BodyFactory.CreateCompoundPolygon(IceFarseerManager.StaticWorld, list, 1f, BodyType.Dynamic);
- //Body = BodyFactory.CreateCircle(IceFarseerManager.Instance.World, polygonTexture.Width * 0.5f, 1.0f, SceneItem.Position, SceneItem);
- FarseerBody.BodyType = BodyType.Static;
- FarseerBody.IgnoreGravity = true;
- FarseerBody.Enabled = true;
- FarseerBody.Awake = true;
- FarseerBody.CollidesWith = Category.All;
- FarseerBody.CollisionCategories = Category.All;
- FarseerBody.OnCollision += new OnCollisionEventHandler(Game1.Body_OnCollision);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement