[LibGdx]PPM conversion Error

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)

http://i.minus.com/iwmEg8BniGHiD.gif

EDIT: In my main launcher the window width and height:

public static final int V_WIDTH = 1000;
	public static final int V_HEIGHT = 1000/16 * 9;

In my main screen where I display the boxes:

camera = new OrthographicCamera(Gdx.graphics.getWidth()/ PPM, Gdx.graphics.getHeight()/ PPM);

Full Screen Class: http://pastebin.java-gaming.org/3f37157410b1e

Post the native output error from the console.

Also your whole PPM conversion is overly complicated.

To keep things simple, use a fixed viewport like so:

camera = new OrthographicCamera(16, 9);

^ This here will set the viewport to 16x9 meters, which is obviously a 16:9 ratio.

Then you pick a scale, such as 1/100f. Then you can multiply any pixel values to convert them to meter values.

EDIT:

I suspect you have this error -

AL lib: (EE) alc_cleanup: 1 device not closed
Assertion failed!

Program: A:\Java 7\bin\javaw.exe
File: /var/lib/jenkins/workspace/libgdx/gdx/jni/Box2D/Collision/Shapes/b2PolygonShape.cpp, Line 397

Expression: area > 1.19209289550781250000e-7F

This means your shape is far to small and the vertices are reversed and it can only be creating, I believe counter clock wise.

Can you give me an example of what you mean by this?

Make it a float: float PPM = 6.0f;

An int won’t work because if you do 5/6 it returns 0, like you do here:

shape.setAsBox(5 / PPM, 5 / PPM);

Everything works fine, I just don’t get why the camera is 16,9 when I initialize it to be 16:9. What I mean:

camera = new OrthographicCamera(16, 9);
		camera.setToOrtho(false, 1000/ PPM, 1000/16*9/ PPM);

Am I doing somthing wrong here?

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:

sprite.setSize(sprite.getWidth() * SCALE, sprite.getHeight() * 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.