Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by The_r0nin · Apr 29, 2011 at 11:44 AM · rotationjavascriptquaterniongimbal-lockaircraft

Need help converting Euler-based rotation to Quaternion-based (gimbal lock).

I am working on a mouse-based flight model for a game, and I've run into an issue that I can't quite solve. I have a custom cursor used to direct the roll of the aircraft: the direction the mouse cursor is from the center of the screen describes the up vector of the aircraft.

// Rotate vehicle from mousePosition
var screenVec: Vector3 = Vector3(Input.mousePosition.x - sCenter.x, Screen.height-Input.mousePosition.y - sCenter.y, 0);
var tarAngle: float = (Mathf.Atan2(screenVec.y, screenVec.x)*Mathf.Rad2Deg)+90;
if (tarAngle < 0) tarAngle+= 360;
var sangle: float = Mathf.MoveTowardsAngle(transform.eulerAngles.z, -tarAngle, rollSpeed*Time.deltaTime);
transform.RotateAround(transform.position,transform.forward,-tarAngle-sangle);

In addition, I have the aircraft pitch in the direction of its up vector depending on how far from the center of the screen the mouse cursor is (I have this bound to Input keys at the moment so that I can test it... same with forward motion):

// Determine pitchSpeed var newPitch: float = Vector3.Distance(screenVec,Vector3.zero)/sCenter.y; newPitch = -Mathf.Min(newPitch, 1); if (newPitch > -.2) newPitch = 0; newPitch *= pitchSpeed * Time.deltaTime;

// Rotate and move plane var moveForward: float = Input.GetAxis("Vertical")*5; if (Input.GetKey("a")) transform.RotateAround(transform.position,transform.right,newPitch); transform.Translate(0,0,moveForward*Time.deltaTime);

I also have the Camera slaved to the plane (in a separate script attached to the camera):

var plr: GameObject;

function FixedUpdate () { var newPos: Vector3 = plr.transform.position - (plr.transform.forward*10) + (plr.transform.up * 2); transform.position = newPos; transform.LookAt(plr.transform); }

This all works well, except when the plane approaches completely nose-up or nose-down. Then I get gimbal-lock and the plane spins wildly (due to the mouse code). I don't know how I would create a Quat to describe the 2D vector rotation of the plane based on the mouse position (I've tried a straight conversion using Quaternion.Euler(0,0,sangle) and I get the same result).

If anyone could help me make this work using only Quats, maybe the gimbal lock will no longer be an issue (I assume that's the reason for using them in the first place)...

Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
5

Answer by poncho · Apr 29, 2011 at 01:31 PM

if you want to change vector3 Euler angle to Quaternion, you can use the Quaternion.Euler(x,y,z); function, is the function i use, hope it helps

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image The_r0nin · Apr 29, 2011 at 02:31 PM 0
Share

Thanks, but I mentioned in the post that when I tried that method, I still got gimbal lock (because, I suppose, the original angles were approaching zero or 180 before the conversion).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Rotation along the x-axis to that player can turn around? 1 Answer

Gradual Rotation of Character from 2 Axis Input Sources 0 Answers

Avoiding Gimbal Lock 1 Answer

new Array storing rotations 1 Answer

Quaternion rotation flips axises when upside down 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges