Game Development Community

Individual tile world position

by Bucko · in Torque Game Builder · 04/08/2005 (5:09 pm) · 13 replies

Is there a way to get a particular (ix,iy) tiles world position?
Aside from extracting the collision polygon and calculating the tiles center from there (this may not even be possible as coll poly corners are fractions of sprite dim).
How do you get a tiles pos from the tile index and dimensions/scale of the tilemap?

#1
04/08/2005 (10:03 pm)
I use the following
function calcwposfromtile(%tilexy,%object) {
	%pxy = %object.getPosition();
	%x = GetWord(%tilexy,0);
	%y = GetWord(%tilexy,1);
	%diffxy = GetWord(%tilexy,0) - getword($collisionlayer.pickTile(%pxy),0) SPC GetWord(%tilexy,1) - getword($collisionlayer.pickTile(%pxy),1);
	%dest = GetWord(%pxy,0) + (getWord(%diffxy,0) * $tilesize) SPC GetWord(%pxy,1) + (getWord(%diffxy,1) * $tilesize);		
	return(%dest);
}

But beware, %object has to be set using something like this:
$player.setPosition($layerfive.pickTile("10 10"));

Also, I havn't tried using that for odd size tiles, so I don't know well it would work.
(Since the tiles are 4x4 and $tilesize is say, "4")

If anyone has a better way, i'd be interested as the above code is a bit of a hack.
#2
04/09/2005 (5:58 am)
The reason I haven't provided one is because a logical tile can appear more than once with wrapping on so it becomes ambiguous.

It is possible to do this for a physical tile though but this info would have to be queried using a particular worldspace pos rather than a logical tile coordinate similar to a pick function but as a member of the tile-layer itself, similar to the "pickTile()" function.

- Melv.
#3
04/09/2005 (12:47 pm)
Maybe, but the actual center of a tile would appear in one place only, with wrapping or not.
I see your point though; if a sprite would hit a particular tile and someone would query its worldpos it could very well have return the wrapped one with the centerpoint instead of the tile actually hit.
One could return each possible worldpos with the one with the centerpoint first. Up to four returned worldpositions would be returned if my understanding of how wrapping works is correct.
I'll write my own method as I need it for my own game: I use a particular tile image to denote the starting point for the players sprite. This way, one can set up a map with start and exit points entirely within the tileEditor, which make it much easier for my kids to do their own maps.
#4
04/09/2005 (2:01 pm)
I don't think you quite got my point. ;)

When you pick tiles at the moment, it's a via "logical" tile query. A logical tile does not have a single position in the world, hence the ambiguity,

In the collision case it's unambiguous as it's a specific instance. I'm only concerned with how you want to specify the query for the "pick".

If you want to ask where the center-point of a logical tile is, it can't be done without restriction. If you want to know the tile-details at a specific world-position, that can be done because it will be a specific instance of a logical tile. Not sure what that function would be called but I could think of something.

This is something I can look at and maybe even get in for the next update.

- Melv.
#5
04/09/2005 (2:41 pm)
Well, I want to specify my pick in logical tile query if I got your terminology right; If I want to know the center of the tile horizontal nbr 3, vertical nbr 2 I'd call $myTileLayer.getTilePosition("3 2"). And yes, they should be zero based indices.

Ah, now I get it (I think)!
The above indices refers to the original un panned/wrapped tile positions, not the indices of tile that have moved due to panning/wrapping. Don't worry though, I've added a 'getTilePosition() C++ call myself that works the way I want it but I won't post it as it doesn't take panning/wrapping into account at all.
#6
04/10/2005 (3:20 am)
Bucko,

Yes, I should have mentioned that, sorry. By logical, I mean the tile in memory, by physical, I mean the tile(s) on screen. Analogous to texels and pixels.

In simple cases, it's easy to calculate it but with wrapping on, it becomes ambiguous. Also, rotating adds a slight complexity as well. The only way to access it is via a world-space query to remove the ambiguity.

Sorry if I wasn't so clear on describing the issue. :)

- Melv.
#7
05/04/2005 (9:39 pm)
Ok, so what exactly does pickTile() return if it's not the world coordinates of that tile? Unless you pass a world position to pickTile()? It's not clear in the docs which is which.
#8
05/04/2005 (11:02 pm)
As the documentation states, the logical tile position e.g. the position used when setting tiles. You know the one, "setStaticTile()".

"pickTile()" turns a world-position into a logical tile position. Note that the logical tile-position can appear multiple times when wrapping is on.

- Melv.
#9
05/04/2005 (11:36 pm)
Ok, it's not clear in the docs that you pass world coordinates to pickTile(). That's what had me confused.

It says, [Parameters] "int tileX / int tileY" which sounds like it means a tile index. Further, it says, [Detail] "tileX/Y - Selected Tile" which also sounds like it should be a tile index instead of world coordinates. Maybe "worldX / worldY" would be better? Don't mean to be critical, just trying to help make the docs the best they can be.

Thanks for the clarification. :)
#10
05/05/2005 (12:38 am)
@Jason: Sorry! I forgot that we're out of sync on the doco! The doco has been updated/had fixes and I kinda' forget who's got what!

You are correct, that description is totally misleading! The new doco has the correct entry.

Sorry for the understandable confusion.

- Melv.
#11
05/06/2005 (5:54 am)
@Melv: In regard to this...
Quote:If you want to ask where the center-point of a logical tile is, it can't be done without restriction.
...what if a .getTileWorldPosition( x, y ) call returned all logical instances' positions? At least we'd then have the world positions to sort through if there were multiples. Finding the world position of a tile is a very useful function, and so maybe this could resolve the ambiguities, albeit, imperfectly.
#12
05/07/2005 (1:03 am)
@Jason: It would certainly be possible; I've had something like this one on my list for quite a while. Not sure exactly when I'll get around to it.

- Melv.
#13
01/26/2006 (4:12 pm)
This issue is addressed in another thread as well, under Torque 2D Private Forums >> Tiles.

www.garagegames.com/mg/forums/result.thread.php?qt=37087