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)
public override Vector2 Position
FarseerPhysics.DebugViewXNA.DebugViewXNA.Instance.DrawPoint(body.WorldCenter, 0.2f, Color.Red);
base.Position = value;
public ComplexShape(Scene _parent, BodyType _type, Vector2 _position, float _rotation, String _texture)
parent = _parent;
world = parent.World;
texture = _texture;
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);
//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.Body.BodyType = _type;
body = compound.Body;
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));
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
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.
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.
I have just fixed by your suggestion.