PDA

Click to See Complete Forum and Search --> : 3d Mapping


wossname
Aug 16th, 2003, 11:53 AM
How can I show a 3d point on a 2d screen (picturebox)?

Given the following...

* The 'camera' position as (X,Y,Z)
* The direction that the camera is pointing in, as heading (from 0 degrees) and elevation (upwards is 0 degrees)
* The 3d coordinates of the point in question as (X,Y,Z)

I've been busting my ass for days over the trigonometry but I can't get it working...

Public Function Translate3D2D(ByVal X As Single, ByVal Y As Single, ByVal Z As Single) As POINTAPI

'POINTAPI is just a UDT with X and Y (both single) members

Dim Ret As POINTAPI
Dim dx As Single, dy As Single, dz As Single
Dim Ax As Single, Ay As Single, Dist As Single

dx = X - mcamCamera.X
dy = Y - mcamCamera.Y
dz = Z - mcamCamera.Z

'Ax is the horizontal angle between the cam heading and the point
Ax = mcamCamera.Heading - (Atn(dx / dz) + 180) 'probably wrong

'dist is the horizontal distance (xy plane)
Dist = Sqr((dx * dx) + (dy * dy))
'Ay is the vertical angle between the cam elevation and the point
Ay = mcamCamera.Elev - Atn(dy / Dist) 'probably wrong

'convert these angles into screen coordinates
Ret.X = (mcamCamera.ZDist * Tan(Ax))'probably wrong
Ret.Y = (mcamCamera.ZDist * Tan(Ay))'probably wrong

Translate3D2D = Ret 'pass results back to the calling procedure

End Function

Where am I going wrong?

Please help.

Regards, Adam

krtxmrtz
Aug 19th, 2003, 03:33 AM
This I've found browsing through my old notes. I remember it used to work but haven't tried it recently.

(Refer to the drawing):

R: distance from origin O to origin sO
D: distance from origin sO to observer
Theta: azimuthal angle
Phi: polar angle
(x, y, z): 3D coordinates of a point that we want to represent in 2D
(cx, cy): coordinates of origin sO relative to the screen's lower left corner
(sx, sy): the sought 2D coordinates

Let,

xe = -x sin(Phi) + y cos(Phi)
ye = -x cos(Phi) cos(Theta) - y sin(Phi) cos(Theta) + z sin(Theta)
ze = -x cos(Phi) sin(Theta) - y sin(Phi) sin(Theta) - z cos(Theta) + R + D

Then:

sx = cx + D * xe / ze
sy = cy + D * ye / ze

krtxmrtz
Aug 19th, 2003, 04:30 AM
Here's a demo project. Looks like once you've set up the approppriate scale to the picturebox, cx and cy are irrelevant...

Btw, what I've called "Rho" in this project corresponds actually to R + D (see drawing in the previous post).

wossname
Aug 19th, 2003, 09:16 AM
Great, this looks promising. I'll give it a try tonight.

Thanks.

cyborg
Aug 19th, 2003, 10:32 AM
i would also really like to know how to do this!

krtxmrtz
Aug 19th, 2003, 11:16 AM
Originally posted by cyborg
i would also really like to know how to do this!
If you mean the derivation I don't have it, I transfered the equations directly from some photocopies I made from a book a few years ago.