Chaotic Motion  1.0
ChaoticMotion.cs
1 using UnityEngine;
2 using System.Collections;
3 
19 
20  // optional chaos trail renderer (better than TrailRenderer, since it
21  // adds intermediate points from integration to ensure smooth curve
22  // at all speeds)
23  private ChaosTrail chaosTrail;
24 
25  private ChaosEqn chaosEqn;
26 
27  // Initial position in world space
28  private Vector3 initialPosition;
29  private Vector3 lastTrail;
30 
31 
32  // Define these as class variables so they can
33  // be alloc-ed once in Awake and not in every Evolve()
34  // Internal physics position
35  private float[] x;
36  // Runge-Kutta intermediate values
37  private float[] a_n;
38  private float[] b_n;
39  private float[] c_n;
40  private float[] d_n;
41  private float[] arg;
42 
43  private bool diverged;
44  private bool paused;
45 
46  void Awake() {
47  Init();
48  }
49 
53  public override void Init() {
54  x = new float[3];
55  a_n = new float[3];
56  b_n = new float[3];
57  c_n = new float[3];
58  d_n = new float[3];
59  arg = new float[3];
60 
62  initialPosition = transform.position;
63  lastTrail = transform.position;
64 
65  StartAt(chaosEqn.paramBundle.initialPosition);
66 
67  // check for a ChaosTrail child
68  chaosTrail = GetComponentInChildren<ChaosTrail>();
69 
70  paused = false;
71  TimeInit();
72  }
73 
74  private void StartAt(Vector3 xstart) {
75  x[0] = xstart.x;
76  x[1] = xstart.y;
77  x[2] = xstart.z;
78  }
79 
80  // Evolve the dynamical system using a 4th order RK integrator
81  private Vector3 Evolve(float h) {
82 
83  Vector3 xout = Vector3.zero;
84  chaosEqn.Function(ref x, ref a_n);
85  float h_frac = h/2f;
86  arg[0] = x[0] + h_frac * a_n[0];
87  arg[1] = x[1] + h_frac * a_n[1];
88  arg[2] = x[2] + h_frac * a_n[2];
89  chaosEqn.Function(ref arg, ref b_n);
90  arg[0] = x[0] + h_frac * b_n[0];
91  arg[1] = x[1] + h_frac * b_n[1];
92  arg[2] = x[2] + h_frac * b_n[2];
93  chaosEqn.Function(ref arg, ref c_n);
94  h_frac = h;
95  arg[0] = x[0] + h_frac * c_n[0];
96  arg[1] = x[1] + h_frac * c_n[1];
97  arg[2] = x[2] + h_frac * c_n[2];
98  chaosEqn.Function(ref arg, ref d_n);
99  h_frac = h/6f;
100  xout.x = x[0] + h_frac*(a_n[0] + 2f * b_n[0] + 2f * c_n[0] + d_n[0]);
101  xout.y = x[1] + h_frac*(a_n[1] + 2f * b_n[1] + 2f * c_n[1] + d_n[1]);
102  xout.z = x[2] + h_frac*(a_n[2] + 2f * b_n[2] + 2f * c_n[2] + d_n[2]);
103  x[0] = xout.x;
104  x[1] = xout.y;
105  x[2] = xout.z;
106  return xout;
107  }
108 
109  void FixedUpdate() {
110 
111  if (diverged || paused) {
112  return;
113  }
114 
115  Vector3 position = transform.position;
116 
117  int numSteps = CalcNumSteps();
118 
119  for (int i=0; i < numSteps; i++) {
120  // apply the parameter bundle offset and scale (to center and size the attractor w.r.t
121  // to the local position of the attractor)
122  Vector3 phyPos = (Evolve(DT) + chaosEqn.paramBundle.offset) * chaosEqn.paramBundle.scale;
123  // check for blowup
124  if (float.IsNaN(phyPos.x) || float.IsNaN(phyPos.y) || float.IsNaN(phyPos.z)) {
125  Debug.LogWarning("Position diverged");
126  diverged = true;
127  return;
128  }
129 
130  // apply world offset and world scaling
131  position = Vector3.Scale(phyPos, evolveScale) + initialPosition;
132  position = transform.rotation * position;
133  if (chaosTrail != null) {
134  if (Vector3.Distance(lastTrail, position) > chaosTrail.minVertexDistance) {
135  chaosTrail.AddPoint(position);
136  lastTrail = position;
137  }
138  }
139  }
140  transform.position = position;
141  }
142 
143  public ChaosEqn GetEquation()
144  {
145  return chaosEqn;
146  }
147 }
ParamBundle paramBundle
The parameter bundle that is used to evolve the system.
Definition: ChaosEqn.cs:19
void AddPoint(Vector3 point)
Adds a point to the trail.
Definition: ChaosTrail.cs:40
ParamBundle customParams
Custom parameter values for evolution (valid only if selectedParams exceeds number of params listed i...
static ChaosEqn Create(int index, int paramIndex, ParamBundle customParams)
Create the ChaosEqn implementation for the specified index in the eqnList, with the selected param bu...
Chaotic system. Base class for chaotic scripts. Holds the type of system selected, the parameter set or the custom parameters to be used.
float scale
scale to ensure that attractor fits in box of size 10
Definition: ParamBundle.cs:25
float minVertexDistance
Distance interval for points to be added to the renderer.
Definition: ChaosTrail.cs:17
int selectedEqn
Number of chaos system to evolve (w.r.t. ChaosFactory list)
Chaos trail. Update a trail using a LineRenderer. This allows the path between points to be filled in...
Definition: ChaosTrail.cs:14
abstract void Function(ref float[] x_in, ref float[] x_dot)
Evaluate the first order evolution of the attractor, given the current position.
Vector3 evolveScale
Scale to apply to phyics evolution when mapping back to world space.
Vector3 initialPosition
Initial position for evolution in physics space.
Definition: ParamBundle.cs:28
Chaos eqn. Base class for all equations that define a 3D chaotic system.
Definition: ChaosEqn.cs:9
Vector3 offset
offset to center attractor at local origin
Definition: ParamBundle.cs:22
Chaotic motion. Evolve the user specified chaotic system with the parameters held by the ChaoticSyste...
override void Init()
Init the equation using the selected equation and parameter bundle.
int selectedParams
Number of parameter bundle selected for evolution (per chaotic equation)