I'm looking for an Algorithm or Pattern to calculate where objects can be placed on multiple resolutions. So far, I have got the X and Y screen size, but im not sure how to turn it into a calculation that would place something such as a drawText() at a location on the screen no matter the screen size.
I was thinking perhaps using percentages might be easier to work with. I have a program that draws text to the screen at the top left corner indicating what position the screen has been touched.
when i run this on the emulator, with the
drawText(info, 10,10, paint);
it displays great, but when i run it on my phone (Droid 2 Global) the top of the text is cut off.
In short:
Is there any way to draw something to the screen (using SurfaceView) that will remain in the same spot over multiple screen dimensions / dpi?
Thanks for your time.
It's no perfect solution that i've seen so far.
I came across this issue by specifying the position for the particular item for the specific screen ratio(native screen resolution in an emulator), then recalculate its position and scale it up/down when running it in the different screen size.
displayXpos = constDevelopmentScreenXpos (targetDeviceScreenHeight/constDevelopmentScreenHeight) etc..
displayXScale = similarAlgorithm
This is not the best, but it give you some idea.
Also, i fill some 'limbo' area with a background and choose to not care it when the target device X:Y screen ratio is different from development device.
HTH
ya, it is better to use calculation in percentage
first get the total size avail of the screen, then calculate percentage on it and the place the control based on calculation
to get avail size
Display mDisplay= activity.getWindowManager().getDefaultDisplay();
int width= mDisplay.getWidth();
int height= mDisplay.getHeight();
Related
I am using the formula to calculate speed and am using the screen width as distance and fps as time. The speed of the sprite is still different when moving across the screen. How should I change the formula in order to make the speed feel the same on all screen sizes?
public static float distanceX = Screen.width;
public static float time = 60f ;
public static float speed = Player.distanceX / Player.time;
To answer the question, you have to define what the "feel" needs to be. For example, suppose the device is held in landscape orientation. A 4:3 device is much narrower than a 16:9 device, so if you have a bunch of squares arranged horizontally on the screen, you'll have more of them on the 16:9 device than on the 4:3 device.
If your goal is to move from one side of the screen to the other in a constant amount of time, then the formula you show in your question is correct.
If your goal is to move past N squares in a constant amount of time, then you need a different formula. It sounds like this is what you want.
One approach is to define a device-independent "arena" in which everything moves, and then fit it to the device. For example, you could define the world to be 1000x1000, and that things move at 100 units per second. You then fit the arena to the screen by either cutting off the top and bottom, or letter-boxing the sides. A side-scrolling platformer would usually match the arena to the device height, and then show a wider or narrower field of view horizontally. (For an example of this approach, see Android Breakout.)
On a related note, it's unwise to assume that a device has a certain fixed frame rate. Some advice can be found in this appendix.
I am very beginner to game programming and Libgdx. I am really confused what camera viewport size should I use. In some articles i found that they used 480x800 which is same as target device size. In some articles I found the use meters as 5x5 meter.
So which method is better and how (if you can give some benefits).
If I use meter unites for camera viewport then which is first mapped, width or height.
If i use 5x5 meter for 480x800 pixels device then visible area of world
height = 5 meter = 480px and
width = 800/480 * 5 = 8.33 meter
or
width = 5 meter = 800px and
height = 480/800 * 5 = 3 meter
Is it correct calculation of visible world size and which is used first or second.
I am confused when they start using meter for size everywhere instead of pixels. like actor size is 1x1 meter even it is only 64x64 px. It is really difficult to estimate position and size for me.
Please link any good article about camera and camera units.
Whatever dimensions you specify, they'll be mapped to the entire screen by default. If the aspect ratios don't match, the displayed graphics will be stretched. So if you set your camera to a 5x5 coordinate system on a non-square screen without changing the drawing area, it'll be heavily distorted. If you render it in a square desktop window, it'll be fine.
The advantage of using smaller coordinate systems is that it's easier to calculate with, and possibly more meaningful in the context of a game - e.g. you can think of them as meters, as you said. It's useful in cases where the content matters more than the exact positions on the screen - like drawing the game world.
Using larger coordinates which match the resolution of some devices can be more useful when you're drawing UI. You can see how large you should make each image, for example, if you target that resolution. (Scaling can cause distortions.)
But ultimately, it's a matter of preference. Personally, I like smaller coordinate systems more, so I recently coded my level select menu in a 20*12 system. (I did run into problems when rendering a BitmapFont though - they were not very well made for scaling like this.) Others might prefer to work with resolution-sized coordinates for gameplay rendering as well. What matters is that you should make sure you're not distorting the graphics too much by badly matching aspect ratios.
try 136 for Width and 204 for Height
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!
Your margins are the wrong value. You uploaded full-size images, but if you draw vertical lines # 50px and 750px, they intersect the drawing area. So, you are either using the wrong value in your calculations, or somewhere else you are drawing in what is supposed to be margin.
I am trying to make an app whereby a picture will be shown, there're a few objects that user needs to identify (i.e. a cup), by touching the cup in the picture on the screen, a circle will be drawn if it's valid.
So far, I have a surfaceview with a bitmap as drawable to be displayed full screen, and upon touching the screen, a circle will be drawn to the view.
I could only think of getting the coordinate of the cup manually in the picture(hardcoded), and check it against the coordinate from touch event.getX() and event.getY(). But this would not work as screen resolution changes.
What is a better way in doing so? It's like I'm trying to find a way to precisely allow touch on certain areas which I've defined in my app.
You can store the coords as you were suggesting but adjust them based on the size of the SurfaceView vs. the size of the BitmapDrawable. For example, if you know a given area is 100px from the left, you could take your scale as surfaceView.getWidth() / bitmapDrawable.getIntrinsicWidth(). Multiply your scale times your value (100px) to get the final position. If you're keeping the same aspect ratio on the image, you need to get your scale from the larger of the two (width vs. height) and use that same scale for height and width.
In a given resolution, is the location of cup fixed or it varies there as well.
Also, what about the orientation (landscape, portrait)? does the location change accordingly as well?
There are standard resolutions, so you can always think of mapping the location with resolution.
Also, you can get X, Y coordinate and check if it falls within a range based on height and width of cup image.
First of all find out the size of screen (screen dimensions).
Now place objects relatively corresponding to your screen area.
eg. keep cup on SCREENWIDTH - 60 and check coordinates of touch similarly.
How to draw, say, a rectangle on the screen with it being proportional to the current device?
e.g. a rectangle, centered on the viewport, one pixel smaller than the screen on each border.
I can live with Orthogonal, but would like perspective (basically everything at Z=something should be proportional to the screen, and the upper parts of the elements being distorted by perspective)
I can calculate everything on my own if i know the relation... but i don't have a starting point.
I could experiment and get to a relation myself... i even resorted to that while coding for the Wii, but that's a really bad decision on Android and all the screen ratios/sizes out there...
seems that at z=1 you can fit in the screen all -1,-1-1,1 quads.