- Home /
Rotating, stopping, rotating again.
So I'm working on a mechanic with the Kinect that involves turning a wheel. The way I am doing it currently is getting the angle between the player's two hands. (kp refers to kinect stuff, it just tells if the hand is open or closed).
void Update () {
heading = lCursor.transform.position - rCursor.transform.position;
if (kp.rightIsClosed) {
currAngle = Mathf.Atan2 (heading.y, heading.x);
transform.rotation = Quaternion.Euler (0f, 0f, (Mathf.Rad2Deg * currAngle) - (oldAngle));
}
if (kp.rightIsOpen) {
oldAngle = transform.eulerAngles.z;
}
}
The problem I am having is that when you turn the wheel, let go, re-position your hands, and try to turn again the wheel snaps to the new hand position. What I want is that when you let go, that becomes the new 'zero', imagine turning a valve. Its not one continous turn but several small turns. I tried at the bottom to get the angle when you when you let go as oldAngle and then when you regrab, subtract that value from the new so you are only rotating the new amount. I'm pretty stuck so any help would be greatly appreciated.
How does the user turn the wheel? Is it with a single hand like wheel of fortune or with two hands price is right style? I'm not sure I understand what lCursor
and rCursor
are, so I don't understand what heading
represents.
@sirbranedamuj its like a pirate ship wheel. so its more like they are turning a bar between the two hands.
@robertbu i don't think so. if you turn something 30 degrees, let go and grab it to turn another 30, if you add the first 30 you'd be at 90, not 60. i think my problem is more with getting the value of oldAngle. if the wheel starts at 0 and i grab it and let go it stays at zero. if i grab it a second time it immediately flips to 180(or -180).
Answer by robertbu · Jul 30, 2014 at 07:02 PM
Here is a quick script for how I might approach the problem. I'm not sure of frame of reference, but I think your heading vector is backwards. That is Vector3.right is the 0.0 angle for Atan2, so looking towards positive 'z' , you want to subtract the left from the right. But maybe the names don't mean what I think.
private bool grabbed = false;
private float startAngle = 0.0;
private Quaternion startRot;
void Update () {
if (kp.rightIsClosed) {
Vector3 heading = rCursor.transform.position - lCursor.transform.position;
float currAngle = Mathf.Atan2(heading.y, heading.x) * Mathf.Rad2Deg;
if (!grabbed) {
startAngle = currAngle;
startRot = transform.roation;
grabbed = true;
}
else {
currAngle -= startAngle;
transform.rotation = Quaternion.AngleAxis(currAngle, Vector3.forward) * startRot;
}
}
else {
grabbed = false;
}
}
@robertbu AH! That helped a ton! You are gem, thanks so much. You're solution is spot on if you want to post it as an answer.
Answer by sirbranedamuj · Jul 30, 2014 at 07:09 PM
transform.rotation = Quaternion.Euler (0f, 0f, (Mathf.Rad2Deg * currAngle) - (oldAngle));
Setting the rotation like this is not adding to the rotation. You're creating a brand new rotation that is rotating x, y, and z degrees from origin.
If your object was at 30 degrees and they let go, then try to turn another 30 degrees, your object will get set to 0 degrees rotated from origin. You need to add your previous angle to your rotation.