# Calculate max tiles for screen size?

This topic is 2504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey

I have a isometric map in a window which alters in size depending on user's settings.

But i want to limit only how ever many fit the screen. Lets say screen width and height is: 1200 by 600 pixels. Isometric tiles are 64 by 32 pixels.

Now the obvious way is width / 64 & height / 32 - then multiply the answers together. The above only works if its a birds eye view and not an isometric.

So i was wondering what is the correct way to calculate the total max tiles that can display for isometric view?

##### Share on other sites
Don't use integer math (or make sure you are rounding up) and double your result. Maybe add another row/column depending on your offset to be safe. Unless you want your result to be absolutely precise.

##### Share on other sites
I'm sure there's a sound mathematical correlation based off of the angle of the camera and height from the map, unfortunately I'm not aware of it.

The easiest way I can think of is to do ray-to-plane calculations from your view frustum edges to the ground any time your camera changes in height or angle.
From there it's fairly trivial to calculate the visible area, converting that into a number of visible tiles. Obviously you'd want to cache your results and do this as infrequently as possible, but the upside is that it allows you to compensate for a non-flat ground. I'd expect you could construe information such as camera-to-tile offsets that would allow you to move along the X/Z plane without having to recalculate the camera-to-tile offset and visible tile range.

Hopefully this helps, best of luck!

##### Share on other sites

Don't use integer math (or make sure you are rounding up) and double your result. Maybe add another row/column depending on your offset to be safe. Unless you want your result to be absolutely precise.

Im not sure this really answers or explains much =/

@SeiryEnder

Thankfully my camera is always a fixed angle its a fixed isometric game so that saves some of the complications! But the maths is hard

##### Share on other sites
It seems like it would be more complicated, but it's simply:

[source]
rows = (screen.height / tile.height) + 1
cols = (screen.width / tile.width) + 1

max_tiles = 2 * r * c
[/source]

The more important question is what exactly you hope to achieve here -- In a single-player game the screen size doesn't really matter if the player isn't compared to others -- if its multiplier, or there is a competitive element, then showing more tiles is more information, giving some players a competitive edge. To balance that out you'd need to ensure that all players only see the same number of tiles, and also have the same screen shape -- but you get to choose those details, you wouldn't need to calculate them.

Its an interesting math exercise, but I don't see how you intend to leverage this information.

##### Share on other sites
Loading more tiles that you can visually see on you're screen is a waste of resources. As screens vary in size this is the only way to improve performance.

Your solution doesn't work as it only loads a full diamond but there is still 4 corners of the map that does not cover the screen =/.

##### Share on other sites
I hope you didn't expect that calculating the number of visible tiles and then loading that number of tiles would do any good and result in anything meaningful. How to only LOAD the visible tiles would first require to know how you are indexing your tiles and is a completely different beast than just calculating how many tiles fit into a rectangle.

The best way to understand iso is to grab a piece of paper and a pen or at the very least, draw the x/y coordinates on every tile.

You should also carefully ask yourself if you REALLY want disc activity every single time someone is scrolling around the map. Unless you have a really huge world, not loading the whole thing is just wasted effort. How are you going to update the parts that aren't visible? How are enemies that are off the screen doing their movement and AI? If the view jumps to a different part of the map, are you only THEN starting to actually load it?

##### Share on other sites
Well we have gone a bit off topic here but as your curious. HTML5 and JavaScript simply is not as powerful as C++ etc... these optimizations are v.much needed.

I have my game engine complete i just need to work out how to limit the tiles loading based on screen size. A formula which i have not been able to work out - nor has any one else according to google.

(Saying that ... no one has thought to do it ) which seems bonkers because it would mean you could have unlimited sized map with no drop in performance.

##### Share on other sites
I have my game engine complete i just need to work out how to limit the tiles loading based on screen size.

I think that there is some confusion from when you say "loading".
You may instead mean "render culling" which is to say that you only draw tiles that are within visible range of the camera.

Disk reads are among the slowest operations you can do on a computer due to physical hardware limitations. You want to avoid them as much as possible. In this respect, it is advantageous to load all but the largest tile maps into memory ahead of time. Having your tilemap sitting in RAM doesn't really hurt you very much, and is fairly language independent. It is just as advantageous in Java as it is in C++. The only time this is an exception is in a very memory-constrained system, such as a gameboy.

What you gain from render culling is processing speed. You don't want to render tiles that you can't see. That's just wasted FPS.

##### Share on other sites

Loading more tiles that you can visually see on you're screen is a waste of resources. As screens vary in size this is the only way to improve performance.

It makes a bit more sense now -- it sounds like you want to do something like streaming tiles into view, like google-maps does when you scroll around? In any event, like I said earlier, this has *nothing* to do with the exact number of tiles that you will see on screen -- if you wanted to determine it programmatically (say, so that the user can resize the screen freely -- though you have to be careful that this doesn't break the competetive balance, as I said), then you can get the number as a result of the necessary calculations, but the number of tiles is not the goal itself.

Your solution doesn't work as it only loads a full diamond but there is still 4 corners of the map that does not cover the screen =/.
[/quote]

I'm pretty certain that it works, actually -- if you want a rough order of magnitude sanity check, think about it like this: Each 64x32 pixel area that contains 1 isometric tile has enough area to support 2isometric tiles of identical size -- that is, the empty corners of a single tile add up to the area of a whole tile -- and further, it's not 2 because your tile is twice as wide as it is tall, but because of the diamond shape -- it would hold for 32x32 "iso" tiles, as well as it would for 48x32, 96x32, or 427x9. If we imagine this occurring in-game (that is, repeated in a large grid), we can say that the average number of tiles contained per 64x32 area is, in fact, 2 since tiles that partially occupy one of these imaginary rectangles are shared with the neighboring imaginary rectangles. If we shift the view slightly up/down or left/right we need an additional row.length+1 or col.length+1 to cover areas that would otherwise be empty due to the move, but if we continue to shift the screen, the opposite edge will go off-screen before we need a new row or column -- that's why its rows+1 and columns+1, and not +2.

I think the more likely error you are making is that you are taking this number and using that to define the rectangular extents of your map data that you will draw, which is being mapped into a diamond-like shape in your isometric renderer -- if you do that, you would end up clipping the corners of your view. What you need to do is map the extents of the screen backwards into your map data -- so you'd end up sampling your map data in a diamond-like shape, not rectangular.

Rectangular sampling of the map data is actually a fairly complex geometry problem due to the fact that the screen could take on any shape (say, tall and narrow, or short and wide) -- it's certainly do-able, but those kinds of pathological cases result in overdraw that will be several times larger than what's drawn on screen. If you're really concerned about performance, you want to only sample the map tiles that will be drawn.

Do you get what I mean by that?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634078
• Total Posts
3015362
×