See which way a node is heading

Hi, currently I have all this code for working out which way I am heading in the game:

```    public String getDirection()     {        String a = "";        if(m_character.getWorldRotation().y > -0.3 & m_character.getWorldRotation().y < 0.3)        {           a="n";        }                if(m_character.getWorldRotation().y > -0.4 & m_character.getWorldRotation().y <= -0.3)        {           a="nw";        }        if(m_character.getWorldRotation().y > -0.7 & m_character.getWorldRotation().y <= -0.4)        {           a="w";        }        if(m_character.getWorldRotation().y > -0.95 & m_character.getWorldRotation().y <= -0.7)        {           a="sw";        }        if(m_character.getWorldRotation().y > -1 & m_character.getWorldRotation().y <= -0.95)        {           a="s";        }        //second set        if(m_character.getWorldRotation().y < 0.3 & m_character.getWorldRotation().y >= 0.1)        {           a="ne";        }        if(m_character.getWorldRotation().y < 0.7 & m_character.getWorldRotation().y >= 0.3)        {           a="e";        }        if(m_character.getWorldRotation().y < 0.92 & m_character.getWorldRotation().y >= 0.7)        {           a="se";        }```

Is there a better way of getting the direction that either a node, or a camera is facing? This way seems to be having problems with accuracy!

There's no way this would ever work. It would had a chance if the world rotation was a direction vector. But, it's a quaternion, which is a complex mathematical rotation thingy not many people can understand - so your method is completely wrong as of now.

The most accurate way to do this would be to create a vector for each direction (north, south, east, west, etc) and then DOT that vector with the direction vector (which you must get from the world rotation quaternion), the direction that results in the greatest DOT value is the direction the character is facing.

I think you can also do it using the rotation Quaternion.

Firstly, take the direction your character mesh is facing in local coordinate space. Say your character faces local Z.

You can have the direction your character is facing in local or absolute space, just using the rotation Quaternion of the node that holds your character rotation.

m_character.getLocalRotation().mult(Vector3f.UNIT_Z).normalizeLocal() is your character orientation vector in local coordinate space.

m_character.getWorldRotation().mul(Vector3f.UNIT_Z).normalizeLocal() is your character orientation vector in world coordinate space.

What you are doing is to calculate the new position of UNIT_Z after rotating it as much as your node, which efectively gives you a vector pointing in the same direction your character is facing (that's what the "Quaternion x Vector" function does).

I hope it helps. Correct me if I am wrong.

One more thing: I believe you don't need that "normalizeLocal()" call, since your Vector is unitary before multiplication, it will be unitary afterwards. Can one of the hardcore math guys here shed some light over this?

Thats brilliant! Thanks!

I forgot to mention that, after calculating the vector in the way I described, you still need to apply Momoko_Fan's method to find which direction (in a compass: N, S, E, O) your character is facing.

Vector 3f yourFacingVector = m_character.getWorldRotation().mul(Vector3f.UNIT_Z).normalizeLocal() ;

float facingNorth = yourFacingVector.dot(Vector3f.UNIT_X);

float facingSouth = yourFacingVector.dot(Vector3f.UNIT_X.negate());

float facingEast = yourFacingVector.dot(Vector3f.UNIT_Z);

float facingWest = yourFacingVector.dot(Vector3f.UNIT_Z.negate());

The largest value is the direction.