Quaternion vector helpers (GetUp,GetForward etc) [Snippet]

Dont know if this would be useful to anyone / there is a better solution. Please provide fixes / feedback if you encounter issues. Converted from c++ code.

[java]
public Vector3f GetUpVector()
{
float x = this.Geometry.getWorldRotation().getX();
float y = this.Geometry.getWorldRotation().getY();
float z = this.Geometry.getWorldRotation().getZ();
float w = this.Geometry.getWorldRotation().getW();
return new Vector3f( 2 * (x * y - w * z),
1 - 2 * (x * x + z * z),
2 * (y * z + w * x));
}

public Vector3f GetForwardVector()
{
float x = this.Geometry.getWorldRotation().getX();
float y = this.Geometry.getWorldRotation().getY();
float z = this.Geometry.getWorldRotation().getZ();
float w = this.Geometry.getWorldRotation().getW();
return new Vector3f( 2 * (x * z + w * y),
2 * (y * x - w * x),
1 - 2 * (x * x + y * y));
}

public Vector3f GetRightVector()
{
float x = this.Geometry.getWorldRotation().getX();
float y = this.Geometry.getWorldRotation().getY();
float z = this.Geometry.getWorldRotation().getZ();
float w = this.Geometry.getWorldRotation().getW();
return new Vector3f( 1 - 2 * (y * y + z * z),
2 * (x * y + w * z),
2 * (x * z - w * y));
}

public Vector3f GetLeftVector(){

return GetRightVector().negate();
}

public Vector3f GetBackwardVector(){
return GetForwardVector().negate();
}

public Vector3f GetDownVector(){
return GetUpVector().negate();
}
[/java]

Why don’t you simply use the quaternion to rotate the vector ?

For example :
[java]
frontVector = geometry.getWorldRotation().mult(Vector3f.UNIT_Z)
[/java]

Not to say your method is not good… It seems just redundant with Quaternion’s job.

2 Likes

Didnt know this existed, found it easier to convert what i had - Thanks :slight_smile:

Well since the quaternion represents a rotation, just use for example a Vector pointing to the right, rotate it with the quaternion (see post above) and you get a rotation local vector that points to the realtive right.