Page 2 of 2

Re: Using quaternions

Posted: Wed Oct 22, 2014 5:07 pm
by Derrik
Good spot! Sometimes code just needs another pair of eyes to see the problem. I fixed that and managed to get much further, I almost have all of the maths I need to start work on my game!

I've attached my current status, in the demo, rotate with the D-Pad and press A to translate at the direction the teapot is rotated in (towards the handle). It kind of works, if you rotate only on one axis it always translates correctly, however if you rotate on both it starts to translate in the wrong direction.

Thanks once again!

Re: Using quaternions

Posted: Fri Oct 24, 2014 4:26 pm
by Derrik
With help from IRC, I've rethought my approach and was able to get the model to translate in the direction it is rotated at.

My problem now however is that I cannot get the new position which the model has been translated at.

I first get the position matrix before movement has been applied:

Code: Select all

getPositionMatrix(&initialMatrix);
Apply translation:

Code: Select all

glTranslatef32(0, 0, 1 << 12);
Get the position matrix again:

Code: Select all

getPositionMatrix(&resultantMatrix);
And then calculate the delta position based on the the difference between the matrices:

Code: Select all

#define MATRIX_X 12
#define MATRIX_Y 13
#define MATRIX_Z 14

deltaPosition.x = resultantMatrix.m[MATRIX_X] - initialMatrix.m[MATRIX_X];
deltaPosition.y = resultantMatrix.m[MATRIX_Y] - initialMatrix.m[MATRIX_Y];
deltaPosition.z = resultantMatrix.m[MATRIX_Z] - initialMatrix.m[MATRIX_Z];
Unfortunately it isn't calculating the correct delta position.

I've attached my source:

Re: Using quaternions

Posted: Fri Oct 24, 2014 5:15 pm
by Derrik
Never mind, the delta position is calculated correctly. I just forgot to reverse the rotation before applying the inverse of delta position.

Re: Using quaternions

Posted: Sun Oct 26, 2014 12:56 pm
by Derrik
Just for the benefit of anyone who might find this useful, here's a complete demo of using quaternions with the camera following the model.

Re: Using quaternions

Posted: Tue Oct 28, 2014 11:04 pm
by Derrik
I'm not sure if this needs its own topic or not.

Using the D-Pad you can rotate the teapot to a direction. Using L you can reposition the camera to be facing the model.

I want the teapot to always face in the direction relative to the camera, not the world.

So for example, I want pressing up to always make the teapot face away from the camera. At the moment it always makes the teapot point North.

Re: Using quaternions

Posted: Tue Oct 28, 2014 11:09 pm
by Derrik
Sorry, the file didn't upload correctly. Here it is.