matthewlynch.net Musings on programming, electronics, and other such things.

17Apr/100

Block Map Generator Video

In my last post I mentioned I have been experimenting with an isometric map generator. I thought I might share a video of the results so far.

It is coded in C# and XNA, and the video simply shows me hitting the "randomly generate a map" button a number of times.

Filed under: 2D Graphics, C#, XNA No Comments
17Apr/100

Isometric Helper Algorithms

Recently I have been experimenting with an isometric map generator. It is still in the very early stages, and I am not sure where I am going to go with it, but I thought I would share some of the algorithms I pieced together during my experiments.

Convert Screen Space to Map Space

Inputs

  • float2 screenPoint - The point (pixel/coordinate) in screen space you wish to convert.
  • int tileScreenWidth - The width (in pixels) of a tile in screen space.
  • int tileScreenHeight - The height of a tile in screen space.
  • float2 screenOriginPoint - The top-left most point in screen space of the top-left most tile in map space. Where the tiles start drawing.
  • float2 screenOffset - How far (x/y) screen space has been shifted from the origin point. Used for scrolling.

Outputs

  • float2 mapPoint - The calculated point (grid reference) in map space.

double tw, th, tx, ty;
double sx, sy;

tw = tileScreenWidth;
th = tileScreenHeight;

sx = screenPoint.X - (screenOriginPoint.X + screenOffset.X);
sy = screenPoint.Y - (screenOriginPoint.Y + screenOffset.Y);

tx = Math.Round((sx / tw) + (sy / th)) - 1;
ty = Math.Round((-sx / tw) + (sy / th));

mapPoint.x = tx;
mapPoint.y = ty;

Convert Map Space to Screen Space

Inputs

  • float2 mapPoint - The point (grid reference) in map space you wish to convert.
  • int tileScreenWidth - The width (in pixels) of a tile in screen space.
  • int tileScreenHeight - The height of a tile in screen space.
  • float2 screenOriginPoint - The top-left most point (pixel/coordinate) in screen space of the top-left most tile in map space. Where the tiles start drawing.
  • float2 screenOffset - How far (x/y) screen space has been shifted from the origin point. Used for scrolling.

Outputs

  • float2 screenPoint - The calculated point (pixel/coordinate) in screen space.

double tw, th, tx, ty;
double sx, sy;

tw = tileScreenWidth;
th = tileScreenHeight;

tx = mapPoint.X;
ty = mapPoint.Y;

sx = Math.Truncate((tw / 2) * tx - (tw / 2) * ty) + (screenOriginPoint.X + screenOffset.X);
sy = Math.Truncate((th / 2) * tx + (th / 2) * ty) + (screenOriginPoint.Y + screenOffset.Y);

screenPoint.x = sx;
screenPoint.y = sy;

Filed under: 2D Graphics No Comments