• 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 /
  • Help Room /
avatar image
Question by BaconKnight · Jul 12, 2018 at 08:54 AM · rotationanglesvectorscalculation

Angle between two Vectors split up into x,y,z components

Hello, I am developing an application for the HoloLens which is testing different aids to guide the users visual attention. The user finds himself in a environment with objects floating around him, where he has to find the next target object out of all the target objects.

What i want to do is calculate the angle between the main cameras forward (the direction the player is currentlylooking) and a target object the player has to look at next. I need to calculate this once before every target object. Then i want to track how much rotation the user is actually doing. I want those optimal and real angles split up into x,y,z components so I can compare the different guiding methods in respect to the axes.

My idea was to project the forward and target vector into the corresponding planes and then calculate the angle. As the user cant rotate himself around the x-axis, just move his head up and down, i nedd to correct for impossible x-axis angles. To do this I multiply the y component of the camera's forward by (-1) if the forward and target vector point in different quadrants. So for the initial rotation on the x-axis rotation i would do the following:

 //calculate Vectors for target and camera forward
 Vector3 targetDir = target.position - camera.position;
 Vector3 forward = camera.forward;
 
 //project the Vectors into the yz-plane and calculate the angle
 Vector3 TargetProj = Vector3.ProjectOnPlane(targetDir, Vector3.right);
 Vector3 ForwardProj = Vector3.ProjectOnPlane(forward, Vector3.right);
 //compensate for impossible rotation maneuvers
 if ((TargetProj.z < 0 && ForwardProj.z > 0) | (TargetProj.z > 0 && ForwardProj.z < 0))
             {
                 ForwardProj.y = ForwardProj.y * (-1);                                              
             }

 float angleX = Vector3.Angle(TargetProj, ForwardProj);
 

For the actual rotation the user is doing i would calculate the difference between updates on the cameras forward vector projected on the yz-plane:

 void Update(){
  float angleX;
   Vector3 newForwardX = Vector3.ProjectOnPlane(forward_new, Vector3.right);
   Vector3 oldForwardX = Vector3.ProjectOnPlane(forward_old, Vector3.right);
 //compensate for impossible rotation maneuvers
     if ((TargetProj.z < 0 && ForwardProj.z > 0) | (TargetProj.z > 0 && ForwardProj.z < 0))
                 {
                     ForwardProj.y = ForwardProj.y * (-1);                                              
                 }

   float angle_x = Vector3.Angle(newForwardX, oldForwardX);
        angle_x = Mathf.Abs(angle_x);
        angleX +=angle_x;
        forward_old=forward_new;
     }

In the following picture the blue line is the cameras forward, the yellow line is the target vector. The green objects form a tunnel, to guid the user to the next target. After clicking on the target, the next target is activated and i need to calculate the inital, optimal, rotation in x,y,z and record the actual rotation in x,y,z. alt text The problem is that when the user is moving his head to turn to the target and he isnt keeping his head level with the ground, the projection of vectors changes and the angle gets totally wrong.

Has anybody done something similar or can offer any help? Thank you!

problem-angle.png (95.8 kB)
Comment
OpperDarwin

People who like this

1 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

178 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Applying a rotation relative to the object's starting rotation 0 Answers

Free spinning on one axis then snapping to nearest 36 degrees 1 Answer

Enemy not facing player when enter rotation orbit 0 Answers

Rotation before 0 and after 360 0 Answers

Need solution for rotation with player input of X, Y, Z 1 Answer


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