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);
}
}