Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior of Quaternion rotation for 2D Vectors #7

Open
moepforfreedom opened this issue Aug 18, 2022 · 0 comments
Open
Assignees

Comments

@moepforfreedom
Copy link
Contributor

The current Quaternion implementation shows different behavior when applied to 2D vectors compared to the 3D counterpart, this breaks some of the normal properties of Quaternions. Specifically, rotating a vector by a Quaternion using Quaternion::RotateVector() and rotating the result back using Quaternion::UnrotateVector() should return the original vector. This is the case for 3D vectors but doesnt apply in the 2D case. The following code snippet demonstrates the difference:

Quaternion testQuat = Rotation(EulerAngles({ 20.0f, 0, 0 }, EulerConstants::EulerOrderXZYS)).GetQuaternion();
Vector3D testVec(1, 2, 0);
Vector3D testRot = testQuat.RotateVector(testVec);
Vector3D testRotInv = testQuat.UnrotateVector(testRot);
printf("rotated: %.2f, %.2f, %.2f\n", testRot.X, testRot.Y, testRot.Z);
printf("unrotated: %.2f, %.2f, %.2f\n", testRotInv.X, testRotInv.Y, testRotInv.Z);

Vector2D testVec2D(1, 2);
Vector2D testRot2D = testQuat.RotateVector(testVec2D);
Vector2D testRotInv2D = testQuat.UnrotateVector(testRot2D);
printf("rotated 2D: %.2f, %.2f\n", testRot2D.X, testRot2D.Y);
printf("unrotated 2D: %.2f, %.2f\n", testRotInv2D.X, testRotInv2D.Y);

This difference is caused by the implicit use of a 3D Vector with 0 as a Z coordinate in Quaternion::RotateVector(Vector2D v) since a normal 3D rotation can result in a nonzero Z coordinate, eg in the given test case rotating (1, 2, 0) results in (1.00, 1.88, 0.68), discarding the Z coordinate breaks the inverse transform.

A possible workaround could for example be extracting the corresponding 2D transformation in the XY plane from the Quaternion (eg using the corresponding euler angles) and using that in the 2D rotation functions.

@coelacant1 coelacant1 self-assigned this Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants