Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Could someone point me in the right direction as to how this might be achieved? 3D math / geometry often throws me.

I'm looking for something like this (ideally in C#):

public Vector3 getCartesianFor(float elevation, float asimuth, float polar)
{
   return ????;
}
share|improve this question

3 Answers 3

up vote 4 down vote accepted

http://blog.nobel-joergensen.com/2010/10/22/spherical-coordinates-in-unity/

public static void SphericalToCartesian(float radius, float polar, float elevation, out Vector3 outCart){
    float a = radius * Mathf.Cos(elevation);
    outCart.x = a * Mathf.Cos(polar);
    outCart.y = radius * Mathf.Sin(elevation);
    outCart.z = a * Mathf.Sin(polar);
}


public static void CartesianToSpherical(Vector3 cartCoords, out float outRadius, out float outPolar, out float outElevation){
    if (cartCoords.x == 0)
        cartCoords.x = Mathf.Epsilon;
    outRadius = Mathf.Sqrt((cartCoords.x * cartCoords.x)
                    + (cartCoords.y * cartCoords.y)
                    + (cartCoords.z * cartCoords.z));
    outPolar = Mathf.Atan(cartCoords.z / cartCoords.x);
    if (cartCoords.x < 0)
        outPolar += Mathf.PI;
    outElevation = Mathf.Asin(cartCoords.y / outRadius);
}
share|improve this answer
    
Exactly what was after ... just need to figure out how to do this without the unity math libs ... any recommends on math libs for .Net ... or is this built in somewhere? I could only see these functions for doubles. –  Wardy Aug 13 at 12:25

  • r: radial distance
  • θ: inclination
  • φ: azimuth

via Wikipedia

public Vector3 getCartesianFor(float radius, float inclination, float azimuth)
{
   return new Vector3(radius*Sin(inclination)*Cos(azimuth), radius*Sin(inclination)*Sin(azimuth), radius*Cos(inclination));
}

Spherical coordinates

share|improve this answer
    
You should elaborate about the meanings of the (literal) Greek in your answer. –  Seth Battin Aug 11 at 18:31
    
@SethBattin Thank you for the suggestion! The answer has been updated. –  MLM Aug 11 at 18:36
2  
What you call inclination is probably the complement of what OP means for elevation, ie. the angle from the xy plane to the vector, not from vector to the z axis. –  MestreLion Aug 11 at 22:55

If by polar you mean the magnitude of the vector, and elevation is defined as the angle between vector and the xy plane (as the name implies), the function would be:

public Vector3 getCartesianFor(float elevation, float asimuth, float polar)
{
   return new Vector3(polar*Cos(elevation)*Cos(asimuth),
                      polar*Cos(elevation)*Sin(asimuth),
                      polar*Sin(inclination));
}

Note that this is very similar to MLM's answer, it all depends on how you define your elevation angle. I've also respected your function template signature, but I suggest a few changes:

  • asimuth is a typo, the correct spelling would be azimuth
  • polar usually refers to the 2D coordinate system (r, θ). The magnitude of the vector is commonly referred to as radius
  • A more conventional order would have radius as the first parameter.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.