Converting an Azimuthal Coordinate to Vector3D?

Hey guys,

I’m following this really great tutorial on steering behaviors and converting it over from AS 3.0 to C# for Unity (obviously). All is going well. I’ve managed to get seek/flee/arrive working, but there’s one line for wander I’m having issues with.

Apparently there’s an overload for Vector3 in AS 3.0 that builds a vector from its azimuthal coordinates (eyes glazing over).

public Vector3D(double alpha,
                double delta)

    Simple constructor. Build a vector from its azimuthal coordinates

        alpha - azimuth (α) around Z (0 is +X, π/2 is +Y, π is -X and 3π/2 is -Y)
        delta - elevation (δ) above (XY) plane, from -π/2 to +π/2

What’s the trick to converting “displacement = new Vector3D(0, -1);” in AS 3.0 to a Vector3D? Thanks in advance… you guys are generally awesome at helping out.

You will have to write your own function/constructor that does the conversion for you. You can use this: Azimuth - Wikipedia as a reference. The basic idea is that the vector is described using a horizontal and vertical angle. I guess in your case (wander behaviour) the vector should describe a direction, not a point, so you can probably use some Quaternion math to get the result you want.

If I understood this correctly something like this should work for you:

using UnityEngine;
using System.Collections;

public class QuaternionTest : MonoBehaviour {

	// Use this for initialization
	void Start () {
		float az = 90;
		float el = 0;

		Debug.Log(AzimuthToVector3(az, el));

	Vector3 AzimuthToVector3 (float az, float el)
		Vector3 vec = Vector3.forward;
		vec = Quaternion.Euler(-el,az,0) * vec;
		return vec;

Note that the Y and Z axes seem to be switched between Unity and Flash and as phodges mentioned your tutorial uses radians rather than degrees, so you will have to convert the values of az and el.