How do I create body for irregular 2D objects

Topics: Developer Forum, User Forum
Aug 25, 2011 at 11:35 AM
Edited Aug 25, 2011 at 12:03 PM

Im trying to create a body of a 2D sprite that is irregular using Farseer 3.3.1.

Do I have to create set of vertices to create the shape? And then which function would work, BodyFactory.CreateCompoundPolygon or BodyFactory.CreatePolygon?

Aug 25, 2011 at 4:44 PM

take a look at this: http://farseerphysics.codeplex.com/discussions/254993

Aug 25, 2011 at 8:24 PM
Edited Aug 25, 2011 at 8:27 PM

Thanks Hellgate, I looked at the discussion and the farseer samples provided and did that Now I'm having the following problem.

http://pic.twitter.com/UUZXyBQ

Here is the code

 

 

basketTexture = Content.Load<Texture2D>(@"Textures\bucket");

// data from texture
uint[] data = new uint[basketTexture.Width * basketTexture.Height];

 // Transfer the texture data to array 
 basketTexture.GetData(data);

 // Find the vertices
 Vertices textureVertices = PolygonTools.CreatePolygon(data, basketTexture.Width, false);

Vector2 centroid = -textureVertices.GetCentroid();
textureVertices.Translate(ref centroid);


basketOrigin = -centroid;

textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 4f);

List<Vertices> list = BayazitDecomposer.ConvexPartition(textureVertices);


//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;
Aug 26, 2011 at 8:56 AM

where at your code does the error apper?

i think here: Vertices textureVertices = PolygonTools.CreatePolygon(data, basketTexture.Width, false);

Aug 26, 2011 at 10:05 AM
This line causes the problem
_compound = BodyFactory.CreateCompoundPolygon(world, list, 1f, BodyType.Dynamic);
Aug 26, 2011 at 11:20 AM

Ah I solved the problem. scale was set to 0.
Now how would i get to collide the object with other body object. It other body just seems to go through it.
And Applying force on this body makes it rotate rather move in particular directions

Aug 27, 2011 at 10:57 AM
emwhytee wrote:

Ah I solved the problem. scale was set to 0.
Now how would i get to collide the object with other body object. It other body just seems to go through it.
And Applying force on this body makes it rotate rather move in particular directions


applying a force at the origin can't cause a rotation!

and may check the collision category