Archived

This topic is now archived and is closed to further replies.

Sfpiano

Need help determining which tile the mouse is in

Recommended Posts

I''m working in D3D on a 2D diamond iso map. I have no idea how to determine which tile the mouse is in. I read TANSTAAFL''s tutorial on it, and I understand the principles behind it, but I don''t know how to implement. Using that tutorial, I tried writing some equations, but those aren''t working either.

Share this post


Link to post
Share on other sites
Where is this tutorial of TANSTAAFL's? I have been searching for a method to perform clicked iso tile recognition without using a mouse map and have found none.


1
0 <> 2
3

I did come up with a method, which I am not certain is the most efficient method. I have a tile with a 2:1 ration, therefore y = .5x OR x = 2y.

I first find which rectangular tile was clicked: X / TileWidth. I then divide the "rectangular tile" into four sections (with an if). After which I select a vertex and subtract MouseY from this VertexY, to find the displacement or EffectiveY. I then multiply it by 2 and Add it to the VertexY to get the X. ( x = 2y). I then find if the Solved X (the corresponding x to the clicked Y on the slope of the tile) is less or greater then the Clicked X, based on the sector and perform the necessary adjustments. i.e ++,--

\
-> . \<-- (SolvedX,Y)
(x,y)


Looking at the lower left sector (vertex 0) and assuming the tile size is 128x64 then: By subtracting the Y from 32 and multiplying it by 2 wes gets the x, since x = 2y.
Test cases: y = 40, 40 - 32 = 8 * 2 - 16. So 32 + 16 = 48. Better yet y = 64. 64 -32 = 32. 32 + 32 = 64.

The Equation:

(2(Y-VertexY))+ VertexY = 2y - 2VertexY+VertexY

SlopeX = 2y - VertexY

---------------------------------------------------------------
Full Example

Clicked X, Y (5,40) lower- right on a 128x64
MapY = Y / TileHeight
MapX = X / TileWidth

SlopeX = 2y - VertexY

SlopeX = 80 - 32 = 48
5 < 48 , MapX--;MapY++


[edited by - Daerax on July 17, 2003 4:21:54 AM, tried to save space]

[edited by - Daerax on July 17, 2003 4:29:51 AM]

Share this post


Link to post
Share on other sites
I used a checkpixel method.

I get the pixel the mouse is pointing to, then I check the tile
graphic to see if the pixel is a color key. If it is, there
you go, if not, check the surrounding tiles.

Currently, I''m checking DDraw7 vid surfaces which should be a bit
slow(ran fine on my Cel433, but I didn''t have a whole lotta stuff
going on). I should probably have it check a file in system
memory instead.

This is a good way around TANSTAAFL''s method if you want to be
able to select things in the way of a tile or odd-shaped tiles
that encompass more than the basic tile space.

Still, cheers to TANSTAAFL! ^,^

-Hyatus
"da da da"

Share this post


Link to post
Share on other sites
Here''s how I did it. First I created a surface to store the mouse map in:


RECT rect = {0, 0, 64, 32};
D3DXIMAGE_INFO info;
g_pD3DDevice->CreateOffscreenPlainSurface(64, 32, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pMMSurface, NULL);
D3DXLoadSurfaceFromFileA(pMMSurface, NULL, &rect, "mouseMap.bmp", NULL, D3DX_DEFAULT, 0, &info);


Then I had to actually get the color of the pixel at the spot:

D3DLOCKED_RECT info;
ULONG color; //color in x,y positions

DWORD blue;
DWORD green;
DWORD red;

pTexSurface->LockRect(&info,NULL,D3DLOCK_READONLY);

DWORD* surface = (DWORD *)info.pBits;
int lpitch = info.Pitch>>2; //Get the right pitch

color= surface[mmX + mmY*lpitch]; // pull out the color

pTexSurface->UnlockRect();

red = (color & 0x00FF0000) >> 16;
green = (color & 0x0000FF00) >> 8;
blue = color & 0x000000FF;

Share this post


Link to post
Share on other sites