Question: Unity C#: Simulate the movement of a particle using the formula: p(t)=(a*cos^3(t),a(sin^3(t),0) Build off of the script: public class VisualizeWithLineRenderer : MonoBehaviour { [SerializeField] LineRenderer

Unity C#: Simulate the movement of a particle using the formula: p(t)=(a*cos^3(t),a(sin^3(t),0) Build off of the script: public class VisualizeWithLineRenderer : MonoBehaviour {

[SerializeField] LineRenderer lineRenderer; public int NPoints=5;

[Header("Asteroid Parameters")] public float a = 5; public float b = 3; public enum VisualizationName { None, F_YequalX2, //y=f(x); f(x)=x^2 F_YequalSinX, //y=f(x); f(x)=sin(x) YPequalF_via_Euler_F_2X, //y'=f(x,y); f(x,y)=2*x YPequalF_via_Euler_F_Goose, //y'=f(x,y); f(x,y)=... F_Vequal_Circle, //V=V(t); ...or (x,y,z)=(x(t),y(t),z(t)); F_Vequal_Ellipse, //V=V(t); ...or (x,y,z)=(x(t),y(t),z(t));

} // Start is called before the first frame update public System.Func F_YequalX2 = x => x*x; public System.Func F_YequalSinX = x => Mathf.Sin(x); public System.Func F_2X = (x, y) => 2 * x; public System.Func F_Circle = (t,r) => new Vector3(r*Mathf.Cos(t),r*Mathf.Sin(t),0); public System.Func F_Ellipse = (t, a, b) => new Vector3(a * Mathf.Cos(t), b * Mathf.Sin(t), 0);

public VisualizationName visualizationName = VisualizationName.F_YequalX2; void Start() { Vector3 pos = this.transform.position; switch (visualizationName) { case VisualizationName.F_YequalX2: //RenderLine(GetYequalX2(NPoints), Color.green); RenderLine(GetYequalF_X(5, 6, pos.x, pos.y, pos.z,F_YequalX2), Color.green); break; case VisualizationName.YPequalF_via_Euler_F_2X: RenderLine(GetYPequalF_via_Euler(10, 11, pos.x, pos.y, pos.z, F_2X), Color.blue); break; case VisualizationName.F_YequalSinX: RenderLine(GetYequalF_X(Mathf.PI*2,NPoints, pos.x, pos.y, pos.z, F_YequalSinX), Color.yellow); break; case VisualizationName.F_Vequal_Ellipse: RenderLine(GetVequalF_T(Mathf.PI * 2, NPoints, pos.x, pos.y, pos.z, F_Ellipse), Color.magenta);

break; default: print("No visualization exists for this choice!"); break;

}

} ///

/// Used to visualize an array of points in 3D given as vectors. /// Uses the LineRenderer component. /// For now some configuration parameters are hardcoded (startWidth etc). /// Some others are left at their default values. /// If you need a more sofisticated rendering, create an overload /// whereby you pass a configuration dictionary. /// /// /// void RenderLine(Vector3[] points, Color color) { lineRenderer = GetComponent(); lineRenderer.positionCount = points.Length; lineRenderer.startWidth = 0.2f; //hardcoded; lineRenderer.endWidth = 0.2f; //hardcoded; lineRenderer.startColor = color; lineRenderer.endColor = color; for (int i = 0; i < points.Length; i++) { lineRenderer.SetPosition(i, points[i]); }

} ///

/// Can be used to Visualize functions of type y=f(x) (or x=x(t), y=y(t) etc) /// /// Length of interval [x0,x0+L] /// Number of points /// /// /// /// The function to visualize /// Vector3[] GetYequalF_X(float L, int NPoints, float X0, float Y0, float Z0, System.Func F) { float H = System.Convert.ToSingle(L) / (NPoints - 1); Vector3[] result = new Vector3[NPoints]; result[0].x = X0; result[0].y = Y0; result[0].z = Z0;

for (int i = 1; i < NPoints; i++) { float x = result[i - 1].x; //, y = Y0+F(x); result[i].x = x + H; result[i].y = Y0 + F(x+H); result[i].z = Z0; } return result; } ///

/// Can be used to visualize the function y you get from Euler Scheme /// that satisfies y'=f(x,y) with initial conditions y(x0)=y0 /// /// /// /// /// /// /// The function F in y'=F(x,y) /// Vector3[] GetYPequalF_via_Euler(float L, int NPoints, float X0, float Y0, float Z0, System.Func F) { //y'=f(x,y) //y0=y(x0) float H = System.Convert.ToSingle(L) / (NPoints - 1); Vector3[] result = new Vector3[NPoints]; result[0].x = X0; result[0].y = Y0; result[0].z = Z0;

for (int i = 1; i < NPoints; i++) { float x = result[i-1].x, y = result[i - 1].y; result[i].x = x+H; result[i].y = y +H*F(x,y);//Euler Scheme result[i].z = Z0; } return result; } ///

/// Visualization of V'=Vf(t,a,b); //( /// /// /// /// /// /// /// /// Vector3[] GetVequalF_T(float L, int NPoints, float X0, float Y0, float Z0, System.Func F) { float H = System.Convert.ToSingle(L) / (NPoints - 1); Vector3[] result = new Vector3[NPoints]; result[0].x = X0; result[0].y = Y0; result[0].z = Z0; //result[0].x = a; //result[0].y = 0; //result[0].z = 0; float angle=0; // = result[i - 1].x; //, y = Y0+F(x);

for (int i = 1; i < NPoints; i++) { angle+=H; result[i] = result[0]+F(angle, a, b); } return result; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!