Hi I'm Rich, here have some Spirals...

Hello everyone, I haven’t really introduced myself, I’ve been using Unity solidly for just over a month, haven’t really made any games but I do love me a good math function!! This forum has been a great resource and thank you to anyone that has helped em out thus far!

Been working through the graph tutorial from Catlike coding and extended it to create Spirals and springs, but I have some questions about function delegates and using public variables inside functions. I have a spring equation something like Sin(6 * x) where 6 is the amount of times it wraps/coils around itself…

I’ve been wondering say if I wanted to control the wrap around would I need to add another variable to all the functions in the function delegate even if they don’t use the variable?

Also if anyone has any comments on where to take it next, I’m all ears! I’m thinking either object pools and instantiation of objects to the beat or maybe inject some controlled randomness into the equation and use it for some procedural spiral-esque weirdness!

Anyways hello and here’s some spirals, it’s working so hack away:

using UnityEngine;
using System.Collections;

public class GraphFun : MonoBehaviour {

    [Range(10,1000)]
    public int resolution = 10;
    public float scale = 0.1f;

    private int currentRes;
    private ParticleSystem.Particle[] points;

    public enum FunctionOptionX
    {
        Linear,
        Expo,
        Sine,
        Parabola,
        CosX,
        SpiralX,
        SpringX,
        OtherSpiralsX
    }

    public enum FunctionOptionY
    {
        SpringY,
        OtherSpiralsY,
        SinY,
        SpiralY
    }

    public FunctionOptionX function1;

    public FunctionOptionY function2;

    void Start()
    {
        CreatePoints ();
    }

    // Use this for initialization
    private void CreatePoints () {
        if (resolution < 10 || resolution > 1000) {
            Debug.LogWarning ("You're out of your element Donnie", this);
            resolution = 10;
        }
        currentRes = resolution;
        points = new ParticleSystem.Particle[resolution];
        float increment = 1f / (resolution - 1);
        for (int i = 0; i < resolution; i++)
        {
            float x = i * increment;
            points [i].position = new Vector3 (x, 0f, 0f);
            points [i].color = new Color (x, 0f, 0f);
            points [i].size = 0.1f;
        }
    }

    private delegate float FunctionDelegate1 (float x);
    private static FunctionDelegate1[] functionDelegates1 = {
        Linear,
        Expo,
        Sine,
        Parabola,
        CosX,
        SpiralX,
        SpringX,
        OtherSpiralsX
    };

    private delegate float FunctionDelegate2 (float x);
    private static FunctionDelegate2[] functionDelegates2 = {
        SpringY,
        OtherSpiralsY,
        SinY,
        SpiralY
    };
   
    // Update is called once per frame
    void Update () {
        if (currentRes != resolution || points == null) {
            CreatePoints ();
        }
        FunctionDelegate1 f1 = functionDelegates1 [(int)function1];
        FunctionDelegate2 f2 = functionDelegates2 [(int)function2];
        int wrapper = 6;

        for (int i = 0; i < resolution; i++) {
            Vector3 p = points [i].position;
            float angle = (i * Mathf.PI * 2 / resolution);

            p.x = f1(angle) * scale;
            p.y = f2(angle) * scale;
            p.z = angle;
            points [i].position = p;
            Color c = points[i].color;
            c.g = p.y;
            points [i].color = c;
        }
        GetComponent<ParticleSystem>().SetParticles (points, points.Length);
    }

    private static float Linear (float x)
    {
        return x;
    }

    private static float Expo (float x)
    {
        return x * x;
    }

    private static float Parabola (float x)
    {
        x = 2f * x - 1f;
        return x * x;
    }

    private static float Sine (float x)
    {
        return 0.5f + 0.5f * Mathf.Sin(2 * Mathf.PI * x);
    }

    private static float SpiralX (float x)
    {
        return Mathf.Exp(x) * Mathf.Cos(x);
    }

    private static float SpiralY (float x)
    {
        return Mathf.Exp(x) * Mathf.Sin(x);
    }

    private static float SinY (float x)
    {
        return Mathf.Sin(x);
    }

    private static float CosX (float x)
    {
        return Mathf.Cos(x);
    }

    private static float OtherSpiralsX (float x)
    {
        return x * (Mathf.Cos(6*x));
    }

    private static float OtherSpiralsY (float x)
    {
        return x * (Mathf.Sin(6*x));
    }

    private static float SpringX (float x)
    {
        return Mathf.Cos(6*x);
    }

    private static float SpringY (float x)
    {
        return Mathf.Sin(6*x);
    }
       
}
1 Like

You’re correct. If you change the signature of your delegate, you’ll need to change the signature of all of the methods that implement the delegate to match. You can’t have a single delegate with multiple signatures.

However… delegates can have default values, and those default values are substituted when invoking the delegate.

Consider

    private delegate void SomeDelegate(int a = 6, int b = 1);

    private void Start()
    {
        SomeDelegate del = Sum;
        del();    // prints "Sum: 7" since 6 + 1 = 7
    }

    private void Sum(int a, int b)
    {
        Debug.Log("Sum: " + (a + b));
    }

You could leverage this with either nullable values, or just ignore the unused values in the methods that don’t need a secondary value.

Hey buddy, this my be odd and stupid, but im trying tu use de Catlike tutorial, but in the recent versions, the particle systems doesnt work like in the realease in the tutorial, can you tell me how you configure them to work like in the tutorial? i been looking for someone who has used it :C

Save the project and let Unity update the syntax, it’ll be something like Getcomponent part that’s not included in the old version. It’ll automatically update to something like: GetComponent().SetParticles(points, points.Length);

will it be too much for ask you for a basic version of your develop? im a ittle(a LOT) lost… :C ill send pizza and soda as reward thx in advise.

I can github the project file if that helps.

https://github.com/richhop/GraphJammer