Understand ViewPort and Box2D Coordinates

Anything libgdx related goes here!

Understand ViewPort and Box2D Coordinates

Postby Red&Black » Sat Mar 10, 2018 3:35 pm

Hello I would like to understand ViewPort and Box2D coordinates.

Let's make a practical example: The background bitmap of my game is 320x240 pixel.
I should use an StretchViewport(320, 240) to keep it always the same on different devices/resolutions.
Let's suppose that my bitmap player is 40x40 pixel, I should create a new Actor with bounds to (x - 20, y - 20, 40, 40).
The game looks fine. Everything has the correct size on any display resolution.

Now let's suppose that I would make my player a Box2D body, so I should create a new Body and to this body I should attach a new fixture, for simplicity suppose a simple box PolygonShape which dimensions should this box has? Or better, how convert 40pixel in Box2D size? Which is the Box2D unit?

Thank you :)
Red&Black
 
Posts: 16
Joined: Fri Feb 02, 2018 8:28 pm

Re: Understand ViewPort and Box2D Coordinates

Postby evilentity » Sat Mar 10, 2018 4:15 pm

Stretch is like the worst viewport you can use. Its not correct. Its distorted on most of them. Pixels is most likely not something you should be thinking about. https://xoppa.github.io/blog/pixels/

And the box2d body should be 1x1. Box2d is designed to work with stuff around a meter. It ususaly makes sense to make basic game thing 1x1 or something close enough to that.
Looking for a freelancer? PM me!
evilentity
 
Posts: 4216
Joined: Wed Aug 24, 2011 11:37 am

Re: Understand ViewPort and Box2D Coordinates

Postby Red&Black » Sat Mar 10, 2018 4:42 pm

I would like to use Stretch because my idea is to make a pixel 8bit style game, so for this goal is the best choice I've found. Working with a virtual screen size with fixed number of pixel is exactly what I need for this game, I've already tested the graphic on many devices with different resolution and it display always fine :D
But I need a constant conversion between virtual viewport and Box2D game.
Red&Black
 
Posts: 16
Joined: Fri Feb 02, 2018 8:28 pm

Re: Understand ViewPort and Box2D Coordinates

Postby evilentity » Sat Mar 10, 2018 6:01 pm

Well, personally, i dont really like rectangular pixels.
Looking for a freelancer? PM me!
evilentity
 
Posts: 4216
Joined: Wed Aug 24, 2011 11:37 am

Re: Understand ViewPort and Box2D Coordinates

Postby tenfour04 » Mon Mar 12, 2018 5:50 pm

Most phones are 16:9, but some aren't and your game will look like crap on those ones.

Use an ExtendViewport. Make a background big enough to cover at least 18.5:9 and keep your gameplay inside the center 16:9 area. Use black bars on narrower screens, since those are rare. Or you can go the extra mile and also make your background big enough to cover 4:3.
tenfour04
 
Posts: 1179
Joined: Sat Jun 18, 2011 3:24 pm

Re: Understand ViewPort and Box2D Coordinates

Postby Red&Black » Tue Mar 13, 2018 4:28 pm

Thank you for the tips, I've tried on 18:9 device and I've understand the issue, so I'll switch to fit or extend viewport, but what about the original question? I would like to use however a virtual display with fixed dimension, how to transform viewport coords into box2d world?
Red&Black
 
Posts: 16
Joined: Fri Feb 02, 2018 8:28 pm

Re: Understand ViewPort and Box2D Coordinates

Postby evilentity » Tue Mar 13, 2018 9:22 pm

If you setup it in sensible way, you dont have to. You make 1x1 box. You attach to it 1x1 sprite or whatever. Done. Read the blog.
Looking for a freelancer? PM me!
evilentity
 
Posts: 4216
Joined: Wed Aug 24, 2011 11:37 am

Re: Understand ViewPort and Box2D Coordinates

Postby litelitelite » Tue Mar 13, 2018 9:41 pm

On the viewport question, the fit viewport seems the nicest for me. If you want to have a background that fills the screen without black bars, then you can use another viewport (e.g. extend viewport) to just hold the background.

(edit - you probably don't want to do the below...)
On Box2D, I found that I needed to get my head around the idea that you are running a Box2D simulation in the background and then painting your scene each frame based on the current position of everything in the simulation. So long as the aspect ratio is the same for the Box2D sim and the virtual screen, you simply map the Box2D sim to your playable area using some scaling factor (pixels per metre). So if your 40px sprite would be 1m tall in real life, then you scale everything by 40 to go from Box2d coords to pixel coords and by 1/40 to go back again (for example, for screen touch input).

There is a short but maybe useful article here that covers this same idea in different words: http://box2d.org/2011/12/pixels/
Last edited by litelitelite on Thu Mar 15, 2018 10:16 pm, edited 1 time in total.
litelitelite
 
Posts: 19
Joined: Sat Jul 15, 2017 9:43 pm

Re: Understand ViewPort and Box2D Coordinates

Postby tomski » Wed Mar 14, 2018 8:23 am

That is incorrect.

As the article says,
If your game must work in pixel units...


LibGDX does not impose any such limitation. You should be using world units for both box2d and drawing. No pixels or weird pixel per meter conversions are required. If anything it makes things worse.
tomski
 
Posts: 729
Joined: Wed Jun 26, 2013 6:06 pm

Re: Understand ViewPort and Box2D Coordinates

Postby litelitelite » Thu Mar 15, 2018 10:15 pm

OK, thanks for response, I get it. You are of course correct and I guess I was confused by all the how-tos I read that included PPM scaling!

I don't know why it didn't occur to me to set the viewport to world scale rather than the scale of typical screen-pixels...

Man, that does simplify things a lot, I only wish I could go back a couple of years now and avoid a bunch of wasted time :) Thanks!
litelitelite
 
Posts: 19
Joined: Sat Jul 15, 2017 9:43 pm


Return to Libgdx

Who is online

Users browsing this forum: Google [Bot] and 26 guests