Game Development Community

astar question

by Steve D · in Torque Game Builder · 02/17/2009 (7:37 pm) · 7 replies

I know this is a long shot but I figured I would ask - is there a way via script to get the world coordinates of the center of a tile square?

If not, how hard would it be for a programmer to create this function?

The reason I ask is if you have a RPG type isometric battle with a handful of guys, I don't see how the built in astar system would work for that. The sprites need to have collision turned on (for triggers and such) and when a player moves them into position chances are they will actually take up more then 1 square, ie be half in 1 square and half in the other. If you just mark one square as being occupied I would see a bunch of problems when the sprites try to pass each other, line up next to each other, etc.

I'm thinking if all the sprites are the same size and all fit within a tile square and you could make them go to the center of the square. This way when you set that square as unpassable it wouldn't be an issue for another sprite to occupy the square next to it or pass it, etc.

I am referring to battle types such as the old fallout or Baulders Gate games. I think the tile system is great in TGB but I don't see how I can use the built in astar for the purpose I just stated.

Any comments, suggestions or thoughts would be greatly appreciated.

#1
02/18/2009 (8:00 am)
I don't think there's a method for it but does something like this seem reasonable (untested)?
function getTileCenter(%tileX, %tileY)
{
//get the world coordinates of the tileLayer
%tileLayerPosX = tileLayerName.getPositionX();
%tileLayerPosY = tileLayerName.getPositionY();

//get the size of each tile in the tileLayer
%tileSizeX = tileLayerName.getTileSizeX();
%tileSizeY = tileLayerName.getTileSizeY();

//calculate the world coordinates
//subtract half of tile width/height to get center of tile
%worldCoordX = %tileSizeX * %tileX - (.5 * %tileSizeX) + tileLayerPosX;
%worldCoordY = %tileSizeY * %tileY - (.5 * %tileSizeY) + %tileLayerPosY;
}
#2
02/18/2009 (8:14 am)
Thanks a bunch Patrick, I will test this out and let you know the results!
#3
02/18/2009 (8:28 am)
Very welcome, I hope it helps!
#4
02/18/2009 (11:34 am)
It would be more useful like this though...
function getTileCenter(%tileLayerName, %tileX, %tileY)
{
//get the world coordinates of the tileLayer
%tileLayerPosX = %tileLayerName.getPositionX();
%tileLayerPosY = %tileLayerName.getPositionY();

//get the size of each tile in the tileLayer
%tileSizeX = tileLayerName.getTileSizeX();
%tileSizeY = tileLayerName.getTileSizeY();

//calculate the world coordinates
//subtract half of tile width/height to get center of tile
%worldCoordX = %tileSizeX * %tileX - (.5 * %tileSizeX) + tileLayerPosX;
%worldCoordY = %tileSizeY * %tileY - (.5 * %tileSizeY) + %tileLayerPosY;

%tileCenter = %worldCoordX @ " " @ %worldCoordY;

return %tileCenter;
}
#5
02/18/2009 (2:22 pm)
The code is found on the forums and in the astar code as well. I have used it. I am not home to paste the code for you.
#6
02/20/2009 (6:59 am)
That code didn't appear to work correctly but here is my version which appears to work. I also coded (and still testing it) a script based astar system. What makes it different then the built in version is my version will return, in order, all the tiles from point A to point B and then get coordinates to each tile to send the object to.

function getTileCenter(%layer, %tileX, %tileY)
{
//get the world coordinates of the tileLayer
%LayerPosX = %Layer.getPositionX();
%LayerPosY = %Layer.getPositionY();

// get tile width and height

%tilesize = %layer.GetTileSize();
%tilewidth = getWord(%tilesize, 0);
%tileheight = getWord(%tilesize, 1);

// get the upper left X and Y

%width = %layer.GetWidth();
%height = %layer.GetHeight();
%x = %layerPosX - (%width / 2);
%y = %layerPosY - (%height / 2);

// get what half the tile is

%halfx = %tilewidth / 2;
%halfy = %tileheight / 2;

// get the final position

%xcoord = %tilex * %tilewidth + %halfx;
%ycoord = %tiley * %tileheight + %halfy;

%x = %x + %xcoord;
%y = %y + %ycoord;

return %x SPC %y;
}


#7
02/20/2009 (7:12 am)
Glad you got it working!