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
0
Question by ml1985 · Mar 22, 2015 at 11:15 PM · quaternions

Rotating gears example

Hey Guys

I have a question regarding rotations which I am finding quite difficult.

I have a wheel-shaped GUI with a script attached which allows the user to rotate by touch input. The script below shows how the position of the touch input is used to compute an angle (0-360) and this angle is used in the Quaternion.AngleAxis function in order to rotate the GUI accordingly.

 using UnityEngine;
 using System.Collections;
 
 public class vec2 : MonoBehaviour {
 
     public Vector2 p_vector2;
     public float A;
     public float B;
     public float currangle;
     public Vector3 rotationAxis = Vector3.down;
     public Quaternion rotation;
         
     void Update () {
 
                         p_vector2 = Input.GetTouch (0).position;
 
                         A = p_vector2.x - 43;
                         B = p_vector2.y - 43;
 
                         if (A < 0) {
 
                                 currangle = 360 - (Mathf.Atan2 (A, B)*Mathf.Rad2Deg *-1);
 
                         } else {
 
                                 currangle = Mathf.Atan2 (A, B)*Mathf.Rad2Deg;
                         }
 
         rotation = Quaternion.AngleAxis (currangle, rotationAxis);
 
         transform.rotation = Quaternion.AngleAxis (currangle,rotationAxis);
 
     }
 }

Now I have three gears in the scene that have a different number of teeth each. One has 10 the next one has 8 and the third has 30. Ideally I would like the 10 tooth gear to rotate with the same angular velocity as the wheel shaped GUI - Input. The 10 tooth gear will in turn rotate the 8 tooth gear and the 8 tooth gear will rotate the 30 tooth gear. I can easily calculate the angular velocities needed for the 8 tooth gear and 30 tooth gear to make it look like all gears are rotating naturally.

I have tried different approaches but haven't come across any solution yet: 1) My first approach was to use the physics engine, but it turns out my mesh isn't well refined for it to work. 2)To simply multiply the currangle variable of the GUI to rotate the other gears at the right speed, this causes the euler angles to skip and doesn't look natural. 3) I tried also to derive the angular velocity directly from the GUI script by subtracting the angle in the previous frame by the current angle and dividing it by the time between the frames. This didn't work out at all, I think it gave ridiculous answers between 359.99 and 0.

Are there any other paths I may try in this case? Any help would be greatly appreciated.

Comment
Add comment · Show 1
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 Graham-Dunnett ♦♦ · Oct 01, 2015 at 08:10 PM 0
Share

I'd take this in a much more simple way. We need to rotate a gear as the finger moves. So, we can just compute an angle as the finger arrives, and how that angle changes as the finger moves. This angle can then rotate the gear. Now, an attached gear needs to rotate at a different rate. This attached gear doesn't have to be attached in any real way. Also, for objects being rotated on a touch screen won't need to be rotated accurately... so I'd just simplify these to whole angles. (So a rotation by 43.21 degrees is pointless and can just be 43 degrees.)

Attached is a simple example:

 using UnityEngine;
 using System.Collections;
 
 public class rot360 : $$anonymous$$onoBehaviour {
 
     int w, h;
     int centreW, centreH;
     public GameObject go1, go2;
     int lastRotate = 0;
     
     // Use this for initialization
     void Start () {
         w = Screen.width;
         h = Screen.height;
         centreW = w/2;
         centreH = h/2;
     }
     
     // Update is called once per frame
     void Update () {
         w = (int)Input.mousePosition.x - centreW;
         h = (int)Input.mousePosition.y - centreH;
         
         int f = (int)($$anonymous$$athf.Atan2(h, w) * $$anonymous$$athf.Rad2Deg);
 
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
             lastRotate = f;
         } else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space)) {
             if (f != lastRotate) {        
                 go1.transform.Rotate(0,0,f-lastRotate);
                 go2.transform.Rotate(0,0,2.0f*(f-lastRotate));
                 lastRotate = f;
             }
         }
     }
 }

This has two quads, go1 and go2. These are just quads with textures on. Also, since I don't have an iOS handy, this is just doing rotation on the $$anonymous$$ac I am using, so use the $$anonymous$$eyDown stuff. One object is large and is in the centre of the screen. As the mouse moves I calculate how the mouse converts into an angle f. Then, if the space is pressed I make a note, and then as the mouse moves with the space kept down I rotate the big quad. And then I rotate a small quad by two times this angle. If the two quads had circle textures, and the big one had a circumference of 1, and the small one had a circumference of 0.5, then the small one would rotate twice as fast as the big one. So, in my little example the second quad rotates twice as fast. Hope this gives some ideas.

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Rotate a Cube on swipe gesture iOS, for a specific time? 0 Answers

How to steer a vehicle 1 Answer

How to move relative to the orientation 1 Answer

How do i make my Mouselook behave when changing the z rotation? 0 Answers

Headlight rotation issue when moving the camera 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