Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Richop, May 3, 2016.

  1. Richop

    Richop

    Joined:
    Mar 18, 2016
    Posts:
    136
    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:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class GraphFun : MonoBehaviour {
    6.  
    7.     [Range(10,1000)]
    8.     public int resolution = 10;
    9.     public float scale = 0.1f;
    10.  
    11.     private int currentRes;
    12.     private ParticleSystem.Particle[] points;
    13.  
    14.     public enum FunctionOptionX
    15.     {
    16.         Linear,
    17.         Expo,
    18.         Sine,
    19.         Parabola,
    20.         CosX,
    21.         SpiralX,
    22.         SpringX,
    23.         OtherSpiralsX
    24.     }
    25.  
    26.     public enum FunctionOptionY
    27.     {
    28.         SpringY,
    29.         OtherSpiralsY,
    30.         SinY,
    31.         SpiralY
    32.     }
    33.  
    34.     public FunctionOptionX function1;
    35.  
    36.     public FunctionOptionY function2;
    37.  
    38.     void Start()
    39.     {
    40.         CreatePoints ();
    41.     }
    42.  
    43.     // Use this for initialization
    44.     private void CreatePoints () {
    45.         if (resolution < 10 || resolution > 1000) {
    46.             Debug.LogWarning ("You're out of your element Donnie", this);
    47.             resolution = 10;
    48.         }
    49.         currentRes = resolution;
    50.         points = new ParticleSystem.Particle[resolution];
    51.         float increment = 1f / (resolution - 1);
    52.         for (int i = 0; i < resolution; i++)
    53.         {
    54.             float x = i * increment;
    55.             points [i].position = new Vector3 (x, 0f, 0f);
    56.             points [i].color = new Color (x, 0f, 0f);
    57.             points [i].size = 0.1f;
    58.         }
    59.     }
    60.  
    61.     private delegate float FunctionDelegate1 (float x);
    62.     private static FunctionDelegate1[] functionDelegates1 = {
    63.         Linear,
    64.         Expo,
    65.         Sine,
    66.         Parabola,
    67.         CosX,
    68.         SpiralX,
    69.         SpringX,
    70.         OtherSpiralsX
    71.     };
    72.  
    73.     private delegate float FunctionDelegate2 (float x);
    74.     private static FunctionDelegate2[] functionDelegates2 = {
    75.         SpringY,
    76.         OtherSpiralsY,
    77.         SinY,
    78.         SpiralY
    79.     };
    80.    
    81.     // Update is called once per frame
    82.     void Update () {
    83.         if (currentRes != resolution || points == null) {
    84.             CreatePoints ();
    85.         }
    86.         FunctionDelegate1 f1 = functionDelegates1 [(int)function1];
    87.         FunctionDelegate2 f2 = functionDelegates2 [(int)function2];
    88.         int wrapper = 6;
    89.  
    90.         for (int i = 0; i < resolution; i++) {
    91.             Vector3 p = points [i].position;
    92.             float angle = (i * Mathf.PI * 2 / resolution);
    93.  
    94.             p.x = f1(angle) * scale;
    95.             p.y = f2(angle) * scale;
    96.             p.z = angle;
    97.             points [i].position = p;
    98.             Color c = points[i].color;
    99.             c.g = p.y;
    100.             points [i].color = c;
    101.         }
    102.         GetComponent<ParticleSystem>().SetParticles (points, points.Length);
    103.     }
    104.  
    105.     private static float Linear (float x)
    106.     {
    107.         return x;
    108.     }
    109.  
    110.     private static float Expo (float x)
    111.     {
    112.         return x * x;
    113.     }
    114.  
    115.     private static float Parabola (float x)
    116.     {
    117.         x = 2f * x - 1f;
    118.         return x * x;
    119.     }
    120.  
    121.     private static float Sine (float x)
    122.     {
    123.         return 0.5f + 0.5f * Mathf.Sin(2 * Mathf.PI * x);
    124.     }
    125.  
    126.     private static float SpiralX (float x)
    127.     {
    128.         return Mathf.Exp(x) * Mathf.Cos(x);
    129.     }
    130.  
    131.     private static float SpiralY (float x)
    132.     {
    133.         return Mathf.Exp(x) * Mathf.Sin(x);
    134.     }
    135.  
    136.     private static float SinY (float x)
    137.     {
    138.         return Mathf.Sin(x);
    139.     }
    140.  
    141.     private static float CosX (float x)
    142.     {
    143.         return Mathf.Cos(x);
    144.     }
    145.  
    146.     private static float OtherSpiralsX (float x)
    147.     {
    148.         return x * (Mathf.Cos(6*x));
    149.     }
    150.  
    151.     private static float OtherSpiralsY (float x)
    152.     {
    153.         return x * (Mathf.Sin(6*x));
    154.     }
    155.  
    156.     private static float SpringX (float x)
    157.     {
    158.         return Mathf.Cos(6*x);
    159.     }
    160.  
    161.     private static float SpringY (float x)
    162.     {
    163.         return Mathf.Sin(6*x);
    164.     }
    165.        
    166. }
    167.  
     
    ThermalFusion likes this.
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    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
    Code (csharp):
    1.     private delegate void SomeDelegate(int a = 6, int b = 1);
    2.  
    3.     private void Start()
    4.     {
    5.         SomeDelegate del = Sum;
    6.         del();    // prints "Sum: 7" since 6 + 1 = 7
    7.     }
    8.  
    9.     private void Sum(int a, int b)
    10.     {
    11.         Debug.Log("Sum: " + (a + b));
    12.     }
    You could leverage this with either nullable values, or just ignore the unused values in the methods that don't need a secondary value.
     
  3. Lil Shadow EX

    Lil Shadow EX

    Joined:
    May 18, 2016
    Posts:
    2
    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
     
  4. Richop

    Richop

    Joined:
    Mar 18, 2016
    Posts:
    136
    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<ParticleSystem>().SetParticles(points, points.Length);
     
  5. Lil Shadow EX

    Lil Shadow EX

    Joined:
    May 18, 2016
    Posts:
    2
    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.
     
  6. Richop

    Richop

    Joined:
    Mar 18, 2016
    Posts:
    136
    I can github the project file if that helps.
     
  7. Richop

    Richop

    Joined:
    Mar 18, 2016
    Posts:
    136