Sign in to follow this  
Narf the Mouse

[Unity game engine] Quaternion "free-look" script suffering gimbal lock

Recommended Posts

Narf the Mouse    322
Given that I'm operating solidly in three dimensions, this shouldn't happen - Provided I coded it properly.

The code in question:
[code]
using UnityEngine;
using System.Collections;

public class MouseLookFree : MonoBehaviour {

// The current orientation.
Quaternion orientation ;

// Use this for initialization
void Start () {
// If we get a quaternion rotation from World Forward to Local Forward,
// we effectively just got the quaternion orientation.
orientation = Quaternion.FromToRotation(
Vector3.forward,
transform.localToWorldMatrix.MultiplyVector( Vector3.forward )
) ;
}

// Update is called once per frame
void Update () {
// Make sure the orientation is updated.
orientation = Quaternion.FromToRotation(
Vector3.forward,
transform.localToWorldMatrix.MultiplyVector( Vector3.forward )
) ;

// Get a horizontal and vertical quaternion rotation from an angle and axis.
var rotateHorizontal = Quaternion.AngleAxis( Input.GetAxis( "Mouse X" ), Vector3.up ) ;
var rotateVertical = Quaternion.AngleAxis( Input.GetAxis( "Mouse Y" ), Vector3.left ) ;

orientation = rotateHorizontal * orientation ;
orientation = rotateVertical * orientation ;

transform.localRotation = orientation ;
}
}
[/code]
My other thought is that Unity uses Euler angles as its underlying representation, but I want to eliminate user error before I say that.

Share this post


Link to post
Share on other sites
mhagain    13430
You're falling for the common misconception that just using quaternions is enough to avoid gimbal lock. That's not 100% true - the cause of gimbal lock comes from concatenating multiple successive rotations, and it doesn't matter what representation is used for those rotations. The advantage of quaternions in this case is that they can allow you to express the full set of rotations in a single move, which you can't do with Euler angles, but unless you actually do this you're still prone to gimbal lock.

So in your case you're using quaternions, but you're still concatenating 3 rotations. What you need to do instead is construct a single quaternion from your raw rotation data and use that instead.

Share this post


Link to post
Share on other sites
Narf the Mouse    322
[quote name='mhagain' timestamp='1333238714' post='4927057']
You're falling for the common misconception that just using quaternions is enough to avoid gimbal lock. That's not 100% true - the cause of gimbal lock comes from concatenating multiple successive rotations, and it doesn't matter what representation is used for those rotations. The advantage of quaternions in this case is that they can allow you to express the full set of rotations in a single move, which you can't do with Euler angles, but unless you actually do this you're still prone to gimbal lock.

So in your case you're using quaternions, but you're still concatenating 3 rotations. What you need to do instead is construct a single quaternion from your raw rotation data and use that instead.
[/quote]
...Huh, thanks. Working...

Share this post


Link to post
Share on other sites
Narf the Mouse    322
[url=http://answers.unity3d.com/questions/183716/how-to-make-a-simple-full-rotational-quaternion-fo.html]Look what you can find on Google[/url]. :)

[code]
using UnityEngine;
using System.Collections;

public class MouseLookFree : MonoBehaviour {

public string Yaw = "Mouse Y" ;
public string Pitch = "Mouse X" ;
public string Roll = "Mouse ScrollWheel" ;


// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
transform.Rotate( -Input.GetAxis( Yaw ), Input.GetAxis( Pitch ), Input.GetAxis( Roll ) * 90, Space.Self ) ;
}
}
[/code]
Next: Torque?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this