Game Development Community

World Position to Tilemap Position

by Matt Huston · in Torque Game Builder · 04/19/2007 (12:43 am) · 6 replies

I have my player on a tileLayer. I obviously know my players world position, however I need to find out his position on the tileLayer. I have seen a couple of different functions that do the opposite (Tile Position to World Position) has anyone written a World Position to Tilemap position function?

#1
04/19/2007 (12:47 pm)
This is off the top of my head, and I haven't scripted for a while, but see how this goes.

$PlayerPosition = $tileMap.getLocalPoint($mainPlayer.getPositionX(), $mainPlayer.getPositionY());

Then, to access the X value:
$xTileValue = getWord($PlayerPosition,0);
$yTileValue = getWord($PlayerPosition,1);

Hope that works :)
#2
04/20/2007 (12:33 am)
Sorry, I didn't want the local point, I wanted the actual Tile X/Y coordinates. So it would return X = 3, Y = 4, etc.
#3
04/20/2007 (2:00 am)
Just divide $xTileValue with the tile size?
#4
04/20/2007 (2:59 am)
@Magnus

Good idea but it doesn't work because the values recieved from $tileMap.getLocalPoint are coordinate values which range from -1 to 1 on the X/Y
#5
04/20/2007 (9:51 am)
Matt,

%tile = %layer.pickTile(%player.getPosition());

It's actually a whole lot simpler then it sounds ... pickTile takes world coordinates, and returns back the tile X/Y coordinates ... so if tile 4,5 is at world position 45,65 and you call pickTile(43x65) it'll return 4,5 ... or maybe 3,5 depending on your tile size, hehe ...


reversing this, to retrieve the world position of a tile ... ie; you know you want to move an object from tile 1,1 to tile 10,10 ... is a bit more complicated, cause there's no reverse function for pickTile ... but I've come up with a reverse function ... pretty sure I've posted it on the forums or in a .plan ... don't have the code on hand, but if this is needed, let me know (email or reply to this thread) and I'll dig it up and post it ...


#6
04/21/2007 (12:45 pm)
Found the code -- figured I'd post it in case any passer-bys are interested ...

This code gives you the World Position of a Tile (the world X/Y of the center of the tile), it's useful when using Phil Shenk's A* Path Finding resource ... since it takes start/end points as world positions, and not tile coordinates ...
function t2dTileLayer::getTileWorldPosition(%this, %tileXPosition, %tileYPosition)
{
   // determine if we were passed a word-list or x/y coords
   if(getWordCount(%tileXPosition) > 1)
   {
      %tilex = getWord(%tileXPosition, 0);
      %tiley = getWord(%tileXPosition, 1);
   }
   else
   {
      %tilex = %tileXPosition;
      %tiley = %tileYPosition;
   }
   
   // %buff just stores stuff real quick -- I like 'getSize' over 'getSizeX', personal preference
   %buff = %this.getTileSize();
   %tsx = getWord(%buff, 0);
   %tsy = getWord(%buff, 1);
   
   %buff = %this.getTileCount();
   %tcx = getWord(%buff, 0);
   %tcy = getWord(%buff, 1);
   
   %p = %this.getPosition();
   %px = getWord(%p, 0);
   %py = getWord(%p, 1);
   
   // get the top-left
   %tlx = -(((%tcx * %tsx) / 2) + (%tsx / 2));
   %tly = -(((%tcy * %tsy) / 2) + (%tsy / 2));
   
   // determine tile world position
   %wx = %px + (%tlx + (%tsx * (%tilex++)));
   %wy = %py + (%tly + (%tsy * (%tiley++)));
   return %wx SPC %wy;
}