Godot: Add map() function, for mapping values from a range to another

Created on 9 Aug 2017  路  36Comments  路  Source: godotengine/godot

I used Processing and it contains a very useful function map()
it works like this: var result = map(v, a,b, x,y) you pass a value v which is between a and b and the function will return a number between x and y

ex: var r = map(60, 20,100, 200, 240) this will return 220
or: var g = map(0.43, 0,1, 0,255)

From here i saw that the actual java code used is:
static public final float map(float value, float istart, float istop, float ostart, float ostop) {
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}

I really love to add it myself but i never worked in other's codes and i don't know how and where to put it so please somebody add it .. its very usefull

feature proposal gdscript

Most helpful comment

All 36 comments

You can do this in GDScript. Also map is a very vague, if not misused (which typically means this), term for this.

This seems an oddly specific function with an awful name (as @tagcup said, map usually means applying a function to all elements in a list). It seems to me the same as lerp but with the ability to select the range instead of using only 0-1. In fact, your second example (var g = map(0.43, 0,1, 0,255)) is exactly what lerp does.

Agree with vnen for the naming part. But i'm not even sure it's really needed, as we allready have lerp.
Anyway this could be called somehow like rlerp, (for range lerp) ?

Could be marked as Junior job I guess

But why does this have to be in the core? You can implement such specific functions in two lines in gdscript.

I agree that the name map() is wrong, i like rlerp() .. but it's very annoying to implement it every time in gdscript .. it's clearly more complicated than Vector2.length() so its like saying "implement Vector2.length() in your gdscript", there are alot of other functions that are very easy to implement in gdscript so why exclude this one? :/

"Why exclude this one" is the wrong question, to wrong person.
Instead, you need to give a compelling reason to why this particular function should be in the core.

Everybody has that sort of utility functions. If we add all of those to the core, it will be a heap of incoherent utility box.

FYI, you can reuse functions defined in other files in GDScript.

And if you need performance, you can use NativeScript for such functions now.

Well, this is a tool math function. I perfectly understand why it may be in the math API.
Usually we refer either to the C or the python API for those function. Can you find an equivalent there ?

Why do think being mathematical justifies adding it to the core? I can literally give you thousands of useful mathematical functions (just open Mathematica's help, or check out Julia's libraries).

It's very useful in changing a number from one range to another, i used it before in other places for changing world to pixel coordinates and vice versa (very useful in creating mandelbrot), i don't have any example for Godot in my head right now :/, but how bad is it to include something that is not very useful in core :|

Ok, so I can ask you the opposite question. Why not adding it to the core ? This is not a corner case function and it is useful, it seems, at least to Processing users.

We are not talking about some complex math function, this is just a math tool to interpolate from one range to another. It's easy to implement a faces many possible usage.

I still don't say should be added, but I understand that the question may be asked: This function is simple enough to serve in a lot of situations but it is also complex enough not to be in the core math API. The question is not simple.

Was this page helpful?
0 / 5 - 0 ratings