• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
RPTD

Matrix to Quaternion calculation instable

2 posts in this topic

For animation purpose I move between matrix and quaterions in different places and for this I use the tracing method found on the internet:

const double trace = a11 + a22 + a33 + 1.0;
if( trace > 0.0001 ) {
    const double s = 0.5 / sqrt( trace );
    return decQuaternion( ( a32 - a23 ) * s, ( a13 - a31 ) * s, ( a21 - a12 ) * s, 0.25 / s );
}else if( a11 > a22 && a11 > a33 ){
    const double s = 2.0 * sqrt( 1.0 + a11 - a22 - a33 );
    return decQuaternion( 0.25 * s, ( a12 + a21 ) / s, ( a13 + a31 ) / s, ( a23 - a32 ) / s );
}else if( a22 > a33 ){
    const double s = 2.0 * sqrt( 1.0 + a22 - a11 - a33 );
    return decQuaternion( ( a12 + a21 ) / s, 0.25 * s, ( a23 + a32 ) / s, ( a13 - a31 ) / s );
}else{
    const double s = 2.0 * sqrt( 1.0 + a33 - a11 - a22 );
    return decQuaternion( ( a13 + a31 ) / s, ( a23 + a32 ) / s, 0.25 * s, ( a12 - a21 ) / s );
}

The matrix is in row major order and quaterions are in the (x,y,z,w) format.

 

If I do for example a small sweep (from [0,175°,0] to [0,185°,0]) across the XZ plane (hence with Y axis fixed to [0,1,0] where I'm using DX coordinate system) around the backwards pole (0,180°,0) I end up with a slight twiching of the camera near the [0,180°,0] point. I tracked it down to the calculated quaterion to be slightly off near the point where you use any other than the first if-case. Augmenting the step value to 0.0001 did help in some cases but not in this one here. I even went all the way up to 0.01 in which case the slight twiching just moved a bit further away from the problem point.

 

I also do not think the quaterion-to-matrix is the culprit since this code there does not use any if-cases and thus should be stable. Furthermore tweaking the above mentioned value does modify the error behavior so it has to be this code causing troubles. I can at the time being cheat around the problem but I'm looking for a proper solution.

 

So my question is what other possibility is there to calculate a quaterion from a matrix which is stable? Is there a known problem with the trace based method used here that doesn't work around the backwards point? I'm concerned more about an error free solution than the fastest solution on earth.

0

Share this post


Link to post
Share on other sites

Try this conversion method, it is the same one used in the OGRE engine. Note: totally untested in this form.

/// Create a new quaternion from a 3x3 orthonormal rotation matrix.
/// Quaternion = {a, b, c, d}, Matrix3D = column major
Quaternion( const Matrix3D<T>& m )
{
	// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
	// article "Quaternion Calculus and Fast Animation".
	T fTrace = m.x.x + m.y.y + m.z.z;
	
	if ( fTrace > T(0) )
	{
		// |w| > 1/2, may as well choose w > 1/2
		T fRoot = math::sqrt(fTrace + 1.0f);  // 2w
		a = 0.5f*fRoot;
		fRoot = 0.5f/fRoot;  // 1/(4w)
		
		b = (m.y.z - m.z.y)*fRoot;
		c = (m.z.x - m.x.z)*fRoot;
		d = (m.x.y - m.y.x)*fRoot;
	}
	else
	{
		// |w| <= 1/2
		Index nextIndex[3] = { 1, 2, 0 };
		Index i = 0;
		
		if ( m.y.y > m.x.x )
			i = 1;
		
		if ( m.z.z > m[i][i] )
			i = 2;
		
		Index j = nextIndex[i];
		Index k = nextIndex[j];
		
		T fRoot = math::sqrt( m[i][i] - m[j][j] - m[k][k] + T(1) );
		
		T* apkQuat[3] = { &b, &c, &d };
		*apkQuat[i] = T(0.5)*fRoot;
		
		fRoot = T(0.5)/fRoot;
		
		a = (m[j][k] - m[k][j])*fRoot;
		*apkQuat[j] = (m[i][j] + m[j][i])*fRoot;
		*apkQuat[k] = (m[i][k] + m[k][i])*fRoot;
	}
}
Edited by Aressera
0

Share this post


Link to post
Share on other sites

Sorry for not replying sooner but the forum failed to send me a notification about you having posted so I assumed nobody answered until I decided to still check back in case the forum soft is broken (as it is).

 

So to your post... I could be wrong but is this not the same as my version just written in a non-un-rolled version? Maybe I'm missing something but it looks similar to me.

 

 

Try this conversion method, it is the same one used in the OGRE engine. Note: totally untested in this form.

/// Create a new quaternion from a 3x3 orthonormal rotation matrix.
/// Quaternion = {a, b, c, d}, Matrix3D = column major
Quaternion( const Matrix3D<T>& m )
{
	// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
	// article "Quaternion Calculus and Fast Animation".
	T fTrace = m.x.x + m.y.y + m.z.z;
	
	if ( fTrace > T(0) )
	{
		// |w| > 1/2, may as well choose w > 1/2
		T fRoot = math::sqrt(fTrace + 1.0f);  // 2w
		a = 0.5f*fRoot;
		fRoot = 0.5f/fRoot;  // 1/(4w)
		
		b = (m.y.z - m.z.y)*fRoot;
		c = (m.z.x - m.x.z)*fRoot;
		d = (m.x.y - m.y.x)*fRoot;
	}
	else
	{
		// |w| <= 1/2
		Index nextIndex[3] = { 1, 2, 0 };
		Index i = 0;
		
		if ( m.y.y > m.x.x )
			i = 1;
		
		if ( m.z.z > m[i][i] )
			i = 2;
		
		Index j = nextIndex[i];
		Index k = nextIndex[j];
		
		T fRoot = math::sqrt( m[i][i] - m[j][j] - m[k][k] + T(1) );
		
		T* apkQuat[3] = { &b, &c, &d };
		*apkQuat[i] = T(0.5)*fRoot;
		
		fRoot = T(0.5)/fRoot;
		
		a = (m[j][k] - m[k][j])*fRoot;
		*apkQuat[j] = (m[i][j] + m[j][i])*fRoot;
		*apkQuat[k] = (m[i][k] + m[k][i])*fRoot;
	}
}
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0