How can I convert 3D space coordinates to 2D space coordinates?

Conros picture Conros · May 26, 2011 · Viewed 28.1k times · Source

I'm using a Lua API. I'm trying to transform 3D space coordinates to 2D space coordinates. I've asked google but I can't find anything apart from snippets using OpenGL function. I don't need source code or anything just a simple way on how to do it? I can acquire the Camera's perspective, the original position to be transformed, window size and aspect ratio if that's any help? Thanks in advance for any suggestions :)

Answer

ReturningTarzan picture ReturningTarzan · May 26, 2011

If you're talking about transforming world-space (x,y,z) coordinates to screen-space (u,v) coordinates, then the basic approach is:

u = x / z;
v = y / z;

If the camera is not at the origin, transform (x,y,z) by the view matrix before the projection matrix. You may also want to adjust for the camera perspective, aspect ratio etc., in which case I'd refer to this Wikipedia article.

My apologies if you're looking for something specific to the Lua API, which I am not familiar with.