# 3d Tile Grid mouse selection

Page 1 of 1

## 5 Replies - 1228 Views - Last Post: 23 March 2013 - 10:29 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316333&amp;s=df48a553cf22b6c2c40949b4055cb18b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kamlanie

Reputation: 1
• Posts: 16
• Joined: 06-July 12

# 3d Tile Grid mouse selection

Posted 22 March 2013 - 09:15 AM

Hello all! im back again with my little project / game and i've run into a snag.

What would be the best solution to checking the X & Y of the mouse to a tile on the tile grid?

right now, my "just get it working" hack-job:
```	public void leftClick(int x, int y) {
// checks to see if mouse clicked within the Tile Grid
if (x > levelX && x < levelX + (WIDTH * Tile.WIDTH) && y > levelY && y < levelY + (HEIGHT * Tile.HEIGHT)) {
// convert Pixel X&Y to grid X&Y
int xx = (x - (int) levelX) / Tile.WIDTH;
int yy = (y - (int) levelY) / Tile.HEIGHT;
// find the topmost tile that == air
for (int z = DEPTH - 1; z >= 0; z--) {
if (tiles[xx][yy][z].getId() == 0) {
// set the tile @ xx,yy,z to a different tile
tiles[xx][yy][z] = selection.getSelection(xx, yy, z);
break;
}
}
}
}
```

The other solution i have come up with, is running 3 for loops for x,y,z

i dont really like doing it this way, and it dosnt work quite right:
```	public void leftClick(int x, int y) {
for (int xx = 0; xx < WIDTH; xx++) {
for (int yy = 0; yy < HEIGHT; yy++) {
for (int zz = 0; zz < DEPTH; zz++) {
if (x > tiles[xx][yy][zz].renderX && x < tiles[xx][yy][zz].renderX + Tile.WIDTH && y > tiles[xx][yy][zz].renderY && y < tiles[xx][yy][zz].renderY + (50 + Tile.HEIGHT) && tiles[xx][yy][zz].getId() != 0 && tiles[xx][yy][zz].shouldRender) {
System.out.println(xx + " " + yy + " " + zz);
return;
}
}
}
}

}
```

any suggestions would be greatly appreciated! =D

Is This A Good Question/Topic? 0

## Replies To: 3d Tile Grid mouse selection

### #2 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: 3d Tile Grid mouse selection

Posted 22 March 2013 - 09:52 AM

What is a tile? A simpler way might be to make your tiles a Component, either using an existing child component or subclassing it yourself, and add a single mouse listener(s) to them, multiple if needed. I believe I owe CasiOo credit for this suggestion.

### #3 kamlanie

Reputation: 1
• Posts: 16
• Joined: 06-July 12

## Re: 3d Tile Grid mouse selection

Posted 22 March 2013 - 10:05 AM

Forgive me for not explaining better, This is a game level. Tiles are basically classes with images.

For example: the image below shows a tile grid of 10 WIDTH, 6 HEIGHT, 6 DEPTH. Now, half of the depth is == air (an invisible tile with no properties) I am trying to have it change what type of tile (grass / dirt/ air/ etc.) when you click.

### #4 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: 3d Tile Grid mouse selection

Posted 22 March 2013 - 10:10 AM

My suggestion might still be relevant, but I'll leave it to the game experts. Be patient, and one of them will be by. There's also a forum specifically for game development that you may want to post your question to or request that this post be moved to that forum. You can report your own post, request the move, and a mod will take care of it, if s/he agrees with the move.

### #5 anonymous26

• D.I.C Lover

Reputation: 2
• Posts: 3,638
• Joined: 26-November 10

## Re: 3d Tile Grid mouse selection

Posted 22 March 2013 - 04:21 PM

The representation of a tiling/tiled system is 2D, I have no idea why you have gone to three dimensions. You can solve what you are trying to do using 2D (or even a 1D array if you're prepared to do a little math with the indexing).

Now to your problem of selecting the appropriate tile that you have selected with your mouse:

1. You need to grab the client area of the window, in terms of the number of pixels in the x and y directions.
2. You need some kind of event mechanism that can capture mouse position and button clicks.
3. Your tiles should hopefully evenly partition the client area of the window into a fixed number of tiles in the x and y directions.

In the code you have posted you have none of this implemented.

### #6 kamlanie

Reputation: 1
• Posts: 16
• Joined: 06-July 12

## Re: 3d Tile Grid mouse selection

Posted 23 March 2013 - 10:29 AM

Ok, noone seems to understand what I am saying.

This is going to be an Isometric(ish) version of minecraft

I have a tilegrid:
```Tile[][][] tiles = new Tile[WIDTH][HEIGHT][DEPTH];

```

a tile is:
```	public Tile(int id, int x, int y, int z) {
this.id = id;
this.shouldRender = true;
this.x = x;
this.y = y;
this.z = z;
}

```
The x,y,z, is the position of the tile in the fore mentioned tile grid.

this code calculates where(in pixels) to render the tile using a levelX and levelY float offset:
```	public void update(float levelX, float levelY) {
this.renderX = levelX + (this.x * Tile.WIDTH);
this.renderY = (levelY - 50) + (y * Tile.HEIGHT) + (z * Tile.DEPTH);
}

```

The image to render is contained in a subclass of Tile, for example grassTile:
```	public GrassTile(int id, int x, int y, int z) {
super(id, x, y, z);
try {
this.sprite = new Image("Grass Block.png");
} catch (SlickException e) {
e.printStackTrace();
}
}

```

Now, when i left click, the x and y in pixels are sent to:
```	public void leftClick(int x, int y) {
for (int xx = 0; xx < WIDTH; xx++) {
for (int yy = HEIGHT - 1; yy >= 0; yy--) {
for (int zz = 0; zz < DEPTH; zz++) {
// ignores invisible tiles, and air tiles
if (tiles[xx][yy][zz].shouldRender && tiles[xx][yy][zz].getId() != 0) {
// is mouse on said tile?
if (x > tiles[xx][yy][zz].renderX && x < tiles[xx][yy][zz].renderX + Tile.WIDTH && y > tiles[xx][yy][zz].renderY + 50 && y < (tiles[xx][yy][zz].renderY + Tile.HEIGHT) + (zz * Tile.DEPTH) && zz > 0) {
tiles[xx][yy][zz - 1] = selection.getSelection(xx, yy, zz - 1);
return;
}
}
}
}
}
}

```

I have created a graphical debug mode that shows the x,y,z, and colors the clickable area in red

Everything seems to work fine on the surface of the grid:

now, when i left click to add a grass tile to 0,0,2; it still seems to be working just fine:

right now, the tile grid is 6 tiles deep, half of wich are set to air, so that you may build up 2 tiles. the rest are set to grass and dirt. When i add one more tile on top, things start to act up: