Sunday, August 20, 2017

How do I find which isometric tiles are inside the cameras current view?


I'm putting together an isometric engine and need to cull the tiles that aren't in the camera's current view. My tile coordinates go from left to right on the X and top to bottom on the Y with (0,0) being the top left corner.


If I have access to say the top left, top right, bottom left and bottom right corner coordinates, is there a formula or something I could use to determine which tiles fall in range?



This is a screenshot of the layout of the tiles for reference. screenshot


If there isn't one, or there's a better way to determine which tiles are on screen and which to cull, I'm all ears and am grateful for any ideas. I've got a few other methods I may be able to try such as checking the position of the tile against a rectangle. I pretty much just need something quick. Thanks for giving this a read =)



Answer



X+Y is constant for all tiles in a column, and X-Y is constant for all tiles in a row. In your current screenshot, it seems like your screen is centered at (X=15,Y=15) or alternatively, at (X+Y=30,X-Y=0)



  • 20 <= X+Y < 40 (vertical axis)

  • -8 <= X-Y < 10 (horizontal axis)


Your loop should be based on X+Y, and X-Y, not precisely X and Y.


// current viewport is x+y=20to40 by x-y=-8to10 

for(a=20;a<40;a++) {
for(b=-8;b<10;b++) {
if ((b&1) != (a&1)) continue;
x = (a+b)/2;
y = (a-b)/2;
// do stuff with X and Y here
}
}​

Here's the demo



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...