• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Bikonja

2D isometric picking problem

6 posts in this topic

Hello all, I have a problem with my 2D isometric picking.
I've asked on gamedev.stackexchange too, but so far I have no answer. You can read the stackexchange question [url="http://gamedev.stackexchange.com/questions/44834/2d-isometric-picking"]here[/url]. It contains details. I will write a short version here.

Here's the code I have for drawing the tiles
[CODE]
// Draw code
int col = 0;
int row = 0;
for (int i = 0; i < nrOfTiles; ++i)
{
// XOffset and YOffset are currently hardcoded values, but will represent camera offset combined with HUD offset
Point tile = IsoToScreen(col, row, TileWidth / 2, TileHeight / 2, XOffset, YOffset);
int x = tile.X;
int y = tile.Y;
spriteBatch.Draw(_tiles[i], new Rectangle(tile.X, tile.Y, TileWidth, TileHeight), Color.White);
col++;
if (col >= Columns) // Columns is the number of tiles in a single row
{
col = 0;
row++;
}
}
// Get selection overlay location (removed check if selection exists for simplicity sake)
Point tile = IsoToScreen(_selectedTile.X, _selectedTile.Y, TileWidth / 2, TileHeight / 2, XOffset, YOffset);
spriteBatch.Draw(_selectionTexture, new Rectangle(tile.X, tile.Y, TileWidth, TileHeight), Color.White);
// End of draw code
public Point IsoToScreen(int isoX, int isoY, int widthHalf, int heightHalf, int xOffset, int yOffset)
{
Point newPoint = new Point();
newPoint.X = widthHalf * (isoX + isoY) + xOffset;
newPoint.Y = heightHalf * (-isoX + isoY) + yOffset;
return newPoint;
}
[/CODE]


And here is the code for getting which tile is the mouse on
[CODE]
public Point? ScreenToIso(int screenX, int screenY, int tileHeight, int offsetX, int offsetY)
{
Point? newPoint = null;
int nX = -1;
int nY = -1;
int tX = screenX - offsetX;
int tY = screenY - offsetY;
nX = -(tY - tX / 2) / tileHeight;
nY = (tY + tX / 2) / tileHeight;
newPoint = new Point(nX, nY);
return newPoint;
}
[/CODE]

This code is close, but not even close enough for a public beta or something like that (even though this is just a proof of concept, which I will later transform into my game). I have no idea why this code is so close. I drew the tiles by thinking up the formula, and by googling I believe I've seen that it is correct (however it's still possible it's not). The code for picking - what ever I came up with, didn't work. What ever I googled, didn't work. This code I currently have is a derivation of something I googled - it was not really close, but closer than others, so I modified it and it's even closer now - I did it by trial and error as I have no idea behaved and behaves the way it did/does.

Can anyone point out to me my error? Whenever I think about it, it doesn't make sense to me that it behaves that way. I tried reversing the formula for drawing and it didn't pan out which doesn't make sense either. I must be doing something wrong then so please, if someone sees my error, could you point it out?
0

Share this post


Link to post
Share on other sites
The actual answer depends on a few things but one thing I can definitely tell you is that there shouldn't be any maths happening between the x and y mouse coordinates. Anyway, this is the way I remember that I did it. It probably wasn't the most clever or elegant but it was simple and I like simple.

You'll want to divide the calculated cursor position by the tileHeight/Width and ROUND to the nearest number. This will need to be done with a double or float because ints automatically truncate (round) up or down depending on their sign (negative/positive). Up for negative and down for positive values. You can avoid changing things via casting to int after calculating using a double or float variable with an integer.

If the tiles have moved, the tile map is bigger than what the screen is capable of displaying, you'll need to calculate the map position of the screenspace 0, 0 tile and offset the values to account for it.

This wouldn't help with transparency (or overlapping tiles), where a much different approach would need to be taken, and would only work if all tiles are the same size which it looks like they are from the source code provided.

Hope that helped.
0

Share this post


Link to post
Share on other sites
These are my equations for the ISO-conversions (i use a hard-coded slope of 4:1 pixels (x:y)):
ISO_X,ISO_Z are the left-bottom offset of the iso space

[Code]
void World2ISO(int wx, int wz, int wh, int& isox, int& isoy)
{
isox = wx - wz + ISO_X;
isoy = (-(wx + wz) / 4) - wh + ISO_Z;
}

void ISO2World(int isox, int isoy, int& wx, int& wz, int& wh)
{
wh = 0;
wx = (isox/2) - (2*isoy);
wz = -(isox/2) - (2*isoy);
}

[/Code]

Maybe that helps you a bit Edited by AticAtac
0

Share this post


Link to post
Share on other sites
@MachinaX
What do you mean by "there shouldn't be any maths happening between the x and y mouse coordinates"?
What you're saying is this?
[CODE]
public Point? ScreenToIso(int screenX, int screenY, int tileHeight, int offsetX, int offsetY)
{
Point? newPoint = null;
int nX = -1;
int nY = -1;
int tX = screenX - offsetX;
int tY = screenY - offsetY;
nX = Math.Round((double)tX / (double)tileHeight / (double)tileWidth);
nX = Math.Round((double)tY / (double)tileHeight / (double)tileWidth);
newPoint = new Point(nX, nY);
return newPoint;
}
[/CODE]

If this code is what you're saying, then it doesn't work (I mean, after fixing small, insignificant for this, errors like nX being in while Math.Round returns decimal). It selects something way further than under mouse cursor (out of screen). But the code doesn't make sense to me either - why would I divide with tileHeight/tileWidth?
I'm also not sure what you mean by the sentence "If the tiles have moved,...", do you mean the -offset I have in tX and tY variables?
As for the transparent parts, I'll implement a color map for that, but first I want to get to a stage where the rest actually works.

@AticAtac
I couldn't get your code to draw properly so I didn't try your picking code. I'm probably missing something, again... But I'm at work now so not much time to play with this. When I get home I'll play around more a bit.

Thank you to you both for your contributions.
0

Share this post


Link to post
Share on other sites
The matrix solution mentioned [url="http://gamedev.stackexchange.com/questions/31113/how-can-i-improve-my-isometric-tile-picking-algorithm"]here[/url] is essentially what you want to use to generate the correct formulas. If you plug your tile size into a 2x2 matrix representing your isometric basis, you'll get this:
[CODE]
| TileWidth / 2, TileWidth / 2 |
| -TileHeight / 2, TileHeight / 2 |
[/CODE]

I was able to infer the transformation from your IsoToScreen code, since this math:
[CODE]
newPoint.X = widthHalf * (isoX + isoY) + xOffset;
newPoint.Y = heightHalf * (-isoX + isoY) + yOffset;
[/CODE]

Is equivalent to:
[CODE]
| widthHalf, widthHalf | * | isoX | + | xOffset |
| -heightHalf, heightHalf | | isoY | | yOffset |
[/CODE]

The inverse of that basis matrix is, if I did my mental math correctly:
[CODE]
| 1 / TileWidth, 1 / -TileHeight |
| 1 / TileWidth, 1 / TileHeight |
[/CODE]

Which gives us the equations:
[CODE]
nX = (int)((double)(tX / TileWidth) - (double)(tY / TileHeight));
nY = (int)((double)(tX / TileWidth) + (double)(tY / TileHeight));
[/CODE]

Might need a little tweaking, but that should be the general form. Edited by Zipster
0

Share this post


Link to post
Share on other sites
Thanks for the math!
However it's not working. It selectes only the tile that would be 0,0 in the original system (the non rotated, top view) and it does so for about width/2 to the left and height/2 above. This made me realize though that I failed to mention my coordinate system (XNA) has it's (0,0) at the top left, not the bottom left of the window.

Yesterday I've tried thinking up some formulas, but to no avail. I was sure this would be correct, but it doesn't work for some reason... I'll have to try again after work..

[CODE]
x = mX / TileWidth + (mY / TileHeight / 2 - maxX / 2) * TileWidth
y = mY / TileHeight + (mX / TileWidth / 2 - maxY / 2) * TileHeight
[/CODE]

where
mX is the mouse X position, offsetted by the X drawing offset
mX is the mouse Y position, offsetted by the Y drawing offset
maxX is the number of virtual tiles on X axis
maxY is the number of virtual tiles on Y axis

what virtual tiles means is, the bounding rectangle of a drawn tile, divided by 2, by an axis is in the non-rotated top view, not the isometric view.

Ofcourse, it should work only for that virtual tile and then I could use a color map to determine which exact tile is selected, but even without a color map, I can see this does not work.
0

Share this post


Link to post
Share on other sites
Yeah, I moved some stuff around and my determinant had the wrong sign. I edited my original post and fixed the equations [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

I've also [url="http://pastebin.com/gNaQctpk"]posted some code[/url] which demonstrates the math. It will visualize an isometric grid and show you which tile is under the mouse (just pass it a graphics context to draw and a mouse position to update the mouseover). Tile sizes are in pixels, so you will want to make them larger! Edited by Zipster
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0