0
\$\begingroup\$

I am trying to represent a 3d cone light's direction using only 2 variables. Right now I represent it with two points (6 variables) but I know that the direction in 3d space can be represented by two angles, rotating an arbitrary vector (e. g. pointing upwards) around two fixed axes step-by-step.

These calculations would be done in a shader, so I need a formula similar to the one in this question, that would allow me to perform such rotations. Could anyone suggest it?

UPDATE:

Following the Babis' answer I made a simple console project that computes phi and theta from two 3d points and then recomputes the normalized direction vector using the angles. However, the algorithm seem to always give me the wrong dir.Y value for some reason:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xna.Framework;

namespace DirectionIn2Angles
{
    class Program
    {
        static void Main(string[] args)
        {
            Vector3 first = new Vector3(1000, 1200, 85.245f);
            Vector3 second = new Vector3(1150, 85, 750);

            Vector3 distance = second - first;
            float length = distance.LengthSquared();
            distance.Normalize();
            Console.WriteLine(distance.ToString());

            float cosFSinY = distance.X;
            float sinFSinY = distance.Y;
            float cosF = distance.Z;
            float F = (float)Math.Acos(distance.Z);
            float Y = (float)Math.Asin(distance.X / distance.Z);

            float SinY = (float)Math.Sin(Y);
            float CosF = (float)Math.Cos(F);
            distance.X = CosF * SinY;
            distance.Y = (float)Math.Sin(F) * SinY;
            distance.Z = CosF;


            Console.WriteLine(distance.ToString());
            Console.ReadKey();
        }
    }
}

The values written in the console are:

x: 0,1147876; y:-0,8532547; z: 0,5087044

x: 0,1147876; y:0,1942689; z: 0,5087044

UPDATE:

This really is very wierd, I tried computing phi and theta using formulas from this article, but it now gives me the wrong dist.X! What could I be doing wrong?

UPDATE:

OK, that was just me being blind, I misread the formula. Here is the solution:

SOLUTION:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xna.Framework;

namespace DirectionIn2Angles
{
    class Program
    {
        static void Main(string[] args)
        {
            Vector3 first = new Vector3(1000, 1200, 85.245f);
            Vector3 second = new Vector3(1150, 85, 750);

            Vector3 direction = second - first;
            direction.Normalize();
            Console.WriteLine(direction.ToString());

            float CosYSinF = direction.X;
            float SinFSinY = direction.Y;
            float CosF = direction.Z;

            float F = (float)Math.Acos(direction.Z);
            float Y = (float)Math.Atan2(direction.Y, direction.X);

            float SinF = (float)Math.Sin(F);
            direction.X = (float)Math.Cos(Y) * SinF;
            direction.Y = SinF * (float)Math.Sin(Y);
            direction.Z = (float)Math.Cos(F);

            Console.WriteLine(direction.ToString());
            Console.ReadKey();
        }
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

A normalized direction is a point on the unit sphere, so you need 2 angles. I assume you have a coordinate system where Y is up. Your two variables are phi (0 <= phi <= pi) and theta (0 <= theta <= 2pi). You obtain the normalized direction vector as follows:

dir.x = cos(theta)*sin(phi)
dir.y = cos(phi)
dir.z = sin(theta)*sin(phi)

Source for more info here, look up "Equations in R^3"

\$\endgroup\$
1
  • \$\begingroup\$ Hey, @Babis, thanks for the answer! I made a simple console project with the formula from the linked article, but it constantly gives me the wrong dir.Y. I updated the question, do you want to take a look? \$\endgroup\$
    – cubrman
    Dec 13, 2014 at 7:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .