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 :)
2 Answers
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.

- 683
- 1
- 5
- 21

- 1,068
- 1
- 13
- 23
-
It's important to note that this isn't a true perspective projection but a so-called "weak perspective projection", a hybrid of orthographic & perspective – adrian Aug 03 '17 at 03:51
-
1This makes no sense to me. As z approaches 0, x and y approach infinity. Thus a vector (1,1,0) in the world space which should be (1,1) in screen space given appropriate camera settings would end up (infinity, infinity). – David Coombes Aug 31 '18 at 13:11
-
2@DavidCoombes Why doesn't it make sense? If the camera is at the origin, looking in the direction of z, it will never be able to see something above, or on the right side of the camera, so x and y should go to infinity. I always use this example when I want to imagine it: https://www.youtube.com/watch?v=_P00jYB81ZQ When the flags get closer to you (and z gets closer to zero), the x and y get larger, and the flags fly out of screen. – Peter Bruins Nov 20 '19 at 12:48
Use this mathematical formula :—
New2dpos = ((zpos÷fovl)*oldpos)
here, pos=position.
here fovl=distance from camera to screen.(this value can be adjusted if your image stretches)
oldpos= 3d "x" or "y" coordinate. this formula works on both x and y 3d coordinates.
New2dpos means projected coordinates which you can use to project on your 2d plane.
See this image for better explanation :
// 'double[] a' indicates your 3d coordinates.eg: a=[x,y,z];
// int b indicates whether you wanted to return your 2d x coordinate or y coordinate. 0 will return x and 1 will return y.
// if your image stretches then you can adjust the fovl(field of view line)(distance from camera to screen) .
public static double PERSPECTIVE_PROJECTION(double[] a,int b){
int fovl=200;
double oldpos=a[b];
double z=a[2];
double newpos=(double)(fovl/(fovl+z))*oldpos;
return newpos;
}
This java code defines a static method called PERSPECTIVE_PROJECTION that takes in an array of doubles a representing 3D coordinates and an integer b indicating whether to return the 2D x-coordinate or the 2D y-coordinate of the projection.
The variable fovl is set to a default value of 200, which represents the distance from the camera to the screen in the projection space.
The code then retrieves the old position of the specified 3D coordinate "a[b]" and the z-coordinate "a[ 2 ]" of the same coordinate.
The formula (fovl/(fovl+z))*oldpos) is used to calculate the perspective distortion factor based on the distance between the camera and the 3D coordinate. (fovl/(fovl+z)) is then multiplied by the old position oldpos to obtain the new position of the specified 3D coordinate in 2D space. The result is then returned as a double.
Overall, this code performs a simple perspective projection of a 3D coordinate onto a 2D plane, given the camera position and field of view distance. It is a simplified version of the more complex perspective projection formula, but can still produce useful results for certain applications. I have seen many solution solving this problem with trigonometric solutions, I thought that it can be solved more efficiently, So i found this formula (fovl/(fovl+z))*oldpos).
Results with some basic axis rotation (made with java swing) :

- 11
- 3