Author Topic: Yaw, Pitch but no Roll :(  (Read 6268 times)

0 Members and 1 Guest are viewing this topic.

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Yaw, Pitch but no Roll :(
« on: April 30, 2007 »
Wondered if anyone has come across this problem before.

I'm using matricies for rotating meshes and the camera in my engine. Yaw and Pitch work great, but when I try to roll the camera, it rolls along the x axis and is locked to that axis whatever the pitch and yaw is. Added to this, if I roll a mesh say to 45deg when I then move that mesh along the Z axis it moves up and down in a sin fashion along the y axis.

I've recalculated the matrix from the 3 basic rotation matricies for xyz incase I got it wrong so long ago, but it does exactly the same.

And to roll the camera, going along the z axiz is fine, but then trifing the camera laft and right gets screwed up.

Code: [Select]
/*
|0|3|6|
|1|4|7|
|2|5|8|
*/
Sin->x = sin( rotation->x );
Sin->y = sin( rotation->y );
Sin->z = sin( rotation->z );

Cos->x = cos( rotation->x );
Cos->y = cos( rotation->y );
Cos->z = cos( rotation->z );

rMatrix[0] =   Cos->y *  Cos->z;
rMatrix[1] =  -Sin->x * -Sin->y *  Cos->z + Cos->x * Sin->z;
rMatrix[2] =   Cos->x * -Sin->y *  Cos->z + Sin->x * Sin->z;

rMatrix[3] =   Cos->y * -Sin->z;
rMatrix[4] =  -Sin->x * -Sin->y * -Sin->z + Cos->x * Cos->z;
rMatrix[5] =   Cos->x * -Sin->y * -Sin->z + Sin->x * Cos->z;

rMatrix[6] =   Sin->y;
rMatrix[7] =  -Sin->x *  Cos->y;
rMatrix[8] =   Cos->x *  Cos->y;

any ideas?
Challenge Trophies Won:

Offline Voltage

  • Professor
  • Pentium
  • *****
  • Posts: 857
  • Karma: 53
    • View Profile
Re: Yaw, Pitch but no Roll :(
« Reply #1 on: April 30, 2007 »
This is called gimbal lock.  The only solution that I know of, is to use quaternions.

Do a google search on 'quaternions gimbal lock'... gives some interesting articles.

I got as far as researching this solution but I haven't implemented it yet.
Challenge Trophies Won:

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Re: Yaw, Pitch but no Roll :(
« Reply #2 on: April 30, 2007 »
Interesting, I will have a read thanks Voltage :)
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Yaw, Pitch but no Roll :(
« Reply #3 on: April 30, 2007 »
Quaternions are one solution, a good one with lots of uses, but I think you can solve your problem more simply.  Your matrix is almost certainly a tilt, then turn, then roll matrix.  For controlling aeroplanes you want a roll, then tilt, then turn matrix.

So go back to first principles (or google ;)) and find out the three 3d matrices that perform rotations about one axis only.  Then multiply those together in Y,X,Z order.  If you multiply then in Z,Y,X order, you should get the matrix you are presently using, so you can check your maths.

Jim
Challenge Trophies Won:

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Re: Yaw, Pitch but no Roll :(
« Reply #4 on: April 30, 2007 »
Oh cool, thanx Jim, i'll give that a go too.

Luckily, I have already googled the three 3D matrices and brushed up on my matrix multiplying yesterday in testing if my rotation matrix was correct ;)

Left Handed: -


X Rotation
     1   |     0   |     0
     0   |  cos Xa | -sin Xa
     0   |  sin Xa |  cos Xa


Y Rotation
  cos Ya |     0   |  sin Ya
     0   |     1   |     0
 -sin Ya |     0   |  cos Ya


Z Rotation
  cos Za | -sin Za |     0
  sin Za |  cos Za |     0
     0   |     0   |     1


For Right handed invert all the signes on the sins.


I was thinking of trying to make a matrix just for x and y rotation, then push the resulting vector from going through the object matrix and camera matrix through the z rotation matrix. That way I was thinking it should juts rotate everything around the direction the camera is looking.
« Last Edit: April 30, 2007 by Tetra »
Challenge Trophies Won:

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Re: Yaw, Pitch but no Roll :(
« Reply #5 on: May 01, 2007 »
I couldnt manage to implement the rearranging of matricies, for somereason, it always ended up ithe the camera pitch chaning the rotation of the object that was rotated along the y axis, and addint the z component of the matrix made it move in angles that seemd to be following this gimbiling.

So I've read up on Quaternion and found some pretty cool info on it: -

this has nice example class in c++
http://www.3dkingdoms.com/weekly/weekly.php?a=11

and this was a fairly good explination too
http://www.cprogramming.com/tutorial/3d/rotationMatrices.html

I'll be posting my code with the compo entry with this stuff if I get it implemented.

Basically the way I think it work is by having a normalized vector reprisenting a vector you want the object to rotate around at whatever angle to the object, then it rotates the object around that vector by a specific angle.

I'll keep this thread posted.
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Yaw, Pitch but no Roll :(
« Reply #6 on: May 01, 2007 »
This might be an easy fix... lets say you have the original co-ordinates for the vertices and you rotate them by an angle to obtain the transformed co-ordinates. Now you may be able to use the transformed co-ordinates as the base of further rotations. For this to work you would have to rotate by the angle increment and not the complete angle. Worth a try I think but it might not work. if it does it could be easy to implement

[Edit]
Forget what I just said that would be rotating relative to world space and that wont fix your problem only sounded good in my head  :P
« Last Edit: May 01, 2007 by rain_storm »

Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: Yaw, Pitch but no Roll :(
« Reply #7 on: May 21, 2007 »
Ditch eulers.  Instead use the axis-angle representation of rotations. Doesnt suffer from gimbal lock and not as cheesy as quats.

Axis angle:
1. Easy to visualize (Like a lookat function)
2. Fast

Quats:
1. Hard to visualize
2. Slow (ie. get euler rotations, convert to quats, convert to axis-angle) That's 3 steps


Of course quats allow you to interpolate rotations (slerps) but I haven't found a need for them.


Yay!!! First post after a while! BTW, I didn't do anything with my cookies. 
*scratches head...
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Yaw, Pitch but no Roll :(
« Reply #8 on: May 26, 2007 »
lerps or slerps are brilliant for skeletal animation.  Say the artist animates at 24fps, which is common, but you render at 60Hz, then you are able to interpolate out the extra frames and make the animation run more smoothly.  Or perhaps you want a transition from one pose to another that the artist hasn't drawn.

Note that almost all the quaternion sample code on the web is bugged to heck!

Jim
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Yaw, Pitch but no Roll :(
« Reply #9 on: May 27, 2007 »
post by tetra:
Basically the way I think it work is by having a normalized vector reprisenting a vector you want the object to rotate around at whatever angle to the object, then it rotates the object around that vector by a specific angle.

I mostly use 3*3 matrices which i find pretty easy to use and visualise and is fairly compatible with opengl matrices with a little work but it can be a bit of work to get the parent/child entity stuff working but that way of thinking tetra gives me some ideas that it might be possible to simplify it a bit.

k++