I have the following bit of code, which basically takes a Box2D world and converts it to SFML shapes that are then drawn to the screen. For rectangles, this works perfectly, but for other polygons, it produces a strange gap between the object and it's representation in Box2D which manifests as a gap between objects on screen. Can anyone see why this might be the case?
b2PolygonShape* polyShape = (b2PolygonShape*) bodyFixture->GetShape();
ConvexShape shapeToFill;
shapeToFill.setPosition(body->GetWorldCenter().x*SCALE, body->GetWorldCenter().y*SCALE);
int vertCount = polyShape->GetVertexCount();
shapeToFill.setPointCount(vertCount);
for(int vert = 0 ; vert < vertCount ; vert++) {
b2Vec2 aVertex = polyShape->GetVertex(vert);
sf::Vector2f sfVect;
sfVect.x = aVertex.x*SCALE;
sfVect.y = aVertex.y*SCALE;
shapeToFill.setPoint(vert,sfVect);
}
shapeToFill.setRotation(180/b2_pi * body->GetAngle());
UPDATE, SOLVED: The problem lay with this line:
shapeToFill.setPosition(body->GetWorldCenter().x*SCALE, body->GetWorldCenter().y*SCALE);
which needed to be changed to:
shapeToFill.setPosition(body->GetPosition().x*SCALE, -body->GetPosition().y*SCALE);
The world center of a body is not it's centroid, but it's local origin.