Something like a mouse selector on a tile may seem mundane, but it’s actually pretty complex(or in computer science terms “fun”).

When the game starts, it loads a black&white “tile_mask.png” into memory and converts it into a line of 4,096 bits. Black is 0, white is 1.

When a tile is drawn, it checks the given bitfield at index [ (mouseX-drawX)+(mouseY-drawY)*TILE_WIDTH ] to see if the mouse is on a “white” part of the tile_mask.png, which is, in a sense, placed relative to the tile being drawn. If the bit that’s checked is a 1, the mouse is hovering over that tile, if it’s a 0 or out of bounds, then it’s not.

After the check is performed, a pointer to the selected tile is given to the WORLD object, and can then be used to tell other entities in the game world which MAPTILE object is currently under the mouse cursor.

EDIT: All that stuff you just read up there? It’s STUPID and USELESS.

NEW SOLUTION! I figured out the formula to translate the mouse coordinates into the map’s coords, thus reducing the check to O(1).

I did this by using simple algebra on the code used to draw the tiles, so that I could sort of… “reverse” it to get the coordinate component. It’s rough, but it works.

double selectX,selectY, mX, mY;
mX = MOUSE.x+camX;
mY = MOUSE.y+camY-TILE_H*1.5;

selectY = mX/(TILE_W/2) – (mX/(TILE_W/2) + mY/(TILE_H/2))/2;
selectX = mX/(TILE_W/2) – selectY;

selected_tile = Grid_Tiles.get((int)selectX,(int)selectY);



Posted on 04/02/2012, in Uncategorized. Bookmark the permalink. 2 Comments.

  1. If I’m reading this right, this seems like a very convoluted way of doing something that really just involves simple geometry. Also based on what you wrote you are checking this on every tile that gets drawn, as it gets drawn, which seems like a very poor use of processor power. You should be able to figure out using geometry what local region the mouse is on top of, limiting the possible locations to a few tiles you can check, again using geometry. Or you could project the tiles onto the 2d spaces of the computer screen and then it becomes trivial. Since I don’t fully understand from this post what the problem is, I can’t really be specific, but this type of thing almost definitely can be dealt with using a few simple formulas.

    • I considered doing it with some kind of formula, but I wasn’t able to come up with anything.
      This solution is actually pretty inexpensive for the processor, it does one check on a single bit for each tile that is visible on the screen, so for now it’ll work fine. If I do figure out some formula for mapping the mouse’s screen position within the bounds of an isometric tile on the screen, I’ll implement it.

      One disadvantage to what I’m doing however, is that this check takes place in the draw phase, and I’d rather have it in the logic phase. (Tiles themselves do not have a logic phase.)
      The main loop of the game keeps logic and drawing completely separate, but the current solution breaks that paradigm.

      At this stage, I’m only optimizing where needed, trying to concentrate more on adding general features to the framework. It’s very likely that I’ll revisit this in the future.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: