3D Projection

Started by
3 comments, last by smcameron 15 years, 3 months ago
I have two objects in the 3D graphics portion of my GDI Game Engine... // TDPointPair Is An Object Containing Two TDPoints // The Camera Is A TDPointPair // TDPoint Is A Point In 3D Space // Conversion from 3D to 2D // X, Y, Z are the objects 3D Location // DX, DY, DZ copied from wikipedia // CX, CY Are the screen x, y coords Dim CX, CY, DX, DY, DZ As Double DX = Math.Cos(Camera.PointTwo.Y) * (Math.Sin(Camera.PointTwo.Z) * (Y -Camera.PointOne.Y) _ + Math.Cos(Camera.PointTwo.Z) * (X - Camera.PointOne.X)) - Math.Sin(Camera.PointTwo.Y) * (Z - Camera.PointOne.Z) DY = Math.Sin(Camera.PointTwo.X) * (Math.Cos(Camera.PointTwo.Y) * (Z - Camera.PointOne.Z) _ + Math.Sin(Camera.PointTwo.Y) * (Math.Sin(Camera.PointTwo.Z) * (Y - Camera.PointOne.Y) + Math.Cos(Camera.PointTwo.Z) _ * (X - Camera.PointOne.X))) + Math.Cos(Camera.PointTwo.X) * (Math.Cos(Camera.PointTwo.Z) * (Y - Camera.PointOne.Y) _ - Math.Sin(Camera.PointTwo.Z) * (X - Camera.PointOne.X)) DZ = Math.Cos(Camera.PointTwo.X) * (Math.Cos(Camera.PointTwo.Y) * (Z - Camera.PointOne.Z) _ + Math.Sin(Camera.PointTwo.Y) * (Math.Sin(Camera.PointTwo.Z) * (Y - Camera.PointOne.Y) + Math.Cos(Camera.PointTwo.Z) _ * (X - Camera.PointOne.X))) + Math.Sin(Camera.PointTwo.X) * (Math.Cos(Camera.PointTwo.Z) * (Y - Camera.PointOne.Y) _ - Math.Sin(Camera.PointTwo.Z) * (X - Camera.PointOne.X)) CX = (DX - 0) * (0 / DZ) CY = (DY - 0) * (0 / DZ) Return New PointF(CX, CY) Can someone please fix this so that is gets the appropriate perspective drawing point!
Advertisement
Quote:Original post by Robert Colton
CX = (DX - 0) * (0 / DZ)
CY = (DY - 0) * (0 / DZ)
Return New PointF(CX, CY)


What are you trying to accomplish with this? Your function will always return the point (0 | 0).

Wikipedia says thats what the conversion is from the world coords to the screen coords?
Quote:Original post by Robert Colton
Wikipedia says thats what the conversion is from the world coords to the screen coords?


Well just look at the equations. 0/anything is always going to be 0. then you multiply 0 and your other variables, which will always be 0, so you are always going to return 0,0. Doesn't make much sense.
You can derive a simple projection easily enough.

(Oh, hmm, bbcode img tags don't work here, ok.)

Take a look at this image:
http://img.photobucket.com/albums/v256/cmscdj/art/3dprojection.jpg

ez == the distance from you eye to screen in whatever units you're using (make something up, experiment.)

z is the z coordinate relative to the screen plane of the object you want to project,

y is the y coordinate relative to the screen plane of the ojbect you want to project.

We want to find sy, the y coord of the projection on the screen.

We know by similar triangles that

sy/ez == y/(ez + z)

So sy = ez * y/(ez + z);

Change out x for y everywhere in the above, and you get the projected x coordinate.

This topic is closed to new replies.

Advertisement