[Solved] How to change 3d point to 2d pixel location?


If all you want to do is find 2D screen locations from 3D locations its easy if you know similar triangles. No triggers are required. just make up some variable that is about the distance in pixels out of the screen to your eyes – call that the focal_length. You can adjust that figure to make it look more realistic (too low looks really stretchy)

This solution is for looking forward in x, looking backwards just needs and extra line to negative the z which is just needless complexity in my humble opinion but i don’t know your purpose.

first clip out pixels outside of your range (just use a couple of if statements. I’m not gonna pseudocode that out)

then position yourself in the 3D world:

x0 = x - yourx
y0 = y - youry
z0 = z - yourz

(in your example yourx = 0, youry = 0, yourz = -1)

Then project away:

x2d = focal_length * x0 / z0
y2d = focal_length * y0 / z0

Simple huh? I just made this up by thinking about train tracks and am now trying to make a basic 3d game 😉

If you want to get into the math of 3D rotation or hiding faces etc… that’s when things gets … tricky.

By the way depending on how your screen coordinates are, you might need negative signs on one or the other or both of x2d or y2d equations. Meh you figure it out.

solved How to change 3d point to 2d pixel location?