Game Development Community

Translating pixels to and from TX units

by Shawn Simas · in Torque X 2D · 10/22/2007 (10:43 pm) · 2 replies

I recently hooked in a physics engine for an upcoming game I will be working on and realized a problem quite quickly. It's coordinate system is much different than the TX one. It has a top/left corner origin and uses pixel units, whereas TX has a centered origin, using its own units.

I searched for a method in the engine that would allow me to convert units to pixels and the reverse, but couldn't find anything.

Is there such a method in the engine? Or does anyone know how I could go about creating such a function?

Thanks,
Shawn

#1
10/26/2007 (1:12 pm)
Hey Shawn... I'm not familiar with an existing method to accomplish this but I have some ideas of how to write your own.

pseudocode:

Point convertToTopLeft(Point P)
{
float tempX, tempY, newX, newY, screenWidth, screenHeight;
Point NP = new Point();

tempX = P.getX();
tempY = P.getY();

screenWidth = screenObj.getScreenWidth();
screenHeight = screenObj.getScreenHeight();

newX = (screenWidth / 2) + newX
newY = (screenHeight / 2) - newY

NP.x = newX;
NP.y = newY;

return NP;
}

I didn't test this, as I'm at work and don't have the resources. Hope it helps!

Patrick
#2
10/26/2007 (1:32 pm)
I actually did find a solution to this (had to purchase TXPro to view the source of course...)

I created a function that returns the screen to scene ratio and then used this to calculate the new sizes.
I did it using vectors, because in my case I was using it mostly for positions and sizes. Since TXB positions are done in a 4-quadrant grid, I passed in a boolean to flag it as a position or not, if it was a position I would return it based on the quadrants (subtract half the scene width) and if it wasn't (sizes for example) I would just return it as-is.

Seems to be working great. Thanks for the response though!