So I’m trying to learn my way in Libgdx and Box2d nowadays. But there are some concepts I don’t get.
First is, scaling. Both in Box2D Manual (PDF one) and Libgdx documentation, they say that we should scale our bodies and entities between box and world to get desired effects. And they give an example of two constants to achieve this easily:
static final float WORLD_TO_BOX = 0.01f;
static final float BOX_TO_WORLD = 100f;
I was trying to make a bullet when I realized I never used these and so far (I am doing very little right now, exams for 1 month ) nothing went wrong. For example, here is my bullet constructor (Because I am just testing, I preferred to create both sprite and body in the same place):
public MyBullet(MyWorld world) {
this.world = world;
sprite = new Sprite(MyAtlas.get("bullet"));
sprite.setScale(1 / 2f);
myBox = new MyBox(world.getWorld());
body = myBox.dynamicBody(new Vector2(world.getShipBody().getPosition().x, world.getShipBody().getPosition().y), sprite.getWidth() / 4,
sprite.getHeight() / 4, 1f, 1000f, 0f);
body.setGravityScale(0);
}
I set halfWidth as sprite.getWidth() / 4 because I scale the sprite down by 2. And this is my draw method:
sprite.setPosition(body.getPosition().x - sprite.getWidth() / 2, body.getPosition().y - sprite.getHeight() / 2);
sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
sprite.draw(world.getBatch());
Nothing is going wrong there and I did not scale a thing. So, why are we supposed to scale sprites and bodies to/from World to/from Box?
And how do we do that? Because if you consider the constants they give as example, scaling is by 100. I have 32x32 images, so it will be 0.32x0.32…
Is it something relative and am I just assuming that 1 pixel = 1 meter?
And there is another question on my mind which is about creating bodies and entities.
Where should I create body and entity? In the same class (maybe a createBody() method)? Or should I have a kind of ‘Box Factory’? Or something else? What is your approach to that?