3D-projection: How does it work?

Most of the computer games that feature some sort of 3d-graphics usually use 3d-projection to determine where to paint objects on your screen.

You probably think projection is a hard topic, especially if you ever looked at the long formula's at wikipedia, but in this post you will learn, that projection comes down to just one surprisingly simple formula.

The concept behind 3d-projection is pretty simple: To project a point(red) onto a plane(white) you just need to connect(yellow) the point with the position of the virtual camera/observer(green) and the point where the line meets the plane is the projection(magenta):
Untitled.png
If the position of the players eyes is relative to the screen similar as the position of the virtual camera relative to the virtual screen, the player will get optimal 3d-perception.
Many games let you adjust this distance in form of FOV.

The projection process can be packed in a simple general formula which I will derive in the following.
What's nice about projection is, you can look at each dimension of the result seperately. So creating a formula for 2d-projection can be used to describe 3d-projection.

To simplify the resulting formula we assume that the camera is pointing towards the z-axis and is located at (0|0|0).
Let's assume a general point P(x|y|z) that needs to projected.
The x-z-area may look something like this:
Untitled1.png
Here x₂ is the x-value of the projection and d is the virtual distance between camera and plane.
As you can see the blue and the red triangle share the same angle α.
Because both triangles are right triangles:
tan(α) = x/z; tan(α) = x₂/d

x/z = x₂/d

x₂ = d*x/z
Yes, projection really comes down to just this simple formula.
You probably want to know why all those formulas at wikipedia are so complex compared to this? That's because those formula's already involve translating the coordinate system towards the camera and rotating around that camera for the case that the camera is not looking along the z-axis and is located at some other coordinates than assumed here.
Especially for cases like Snake3D where you don't need to rotate the camera this concept is a better implementation.

The formula for y₂ is pretty similar and can be derived on the same way:
y₂ = d*y/z

Now only one question remains: How do you get d?
You most likely want the projection values to be in pixels, so you can directly determine where to paint the result on screen. If that's the case then you would just need to estimate the distance between your head and your computer in pixels(at the resolution of your screen) and thats it.



0
0
0.000
2 comments
avatar

Congratulations @quantumdeveloper! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You distributed more than 500 upvotes. Your next target is to reach 600 upvotes.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

You can upvote this notification to help all Steem users. Learn how here!

0
0
0.000