PolygonShape is not updated by its body

Jan 14, 2013 at 6:06 PM
Edited Jan 14, 2013 at 6:10 PM

Hi there,

I'm trying to define a polygon to be updated by a body bounded by a physical world. Here it goes my attempt:


public
Body _body; public Shape _shape; _primitiveBatch = new PrimitiveBatch(_game.GraphicsDevice); _shape = new PolygonShape(_vertices, 1); Vector2 _position = FarseerPolygon.getCenter(((PolygonShape)_shape).Vertices); _body = new Body(_world); _body.Position = _position; _body.CreateFixture(_shape); _body.BodyType = BodyType.Dynamic; _body.Restitution = 0.9f; _body.Friction = 1f;

The body changes its position but not the shape. Body.CreatePolygon also doesn't work. Any help would really be appreciated because I'm stucked in here... Thank you,


Coordinator
Jan 19, 2013 at 9:09 PM

The shape never changes it position as it is copied over to the fixture, and the vertices of a shape never change anyway, as they are transformed instead.
To draw the vertices the correct place, you need to transform the vertices to world space:

Vector2[] tempVertices = new Vector2[Settings.MaxPolygonVertices]
Transform xf;
body.GetTransform(out xf); //Contains the transformations

for (int i = 0; i < shape.Vertices.Count;; ++i)
{
    //Apply the transformations to each vertex
    tempVertices[i] = MathUtils.Mul(ref xf, poly.Vertices[i]);
}

//Now we can draw the vertices in the tempVertices array.
.... drawing code ....