OpenGL fitting objects to any screen size on Android

Started by
2 comments, last by _WeirdCat_ 3 years ago

I've made my own simple Level Editor in Visual Studio using OpenGL and C++ to create games for android phone. But I've stumbled across a problem where some objects that I have put in my editor do not appear correctly on android nor and sometimes they don't even appear. I have been searching around and could not find a solution. So here is my lLevel Editor's orthographic camera and resolution of the screen:

Camera objects Ortho Matrix:

GoldState::aspectRatio = m_AspectRatio = (float)width / (float)height;
    float orthoLeft = 0.0f; //* m_AspectRatio;
    float orthoRight = 100.0f; //* m_AspectRatio;
    float orthoUp = 100.0f;
    float orthoDown = 0.0f;
    m_OrthoMatrix = glm::ortho(orthoLeft, orthoRight, orthoDown, orthoUp);

And its exactly the same for android too(using ndk).

The aspect ratios are commented out because I was experimenting and without multiplying by aspect ratio entire world will get stretched.

Now to unity's Free Aspect Ratio which I have a feeling is just like my ortho camera without multiplying aspect ratio. What exactly is that and how does every object that I place on unity when I select Free Aspect Ratio stays 1:1 and it is not stretched or anything? If someone can explain this to me that would really help me a lot.

Just one more thing. I have made several games on android using opengl but I specificly made background image to be exact width and height because I was multiplying aspect ratio in my ortho matrix all the time. But this time I just want to extract background and objects positions and sizes and have them keep their aspect without stretching on different resolutions.

If there is any other general solution for this that would be nice if there is not I guess I should use some fixed resolution on level editor and use that same resolution for android?

Ayy lmao

Advertisement

There can't be a general solution if i get you right. I have used an approach like this, and assume it's pretty standard (IDK what Unity does):

The black rectangle is the average aspect ratio of current smart phones. (I have no idea what this currently is in numbers)

And we design our game within this rectangle, but we also ensure there is still enough content within some overlap, so outliers with different ratios (green or red rectangle) can be still supported.

Things like HUD can be placed over the additional area as well pretty easily and then it should look fine and work without using something like black borders. Depends on the game ofc.

For development i have supported dynamic screen size, so on PC i was able to change resolutions using some sliders at runtime. This helped to ensure there won't be problems from different screens.

If pixel art is involved and we want integer scaling it becomes harder, because resolution now matters too, not just aspect ratio. I've had this case for a top down puzzle game on older cell phones. Level design was affected a bit here, but no real problem. I have used a similar black rectangle assumption ensuring X x Y tiles are visible on any device.

Guess that's an obvious answer, but maybe it helps ; )

@JoeJ Hey thanks for an answer! It helped me get some good ideas from an answer.

Ayy lmao

I may misunderstood this: but you want a 1 cm square to appear 1 cm on every screen, first of all you need to get physical screen size let it be vec2 scr_size;

Hiwever you don't want to multiply by aspect ratio, which anyway is not the case cause you already multiple by projection matrix..

Now positioning on the screen… you don't think of it in terms of positioning this square at center 0.5, 0.5 but rather by scr_size / 2.0

Let now scr size be in milimeters

Square_size = vec2(10.0 / scr_size.x, 10.0 / scr_size.y);

You first need to take account for ‘designed’ square sizes and feed that data to vertex buffer

This topic is closed to new replies.

Advertisement