I'm a lazy programmer, I don't want to brain, so I've come up with this function to simplify the pretty much un-intelligible math I was doing to work out rotation.
Now I don't have to math ... well... as much... and it just freakin' works.
Input the min and max values on the gauge, the pizza/pie angle, rotation offest (if straight up isn't the center of the gauge) and even an exponent to 'curve' the value for a non-linear gauge. And away you go to grab that extra whiskey.
Code: Select all
--- rotateNeedle
-- @param needleRef Reference to the object to rotate
-- @param value The value to set the gauge to
-- @param minVal The lowest value on the gauge
-- @param maxVal The highest value on the gauge
-- @param sector The sector angle (pizza shape range) in degrees
-- @param[opt=1] exponent Curve of the gauge value. 1 is linear. 0.5 makes low values higher, and high values lower. 2 makes low values higher and high values lower. minVal is always at the bottom, maxVal is always at te top, the curve is applied in between... this is totally f'd to describe without a picture!
-- @param[opt=0] rotationOffset Center of gauge in degrees. Defaults to 0 (12 O'clock)
function rotateNeedle(needleRef, value, minVal, maxVal, sector, exponent, rotationOffset)
-- set defaults
rotationOffset = rotationOffset or 0
exponent = exponent or 1
-- apply limits to the value to prevent moving out of range
value = var_cap(value - minVal, 0, maxVal - minVal)
-- value percentage between min and max
needlePercent = (value/(maxVal - minVal))^exponent
-- work out the angle of the gauge
needleAngle = ( sector * needlePercent) - (sector / 2) + rotationOffset
rotate (needleRef, needleAngle)
end