Tuesday, June 5, 2007

3D calculation example

I have been asked how to create 3D rotation as in the posting about Crystal Xcelsius.

When x, y, and z go like in the image below, and x = longitude 0, and y = longitude 90 degrees, then for arbitrary latitude and longitude:
x = r * sin(longitude) * cos(latitude)
y = r * cos(longitude) * cos(latitude)
z = r * sin(latitude)

.. where r = radius of ball.
When you add the eye (watching point) to the image, then for arbitrary point a in coordinates (xa, ya, za), you can calculate point a' (xa', ya') in the plane between eye and ball.
The next image helps understand how you calculate ya' in the plane:
xa' = ya/(d-xa)
ya' = za/(d-xa)
.. or if you want to put little less perspective, you can do:
xa' = arctan(ya/(d-xa))
ya' = arctan(za/(d-xa))
The point a' (xa', ya') is input for the XY chart.


Anonymous said...

How do you handle literal lat long like a globe - for example 0 latitude being at the equator and 90 being the top of the pole?

Janne Pyykkö said...

Use radians with sin(), cos() and tan()--not degrees. To get radians, multiply by PI()/180. For example 90 degrees = 90*PI()/180 = PI()/2 radians.

Was this helpful to you?