# Spherical Coordinates

#### Nestharus

Level 31
The spherical coordinate system maps points relative to an origin given two angles and a distance.

What makes this different from the Cartesian coordinate system is that rather than defining a point by its x,y,z distance from the origin, it defines a point by the magnitude of an x,y,z point and two angles (magnitude being distance).

The magnitude is defined as ||p|| or ||r|| = SquareRoot(x*x+y*y+z*z)

Notice how it has 3 dimensions. The z just needs to be added in order to get that third dimension.

The two angles define direction.

In the polar coordinate system, one angle and a distance is needed. This angle is theta, which is how far around a circle a given point goes.

Theta-
Code:
``````x = r*cos(theta)
y = r*sin(theta)
r = ||p|| = SquareRoot(x*x + y*y)
tan(theta) = y/x``````

Note that because z is 0, it doesn't do anything in the polar coordinate system.

By adding a z, the magnitude expands and a second angle is needed, this angle being how far the point is from the northern z-axis (easier this way).

The range of phi goes from 0 to pi, 0 being north and pi being south.

In polar coordinates, phi is always pi/2.

Equations are as follows
Code:
``````x = p*cos(theta)*sin(phi)
y = p*sin(theta)*sin(phi)
z = p*cos(phi)

p = SquareRoot(x*x + y*y + z*z)
r = SquareRoot(x*x + y*y)
tan(theta) = y/x
tan(phi) = r/z``````

Angle Between Points (x0, y0, z0) and (x, y, z).
(x0, y0, z0) would be the first point
Code:
``````where
----xd = x - x0
----yd = y - y0
----zd = z - z0
----pd = SquareRoot(xd*xd + yd*yd + zd*zd)
----rd = SquareRoot(xd*xd + yd*yd)

tan(theta) = yd/xd
tan(phi) = rd/zd``````

Distance between two 3D points
Code:
``````where
----xd = x - x0
----yd = y - y0
----zd = z - z0
----pd = SquareRoot(xd*xd + yd*yd + zd*zd)

distance = pd``````

JASS:
``````thetaBetween = Atan2((y - y0), (x - x0))
phiBetween = Atan2(SquareRoot((x - x0)*(x - x0) + (y - y0)*(y - y0)), z - z0)
distanceBetween = SquareRoot((x - x0)*(x - x0) + (y - y0)*(y - y0) + (z - z0)*(z - z0))``````

Last edited:

#### Magtheridon96

Level 35
I put them in a library for you ^^
JASS:
``````library CoordUtils
// All the math done by Nestharus
// Compiled to one library by Magtheridon96
// I based it on locations, but it can be easily changed to work with coordinates :)
function GetDistance takes location v, location w returns real
local real xd=GetLocationX(w)-GetLocationX(v)
local real yd=GetLocationY(w)-GetLocationY(v)
local real zd=GetLocationZ(w)-GetLocationZ(v)
return SquareRoot(xd*xd+yd*yd+zd*zd)
endfunction

function GetPHI takes location v, location w returns real
return Atan2(GetDistance(v,w),GetLocationZ(w)-GetLocationZ(v))
endfunction

// Returns the theta in radians
function GetTHETA takes location v, location w returns real
return Atan2(GetLocationY(w)-GetLocationY(v),GetLocationX(w)-GetLocationX(v))
endfunction
endlibrary``````

#### PurgeandFire

Level 44
I wish there was a test map/demo code for this to show how to practically use it, but otherwise it is good knowledge.

~Approved.

#### Orcnet

Level 30
yes! the demo map is a must bro! this tut is so useful! good job!

#### Magtheridon96

Level 35
If you create an API for this with functions like: ApplyVectorMovement(unit), this would definitely deserve being a sticky

Replies
4
Views
724
Replies
13
Views
2K
Replies
5
Views
618
Replies
16
Views
2K
Replies
0
Views
671