# Rotating a view of a chunked 2d tilemap

I'm working on a top-down (oblique) tile-based engine. I would like for the tiles to have a definable height in the world, with Characters being occluded by them, etc.

This has led to a desire to be able to "rotate" the view of the world, even though I'm using all hand-drawn graphics and blitting.

Therefor, I need to rotate the actual world itself, or change how the Camera traverses these arrays.

How can, or should, I create individual rotations of 90 degrees, when I have multi-dimensional arrays?

Is it faster to actually rotate the array, to access it differently, or to create pre-computed accessor(?) arrays, something like how my chunks work?

How can I rotate an individual chunk, or set of chunks?

Currently I establish my tile grid like this (tile height not included):

function Surface(WIDTH, HEIGHT)
{
WIDTH = Math.max(WIDTH-(WIDTH%TPC), TPC);
HEIGHT = Math.max(HEIGHT-(HEIGHT%TPC), TPC);

this.tiles = [];
this.chunks = [];

//Establish tiles
for(var x = 0; x < WIDTH; x++)
{
var col = [],
ch_x = Math.floor(x/TPC);

if(!this.chunks[ch_x])
this.chunks.push([]);

for(var y = 0; y < HEIGHT; y++)
{
var tile = new Tile(x, y),
ch_y = Math.floor(y/TPC);

if(!this.chunks[ch_x][ch_y])
this.chunks[ch_x].push([]);

this.chunks[ch_x][ch_y].push(tile);

col.push(tile);
}

this.tiles.push(col);
}
};


Even some basic advice on my data struct would be much appreciated.

• Any tips for improving my question? Jul 7, 2014 at 1:35

I don't think speed is a huge issue between your different methods so you might go with whichever approach you think is easiest. I would absolutely choose to access the arrays differently and not store pre-rotated versions of them.

For rotating one chunk: normal rotation gives you array[x][y] -90 degrees would give you array[WIDTH-1-y][x] 180 degrees would give you array[WIDTH-1-x][HEIGHT-1-y] 90 degrees would give you array[y][HEIGHT-1-x]

I would recommend somehow putting chunk access behind a layer of abstraction. Just ask for tiles on the map relative to the player and change access to the tiles based on the rotation.

As for improving your question, I don't think that's anything anyone can answer. You'd have to consider what motivates people to answer questions. Personally, I almost didn't answer because the question seems too simple and I don't know enough to understand what is giving you trouble so I don't feel that I can help.

• Thank you!! Good advice all around, I really appreciate you working through my vagueries :] I'll ask better next time because of it, I'm sure! --- P.S. Seeing the rotation laid out like that really made a lot click for me, and "putting chunk access behind a layer of abstraction" really drove home some other realizations. Thanks again. Jul 22, 2014 at 0:11