• 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
EarthBanana

Quat rotation confusion

5 posts in this topic

I switched everything to quats.. and all is well except one thing.. ill try to explain the best i can
I build a rotation matrix from a quaternion and use it as such for all my game objects

 

screenSpace = projMat * camMat * translateMat * rotateMat * scaleMat * positionVec

 

and for my camera

 

camMat = rotateMat * translateMat * camOrigin

 

where rotateMat is given by the following

NSMatrix4Df NSQuaternion::getRotationMatrix() const
{
	NSMatrix4Df retMat;
	retMat.setRow(NSVec4Df(getRightVec(), 0.0f), 0);
	retMat.setRow(NSVec4Df(getUpVec(), 0.0f), 1);
	retMat.setRow(NSVec4Df(getTargetVec(), 0.0f), 2);
	return retMat;
}

NSVec3Df NSQuaternion::getRightVec() const
{
	return NSVec3Df(1.0f - 2.0f*y*y - 2.0f*z*z, 2.0f*x*y - 2.0f*w*z, 2.0f*x*z + 2.0f*w*y);
}

NSVec3Df NSQuaternion::getUpVec() const
{
	return NSVec3Df(2.0f*x*y + 2.0f*w*z, 1.0f - 2.0f*x*x - 2.0f*z*z, 2.0f*y*z - 2.0f*w*x);
}

NSVec3Df NSQuaternion::getTargetVec() const
{
	return NSVec3Df(2.0f*x*z - 2.0f*w*y, 2.0f*y*z + 2.0f*w*x, 1.0f - 2.0f*x*x - 2.0f*y*y);
}

I then have a rotate function that when I call rotates the object about a given axis - shown below

void NSQuaternion::rotate(const NSVec3Df & pAxis, float pAngle)
{
	NSQuaternion localRotation, temp(pAxis, pAngle);

	float halfAngle = sinf(DegreesToRadians((pAngle/2.0f)));
	localRotation.x = pAxis.x * halfAngle;
	localRotation.y = pAxis.y * halfAngle;
	localRotation.z = pAxis.z * halfAngle;
	localRotation.w = cosf(DegreesToRadians(pAngle / 2.0f));

	(*this) = (*this) * localRotation;
	normalize();
}

This works - but I'm confused because when I use this rotation function it rotates the object about its Local axis... so if I give it the vector (0,0,1) and the angle 45 it will rotate the object about its local z axis by 45 degrees no matter which way the object is oriented... if I change the last line (before normalize) to

(*this) =  localRotation * (*this);

it then rotates objects correctly about the worlds z axis if i give it (0,0,1) rather than its local axis. The problem is that with this version the targetVector that is returned by the function getTargetVec is not actually the direction the object is facing... hence...

 

when I use the second version the camera is all messed up..

 

Anyone know how to use a quaternion to store rotation information so that the same rotate function can be used by a camera as by an object? I would like both the camera and the objects to be able to rotate around world space vectors rather than their local space

 

If I want a rotation around a local space axis I can get the local space axis with getTargetVec or getUpVec etc..

Edited by EarthBanana
0

Share this post


Link to post
Share on other sites

In principle it should play no role whether the quaternion is for a game entity or the camera, as long as the camera is used like an object in the world. But exactly this is not clear from what the OP shows. My current problems in understanding it are the following:

 

According to this line

screenSpace = projMat * camMat * translateMat * rotateMat * scaleMat * positionVec

I assume that you're using column vector matrices. The quaternion-to-matrix conversion sets the rows of the matrix to the side, up, and forward vectors.

retMat.setRow(NSVec4Df(getRightVec(), 0.0f), 0);
retMat.setRow(NSVec4Df(getUpVec(), 0.0f), 1);
retMat.setRow(NSVec4Df(getTargetVec(), 0.0f), 2);

This hints at the usage of row vector matrices. Are you sure that you use it correctly?

 

Moreover, in 

screenSpace = projMat * camMat * translateMat * rotateMat * scaleMat * positionVec

the camMat need to be the viewMat == the inverse camMat. Have you considered this? The following line

camMat = rotateMat * translateMat * camOrigin

let's me assume that camMat should in fact be the viewMat (it is computed in reverse order), but it isn't clear whether you also invert rotateMat and translateMat accordingly, because what you actually need to apply is the rule

 

    ( T * R )-1 = R-1 * T-1

 

The fact that the inverse rotation is identical to the transposed rotation may explain why you set the row vectors in the matrices instead of the column vectors. However, the quaternion class is not especially for the camera but for general use. As such you should not have hidden side effects in it. Moreover, there is still nothing said about the inversion of the translation.

 

Even if you actually do all this mathematically right (I haven't checked the formulas whether they match), naming it the way you're doing it is confusing, because you violate a common naming convention. As you can see from my post, not following the convention brings up many questions just to make sure we're speaking about the same.

1

Share this post


Link to post
Share on other sites


I assume that you're using column vector matrices. The quaternion-to-matrix conversion sets the rows of the matrix to the side, up, and forward vectors.

retMat.setRow(NSVec4Df(getRightVec(), 0.0f), 0);
retMat.setRow(NSVec4Df(getUpVec(), 0.0f), 1);
retMat.setRow(NSVec4Df(getTargetVec(), 0.0f), 2);

This hints at the usage of row vector matrices. Are you sure that you use it correctly?

 

I am using row vector matrices - In the line there I set the first, second, and third rows of the matrix to the right, up, and target vectors respectively. This is how you are supposed to build the rotation matrix for a quaternion isnt it? Am I supposed to be setting the columns of the matrix rather than the rows?

 

You are correct about camMat needing to be viewMat - but I wasn't aware that I needed to invert the rotation and translation matrices before multiplying them together.. That might explain some issues - the strange thing is that it has worked this way for a long time now and only when I decided to switch things around to Quaternions has it given me trouble..

0

Share this post


Link to post
Share on other sites


I am using row vector matrices ...

Are you sure? Because if you use row vectors, then you need to compute

screenSpace = positionVec * scaleMat * rotateMat * translateMat * viewMat * projMat  

instead. Perhaps you are confusing the meaning of "row / column vectors" and the memory layout "row / column major"? The former one is based on the mathematical prescription of how to compute a matrix product, while the latter one means how the 2D arrangement of elements in a matrix are mapped into the 1D computer memory.

 


… In the line there I set the first, second, and third rows of the matrix to the right, up, and target vectors respectively. This is how you are supposed to build the rotation matrix for a quaternion isnt it? Am I supposed to be setting the columns of the matrix rather than the rows?

The names side (or right), up, and forward (or target, in your case) vectors are used for specific directions, in particular the principal positive axes of a local co-ordinate system. They are to be set as columns if using column vectors, and they need to be set as rows if using row vectors. Not doing so so means to store in fact the transpose and hence inverse rotation. Internally the setRow and setColumn routines need to consider whether row major or else column major storage convention is chosen, and store the values accordingly. Not doing it correctly again means a transpose and hence inverse rotation.

 


the strange thing is that it has worked this way for a long time now and only when I decided to switch things around to Quaternions has it given me trouble..

It is important to go disciplined through this stuff, or else things get messy perhaps elsewhere. Mistakes need not be visible immediately.

 
Well, that is life of a programmer ... yesterday all worked well, today some shit happens surprisingly ;) Even worse if we originally wanted to make things just better...
1

Share this post


Link to post
Share on other sites

Are you sure? Because if you use row vectors, then you need to compute

screenSpace = positionVec * scaleMat * rotateMat * translateMat * viewMat * projMat

 

So.. I am still not 100 percent sure I know which terminology I should be using - but judging by this I'm guessing I should be using the term column vector for my matrices.. After taking your advice - I changed my viewMat to

viewMat = rotateMatTranspose * translateMatInverse * camOrigin

where rotateMatTranspose is created from a Quaternion describing the camera's rotation and translation mat inverse is created from negative of the camera's position relative to its origin... And I then changed my regular Quat rotation transform code to

NSMatrix4Df NSQuaternion::getRotationMatrix() const
{
	NSMatrix4Df retMat;
	retMat.setColumn(NSVec4Df(getRightVec(), 0.0f), 0);
	retMat.setColumn(NSVec4Df(getUpVec(), 0.0f), 1);
	retMat.setColumn(NSVec4Df(getTargetVec(), 0.0f), 2);
	return retMat;
}

which means my rotation transpose matrix is formed with the following:

NSMatrix4Df NSQuaternion::getRotationTransposeMatrix() const
{
    NSMatrix4Df retMat;
    retMat.setRow(NSVec4Df(getRightVec(), 0.0f), 0);
    retMat.setRow(NSVec4Df(getUpVec(), 0.0f), 1);
    retMat.setRow(NSVec4Df(getTargetVec(), 0.0f), 2);
    return retMat;
}

And my transform pipeline for game objects is given by:

mTransform = projMat * viewMat * translateMat * rotateMat * scaleMat * positionVec;

And finally - to rotate a quaternion by an axis and an angle I use the following

void NSQuaternion::rotate(const NSVec3Df & pAxis, float pAngle)
{
	NSQuaternion localRotation;

	float halfAngle = sinf(DegreesToRadians((pAngle/2.0f)));
	localRotation.x = pAxis.x * halfAngle;
	localRotation.y = pAxis.y * halfAngle;
	localRotation.z = pAxis.z * halfAngle;
	localRotation.w = cosf(DegreesToRadians(pAngle / 2.0f));

	(*this) = (*this) * localRotation;
	normalize();
}

I am very very happy to say that taking your advice worked and completely fixed my problems - now I can use a quaternion in general to represent an objects rotation or the camera's rotation - doesn't matter.. The forward, up, and side vectors are now all pointing correctly and objects rotate how I expect them to.

 

Thanks so much! +1 for you

Edited by EarthBanana
0

Share this post


Link to post
Share on other sites
I am very very happy to say that taking your advice worked and completely fixed my problems - now I can use a quaternion in general to represent an objects rotation or the camera's rotation - doesn't matter.. The forward, up, and side vectors are now all pointing correctly and objects rotate how I expect them to.

Hurray! smile.png

 

So.. I am still not 100 percent sure I know which terminology I should be using

Mathematic defines that in a matrix product (look at vectors as a matrix where one of the both dimensions is set to length 1) the count of columns on the left matrix must be equal to the count of rows on the right. Hence you can write a matrix product of a 4x4 matrix and a 4 element vector either so

| a  e  i  m |   | q |
| b  f  j  n | * | r |
| c  g  k  o |   | s |
| d  h  l  p |   | t |

or else so

                 | a  b  c  d |
| q  r  s  t | * | e  f  g  h |
                 | i  j  k  l |
                 | m  n  o  p |

Notice that in the 1st solution the vector is on the right (typical for e.g. OpenGL), and it is written as a column. So we multiply 4 columns (in the matrix) by 4 rows (in the vector), which is mathematically okay. This is the use of column vectors, as you did.

 

Notice that in the 2nd solution the vector is on the left (typical for e.g. D3D), and it is written as a row. So we multiply 4 columns (in the vector) by 4 rows (in the matrix), which is mathematically okay. This is the use of row vectors.

 
Notice at last that I've used the same letters in both solutions, and as such each letter in the one solution mean the same value as in the other, but I have re-arranged them. This is due to the fact that a mathematical correspondence exists by the mean of the transpose operation, which converts between column and row vectors as follows:
    ( M * v )t == vt * Mt
    ( v * M )t == Mt * vt
and at least
    ( Mt )t == M
Edited by haegarr
1

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