Help iso engine design

Started by
3 comments, last by isofreak 17 years, 9 months ago
This is what I have so far 1) load map struct from file 2) draw map to screen using DirectDraw 3) scroll map with mouse and keyboard 4) have multiple layers Having issues with 1) placing objects onto tiles 2) finding position of mouse on map. I am using Visual studio 7 for my compiler and c++ as my language of choice I have read about using a mouse map to find what tile you are on but I am thinking there is a better way of finding the info needed then that. I also read another tutorial that basically just found were the mouse is compared the middle tile. I am thinking that some how need to convert map coords to screen coords then find the tile ids that are bing displayed and and have the mouse find those ids. My end goal is to make a iso map editor. I am trying to start as basic as possible. I want to place tiles on the map and a few objects but not sure how to do this. Any help will be appreciated. Thanks in advanced
Advertisement
Hey! Sounds like me and you are trying to do very similar things. I had to tackle the problem of tile selection too! And I think I read the same articles you did. I couldn’t understand them either, so I created my own method. It uses nothing more then mathematics, so it should definitely work for you like it did for me! I’ll start by just going over the logic and the theory of it, so you get an idea of how it works. I’m not sure how your doing your co-ordinates (straight or tilted), so I’ll use a crappy, but simple to understand coordinate system JUST for this example. After you get the gist of it, it can be modified to suit your own system. Ok, here we go.

If your making an isometric game, I’m assuming your using diamond shaped tiles. And of course if your using diamond shaped tiles, they have to overlap at some point. The problem is that finding your selected tile (by the mouse x,y) would be a no brainer except for the fact that alternate rows of tile are offset because of the overlapping. Just so we can understand each other, we‘ll call them REGULAR and OFFSET rows. (Check out Image 1, you may have to save and enlarge the images to really see them)




The solution, in a nutshell, is to first forget about the offset rows and just find which regular tile the mouse is on. (Image 2)



Then, based on the mouse position inside the REGULAR tile you selected, you find out if the tile needs to be shifted to an OFFSET tile.

Here’s that crappy coordinate system I said I’d use just for this example. (Image 3)



In this example, to find the selected tiles X position, you just divide you mouse X position by your tiles width, then add 1. See?

int selected_x = mouse_x/TILE_WIDTH + 1;

To find the Y, just keep in mind that your only using every OTHER row. Just use half of your tiles height instead of the whole height to compensate for this.

int selected_y = mouse_y/TILE_HEIGHT_HALF + 1;

(Image4)


Your halfway there!

You now have a tile, which is selected_x and selected_y. Based on the mouse position within that tile, you need to find if the mouse is REALLY pointing at that tile, or if it’s really pointing to an OFFSET tile.
To put it simply, is it inside the selected tiles diamond or not? And if it isn’t, which corner (outside the diamond) is it pointing to, and based on that, which OFFSET tile is it? Here’s both the picture, and the formulas to show how this is done. (Image 5)



First, to find the mouse position inside your selected (REGULAR) tile, modulus your mouse position by the tiles dimensions like this…

int modmousex = mouse_x%TILE_WIDTH;
int modmousey = mouse_y%TILE_HEIGHT;

Then your going to need a number to represent the slope of the diagonal lines that form the sides of your diamond. The terminologies probably wrong, but I called it “gradient” in my code.

int gradient = TILE_WIDTH/TILE_HEIGHT;

Then just test to see if the mouse is anywhere outside the diamond, and if it is find the correct OFFSET tile. (These are the formulas in the picture!)

///Find the correct square!!!!!
float temp;


//Upper left
temp = ((float)TILE_WIDTH_HALF - modmousex)/modmousey;

if (temp > gradient)
selected_x -= 1, selected_y -=1;


//Lower left
temp = ((float)TILE_WIDTH_HALF + modmousex)/modmousey;

if (temp < gradient)
selected_x -=1, selected_y += 1;


//Upper right
temp = (modmousex - (float)TILE_WIDTH_HALF)
/modmousey;

if (temp > gradient)
selected_y -= 1;


//Lower right
temp = ((float)TILE_WIDTH + (float)TILE_WIDTH_HALF - modmousex)/modmousey;

if (temp < gradient)
selected_y += 1;


And that’s it! selected_x and selected_y is your selected tile! Of course it doesn’t take much to see how the example x,y coordinate system will cause problems. With just a little modification though, it can be used with a more “normal” coordinate system. If you want to use this method, but need more help implementing it, e-mail me at Boing586@hotmail.com, and be sure to check MY game at http://kingdomthegame.16.forumer.com. Maybe we could collaborate? Anyway, good luck!

[Edited by - DirkDaring on July 6, 2006 7:32:59 PM]
That is exactly what I am looking for. Right now I can’t try because I don’t have access to a computer that I can program on. I am in the army and waiting for my computer to ship over to Germany. But that is what I needed.

My coordinates are tilted the whole map looks like a diamond with diamond tiles. I would love to collaborate. I have been looking for some one to work with on the programming aspects of an isometric game.

Few questions are you using direct3d, direct draw, OpenGL, STL or what?

Another thing I have objects as a separate layer how can you tell what layer you are clicking on? And also how do you put your characters on the map and have them stay on the X, Y position when you are scrolling the map?


Thanks again for all your help
You could also use a "Mouse Map" to determine which tile you selected. More Information here. To determine the selected layer you could maybe use the alphachannel of your layers.

I dont know which problems you have when you try to place objects onto tiles. Usually it is ok to just draw them on top of the map in another layer.
the thing is I dont wnat to use a mouse map I wanted to use something else like what DirkDaring provided which helps me find the tiles now I just need to find out what layer I am on useing the same concept.

This topic is closed to new replies.

Advertisement