This project has moved and is read-only. For the latest updates, please go here.

Rendering Custom Shapes

Oct 20, 2010 at 8:30 PM
Edited Oct 20, 2010 at 9:03 PM

Just a quick question, 

I am using farseer 3.0 in a game i am developing, 

To allow the artists to more easily add content I am using the TextureVerticies method to automatically generate collision hulls,

however they are being created upside down and they are also created off center as the content is not always in the exact centre of the image that is converted,

below is a screenshot highlighting the issue and my code, does anyone have any suggestions. Thanks

(it should also be noted that positive y values produce upwards transformations in my renderer)



using System;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SpriteGame.Rendering;
using System.Collections.Generic;
using FarseerPhysics.Common.Decomposition;

namespace SpriteGame.Entities
    class ComplexShape:BaseSprite
        public override Vector2 Position
                FarseerPhysics.DebugViewXNA.DebugViewXNA.Instance.DrawPoint(body.WorldCenter, 0.2f, Color.Red);
                return body.WorldCenter;
                base.Position = value;

        List<Fixture> compound;

        public ComplexShape(Scene _parent, BodyType _type, Vector2 _position, float _rotation, String _texture)
            parent = _parent;
            world = parent.World;

            texture = _texture;

            List<Vertices> list;
            Vertices verts;

            Texture2D polygonTexture = TextureLibrary.GetTexture(_texture);

            width = polygonTexture.Width * 0.07f;
            height = polygonTexture.Height * 0.07f;

            uint[] data = new uint[polygonTexture.Width * polygonTexture.Height];

            //Find the vertices that makes up the outline of the shape in the texture
            verts = PolygonTools.CreatePolygon(data, polygonTexture.Width, polygonTexture.Height, true);

            //For now we need to scale the vertices (result is in pixels, we use meters)
            Vector2 scale = new Vector2(0.07f, 0.07f);
            verts.Scale(ref scale);

            //Since it is a concave polygon, we need to partition it into several smaller convex polygons
            list = BayazitDecomposer.ConvexPartition(verts);


            //Create a single body with multiple fixtures
            compound = FixtureFactory.CreateCompundPolygon(World, list, 1);
            compound[0].Body.BodyType = _type;

            body = compound[0].Body;
            body.SetTransform(_position, 0.0f);

        public override void Draw(ShapeRenderer _batch)
            //FarseerPhysics.DebugViewXNA.DebugViewXNA.Instance.DrawPoint(Position, 12.8f, Color.Orange);
	    //The origin of the quad to be drawn is automatically set to the midpoint
            _batch.DrawQuad(Position, Rotation, width, height, TextureLibrary.GetTexture(texture)); 


Oct 21, 2010 at 5:59 PM

your physics polygon is almost same as the image.

But how do to it?

my code is nearly similar with you.

however my polygon is difference  about 180 degree with orginal image.

Who can give us good solution about these?

Oct 22, 2010 at 2:35 AM

Louis -

You need to Translate your vertices with this vector -

Vector2(-texture.X / 2, -texture.Y / 2)

CreatePolygon does not return vertices centered around the origin of the texture.

Let me know if that helps.

BlackDragon -

I don't have to flip my texture but I think that is just because of the way I render (with the -y down and +y up)

Flipping your image is as easy as translating by the above vector and then flipping the signs on all the Y components of your vertices.



Also I have noticed that the Bayazit Decomposer sometimes does not produce correct results. I recommend using the Earclip Decomposer to make sure your getting the proper vertices.

Oct 22, 2010 at 5:43 PM

Thank you.

I have just fixed by your suggestion.