I've found these extremely useful, as this is much more efficient than converting the quaternion into a matrix just so you can extract the column/row you need from the matrix for the vector you want.
Vector3 Quaternion::GetForwardVector() const { return Vector3( 2 * (x * z + w * y), 2 * (y * x - w * x), 1 - 2 * (x * x + y * y)); } Vector3 Quaternion::GetUpVector() const { return Vector3( 2 * (x * y - w * z), 1 - 2 * (x * x + z * z), 2 * (y * z + w * x)); } Vector3 Quaternion::GetRightVector() const { return Vector3( 1 - 2 * (y * y + z * z), 2 * (x * y + w * z), 2 * (x * z - w * y)); }
Thank you so much! This is not (and very much should be) built in to Unity's quaternions.
ReplyDeleteAlso not built into Gamebryo. Was helpful having these on my last game project (since Gamebryo lacked this) so I thought it would be worth posting in the case that other math libraries were lacking it as well.
DeleteHi Nic, your post really helped me out on a project for school. I'll be sure to refer classmates to your website in the future.
ReplyDeleteI just started blogging myself and I hope I have some topics that could be of help to you:
www.mindbreakinggames.com
Thanks, not sure why these simple functions are so hard to track down. BUT there's an error in GetForwardVector(). The Y value is incorrect. It should be:
ReplyDeleteVector3 Quaternion::GetForwardVector() const
{
return Vector3( 2 * (x * z + w * y),
2 * (y * z - w * x),
1 - 2 * (x * x + y * y));
}
Very useful article. Many Thanks.
ReplyDeleteI have converted this in to C# for use with XNA and posted an article on my blog: http://blog.diabolicalgame.co.uk/2013/06/quaternion-axes.html
To match the XNA Matrix forward axis I had to change the forward vector for the quaternion to:
return Vector3( -2 * (x * z + w * y),
-2 * (y * z - w * x),
-1 + 2 * (x * x + y * y));
}
It is dangerous to include such a helper method because it assumes the users cordinate system. It also is not doing any less math than the following:
ReplyDeletevar posy = Vector3.Transform(new Vector3(0,1,0), quat);
Hey! Could you provide where you get it formula? References?
ReplyDelete