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.