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.