Graphing functions (Node editor for maths)

Why might I use this?

Imagine (for example) you are designing some UI for your game, and you have a lives counter, with a particle system for effect. You want the number of remaining lives to dictate the particles emission rate, so you add some code like:

myParticleSystem.emissionRate = lives;


Now, the more lives you have, the more particles there is. But now you decide that a Y=X relationship isnt right, so you multiply the number of lives and you get

myParticleSystem.emissionRate = lives * 5;


and this is better, but really you want a greater increase in particles the higher your lives go, so you might try using the Pow() function

myParticleSystem.emissionRate = Mathf.Pow( lives, 2 );


and this is better again, but maybe a different value of Pow, or different multiple of lives would give you the curve that you want. It’s difficult to visualize it, you want a way to adjust the values and quickly see the relationship between the input (lives) and output (particle rate).

This applies to any other potentially non-linear relationship as well, for example:

  • enemy level (0 > 100) - item drop rate (0.05 > 0.1)

  • remaining lives (0 > many) - extra-life powerups (some > few)

  • vehicle speed (0 > fast) - dirt particles (0 > max) or engine volume (off > loud)

So I made this Node-based maths calculator:


You can create a node system, define variables, and adjust the values to see the resulting behavior.
The idea is, create a system that works right for your purpose, then click the button to “Generate Code” which you can paste into your script (not yet implemented edit: done).
I’d also like to put in a function where you can plot known values, an it adjusts the variables to closely match the plot.

It’s also got save/load function, and a few extra controls:
Drag empty area to pan the view
Right-mouse an empty area to add a new node
Right-mouse a node or group selection to delete it
Middlemouse to select groups
Alt to clone the currently selected group

Its got a bunch of node types, so should be able to recreate anything that Mathf. is capable of


20

Here’s the package (1 script and one icon). I’d appreciate some feedback on function and features

1885089–128244–Grapher.unitypackage (41.1 KB)

5 Likes

I have a lot of respect for anyone who creates, but that 4 lives layout does not look that great…

Actually I’ve read that again, and I think I see what you’re getting at now:

-You have some desired output
-You have some predictable input

The transformation between them is unknown:

Input → (Transform) → Output

So you create every possible set of functions and equations, and let the users directly manipulate the unknown transform. This lets people reach their desired output without really understanding HOW to get there.

Few points:

  1. Very good idea. I had a similar thought recently about how to train AI.

  2. Have you considered just using a power series? My calculus and linear algebra is rusty, but I’m pretty sure you can express any function around some point (x,y) as a linear combination of y = sum(ax^n), for some unknown a’s and for n, n+1, n+2, etc. (a + bx + cx^2 + dx^3 + etc).

These solutions are approximations, and they get less accurate the further out you go from your initial condition. For that reason you’d need to make your program reevaluate the power series continuously

So you can drop all of those functions (Sin/cos, pow, square roots, etc). Everything can just be a power series “Sum(k(i)*x^n) for n++ and i in N” if that’s how you want to think about it.

  1. Linear algebra…normalization I think? Should let you find the line of best fit around your initial condition. You’re looking for some combination of weights a, b, c, …, etc in N space. I’m pretty sure there’s a solid way of finding that. Man I might bust out my old text book now that you’ve got me thinking about it.

  2. Ugh…I think differential equations also has a way of solving power series. y(0) = a, y’(0) = b, y’'(0) = c, etc

So from my perspective: Good news and bad news. Good news is that I think you’ve got a great idea, and there’s a straight forward way to implement it (Power series approximation). Bad news is that you’re going to have to buff up on your math to actually do that straight forward method.

Ok I’ve implement the code generation. You click it and choose C# or js, and it puts the graphed functions onto the clipboard, looking this this:

  float EvaluateValue ( float value, float Variance ) {
     return Mathf.Clamp( Mathf.Sin( value * Mathf.Deg2Rad ) + ( ( Random.value ) * Variance ) - ( Variance / 2f ), -1f, 1f );
   }

Including any additional variables you used. It’s also smart enough to detect when you have a group of nodes begin used in more than one input, so separates them to reduce repeat evaluations

    float EvaluateValue ( float value ) {
        float functionBlock1 = Mathf.Sin( value * 2f * Mathf.Deg2Rad );
        return functionBlock1 + Mathf.Pow( functionBlock1, 3.7f ) + Mathf.Pow( functionBlock1, -0.7f );
    }

Editor package updated in OP.

Yeah, purely an example where you might need the value of one thing to reflect the value of another via some function

Power series for curve fitting, Noted.
But if the user wants a sinusoidal relationship, may as well use sin().

1 Like

wow this is definitely amazing! This is the first time i see something like that. Nice work!

Updated to add code generation for the conditional switch node (first input evaluates to index the others)

    float EvaluateValue0 ( float value ) {
        float functionBlock1 = 0;
        int functionBlock2 = Mathf.RoundToInt( Mathf.Repeat( value, 3.141593f ) );
        switch ( functionBlock2 ) {
            case 0:
                functionBlock1 = Mathf.Sin( value );
                break;
            case 1:
                functionBlock1 = Mathf.Cos( value );
                break;
        }
        return functionBlock1;
    }

This looks awesome, thanks! I’m not really great with math so I think this will help me out allot!

Could you possibly do some videos? or give more examples of how to use it more.

Thanks!

Updated OP (finally) with (another) window to add points to the graph, so you can match a curve to known values, by tweaking variables and watching the result.


Might not load your _favorite s_aved graphs from previous versions properly (nerd), but i think it should be fine.

Videos? Aw really?

I don’t really know what to use this for but I do know I want to use it :slight_smile:

Awesome stuff :smile: