Using quaternions

Derrik
Posts: 63
Joined: Wed Aug 14, 2013 8:28 pm

Re: Using quaternions

Post by Derrik » Wed Oct 22, 2014 5:07 pm

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!
Attachments
Quaternions.zip
(159.22 KiB) Downloaded 387 times

Derrik
Posts: 63
Joined: Wed Aug 14, 2013 8:28 pm

Re: Using quaternions

Post by Derrik » Fri Oct 24, 2014 4:26 pm

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:
Attachments
Quaternions.zip
(173.64 KiB) Downloaded 353 times

Derrik
Posts: 63
Joined: Wed Aug 14, 2013 8:28 pm

Re: Using quaternions

Post by Derrik » Fri Oct 24, 2014 5:15 pm

Never mind, the delta position is calculated correctly. I just forgot to reverse the rotation before applying the inverse of delta position.

Derrik
Posts: 63
Joined: Wed Aug 14, 2013 8:28 pm

Re: Using quaternions

Post by Derrik » Sun Oct 26, 2014 12:56 pm

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.
Attachments
Quaternions.zip
(180.52 KiB) Downloaded 375 times

Derrik
Posts: 63
Joined: Wed Aug 14, 2013 8:28 pm

Re: Using quaternions

Post by Derrik » Tue Oct 28, 2014 11:04 pm

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.

Derrik
Posts: 63
Joined: Wed Aug 14, 2013 8:28 pm

Re: Using quaternions

Post by Derrik » Tue Oct 28, 2014 11:09 pm

Sorry, the file didn't upload correctly. Here it is.
Attachments
Quaternions.zip
(180.45 KiB) Downloaded 413 times

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests