• 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 /
This question was closed Nov 25, 2015 at 08:34 PM by TTLMarkwar for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by TTLMarkwar · Nov 24, 2015 at 12:00 PM · quaternionanglemathfslerpmeasure

Measuring the Angle Between Two Transforms for t Param of Quaternion.Slerp

So, I'm using Quaternion.Slerp to rotate a door hinge, but I'm having a hard time coming up with a good way to measure the t parameter.

I have a door. It has a knob. I have a start transform whose rotation corresponds to the closed rotation of the door and whose position corresponds to the approximate position of the knob when the door is closed. The same is true for my end transform, except for the door being open.

I'd like the door to be manually opened by the player moving their "hand" transform over the doorknob and pulling. From here, I thought I could use the dot product to measure the angle between the vector describing the start transform and the player hand transform.

Rad = cos^-1((A dot B) / (||A||*||B||))

or

float currentAngle = Mathf.Acos(Vector3.Dot(start.transform.position, tr.transform.position) / (start.position.magnitude * tr.position.magnitude)); float pull = (currentAngle * Mathf.Rad2Deg) / DegreesOfFreedom;

This isn't working, the angles I'm getting are far too small, so either my math is wrong, or I'm approaching this the entirely wrong way.

I also realize that I'm not accounting for the fact that, for this process to really work, I need to measure the angle ONLY on the two axis that are not the axis of rotation for the door. I figured I could work on that as soon as I get this part figured out.

alt text

EDIT:

I'm waiting for someone else to fix error before I can test, but I think I've over complicated the entire problem.

float currentAngle = Vector3.Angle(start.transform.position, handPos);

The above code should work just as well.

Regarding zeroing the magnitude of the vector along the rotation axis, it be nice to have a way to present a vector as either a row or column vector (a matrix). It would make make doing the matrix math yourself easier.

fullsizerender.jpg (445.2 kB)
Comment
Add comment · Show 8
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 Bonfire-Boy · Nov 24, 2015 at 12:08 PM 1
Share

I may be missing something but I don't see the rotation point used in your calculation of the angle. Shouldn't you be looking for the angle between (start.transform.position - rotationpoint.position) and (handPos - rotationpoint.position), rather than between start.transform.position and handPos ?

avatar image TTLMarkwar Bonfire-Boy · Nov 25, 2015 at 07:48 PM 0
Share

Yes, you're right. I ended up doing that.

avatar image saschandroid · Nov 24, 2015 at 12:58 PM 1
Share

Something like this?

 float a = Vector3.Distance(start.transform.position, rotationpoint.position);
     float c = Vector3.Distance(start.transform.position, handPos);
     float angle = $$anonymous$$athf.Acos(1.0f - ( c * c) / ( 2.0f * a *a ));
     angle *= $$anonymous$$athf.Rad2Deg;

(sorry if there are any typos)

avatar image Bonfire-Boy · Nov 24, 2015 at 01:17 PM 1
Share

I'm thinking more like this....

   Vector3 A = start.transform.position - rotationPoint.position;
   Vector3 B = handPos - rotationPoint.position;

That gives you the A and B vectors to plug into your formula to find the angle between A and B.

avatar image saschandroid Bonfire-Boy · Nov 24, 2015 at 01:34 PM 1
Share

From wikipedia:

alt text

angle.png (21.3 kB)
avatar image Bonfire-Boy saschandroid · Nov 24, 2015 at 02:01 PM 1
Share

I'm just looking at your diagram in the question above, which shows vectors A and B, and a formula for getting the angle between them, and commenting that (in the code in the question) you don't seem to be using the vectors A and B. You seem to be using vectors representing the point at one end of each of them ins$$anonymous$$d.

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by TTLMarkwar · Nov 26, 2015 at 03:48 PM

Like Bonfire said, the problem was I was using the vectors of start and hand, instead of vectors ending at start and hand, and beginning at the rotation point. So it wasn't the math, just my variables. Thanks for your help.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Coroutine with Quaternion.Angle 1 Answer

making a camera that moves depending on mousepos.y 0 Answers

Set the rotation reference to an direction 0 Answers

How do I clamp the Z-Axis Rotation for this code? 1 Answer

Error of angle 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