Jump to content
  • Advertisement
Sign in to follow this  
sultanofswing.90

[XNA 4] Isometric block placement issue

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi people,
i'm currently working on a simple 2D isometric engine using a staggered map, i'm thinking about a simcity clone... Drawing the map and the tile picking are ok, but here's the painful part: block placement in the map. So, here's what i've done:

I've a map class containing
[source]terrainBlock terrain[,];
int height;
int width;
mapObject objectMap[,];
...[/source]

terrain holds all terrain blocks,object holds the blocks the user can place in the map. Here's the map

unledqvau.png


When i click on a terrain block i have something like this

unled2ydy.png

and this is my Update method:

[source]public void Update(MouseState m1)
{
Point selectedTileCoord = ChechTilePicking(m1);

//if we selected a valid tile
if (selectedTileCoord.X != -1 && selectedTileCoord.Y != -1)
{
//if user press the left button
if (m1.LeftButton == ButtonState.Pressed)
{
if (objectMap[selectedTileCoord.X, selectedTileCoord.Y] == null)
{
//create a new block
objectMap[selectedTileCoord.X, selectedTileCoord.Y] = new mapObject(Content,
this,
selectedTileCoord);
}
}


}
}[/source]


seems ok but...look at this now:

unled3xat.png

Oh no! What's happening here? I don't know how to render all the blocks in the right order, once placed in the map. Here's my draw method:

[source]public void Draw(SpriteBatch batch)
{
batch.Begin();
drawTiles(batch);
batch.End();

batch.Begin();
drawBlocks(batch);
batch.End();
}[/source]

so, i draw first all the terrain blocks(right?), then i draw all the map blocks. Here's the drawBlock method:

[source]private void drawBlocks(SpriteBatch batch)
{

for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
if (objectMap[x, y] != null)
objectMap[x, y].Draw(batch);
}
}
}[/source]

and this is the draw method for the mapObject class:

[source]public void Draw(SpriteBatch batch)
{
batch.Draw(texture, rect, Color.White);
}[/source]

where rect is the destination rectangle in the screen.

However, the drawBlocks method should draw every block left to right,top to bottom...but it doesn't work correctly! How can i render correctly every block in the map? =\

Share this post


Link to post
Share on other sites
Advertisement

What did you do to get to the last screenshot... what "caused" it?


nothing special....only placed blocks close togheter. The two previous screenshot are two different situation. In the first the blocks have a certain distance, in the second all the blocks are close togheter, but drawed in the wrong order...

Share this post


Link to post
Share on other sites
Look at how you're actually drawing the tiles. Just looking at the top row you can see what's going on.
(Ignoring the Y for now) you're rendering 0,0 then 1,0 then 2,0. If you look at the arrangements of those tiles you SHOULD be rendering 0,0 then 2,0 then 4,0 (etc) THEN render 1,0 and 3,0, etc.
2,0 is physically setup to be behind 1,0, so it needs to render first.

As far as the solution goes, I'm not sure the best course of action. But that's what your problem is. I think the best solution might be to setup your grid to where the first actual row is 0,0 then 1,0 then 2,0 then 3,0. The next row (shifted over) is 0,1 then 1,1 then 2,1 then 3,1.
However, the rest of your math for click tiles, etc. would have to be modified as well.

Share this post


Link to post
Share on other sites

Look at how you're actually drawing the tiles. Just looking at the top row you can see what's going on.
(Ignoring the Y for now) you're rendering 0,0 then 1,0 then 2,0. If you look at the arrangements of those tiles you SHOULD be rendering 0,0 then 2,0 then 4,0 (etc) THEN render 1,0 and 3,0, etc.
2,0 is physically setup to be behind 1,0, so it needs to render first.

As far as the solution goes, I'm not sure the best course of action. But that's what your problem is. I think the best solution might be to setup your grid to where the first actual row is 0,0 then 1,0 then 2,0 then 3,0. The next row (shifted over) is 0,1 then 1,1 then 2,1 then 3,1.
However, the rest of your math for click tiles, etc. would have to be modified as well.


hmm ok i tried this:

[source]private void drawBlocks(SpriteBatch batch)
{

for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x+=2)
{
if (objectMap[x, y] != null)
objectMap[x, y].Draw(batch);
}

for (int x = 1; x < Width; x += 2)
{
if (objectMap[x, y] != null)
objectMap[x, y].Draw(batch);
}

}
}[/source]

but,again, when i click(for example) tiles 2,2 ,4,2 , 3,1 the result is

unled4ae.png

=|

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!