Selecting the rotation direction for Quaternion.Slerp

Hi! I'm making a spacecraft simulator and I'm working on adding support for mechanical gauges. In order to simplify the code, I'd call an update function where I specify the gauge and the current reading, in whatever units the gauge displays the information (e.g., feet, feet/sec, g's, etc).

In order to do that, I do the following operations in the startup phase:

  • Rotate the needle to the minimum scale value and store its rotation in a quaternion.
  • Rotate the needle to the maximum scale value and store its rotation in a quaternion.

Then, during the simulation, the UpdateNeedle function converts the needle's current reading to an interpolated value between 0 and 1 and calls the Quaternion.Slerp method in order to rotate it to the appropriate position. The function works fine for instruments whose scale doesn't span more than 180 (see the "Oxygen" gauge) but I'm having problems for bigger scales (see the "Altitude" gauge).

alt text alt text

More specifically, the "Altitude" gauge interpolates in the yellow area between the blue and red squares, which is clearly not what I want. How can I make the rotation using the other part of the scale?

Thanks in advance for your help!


For something like this, my inclination would be simply to use angles.

Say the gauge sweeps out an angle of about 340 degrees (as in your image). At 0, the angle of the needle is, say, 250. Given a parametric value t, the angle of the needle is then:

250 - t * 340

Or, more generally:

initial_angle - t * sweep_angle

Assuming a standard 2-d Cartesian coordinate system.

The result may of course go into the negatives (or, more generally, go outside of the range [0, 360)), but that shouldn't be a problem in most cases, and if you need the angle in the range [0, 360), you can always wrap it.