3D space to 2D screen

Started by
3 comments, last by Snork 22 years, 5 months ago
Okay, I''m sure this is a dumb question, but I haven''t been able to figure it out yet. Here''s the setup: I''m using the basic method of screenX = (objectX / ObjectZ) + HALF_SCREEN_WIDTH screenY = (objectY / ObjectZ) + HALF_SCREEN_HEIGHT to project from the 3D world onto the 2D screen. How do I set up the world and the objects in it so that ObjectZ is never zero? I thought the way to simulate moving the camera about inside the world was to move the world about. So if I translate the objects in the world to simulate camera movement, how do I keep the Z values from being zero? Thanks anybody who can help me with this! -Snork "Get busy livin, or get busy dyin."
"Get busy livin', or get busy dyin'."
Advertisement
Well, usually people build near and far planes to their camera. So if the z coordinate is too high or to low, you don''t draw the object (or you clip the object, if you want to be fancy.)

So just set your near clipping plane at something greater than 0, (assuming your camera is at 0) and you will be fine!

First, you forgot something:

X'' = (X/Z) * D
Y'' = (y/Z) * D

Where D is the distance between the camera point
and your projection plane (screen).

You can figure out D as following:

D = (ScreenWidth / 2) / tan(FOVangle / 2)

FOVangle means how big your Field Of View is.
Most Engines have 90 degrees.
NOTE: Most Programming languages'' tan functions
expect the angle in radians.
Angle in rad = angle in deg * (pi/180)


You have to determine, which polygons have a coordinate
with z<=0. Your camera is moving in the world,
and there will be objects with negative or zero z-coords,
those are behind you!! (you cannot prevent that there
are objects behind you )

Don''t draw those polygons.
You have to test all polygons for that.
If not every z-coord of a polygon is <=0, you''ll have to
clip the polygon, into two parts, the one that is visible,
which you draw, and the one which is not visible, and which
you not draw.

Sorry, with the 3d to 2d equations, i forgot
the + half_screenwidth, of course.

for Y you should use: Y'' = half_scrheight - (Y/Z)*D,
if you want positive values beeing on upper side of the screen,
and negative ones lower.
Why do I always remember things AFTER pressing
the "send" button...

NOTE: DO NOT use different D values for X and Y,
use the same. Of course, the Distance of your
projection plane to view point is the same for
X and Y :-)

(I say this, because I saw some guys doing nonsense
like that, it looks ugly, and is incorrect, of course...)

This topic is closed to new replies.

Advertisement