# Quaternion to Matrix

This topic is 2911 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_    1486 _WeirdCat_ Member 1486 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_    1486 _WeirdCat_ Member 1486 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 googletag.cmd.push(function() { googletag.display('div-gpt-ad-1555502746360-0'); }); Advertisement 
 Game Developer Survey We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a $15 incentive for your time and insights. Click here to start! Take me to the survey!$(document).ready(function(e) { window.survey_id = (new Date().getUTCMilliseconds().toString() + new Date().getTime().toString()).toString(); $('.surveylink').each(function(){$(this).attr("href", "http://selfserve.decipherinc.com/survey/selfserve/5c2/190900?list=2&ID="+window.survey_id); }); }); googletag.cmd.push(function() { googletag.display('div-gpt-ad-1555502640120-0'); }); Advertisement Popular Tags 2D 3D Advice Algorithm C# C++ Concept Console Design DX11 Environments GameMaker Gameplay Graphics Java Javascript Learning Mobile Music OpenGL Optimization PC Theory Unity Unreal Popular Now 11 Bare bones AAA team By RickBakerStarted Yesterday at 08:43 AM 23 C++ 40 seconds to compile a .cpp file - is it totally normal? By hyyouStarted October 10 38 C++ Linear interpolation of two vector arrays with different lenghts By C3D_Started October 10 75 OpenGL how do I get my sprite to move up and down once By phil67rpgStarted October 2 googletag.cmd.push(function() { googletag.display('div-gpt-ad-1555502515948-0'); }); Advertisement 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! $(document).ready(function() { setInterval(function() { window.googletag.pubads().refresh(); }, 30000); }); googletag.cmd.push(function() { googletag.display('div-gpt-ad-1555502884201-0'); });$(document).ready(function() { if (ipsSettings.memberID > 0) { ga('send','event','User','Member'); ga('send',{ hitType: 'event', eventCategory: 'User', eventAction: 'Member', eventLabel: ipsSettings.memberID.toString() }); } else { ga('send','event','User','Guest'); } });