Tile to logical coord
by Gary Preston · in Torque Game Builder · 03/11/2005 (11:09 am) · 8 replies
I've seen the pickTile function which allows me to take a world coordinate and returns the tileX and tileY that the coordinates are within. However, is there anyway to do the reverse, given a tileX and tileY return the center of the tile in world coordinates?
The reason I ask is that I would like to start my player at the center of a specific tile and eventually will want to spawn additional sprites at the center of various random tiles.
I imagine I could obtain the tiles x/y size and multply this by the tile I want then offset by 1/2 the tile height/width and finally offset once more to move into the logical coord space. Before I do this though, am I missing an easier way?
The reason I ask is that I would like to start my player at the center of a specific tile and eventually will want to spawn additional sprites at the center of various random tiles.
I imagine I could obtain the tiles x/y size and multply this by the tile I want then offset by 1/2 the tile height/width and finally offset once more to move into the logical coord space. Before I do this though, am I missing an easier way?
#2
03/11/2005 (11:18 am)
You must have one hell of a long list there :P I think I've got a rougth idea of how to do this, I'm going to give it a go tonight, but I'll be sure to drop another post in here if I get stuck :)
#3
03/11/2005 (11:33 am)
If you get it figured out, it would be nice if you'd post the solution for posterity. :) I'm sure others would find it useful.
#4
http://www.garagegames.com/mg/forums/result.thread.php?qt=27178
Somwhere around the middle of the code, its doing just what you are looking for.
03/11/2005 (11:40 am)
Take a look at the code in the second post, of the following thread.http://www.garagegames.com/mg/forums/result.thread.php?qt=27178
Somwhere around the middle of the code, its doing just what you are looking for.
#5
coord = logical tile coordinates
tile_pos = coord*tile_size + tile_layer_pos
I think tile layer's position by default is its center instead of top left, so keep that in mind.
03/11/2005 (11:41 am)
Well that wouldn't be too difficult, the position of a single tile (considering all positions are top left):coord = logical tile coordinates
tile_pos = coord*tile_size + tile_layer_pos
I think tile layer's position by default is its center instead of top left, so keep that in mind.
#6
%tilexy = tile from pickTile(%pos) (That is, the X/Y of the tile on the tilemap)
%lxy = $Layer0.getPosition();
%pxy = $Layer1.getPosition();
%diffxy = GetWord(%tilexy,0) - getword($Layer0.pickTile(%pxy),0) SPC GetWord(%tilexy,1) - getword($Layer0.pickTile(%pxy),1);
%dest = GetWord(%pxy,0) + (getWord(%diffxy,0) * $tilesize) SPC GetWord(%pxy,1) + getWord(%diffxy,1) * $tilesize);
$Layer1.setPosition(%dest);
$Layer0 being the background layer, $Layer1 being a single tile as the player.
Just having another look at it, I have $tilesize set to "4" for the size being "4 4". So YMMV.
03/11/2005 (12:34 pm)
Here's how I did it.%tilexy = tile from pickTile(%pos) (That is, the X/Y of the tile on the tilemap)
%lxy = $Layer0.getPosition();
%pxy = $Layer1.getPosition();
%diffxy = GetWord(%tilexy,0) - getword($Layer0.pickTile(%pxy),0) SPC GetWord(%tilexy,1) - getword($Layer0.pickTile(%pxy),1);
%dest = GetWord(%pxy,0) + (getWord(%diffxy,0) * $tilesize) SPC GetWord(%pxy,1) + getWord(%diffxy,1) * $tilesize);
$Layer1.setPosition(%dest);
$Layer0 being the background layer, $Layer1 being a single tile as the player.
Just having another look at it, I have $tilesize set to "4" for the size being "4 4". So YMMV.
#7
I'll definately get that function in there though as rotations/panning makes things much harder to calculate.
My only issue with the problem (and the reason I've not done it already) is that the system can be displaying the same logical tile at several locations when wrapping is on which makes the call very ambiguous unfortunately.
- Melv.
03/11/2005 (1:01 pm)
Thanks everyone!I'll definately get that function in there though as rotations/panning makes things much harder to calculate.
My only issue with the problem (and the reason I've not done it already) is that the system can be displaying the same logical tile at several locations when wrapping is on which makes the call very ambiguous unfortunately.
- Melv.
#8
03/11/2005 (2:37 pm)
I've got it working after reading through noolness' post. Which after a quick chat on irc brought up some intresting ideas, such as using a tile type to indicate the spawn point for the player and enemies. That way each map can have different positions :)
Employee Melv May
I can help you with that if it'd help?
- Melv.