Untitled
unknown
c_cpp
8 months ago
1.4 kB
10
Indexable
// currAngle and targetAngle are between [0..360), where 360->0.
// Calculate the distance between currAngle and targetAngle
// in both the positive (clockwise) and the negative (counterclockwise)
// directions.
double positiveDistance, negativeDistance;
if (targetAngle < currAngle) {
positiveDistance = targetAngle - currAngle + 360;
negativeDistance = currAngle - targetAngle;
} else {
positiveDistance = targetAngle - currAngle;
negativeDistance = currAngle - targetAngle + 360;
}
// Whichever distance is shorter, move in that direction.
bool movedPositive;
if (negativeDistance < positiveDistance) {
currAngle--;
movedPositive = false;
} else {
currAngle++;
movedPositive = true;
}
// Now, if we overshot the target, we want to clamp to it.
// Start by checking distance again.
if (targetAngle < currAngle) {
positiveDistance = targetAngle - currAngle + 360;
negativeDistance = currAngle - targetAngle;
} else {
positiveDistance = targetAngle - currAngle;
negativeDistance = currAngle - targetAngle + 360;
}
// Check if, after we've moved, the target is now in the opposite
// direction. If it is, then we've overshot and need to clamp.
if (negativeDistance < positiveDistance) {
if (movedPositive) {
currAngle = targetAngle;
}
} else {
if (!movedPositive) {
currAngle = targetAngle;
}
}
Editor is loading...
Leave a Comment