- Home /
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.
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.