Showing as much of a TiledMap as screen allows - libgdx

I have a simple 20 x 20 tile world (TiledMap) that I want to display using an OrthogonalTiledMapRenderer. However, I'd like to show as much of the world as the screen will allow. This means if the screen is bigger, more of the world is shown and if the screen is smaller, less of the world is shown. Is this even possible?
The physical size of my tiles are 16 pixels x 16 pixels. So I thought that creating a 16 * 20 x 16 * 20 (320 x 320 unit?) FillViewport would do the trick, but that will always show the entire world on the screen (zooming in or out depending on screen size). Instead I want zoom to stay at 1 and cut off the world that is too wide or tall for the screen. How can I achieve this effect?
Update
I tried using a ScreenViewport, but it ends up looking like this: .
I've tried setting up my camera's position like so: camera.position.set(viewport.getWorldWidth() / 2, viewport.getWorldHeight() / 2, 0); in my create method, but the camera's position is always 0, 0, 0.
Here is the source code I am using so far.

Related

Figuring out the correct heights for a comparison

I'm sorry the title is vague. I'm creating a pokedex site and I have the pokemon and a photo of Ash. Ash is 140cm tall, the only problem being that some pokemon are tiny and some are huge, I want the images to be no bigger then 400px tall and wide.
So for the biggest pokemon, it is 880cm tall, I need to have that be 400px tall and shrink Ash to be the right height relative to the pokemon. But for the smallest pokemon I need to make Ash 400px tall and change the pokemon's height to be relative to Ash.
I dont even know where to start with this problem so any ideas would be wonderful!
Don't display Ash's photo in full scale when you get out your 400 pixel limit. Use a cross-multiplication when you have a very tall Pokemon to decrease Ash's picture size, and on the other side, use another to enlarge Ash's picture when the Pokemon is very small.
With a 880 cm Pokemon for 400 pixels, while Ash is 140 cm tall, it means that Ash's picture should be (140*(400/880)) = 63 pixels height. Value 400/880 gives you that each centimeter for the Pokemon is 0.45 pixels, and 880/400 gives you that each pixel represents 2.2 cm.
Obviously, you can also prefer to use round numbers, and go for a "1 pixel = 3 cm" scale, so that your Pokemon will use a 294 pixels height image instead.
For very tiny Pokemons, you do the same operation, but by enlarging Ash's picture.
At 400 pixels height, Ash's picture has these characteristics: each pixel worths 0.35 cm, or one centimeter uses 2.85 pixels. So a 10 cm Pokemon will needs a 29 pixels height picture (10*(400/140) = 28.57).
When confident with the formula, you can compute automatically the scale in your program. Since it's very basic arithmetic, implementation language shouldn't be a real problem - JS can do that perfectly, or PHP.
In psuedo-code, I would do something like this
Assuming you have store 'height' in an Ash object and every pokemon object, and that you can later use their 'imgHeight' property later on to size your images.
setScaled = (big, small) => {
big.imgHeight = '400px';
small.imgHeight = (big.height / 400) * small.height
small.imgHeight = (400 / big.height) * small.height;
return [big, small]
}
for(poke in dex){
if(poke.height > 140){
scaled = setScaled(poke, ash);
//now scaled[0] is poke, scaled[1] is ash
}else{
scaled = setScaled(ash, poke);
//now scaled[0] is ash, scaled[1] is poke
}
}

Interface gets extra pixel

I made an interface for a game, using extended viewport and when i resize the screen the aspect ratio changes and every element in scene is scales, but when this happens this is what i get :
This is the most annoying issue i dealt with, any advice ? I tried making the tower n times bigger and then just setting bigger world size for the viewport but same thing happens, idk what is this extra pixels on images..
I'm loading image from atlas
new TextureRegion(skin.getAtlas().findRegion("tower0"));
the atlas looks like this:
skin.png
size: 1024,1024
format: RGBA8888
filter: Nearest,Nearest
repeat: none
tower0
rotate: false
xy: 657, 855
size: 43, 45
orig: 43, 45
offset: 0, 0
index: -1
In the third picture, you are drawing your source image just slightly bigger than it's actual size in screen pixels. So there are some boundaries where extra pixels have to be filled in to make it fill its full on-screen size. Here are some ways to fix this.
Use linear filtering. For the best appearance, use MipMapLinearLinear for the min filter. This is a quick and dirty fix. The results might look slightly blurry.
Draw your game to a FrameBuffer that is sized to the same aspect ratio as you screen, but shrunk down to a size where your sprites will be drawn pixel perfect to their original scale. Then draw that FrameBuffer to the screen using an upsampling shader. There are some good ones you can find by searching for pixel upscale shaders.
The best looking option is to write a custom Viewport class that sizes your world width and height such that you will be always be drawing the sprites pixel perfect or at a whole number multiple. The downside here is that your world size will be inconsistent across devices. Some devices will see more of the scene at once. I've used this method in a game where the player is always traveling in the same direction, so I position the camera to show the same amount of space in front of the character regardless of world size, which keeps it fair.
Edit:
I looked up my code where I did option 3. As a shortcut, rather than writing a custom Viewport class, I used a StretchViewport, and simply changed its world width and height right before updating it in the game's resize() method. Like this:
int pixelScale = Math.min(
height / MIN_WORLD_HEIGHT,
width / MIN_WORLD_WIDTH);
int worldWidth = width / pixelScale;
int worldHeight = height / pixelScale;
stretchViewport.setWorldWidth(worldWidth);
stretchViewport.setWorldHeight(worldHeight);
stretchViewport.update(width, height, true);
Now you may still have rounding artifacts if your pixel scale becomes something that isn't cleanly divisible for both the screen width and height. You might want to do a bit more in your calculations, like round pixelScale off to the nearest common integer factor between screen width and height. The tricky part is picking a value that won't result in a huge variation in amounts of "zoom" between different phone dimensions, but you can quickly test this by experimenting with resizing a desktop window.
In my case, I merged options 2 and 3. I rounded worldWidth and worldHeight up to the nearest even number and used that size for my FrameBuffer. Then I draw the FrameBuffer to the screen at just the right size to crop off any extra from the rounding. This eliminates the possibility of variations in common factors. Quite a bit more complicated, though. Maybe someday I'll clean up that code and publish it.

Decreasing image pixel size in actionscript?

I want to decrease an 480 X 480 bitmap image size to 30 X 30 pixel size but keeping the whole height and width intact. (I do not want to scale or use height/width property! )
So if i divide 480/16 = 30. So i need to take average pixel values of 30 pixel elements and put it into new image.
How to take the average in actionscript 3.0? I looked at getpixels() method, is their any simple way/methods to achieve this?
Let me put in more simple way - I am trying to reduce pixels in an bitmap image from 480 X 480 to 30 X 30, the height and width remain same and i expect some amount of distortion after converting image to 30 X 30.
I did scaling but it reduces width and height, if i again increase width and height it just regains normal pixels. Thanks!
Why don't you simply then make a copy of the whole image in code, but use the simple scaling to scale the copy, and only present that to the user. Also look at this from Stack Overflow
How to resize dynamically loaded image into flash (as3)

Setting PPI For a web template

I want to create a template . If the user monitor at least is 14 inches , It has a ppi for itself maybe 102 or something else.
So What ppi should I set for my web template to avoid the screen horizontal scrolling ? Is it the maximum ppi of the 14 inches monitor ?
ppi/dpi (dots per inch) doesn't work here: Different 14 inch monitors can have different resolutions - the number of pixels displayed on the screen.
For your purposes, the only thing you need to worry about is the user's resolution.
The size of a pixel can differ depending on the device's size, but it doesn't matter for layouting.
If you want to avoid scroll bars, either choose a very low pixel width (960 pixels is deemed ideal by many, because layouts with that width are guaranteed to work on a 1024 x 768 resolution) or use relative widths instead of pixel sizes.
Related:
Smashing Magazine: Fixed vs. Fluid vs. Elastic Layout: What’s The Right One For You?

Defined size doesn't corespond to real size on the screen

I've encountered curious problem, and I'm wondering what is wrong.
When I define for example Sprite and I draw inside rectangle 20 x 20 px, then when measured on screen it have something like 33 x 33 px, while swf window which was defined as 400 x 300 corresponds to definition well.
Where is the problem?
Is it possible that your content is being scaled? I'm assuming you're doing a print screen an measuring it with photoshop or some other program.
If so, give this a try:
stage.scaleMode = StageScaleMode.NO_SCALE;