I have multiple tiles and need to place a new tile on that exact tile spot. I want the tile drawn to place itself in the nearest tile (Pretty much like minecraft2D).
Diagram:
http://i.minus.com/j7XXDSQE6QX4A.png
NOTE: I’m using lbGdx Tiled.
I have multiple tiles and need to place a new tile on that exact tile spot. I want the tile drawn to place itself in the nearest tile (Pretty much like minecraft2D).
Diagram:
http://i.minus.com/j7XXDSQE6QX4A.png
NOTE: I’m using lbGdx Tiled.
How did you divide your map? If you divided them into rows and columns based on tile width and tile height, you have to find the nearest tile. Here’s some psuedocode to get you started.
// First, get the current position of the tile (in pixels)
int pTileX, pTileY;
// Then calculate the possible tile coordinates and store them in a list
Point possible = [];
// Top left
Point tTopLeft = new Point((pTileX - tileWidth/2)/cols, pTileY/rows);
if (isPossible(tTopLeft))
possible.push(tTopLeft);
// Similarly calculate other tiles and if they are valid,
// store them in possible list
...
// Now, calculate the minimum distance point
Point tileCoord = possible[0];
float distance = abs(tileCoord.subtract(new Point(pTileX/cols, pTileY/rows)));
for (Point tile : possible)
{
float newDist = abs(tile.subtract(new Point(pTileX/cols, pTileY/rows)));
if (newDist < distance)
{
tileCoord = tile;
distance = newDist;
}
}
// The correct tile coordinate is in tileCoord
currentTileCoord = tileCoord;
The above is just psuedocode, and it can be largely optimized too. Hope this helps.
A bit too complicated for me to understand. ??? Since I’m using libGdx, couldn’t I just get the cell it’s in with a smaller rectangle and place it in the cell’s position? I just don’t understand what all of your variables mean. Then again, it’s psuedocode. :emo:
Give us your code and we will fix it
No. He learns nothing from that.
I’m assuming you have some sort of bounding box or collision testing enabled object on each tile (a box2D body or Rectangle). Literally all you need to do is, for each click, loop through all the tiles visible in the screen and check if it’s colliding. It’s really not complicated. However I may be misinterpreting your question (not really much of a question, more of a request for code…)
Post the code you have if you can’t get what I suggested working.
Yeah I figured it out. Your answers got me thinking and I made a solution.
I think people with similar problems would like to hear it.
If they can’t figure it out from the information in this thread then maybe they should think about it a little more. It’s not complicated. But I’m glad OP got it working