Game Development Community

World position -> pixel position?

by Riccardo Berta · in Torque Game Builder · 12/29/2008 (2:17 am) · 14 replies

I'm referring to an old post:

http://www.garagegames.com/mg/forums/result.thread.php?qt=26652

"Is there a function to translate a world position to screen coordinates?"

I understood the following functions:
getWorldPoint()
getWindowPoint()
getCanvasPoint()

but i need to translate "world coordinate" to "screen coordinate" (in pixel) referred to entire game screen (not referred only to the portion visible in SceneWindow2D)

nothing has changed since then?

thank you

#1
12/29/2008 (2:25 am)
I could be wrong, but I think you have to do the math yourself. Basically, knowing your resolution, canvas size and position in vectors, you should be able calculate the pixel location.
#2
12/29/2008 (1:14 pm)
Thanks for your answer...

Now my problem is to find an efficient solution to realize that function: in my game i schedule this function every 25milli seconds to verify the player position, so it must be very fast so as not to weigh too much on CPU...


my first idea is:
("background" is an image/object that covers the entire game area; i can see only a portion of "background" = SceneWindow2D ("%background.dimension" is the image dimension in pixel); "%checkPos" the position in "world coordinate system" that i want to convert.

%temp = background.getLocalPoint(%checkPos) ---> normalized player position (between -1 , +1)
%temp = (addVect (%temp, "1 1"))/2 ---> normalized player position (between 0, +1)
%pixelPosition = VectMult (%temp, %background.dimension) ---> the absolute position of player in pixel


some better idea?
any suggestions?
#3
12/29/2008 (4:35 pm)
Hello,

Sounds ok to me but why do you think it would not be fast enough ?

Hmm and why do you schedule it EVERY X milli seconds ? You could test only when the player moves (or has moved). If nothing happens, you don't need to compute what you already know ;)
#4
12/30/2008 (10:18 am)
What you may want is a method on the scene window (SceneWindow2D)

getWindowPoint(X / Y) - Returns Window coordinate of World coordinate.

param x,y
The coordinates in world coordinates you wish to convert to window coordinates. Accepts either (x,y) or (x y)"

return Returns the desired window coordinates as a string formatted as "x y")
#5
12/30/2008 (2:29 pm)
Thanks to all...

for BrunoW: i schedule the function each 25millisec, but if the player is stopped, function does nothing... but usually my player's hero walk around for all time...
I need the best possible solution because each iteration (25millisec) i check the player position (--> i need to know his "world position" (in pixel) obtained with above function) and on the basis of that, i do other simple "things". I was not thinking that function was not fast enough but I would not like it was the bottleneck...


for Matthew: i dont need to get the coordinate on scene window; what i need are the "world coordinate" translated in pixel... world is my entire background image, much more extended than scene window (this last is only the visible portion of world).

any suggestions?
#6
12/30/2008 (2:58 pm)
Ahh, yeah, then your method seems to be the best way. It's a fairly minor calculation in script so shouldn't be a problem. If you wanted to gain additional efficiency you could (if you have a Pro license) add a source code method to do the work for you, though I wouldn't worry about it unless it becomes a problem.
#7
12/30/2008 (3:33 pm)
Thanks for the suggestion but i'm not a good c++ programmer...

i'll make some test with above solution then i'll decide what to do...

thanks
#8
12/31/2008 (5:14 am)
It's been a while since I wrote these functions and I don't have a reference in front of me but as far as I can remember you can use:
getIsWindowPoint(...)
on your t2dSceneWindow to see if a world-coordinate is currently being viewed by this particular scene window which is important to know if you want to attempt to convert that point to a real point on the canvas (window).

If that function returns true then you can proceed to do the conversion.

You can do this by using the function:
getWindowPoint(...)
on the same t2dSceneWindow which converts a world-coordinate into a GUI window-coordinate (t2dSceneWindow GUI element).

To convert this GUI window-coordinate to a Canvas window-coordinate you can use the function:
getCanvasPoint(...)
on the same t2dSceneWindow to give you a Canvas coordinate. I believe this is what you want.

if ( mySceneWindow.getIsWindowPoint( %myWorldPoint ) )
{
   %windowPoint = mySceneWindow.getWindowPoint( %myWorldPoint );
   %canvasPoint = mySceneWindow.getCanvasPoint( %windowPoint );

   // %CanvasPoint is now a main window pixel coordinate...
}
else
{
   echo( "World point is not being rendered!" );
}

Hope this helps,

Melv.
#9
01/02/2009 (10:21 am)
Thanks Melv...

but what i need is a pixel position even if that is not rendered (im using the background image as an absolute
reference system like the "world coordinate")...
#10
01/02/2009 (1:54 pm)
Not sure I fully understand but if you want to extrapolate the pixel position that a world coordinate would be rendered (even if it's not currently) then just omit the "getIsWindowPoint(...)". Obviously you can expect pixel coordinates to be larger than the physical resolution because the world-position is unbounded unlike the physical screen.

Melv.
#11
01/06/2009 (1:47 am)
Thanks Melv...

I'll try...
#12
01/06/2009 (1:58 am)
Tryed with no result: as i suspect %windowPoint and %canvasPoint return the same result in different format:

%windowPoint == xxx.0000 xxx.0000 (as a float...)
%canvasPoint == xxx xxx

...
thanks anyway
#13
01/06/2009 (3:59 am)
You have a problem there as I can assure you that the code is completely different for those calls. I would be happy to look at it if you wanted to send me a working copy of your code?

Melv.
#14
01/07/2009 (9:26 am)
It's very kind of you... but, for now i'm satisfied of the solution presented above:

%temp = background.getLocalPoint(%checkPos) ---> normalized player position (between -1 , +1)
%temp = (addVect (%temp, "1 1"))/2 ---> normalized player position (between 0, +1)
%pixelPosition = VectMult (%temp, %background.dimension) ---> the absolute position of player in pixel


i must try this on different PC to be sure not to load CPU too much...

thanks