Quaternion to Rotation Matrix, incorrect values using Eigen Library
The way you initialize your quaternion is incorrect. If you directly initialize the coordinates of quaternion, you should take the definition into account:
Alternatively, the Quaternion
class in Eigen provides a constructor from an axis-angle representation.
This code:
#include <Eigen/Geometry>
#include <iostream>
void outputAsMatrix(const Eigen::Quaterniond& q)
{
std::cout << "R=" << std::endl << q.normalized().toRotationMatrix() << std::endl;
}
void main()
{
auto angle = M_PI / 4;
auto sinA = std::sin(angle / 2);
auto cosA = std::cos(angle / 2);
Eigen::Quaterniond q;
q.x() = 0 * sinA;
q.y() = 1 * sinA;
q.z() = 0 * sinA;
q.w() = cosA;
outputAsMatrix(q);
outputAsMatrix(Eigen::Quaterniond{Eigen::AngleAxisd{angle, Eigen::Vector3d{0, 1, 0}}});
}
outputs what you expect:
R=
0.707107 0 0.707107
0 1 0
-0.707107 0 0.707107
R=
0.707107 0 0.707107
0 1 0
-0.707107 0 0.707107
DollarAkshay
Looking for a remote job. (Updated on March 20th 2022) Indian guy, who loves programming 🖥️. There is absolutely nothing that I cant do when it comes to tech. I'm not an expert on any single language/framework/topic, I am more like the swiss army knife of programmers, my broad skill set is what makes me a super valuable asset to any company. I think of myself more as an inventor rather than just a software guy 💡. On a more personal note, I am a super polite guy and an avid thinker. I try to live my life by egalitarian principles ☮️ When I am not at work, I am probably working on some of my open-source projects, coding up my Screeps bot, or chilling with my friends on Discord 🍻🎮. I love survival and colony-building games, such as Rust, Forest, Raft, Rimworld, Screeps, etc My true interest lies in inventing new things, whether it's just software, hardware, or a combination of both. I would love to work with highly creative and open-minded people 🧠. If I believe in something I always give it my 100% effort 💯. Link to Resume : https://resume.creddle.io/resume/d87rfbbm6cq
Updated on July 12, 2022Comments
-
DollarAkshay almost 2 years
I am trying to rotate an object by 45 degrees using quaternions about Y-Axis. After Specifying the Quaternion i am trying to get the Rotation Matrix. But the values I see are incorrect
Eigen::Quaterniond q; q.x() = 0; q.y() = 1; q.z() = 0; q.w() = PI/8; // Half of the rotation angle must be specified, even IDK why Eigen::Matrix3d R = q.normalized().toRotationMatrix(); std::cout << "R=" << std::endl << R << std::endl;
OUTPUT :
R= -0.732 -0 -0.680 0 1 -0 0.680 0 -0.732
Since the OpenGL Rotation Matrix along Y-Axis should be :Therefore my expected output should be :
R= 0.707 0 0.707 0 1 0 -0.707 0 0.707
Not only are the values off by a small percent the wrong signs on the values are causing some unexpected rotations. Because of the negative signs my cube is doing a 180 degree turn plus the specified angle. I have broken my head over this for the whole day. Can some one tell me what I am doing wrong ?