Hello
I'm having an issue at the minute with a prototype I'm working on with box2d.
I create a dynamic box and a sprite, these are to act as one object with the sprite for visuals and the box for collisions etc. pretty standard stuff.
The issue is, the further away from the windows origin (top left) the box goes, the further offset the sprite is from the box. So in the top left corner the sprites texture is inside the body's box then in the bottom right corner the textures totally out of the box.
The sprites origin is set as the centre.
In my update function I set the position of the sprite using;
Sprite.setPosition(Body->getPosition().x * 30.f, Body->getPosition().y * 30.f)
Sorry for semi-pseudocode but I just rage quit my laptop and don't want it back in until I have some ideas to try out.
I've hit up Google and read box2d and sfml docs with no joy to why these 2 components would drift out like this.
Thanks for your help.
EDIT:
Code provided below.
entity creation
//body creation
b2BodyDef bodyDef;
bodyDef.position = b2Vec2(pos.x/SCALE, pos.y/SCALE);
bodyDef.type = b2_dynamicBody;
bodyDef.fixedRotation = true;
b2Body* body = world.CreateBody(&bodyDef);
b2PolygonShape shape;
shape.SetAsBox(((size.x)/2)/SCALE, ((size.y)/2)/SCALE);
b2FixtureDef fixtureDef;
fixtureDef.density = 4.f;
fixtureDef.friction = 3.f;
fixtureDef.shape = &shape;
body->CreateFixture(&fixtureDef);
//sprite creation
texture.loadFromFile(filename));
sprite.setTexture(texture);
sprite.setOrigin(_size / 2.f);
sprite.setPosition(spawnPos);
update function
b2Vec2 pos = body->GetPosition();
sprite.setPosition(SCALE * pos.x, SCALE * pos.y);
And some screens
Close to origin
Furthest point from origin
Collisions between bodies working fine