Quaternion to Matrix

This topic is 2606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello again everyone. I need help once again. This time i'm trying to convert a Quaternion to a Matrix that i can use in OpenGL ES functions.

I have a quaternion and a matrix class, and use a static function to try and convert the quaternion to a matrix

 Matrix Quaternion::ToMatrix(Quaternion& q) { float sqw = q.w*q.w; float sqx = q.x*q.x; float sqy = q.y*q.y; float sqz = q.z*q.z; float invs = 1.0f / (sqx + sqy + sqz + sqw); Matrix matrix = Matrix::identity; matrix.mat[0] = ( sqx - sqy - sqz + sqw) * invs; matrix.mat[5] = (-sqx + sqy - sqz + sqw) * invs; matrix.mat[10] = (-sqx - sqy + sqz + sqw) * invs; float tmp1 = q.x*q.y; float tmp2 = q.z*q.w; matrix.mat[4] = 2.0 * (tmp1 + tmp2) * invs; matrix.mat[1] = 2.0 * (tmp1 - tmp2) * invs; tmp1 = q.x*q.z; tmp2 = q.y*q.w; matrix.mat[8] = 2.0 * (tmp1 - tmp2) * invs; matrix.mat[2] = 2.0 * (tmp1 + tmp2) * invs; tmp1 = q.y*q.z; tmp2 = q.x*q.w; matrix.mat[9] = 2.0 * (tmp1 + tmp2) * invs; matrix.mat[6] = 2.0 * (tmp1 - tmp2) * invs; return matrix; }

To test if this works i first create a quaternion using an angle and an axis
Quaternion q = Quaternion::AngleAxis(50.0f, Vector(10.0f, 60.0f, -80.0f));
Then i convert that to a matrix
Matrix toMat = Quaternion::ToMatrix(q);
Next i create a matrix using the same agle axis
Matrix newMat = Matrix::AxisAngleRotation(Vector(10.0f, 60.0f, -80.0f), 50.0f);

Then i loop trough both matrices to print them out, the output of the matrix created from the quaternion is:
0.64 | 0.63 | 0.42 | 0.00
-0.58 | 0.77 | -0.24 | 0.00
-0.48 | -0.09 | 0.86 | 0.00
0.00 | 0.00 | 0.00 | 1.00

Now the output from the matrix created with an angle axis rotation is
36.36 | 275.61 | -239.8 | 0.00
153.04 | 128.61 | -1722.2 | 0.00
-331.73 | -1706.9 | 2286.80 | 0.00
0.00 | 0.00| 0.00 | 1.00

So am i doing something wrong? Or is this just a scaling issue?

Share on other sites

 Now the output from the matrix created with an angle axis rotation is 36.36 | 275.61 | -239.8 | 0.00 153.04 | 128.61 | -1722.2 | 0.00 -331.73 | -1706.9 | 2286.80 | 0.00 0.00 | 0.00| 0.00 | 1.00 So am i doing something wrong? Or is this just a scaling issue? 
 This is very wrong. Creating a rotation matrix from axis-angle rotations should produce an orthonormal matrix, which is a matrix which has the length of all its column (and row) vectors one, (additionally the column and row vectors are orthogonal). The magnitudes of the columns or rows of this matrix are much greater than one. Perhaps you passed in an unnormalized direction vector to the code that creates a rotation matrix based off an angle and a axis direction? For reference, here is the code from my library (though currently not used, atm I route axis-angles to quaternions to matrices). 
 1 
 Share this post Link to post Share on other sites 
 
 
 _WeirdCat_    1471 _WeirdCat_ Member 1471 Posted October 27, 2011 wikipedia quaterions btw it works: void __fastcall tquaterion::makematrix() { if (requiresNormalization() == true) Normalize(); matrix.pointer[0] = w*w+x*x-y*y-z*z; matrix.pointer[1] = 2.0*x*y-2.0*w*z; matrix.pointer[2] = 2.0*x*z+2.0*w*y; matrix.pointer[3] = 0.0; matrix.pointer[4] = 2.0*x*y+2.0*w*z; matrix.pointer[5] = w*w-x*x+y*y-z*z; matrix.pointer[6] = 2.0*y*z+2.0*w*x; matrix.pointer[7] = 0.0; matrix.pointer[8] = 2.0*x*z-2.0*w*y; matrix.pointer[9] = 2.0*y*z-2.0*w*x; matrix.pointer[10] = w*w-x*x-y*y+z*z; matrix.pointer[11] = 0.0; matrix.pointer[12] = 0.0; matrix.pointer[13] = 0.0; matrix.pointer[14] = 0.0; matrix.pointer[15] = 1.0; } [/quote] 1 Share this post Link to post Share on other sites ProjectinMatrix    106 ProjectinMatrix Member 106 Posted October 27, 2011 fuuuuu..... Yeah, i didn't normalize the rotate vector. Added this to the function Vector nr = rotate.Normalized(); #define x nr.x #define y nr.y #define z nr.z Now it works perfectly. Thanks for pointing it out, i feel beyond dumb. 0 Share this post Link to post Share on other sites _WeirdCat_    1471 _WeirdCat_ Member 1471 Posted October 27, 2011 everyone knows that pain 0 Share this post Link to post Share on other sites 
 Sign in to follow this   Followers 0 
 Go To Topic Listing Math and Physics Advertisement 
 Advertisement Are you ready to promote your game? Submit your game for Intel® certification by December 21, 2018 and you could win big!  Click here to learn more. Popular Tags 2D 3D Advice Algorithm C# C++ Concept Custom Design DX11 DX12 Feedback GameMaker Gameplay General Graphics Java Mobile Music OpenGL PC SFX Unity Unreal VR Popular Contributors Week Month Year All Time 1 Septopus 30 2 Zakwayda 15 3 Rutin 15 4 lawnjelly 13 5 Gnollrunner 13 Show More Advertisement Popular Now 13 Java Sick of Javascript bugs, going back to Java By blesseddiscipleStarted Thursday at 01:48 AM 26 OpenGL Texture mapping acts weird on some images. By babaliarisStarted Wednesday at 06:56 PM 10 Yet another interpolation question By rrr333Started Wednesday at 05:32 PM 11 Inspiration for citybuilder game By Gilles MeiresonneStarted Tuesday at 06:23 PM 9 Angular velocity and water physics By lawnjellyStarted Sunday at 06:12 PM Forum Statistics Total Topics 633726 Total Posts 3013573 GameDev.net GameDev.net Articles GameDev.net Event Coverage GameDev.net Forums GameDev.net Blogs GameDev.net Gallery GameDev.net News GameDev.net Projects GDNet Chat All Activity Search In Everywhere This Forum This Topic More options... Find results that contain... All of my search term words Any of my search term words Find results in... Content titles and body Content titles only Home Forums Programming Math and Physics Quaternion to Matrix 
 
 
 × Existing user? Sign In Sign Up Browse Back Articles & Tutorials Back All Categories Audio Business Game Design Industry Programming Visual Arts Columns Back GameDev Unboxed Event Coverage Back All Events Game Developers Conference Power Up Digital Games Conference GameDev.Market Links News Podcasts Back All Podcasts Game Dev Loadout Archive Community Back Beginners Back Beginners Group Beginners Forum Beginners Resources Blogs Calendar Chat Forums Back All Forums Audio Business Game Design Programming Visual Arts Community GameDev Challenges Affiliates Topical Workshops Gallery Groups Back For Beginners GameDev Challenges All Groups Projects Back All Projects Games Game Assets Game Mods Developer Tools Store Forums Back All Forums For Beginners Audio Back Music and Sound FX Games Career Development Business Back Games Career Development Production and Management Games Business and Law Game Design Back Game Design and Theory Writing for Games Programming Back Artificial Intelligence Engines and Middleware General and Gameplay Programming Graphics and GPU Programming Math and Physics Networking and Multiplayer Visual Arts Back 2D and 3D Art Critique and Feedback Community Back GameDev Challenges GDNet Lounge GDNet Comments, Suggestions, and Ideas Coding Horrors Your Announcements Hobby Project Classifieds Indie Showcase Affiliates Back NeHe Productions AngelCode Topical Workshops Careers Back Contractors Hobby Projects Game Jobs Back Browse on GameDev.Jobs Post a Job Members Back Subscriptions Chat Guidelines Leaderboard Online Users Awards Search Back All Activity My Activity Streams Back Latest Topics Featured Blogs Search Important Information By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.   I accept GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry. Sign me up!