Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Report any bugs here and we'll post fixes

Moderators: Sascha Willems, Thomas

Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby tylerp9p » Wed Jul 16, 2008 11:56 pm

Hey guys, I am new to these forums, but not new to Newton. I have been using the Leadwerks Engine by Josh Klint for a while now and have been doing some more complex physics routines lately. He directed me here under the assumption that only Julio could help with my problem.

Here is basically the problem:
I have an entirely physics controlled shipmodel/body. The camera is parented to the ship, and is rotated via mouse movement on the X and Y axes. I can not rotate the ship as I do the camera, as rotating it direcly resets the matrix, so what I did to work around this was calculate the difference between the rotation of the camera and the rotation of the ship and added the difference via torque on the respective axes. I just recently found a fatal error. The camera rotation goes from -180 to 180 (roughly, sometimes swaps at -173/173). So when I rotate the camera and the torque is added, if the rotation jumps from 180 to -180 and vice versa, I get a gimbol lock effect and the ship starts spazzing out (due to improper subtraction being fed since the rotation just randomly jumped) and rotating wildly in the axes that just swapped.

I was told by Josh that this is an issue with Euler angles, and that I would need to use matrix math or quaternions. I have no idea how to do either and was looking for some help.

Here is the code (it is BlitzMax - I will color the physics code red):

CODE BEGINS

mx=Curve(MouseX()-cx,mx,4.0/AppSpeed())
my=Curve(MouseY()-cy,my,4.0/AppSpeed())

MoveMouse cx,cy

camerarotation=EntityRotation(camera,1)
camerarotation.x:+my*0.1
camerarotation.y:-mx*0.1

RotateEntity camera,camerarotation


AddBodyForce(shipmodel,TFormVector(Vec3((KeyDown(KEY_D)-KeyDown(KEY_A))*speed,(KeyDown(KEY_SPACE)-KeyDown(KEY_C))*speed,(KeyDown(KEY_W)-KeyDown(KEY_S))*speed),camera,Null),1)

Local x,y,z:Float

If (camerarotation.x - EntityRotation(shipmodel,1).x) > 0
x = (camerarotation.x - EntityRotation(shipmodel,1).x)
ElseIf (camerarotation.x - EntityRotation(shipmodel,1).x) < 0
x = (camerarotation.x - EntityRotation(shipmodel,1).x)
EndIf
If (camerarotation.y - EntityRotation(shipmodel,1).y) > 0
y = -1 * (camerarotation.y - EntityRotation(shipmodel,1).y)
ElseIf (camerarotation.y - EntityRotation(shipmodel,1).y) < 0
y = -1 * (camerarotation.y - EntityRotation(shipmodel,1).y)
EndIf
If EntityRotation(shipmodel,1).z > 0
z = -1 * EntityRotation(shipmodel,1).z
ElseIf EntityRotation(shipmodel,1).z < 0
z = -1 * EntityRotation(shipmodel,1).z
EndIf
AddBodyTorque(shipmodel,vec3(x*50,y*50,z*50),0)


UpdateWorld()

PositionEntity camera, EntityPosition(shipmodel,1),0
If thirdperson
MoveEntity camera, Vec3(0,5,-15)
EndIf

CODE ENDS

I would appreciate any help you can give me.
tylerp9p
 
Posts: 2
Joined: Wed Jul 16, 2008 11:46 pm

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby Julio Jerez » Thu Jul 17, 2008 7:59 am

In what line the camera angle shange for -180 to 180?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby Leadwerks » Thu Jul 17, 2008 3:45 pm

I am pretty sure using Euler rotations is not the way to do this properly.

What he needs is to calculate the difference between the current rotation and the desired one, and use this to apply Torque to the body to reach the desired rotation. I am not sure how this can be acheived. I have a lot of matrix and quaternion routines in the engine, but I don't know how to calculate the desired torque value.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby Julio Jerez » Thu Jul 17, 2008 4:03 pm

I guess I do nto unsrtand the problem very well
is the code extrating angle from a matrix, or ar eth angles generate algorthmically.
if you are generation teh anglue algorithmicallt tthenm you can generate them indepnetly of each other.
the to gertane teh torque you need to make a quation out o fthe three angles.
for the quatertion and teh vreiuse quatertion you can extrct the rotaion axis. an dteh delta angle, then you can product the general torque.
if you give me and prototype funtion wite teh imput parameter I can write a funtion for you to do teh job.

It is a huge mistake to nenerate torque for each angles, because euelr angle are no in teh same space.
if you concateneta euler like

x * y * z

then only the torque for the z axis is write, the torque applied to the y and z axis are in the space of z therefore you generate erronues results.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby Leadwerks » Thu Jul 17, 2008 5:12 pm

Let's say the current rotation is a euler of 25,15,5.

The rotation we want is 65,48,148.

I can calculate a quaternion for either rotation. I can calculate a 3x3 matrix for either rotation.

How do I turn this into a torque vector to bring the body to the desired rotation?
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby JernejL » Thu Jul 17, 2008 5:34 pm

Isn't there a example of this in newton demos? the old 1.35 had a demo which had character controller ellipsoid that turned with the direction of the camera, and it used addtorque if i'm not mistaken.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby Leadwerks » Thu Jul 17, 2008 6:23 pm

That is only rotation on one axis. It is much harder to figure out a 3D rotation.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby Dave Gravel » Fri Jul 18, 2008 7:27 pm

Like Leadwerk say the character code in the old newton 1.53 is not working in all case.

Here a little exemple with a similar method implementation.
http://orionx3d.googlepages.com/TorqueRotation.zip

The problem after have apply some rotation in a axis i'm not able to apply again rotation in a other axis without get problem.
Or I can't make cross axis value to get something like 45,0,45
If I reset the object I can replace the rotation again without problem.
In the demo the object test is gravity zero.
My math is very bad and I can't help more about it.

My callback code is in the file Unit1.pas at the end.
You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://orionx3d.sytes.net
https://www.facebook.com/dave.gravel1
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 800
Joined: Sat Apr 01, 2006 9:31 pm
Location: Quebec in Canada.

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby Thomas » Sat Jul 19, 2008 7:03 am

Leadwerks wrote:I can calculate a quaternion for either rotation. I can calculate a 3x3 matrix for either rotation.

How do I turn this into a torque vector to bring the body to the desired rotation?
could you also calculate the axis/angle representation of the rotation? i am not sure but maybe this would be useable as a torque then somehow?
Thomas
Moderator
Moderator
 
Posts: 65
Joined: Sat Nov 01, 2003 1:51 pm
Location: austria

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby Leadwerks » Sat Jul 19, 2008 12:08 pm

I have never dealt with angle/axis representations of a rotation.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby Dave Gravel » Mon Jul 21, 2008 1:47 pm

Maybe i'm totally wrong but it's a idea and a question i same time.
It's can come more simple to use a customjoint to generate the rotation ?
You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://orionx3d.sytes.net
https://www.facebook.com/dave.gravel1
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 800
Joined: Sat Apr 01, 2006 9:31 pm
Location: Quebec in Canada.

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby FuzzYspo0N » Tue Jul 22, 2008 1:12 am

Hey,

This thing bugged me for SO long it nearly made me tear my own hair out. i didnt,tho so dont stress.
I found some really insightful stuff at gamedev.net, regarding the maths. For example :

There is a nice FAQ page on maths and matrices and quaternions. Take a look at the following page,
it is titled Q55. How do I convert a rotation matrix to a quaternion?
Etc, there is a lot of info on quaternions, in an easy to understand section of info, WITH code examples and optimisations.
Take your time and read this through, it is more then worth it to understand these sort of things at an early stage (look how many people dont seem to) :)

http://www.gamedev.net/reference/articl ... 91.asp#Q55

http://www.gamedev.net/reference/articl ... le1691.asp

Hope it helps, it explains all the gimbal locks and how to avoid it there as well.
User avatar
FuzzYspo0N
 
Posts: 1
Joined: Tue Jan 08, 2008 9:12 am

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby Julio Jerez » Fri Jul 25, 2008 11:27 pm

Leadwerks wrote:I have never dealt with angle/axis representations of a rotation.


Has you look at the small matrix class in the SDk, if it is something you can use or port, then
I am going to add few more functions to extract stuff llike the angular velocity the will rotation a matrix into another matrix.
I doing it because am going to need it to extract information for keyframe animations. and thought it migh help on what you are looking for.

with a function that extract angular velocity from two matrices the angle axis rotation is veryt easy and intuitive, because the axis
is the direction of the angular velocity vector, and teh angle in the magnitud multioplyed by on half of the time step.

I beleive I posted code for that abput a year ago bu I can not find it.
Anyway I will added it to the dMatrix class and you can using the as it of converted to you programming language.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Torque Rotation Error - Possibly Gimbol Lock - Bad Euler!

Postby Leadwerks » Mon Aug 04, 2008 2:01 am

Thanks, I will take a look when it is ready.

This is something I never figured out before when I implemented picking up objects.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm


Return to Bugs and Fixes

Who is online

Users browsing this forum: No registered users and 7 guests

cron