I have recently been trying to implement my first Android game using a similar approach to the infamous LunarLander. I am rendering the graphics myself without the aid of a game engine, and that seems to be the most complicated part of the endeavor so far.
The Problem
I am determining the width of the screen, and forwarding that information into an algorithm that determines the maximum number of images that can be rendered horizontally given the width and some other margin values (see calculateMaxBeadsInWidth()
below). However, the mathematics seem to not be matching up to the values computed by the algorithm. Basically, it is determining that only X
amount of images can be displayed, when in actuality X + 2
images can easily be displayed on the screen.
The Calculations
I have parsed many debug outputs that show that the actual width of the screen is 800 pixels
, and the image width is 44 pixels
. So, given that the margin is 100 pixels
(50 pixels
on the far left and 50 pixels
on the far right), that leaves 700 pixels
to work with. Now, floor(700 / 44) = 15
, so only 15 images are shown. But there is clearly space for more (see pictures)!!
The Code
My algorithm:
private int calculateMaxBeadsInWidth() {
float eff_width = screenWidth - (BOARD_MARGIN_HORIZONTAL * 2);
return (int) (eff_width / bead_width);
}
Note that the value of BOARD_MARGIN_HORIZONTAL
is 50.0f
.
Some Pictures
This is what is being produced by my algorithm:
However, as you can see it is obvious that you could easily fit at least 2 more beads towards the end of the lines (on the right). For instance, this picture shows what happens when I hard code the number of beads in one row to two more than the algorithm is producing:
Here is the image with the margins detailed with red lines. As you can see there is still plenty of space:
Supplemental Information
I am testing this application on the Google Nexus 7.
I am using getResources().getDisplayMetrics().widthPixels;
to obtain the width of the screen. Perhaps the issue is here?
My Question
Are there some resolution values that I am not taking into consideration here that are affecting the results? Why is this being computed so inaccurately?
If you need any more information I will gladly provide it, any help would be appreciated greatly!