Quick question

ok need fast answer, its just simple math

if
[java]
public int getTileByPos(float x, float y) {
x+=offsetX;
y+=offsetY;
int a = (int) Math.floor(x/tile_size);
int b = (int) Math.floor(y/tile_size);
return a*tiles_in_a_row+b;
}
[/java]
so what should getPosByTile(int tile) be like?

[java]
public Vector3f getPosByTile(Tile tile) {
return tile.getLocalTranslation();
}
[/java]

I assume your tiles are nodes yes?

Ooops… just notice you want to pass in an int rep of the tile. Is the int an index? Or?

yes its tile index, i just need backward calculation

Using just that information, it doesn’t look analytically possible. You have 2 unknowns and only 1 equation to solve them:

i.e (a * tiles_in_a_row + b) = index

a and b both represent equations with an unknown as well, so substituting them won’t help either

This will have to be done numerically.

We need more information (context) about what you are trying to achieve, and can come up with a better solution

1 Like

What wezrule say’s.

Additional it is very likely that your getTilePerPos() is already flawed.

You can’t map the 4 tiles around [0,0] for all tilesizes > 1

I think it can be done depending on some answers to some questions.

  1. can x or y ever be negative?

  2. is the result you want the corner of the tile or the center of the tile?

  3. what are offsetX and offsetY?

I do something similar to this in Mythruna to convert coordinates to cell IDs and back.

ok here’s code
[java]
public class SimpleGrid {

private int tile_size = 10;
private int distance = 50;
private int tiles_in_a_row = 8;


private int offsetX = 0;
private int offsetY = 0;

private int curent_tile = 0;
private boolean reposition = true; 

private Event event;

private HashMap<Integer, Boolean> tiles;

public SimpleGrid(int grid_size, int tile_size, int distance, Event event) {
	this.tile_size = tile_size;
	this.distance = distance;
	this.event = event;
	tiles = new HashMap<Integer, Boolean>();
	
	tiles_in_a_row = (int)Math.floor(grid_size/tile_size);
}

public void setOffset(int x, int y) {
	offsetX = x;
	offsetY = y;
}

public void setPos(float x, float y) {
	int tile = getTileByPos(x, y);
	if (tile != curent_tile) {
		curent_tile = tile;
		reposition = true;
	}
}

public int getCurentTile() {
	return curent_tile;
}

public int getTileByPos(float x, float y) {
	x+=offsetX;
	y+=offsetY;
	int a = (int) Math.floor(x/tile_size);
	int b = (int) Math.floor(y/tile_size);
	return a*tiles_in_a_row+b;
}

/*public vecrot2f getPosByTile(int tile) {
}*/

public void update(float posX, float posY) {
	
	int tile = getTileByPos(posX, posY);

	if (tile == curent_tile && !reposition) return;
	reposition = false;
	curent_tile = tile;

	double a = curent_tile/tiles_in_a_row*tile_size + tile_size/2;
	double b = curent_tile%tiles_in_a_row*tile_size + tile_size/2;
	
	for (int col = 0; col < tiles_in_a_row; col++) {
		for (int row = 0; row < tiles_in_a_row; row++) {
			int i  = col*tiles_in_a_row+row;
			float startX = row*tile_size+tile_size/2;
			float startY = col*tile_size+tile_size/2;
			double d = Math.sqrt((startX-a)*(startX-a)+(startY-b)*(startY-b));
			if (d>distance) {
				if (tiles.containsKey(i)) {
					if (tiles.get(i))
						event.unload(i);
					tiles.remove(i);
				}
			} else {
				if (!tiles.containsKey(i) || !tiles.get(i)) { 
					event.load(i);
					tiles.put(i, true);
				}
			}
			
		}
	}
}

}
[/java]

you still need to answer 2), you’ve not given us enough information to solve it (still too many unknowns). A position inside the tile: middle, top centre etc would certainly be enough to solve it.

Edit: Anyways, I’ll assume you want it in the center (Haven’t tested this, and it makes a few assumptions from glancing at your code):

[java]
float normalizedPosX = 0.5f; // Position inside tile X 0->1
float normalizedPosY = 0.5f; // Position inside tile Y 0->1

x = (index % tiles_in_a_row + normalizedPosX - offsetX) * tile_size
y = (index / tiles_in_a_row + normalizedPosY - offsetY) * tile_size[/java]

finally got some time to work with jme :slight_smile:
actually @wezrule was right
its like this:
[java]
float x = (tile % tiles_in_a_row) * tile_size - offsetX + ((float)tile_size/2);
float y = (tile / tiles_in_a_row) * tile_size - offsetY + ((float)tile_size/2);
[/java]

here’s a video how this simple grid works:
[video]http://www.youtube.com/watch?v=0lsM7T3J7Mw[/video]