Isometric Mapping Issues

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

Recommended Posts

Been reading around various articles for several weeks now. Been trying to get isometic mapping to work for months, but no matter how it is explained to me, I just cannot grasp it. But, I think I am getting close. I put up my Map.cs class which contains my isometric algorithm in my constructor. Also put up the Tile class for a reference. Default tile texture & map results are below the code.

What is really weird is that if I use a flat tile...like grass, it shows up fine...although it does look a little squashed. But my game will be using isometric cube based/shaped textures. So I need it to work with the cubes, but as you can see from the screenshot...its all staggered. I am not very good at math, and it seems like no matter what I change, I just cannot get it.

Also, since I am very bad with math, I might have my Height & Depth might be mixed up. For my code reference...depth refers to stacking cubes on top of each other (terrain height), whereas height refers to how deep the cubes go (on the Y) plane. Right now I am just testing with a depth of 1, so 1 map layer to create a nice flat diamond. Height of 10 and Width of 10. Even still I don't think that is what is wrong with it, I have no idea and would like an outside perspective, thanks.

[b]Map.cs Class[/b]
[CODE]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace IsometricMapping
{
public class Map
{
// Width, height and depth of the map (X, Y, Z)
private int width, height, depth;
// Accessor for width
public int GetWidth
{
get { return this.width; }
}
// Accessor for height
public int GetHeight
{
get { return this.height; }
}
// Accessor for depth
public int GetDepth
{
get { return this.depth; }
}
// Array of arrays of tiles used by this map [depth][width,height]
private Tile[][,] tiles;
// Accessor for the tile array
public Tile[][,] GetTiles
{
get { return this.tiles; }
}
// The default texture assigned to tiles
private Texture2D defaultTexture;
// Accessor for the default texture
public Texture2D GetDefaultTexture
{
get { return this.defaultTexture; }
}
// Default constructor for the Map class
public Map(Texture2D defaultTexture, int width, int height, int depth)
{
// Assign values to fields
this.defaultTexture = defaultTexture;
this.width = width;
this.height = height;
this.depth = depth;
// Initialize the Tile array
this.tiles = new Tile[this.depth][,];
// Set up tile positions
for (int z = 0; z < this.depth; z++)
{
// Initialize tile array by Z-layer
this.tiles[z] = new Tile[this.width, this.height];
for (int y = 0; y < this.height; y++)
{
for (int x = this.width - 1; x >= 0; x--)
{
// Initialize tile at the specified position
this.tiles[z][x, y] = new Tile(this.defaultTexture, new Vector2(
(x * this.defaultTexture.Width / 2) + (y * this.defaultTexture.Width / 2),
(y * this.defaultTexture.Height / 2) - (x * this.defaultTexture.Height / 2)));
}
}
}
}
// Draws the map
public void Render(SpriteBatch spriteBatch)
{
for (int z = 0; z < this.depth; z++)
{
for (int y = 0; y < this.height; y++)
{
for (int x = this.width - 1; x >= 0; x--)
{
spriteBatch.Draw(
this.tiles[z][x, y].GetTexture,
this.tiles[z][x, y].GetPosition,
Color.White);
}
}
}
}
}
}
[/CODE]

[b]Tile.cs Class[/b]
[CODE]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace IsometricMapping
{
public class Tile
{
private Texture2D texture;
public Texture2D GetTexture
{
get { return this.texture; }
}
public Texture2D SetTexture
{
set
{
if (value != null)
this.texture = value;
}
}
public int GetWidth
{
get { return this.texture.Width; }
}
public int GetHeight
{
get { return this.texture.Height; }
}
private Vector2 position;
public Vector2 GetPosition
{
get { return this.position; }
}
public Tile(Texture2D texture, Vector2 position)
{
this.texture = texture;
this.position = position;
}
}
}
[/CODE]

[b]Default Tile Texture:[/b]
[img]http://i806.photobucket.com/albums/yy341/BlueSin2009/XNA%20Development/sampleblock.png[/img]

[b]Map Result:[/b]
[b][img]http://i806.photobucket.com/albums/yy341/BlueSin2009/XNA%20Development/IsoIssues.png[/img][/b]

By the way, I have gotten isometric mapping to work from the guides I have read. However, they are really just a jumping off point, and don't help me write the code the way I want it, similar to the way the code is written above. Also, they usually miss an important step such as terrain height, or are jagged as opposed to diamond, etc.