• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Needing math help

Foldup

Member
Hello! Are you better at math than me or do you know a cool Game Maker trick I'm missing?

I've got a dial that I want to work like a speedometer type thing. It measures hull integrity from 0-10

It seems to me that setting the angle of the sprite to 180degrees makes it read like 0 and setting it to max out at 0degrees sets it all the way up. But I am baffled at how to translate a 0-10 scale into a 180-0 scale.

Any tips on what direction to take?

screencap2.png
 
Last edited:

Foldup

Member
You made that look so easy TailBit. I sincerely appreciate it!

I will try yours too chameleon. That's really great.
 
I assume the issue you're having is that 0 is on the right and 180 is on the left? Maybe something like:

GML:
gauge_angle = 180 - (hull_integrity * (180 / hull_integrity_max));
 

chamaeleon

Member
I will try yours too chameleon. That's really great.
There is absolutely nothing wrong with the math expression solutions, but I kind of like the use of lerp() in a case like this, because it expresses in code the fact that linear interpolation is being performed between two values. Given just a mathematical expression, this is not as apparent. Of course, one can deduce it by recognizing the pattern, but having a function named after the desired functionality provides context by itself (assuming one is familiar with the function in the first place, and what its purpose is).
 

kburkhart84

Firehammer Games
I personally use what FrostyCat calls a relerp() function. It basically just maps one range to another and is called a "map" function by some people as well(I think that's the technical math name for it actually).

Code:
function relerp(oldLow, oldHigh, value, newLow, newHigh)
{
    return newLow+(newHigh - newLow) * (value - oldLow) / (oldHigh - oldLow);
}
 

Foldup

Member
There is absolutely nothing wrong with the math expression solutions, but I kind of like the use of lerp() in a case like this, because it expresses in code the fact that linear interpolation is being performed between two values. Given just a mathematical expression, this is not as apparent. Of course, one can deduce it by recognizing the pattern, but having a function named after the desired functionality provides context by itself (assuming one is familiar with the function in the first place, and what its purpose is).
And THAT is why I asked if anyone is better at math than me! haha.

Yeah, I'm an OK coder, but I mostly have to bash my head against things until I get them working. Being a more jack of all trades means I'm weaker in some areas and math is one of them. It's always been my Achille's heel.
 
Top