0
\$\begingroup\$

I'm starting to see how box2d works now, except I dont know how to scale the screen to diffrent screen sizes and how i can convert Sprite sizes to box2d units. I know box2d uses meters and such, but even with a conversion unit i don't understand how i can link a body to a sprite and set the sprite size to the body size.

Also, I read about viewports, but every tutorial i see does it diffrently and i don't know how to do it anymore

So how do i render my game so that it looks the same on every android screen, and how to i display sprite on box2d bodies? (how exactly does the unit conversion work?)

Thanks!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The conversion from a unit to another is a matter of a simple multiplication. You can implement a ConvertUnits static class with functions like ConvertToGameUnits(), and ConvertToBox2DUnits() to make it easier.

I haven't used libgdx or Box2D, but i have used Farseer Physics engine which is basically identical with box2D but made for C#. With that being said, bodies simply help you with physics simulation, you don't link bodies with sprites. If you wanted to have a rectangle in your game, ConvertToBox2DUnits() function to set the position/width/height of your rectangle, and then you would have to use the body.Position property and your ConvertToGameUnits() method, to draw your sprite on the current rectangle's position.

As for making your game look the same on every screen isn't exactly possible, since not every screen has the same aspect ratio with the game you are making. Lets say you want to draw a 720x1280(16:9) resolution texture on a 480x640(4:3) phone (horizontally). You would draw 50% less pixels (which is a simple scale down to 50%), and also you would have to either place black bars on the top and bottom side of you phone or stretch the image to fit the screen.

I think you should first check some example game written in libgdx and/or box2D and start from there.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .