Game Development Reference
In-Depth Information
7.3.5
Converting between Spherical and Cartesian Coordinates
Let's see if we can convert spherical coordi-
nates to 3D Cartesian coordinates. Exam-
ine Figure 7.8, which shows both spherical
and Cartesian coordinates. We first develop
the conversions using the traditional right-
handed conventions for both Cartesian and
spherical spaces, and then we show conver-
sions applicable to our left-handed conven-
tions.
Notice in Figure 7.8 that we've intro-
duced a new variable d, which is the hor-
izontal distance between the point and the
vertical axis. From the right triangle with
hypotenuse r and legs d and z, we get
z/r = cosφ,
z = r cosφ.
Figure 7.8
Spherical and Cartesian coordinates
for math people
and so we're left to compute x and y.
Consider that if φ = 90 o , we basically
have 2D polar coordinates. Let's assign x and y to stand for the x and y
coordinates that would result if φ = 90 o . From Section 7.1.3, we have
x
= r cosθ,
y
= r sinθ.
Notice that when φ = 90 o , d = r. As φ decreases, d decreases, and by
the properties of similar triangles, x/x
= d/r. Looking at △drz
again, we observe that d/r = sinφ. Putting all this together, we have
= y/y
Converting spherical
coordinates used by
math people to 3D
Cartesian coordinates
x = r sinφ cosθ,
y = r sinφ sinθ,
z = r cosφ.
These equations are applicable for right-handed math people. If we
adopt our conventions for both the Cartesian (see Section 1.3.4) and spher-
ical (see Section 7.3.3) spaces, the following formulas should be used:
Spherical-to-Cartesian
conversion for the
conventions used in this
book
x = r cosp sinh,
y = −r sinp,
z = r cosp cosh.
(7.3)
Converting from Cartesian coordinates to spherical coordinates is more
complicated, due to aliasing. We know that there are multiple sets of spher-
ical coordinates that map to any given 3D position; we want the canoni-
cal coordinates. The derivation that follows uses our preferred aviation-
inspired conventions in Equation (7.3) because those conventions are the
ones most commonly used in video games.
 
Search WWH ::




Custom Search