Game Development Community

using getPosition()

by Takara Mitsumi · in Torque Game Builder · 04/07/2010 (7:28 am) · 6 replies

I'm working on a tetris-style game, and would like to know how do I use getPosition() properly so that it gets the x and y position of objects within the grid, rather than getting it based on the entire world/screen size?

For example, if I do a check to see if a game piece is at position(3,5) on a 8x8 grid, when I use getPosition() and echo the results, I get a large floating number like (-31.00017, -23.990002). Is there a syntax I can use to get the position of an object within a grid?

#1
04/07/2010 (7:42 am)
I'm not aware of anything that does it for you but it's pretty simple. EDIT: Reread your comment... You seem to just want the offset from the edge of the grid, so...
function tileLayerNameOrClass::getTilePosition(%this, %tileX, %tileY)
{
   //prevent asking for an overflow
   if(%tileX > %this.getTileCountX || %tileY > %this.getTileCountY)
      return -1;

   //let me know if you need explanation for this
   %x = %tileX * %this.getTileSizeX();
   %y = %tileY * %this.getTileSizeY();

   return %x SPC %y;
}

I hope it helps.

Patrick
#2
04/07/2010 (7:57 am)
I still get the wrong result when I call the function. I'm getting negative floating point numbers again. I tried using:

echo(%this.getTilePosition(%this.piece[%x] SPC "," SPC %this.piece[%y]));

piece[%x] is supposed to equal 4 on the grid and piece[%y] is supposed to be 5 on the grid.

Instead it's returning (-25.311, -11)
#3
04/07/2010 (8:08 am)
Those numbers could be correct... Remember that in TGB, the center of your scene is 0,0.
#4
04/07/2010 (8:17 am)
take a look at my version of the match3engine post full project dl which has collapsing and all that where the blocks above fall down and search the forum for match3engine there is a tutorial on how it is set up but the tile and grid placement finding should help
#5
04/07/2010 (8:44 am)
I've looked at it and have tried to apply it to my code, however it's still returning negative floating point numbers. Perhaps these numbers are correct, but is there any way to have the center of the scene be shifted to be confined within the area that the game pieces are? Or is this just a limitation of TGB?
#6
04/07/2010 (9:11 am)
If you're looking for the distance from the edge of the grid (always positive), you can use this:
function tileLayerNameOrClass::getTilePosition(%this, %tileX, %tileY)
{
   %x = %tileX * %this.getTileSizeX();
   %y = %tileY * %this.getTileSizeY();

   return %x SPC %y;
}

Unless you give that function negative numbers, you'll get positive numbers back.