[LibGDX + Box2D] Scaling between World and Box

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 :frowning: ) 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?

Scale:
There is a speed-limit, so you need to scale down the world to have fast moving bodies/change something in the config (don’t know if it’s possible in libgdx), also it’s not recommended.
1 pixel = 1 m is a bad idea, bodies will move max 120 px/s.
It’s better to use a camera that handles the render-size, I wouldn’t use pixelToBox2dMeter-values.

Create Box2d-stuff;
Atm I am saving the objects in json-files so I don’t need to “create” them manually.
Some time ago, I used “createDefaultBox2dBody()” inside the entity.class,
for creating different Bullets I had


class bulletUtiles.class

public static Bullet createBullet(Bullettype type){
switch(type)
case....

}

public enum Bullettype{
//usefull names
}


So it appears that my game assumes 1 pixel = 1 meter… Hence the bullet has the same speed as the tank itself ;D
What kind of disadvantages do these BOX_TO_WORLD and WORLD_TO_BOX constants have?

And I am sorry but I wasn’t able to even think of a camera that handles render size. How would I do that?

This is the single most difficult concept I had to grasp when I was starting out with LibGDX and Box2D :stuck_out_tongue:

At first, things may probably be fine, but I believe that if you choose to stick to 1 pixel = 1 meter… there comes a point where if you create object too big and too heavy just so you could render it the way you want on-screen, the physics simulation becomes inaccurate.

I forgot for what mass/size Box2D starts to have problems…

I hardly find time for learning (final exams etc) so I start small. I mean one thing a day, in 1 hour.

So when I read about that scaling thing and started to write my classes, I was waiting for the point where I will use the scaling constants I talk about. But because I only have one character and one ‘enemy’. 2 days ago, I sat down and made a bullet. It does everything I’d expect from it, except for the speed :smiley: Tank and bullet has the same speed. So I figured this is about scaling.

But I can’t find out how I can fix this.

I just need to see some classes where the author had to scale the box to world, or scale the sprite to box.

Any pointers, clues or links will be appreciated.

Start with: http://code.google.com/p/libgdx/wiki/OrthographicCamera
and: http://code.google.com/p/libgdx/wiki/SpriteBatch
Afterwards just create box2d-bodies, don’t care about box2d-toWorld-Size etc.
If you zoom in, the box2d-bodies seems to get bigger, but that’s just because you zoom in. Of course you can also zoom out.
As you will use a spritebatch, the everything will be done for you :slight_smile:

Philbedy,
I read these before. Actually I use both camera and spritebatch like they explain there. But I read them again, with paying more attention. Still I cannot find anything that would help me with designing a camera that handles render-size.
I already use a camera and a spritebatch. I set the spritebatch projection matrix to camera.combined. Maybe that’s what I should change?

Actually, my initial issue was not that. My bullet goes too slow. That should be because of scaling. I mean 1 pixel should be 5 or 10 meters so that I can slow down my ship and bullet seems like it’s going faster I am thinking…

just use camera.zoom = “some float”
if you set camera.zoom = 0.1f your objects are rendered bigger,
if you set camera.zoom = 2f your objects are rendered smaller.

Ah… We had a big misunderstanding here. Probably my thread title is the cause of that. And instead of telling my real problem, I asked about the method I thought to be the solution to that.

I have a tank and a bullet. Although I apply 1000 N force to tank and 10000000 N force to the bullet, they move at the same speed. Because Box2D has a speed limit. I wanted to know how to overcome this.
Not about zooming or scaling images. Actually, I ask about scaling between box2d and libgdx.

Phibedy I apologize for taking your time.

np :slight_smile:
just reduce the size of everything and zoom in -> the speed won’t increase, but:
steps:
0. looks like always

  1. you reduce the size of all box2d-bodies
  2. you zoom in (camera)
    -> looks like always
  3. apply forces
    -> your bullet now seems to move faster (indeed it isn’t moving faster).
  4. scale forces, so the tank will not be as fast as the bullet