GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2015-10-07 00:10:48

leovoel
Member
Registered: 2015-10-06
Posts: 1

wrap(), round_nearest(), ping_pong_mod(), and map()

surprised these two aren't here already.

Expand///wrap(value, max)
//
//  Returns value, wrapped in the range with minimum = 0, and maximum = max - 1.
//  Works with negative values and a negative maximum as well.
//
//      value      value to be wrapped, real
//      max       upper/lower bound of the range, real
//

{
    return ((argument0 % argument1) + argument1) % argument1;
}
Expand///round_nearest(value, x)
//
//  Returns value rounded to the nearest multiple of x.
// 
//      value      value to be rounded, real
//      x          number that value should be a multiple of, real

{
    return round(argument0 / argument1) * argument1;
}

these other two are less common, but still rather useful.

Expand///ping_pong_mod(value, max)
//
//  Returns value, wrapped in the range with minimum: 0, and maximum: max - 1,
//  but it "oscillates" around the range: as in, it will move backwards if it
//  goes above/under max.
//  Works with negative values, although a negative max will not work.
//
//      value      value to be wrapped, real
//      max       upper bound of the range, real
//

{
    //below line can be replaced by var temp = wrap(argument0, argument1)
    //i just didn't want to add a dependency to this really simple script
    var temp = ((argument0 % (argument1 * 2)) + (argument1 * 2)) % (argument1 * 2);
    if(temp >= argument1) {
        return 2 * argument1 - temp;
    } else {
        return temp;
    }
}
Expand///map(value, src_min, src_max, dst_min, dst_max)
//
//  Returns value, mapped from a source range defined by src_min and src_max,
//  to a destination range defined by dst_min and dst_max.
//
//      value          value to be mapped, real
//      src_min      lower bound of the source range, real
//      src_max     upper bound of the source range, real
//      dst_min      lower bound of the destination range, real
//      dst_max     upper bound of the destination range, real
//
{
    return argument3 + (argument4 - argument3) * ((argument0 - argument1) / (argument2 - argument1));
}

all were tested but i can't really guarantee 100% correctness, haha

edit: oh, i see someone already has a map function here. woops.

Last edited by leovoel (2015-10-07 00:51:48)

Offline

Board footer

Powered by FluxBB