I’m having trouble using the pixels per meter conversion in LibGdx. At the moment I am limited to 5 PPM, anything higher crashes. Here is an example (Didn’t get to capture the objects’ full gravity)
If you were not it would not crash, the reason you are crashing is because the scale is most likely tiny and causing inverted vetices.
So I’ll explain what I mean, so lets create a camera:
camera = new OrthographicCamera(16, 9);
Here we create a camera that is 16x9 meters. So you will be able to see 16x9 meters in the frustrum at any given time, you could go ahead and do 32x18, to allow a larger view or whatever, which is useful if you have say a platformer game and it has lots of enemies that are around 1.5-2 meters tall, it would appear very “zoomed in” at 16:9 but at 32x18 it will look further away.
What this basically does is, regardless of resolution of the device, the viewport will not change size and sprites will appear as the same size on everything. so a 200x200 sprite on a 1080p screen will have the exact same physical size on a 240p screen. So lets setup a scale:
final float SCALE = 1/100f;
This means we are using 100 px = 1 meter.
So our 200x200 sprite, we want to create this and put it into our game world. Let’s do that now:
Sprite sprite = new Sprite(new Texture(Gdx.files.internal("SomeAwesomeSprite.png")));
If we try to draw this onto the screen with our current camera projection, all we will see is a bunch of super duper large pixels representing most likely a small portion of the bottom left corner of said sprite. We need to set the size of the sprite to match the scale:
Now our sprite is scaled down to size, you can draw it now properly.
Why I have picked 1/100f as a scale?
Your art needs to be consistant, it will make your life 100x easier if everything is to the same scale. At no point in your code should you be scaling down or scaling up your art, it should be made at the size it will be used in your game. So therefore when creating art we can always divide whatever pixel value we are using by 100 to get the world coordinates.
So a regular human character created at 50x100px would be 0.5m wide and 1m tall, he seems a little short and fat no? A more realistic sprite size would be 25x180px, 0.25m wide and 1.8m tall, that is a little closer to the average height of a human.
This is with a realistic human though, those are boring but you get the point.
Of course this is with an easy, non resizing viewport (which imo is better 90% of the time anyway).
Doing this will also allow you to stick to Box2D’s rules, having things as close to the size of the real thing as possible for optimal performance. If you have a Player and a Bus, they should be drawn in proportion relative to each other.