Universal and easy needle rotation function

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
Mickolodias
Posts: 69
Joined: Mon Sep 13, 2021 3:21 am

Universal and easy needle rotation function

#1 Post by Mickolodias »

After having some trouble with needles @Keith Baxter was kind enough to test and provide some guidance. Also @Sling for pointing out that really, I was probably doing it wrong.

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
I'm one of 'those' mac guys. (and I have no idea why I can't afford to eat)

Post Reply