I know there are already many posts for similar issues, but I've read all of them and am somehow still struggling to get SFML shapes to align to Box2d bodies. The SFML shapes seem to be larger than the Box2d bodies and are offset. It'll probably be something simple, but I've checked over and over again and can't see the problem...
To create an 'object', I give SetAsBox() half of width/height, convert angles between radians and degrees, setOrigin() to half of width/height and divide by my scale (30) where necessary. In my main loop I setPosition() to the body's position * 30, and setRotation() to the body's angle * (180 / b2_pi).
My code is pretty disorganised at the moment, so here are the important bits...
Creating body and shape:
//phys body
b2BodyDef tbodyDef;
tbodyDef.type = b2_dynamicBody;
tbodyDef.position.Set(500 / 30, 500 / 30);
tbodyDef.angle = 0;
tbody = world->CreateBody(&tbodyDef);
b2PolygonShape tdynamicBox;
tdynamicBox.SetAsBox(75 / 2 / 30, 75 / 2 / 30);
b2FixtureDef tfixtureDef;
tfixtureDef.shape = &tdynamicBox;
tfixtureDef.density = 1.0f;
tfixtureDef.friction = 0.3f;
tbody->CreateFixture(&tfixtureDef);
//sfml
tbox = new sf::RectangleShape;
tbox->setSize(sf::Vector2f(75, 75));
tbox->setFillColor(sf::Color(163, 36, 18, 100));
tbox->setOrigin(75 / 2, 75 / 2);
tbox->setRotation(0);
In main loop:
b2Vec2 position2 = tbody->GetPosition();
float32 angle2 = tbody->GetAngle();
tbox->setPosition((position2.x * 30), (position2.y * 30));
tbox->setRotation(angle2 * (180 / b2_pi));
mainWindow->draw(*tbox);
After many hours of confusion and frustration, I give up and hope someone can help me out. Apologies if I missed anything important and for my poor explanation, thanks in advance!