Skip to content

Commit 01b0351

Browse files
committed
Clean Code
1 parent aa99d78 commit 01b0351

15 files changed

+176
-8
lines changed

.gitignore

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
/[Ll]ogs/
1111
/[Uu]ser[Ss]ettings/
1212

13+
Packages/Microsoft.CodeAnalysis.FxCopAnalyzers.3.0.0/
14+
Packages/Microsoft.CodeAnalysis.VersionCheckAnalyzer.3.0.0/
15+
Packages/Microsoft.CodeQuality.Analyzers.3.0.0/
16+
Packages/Microsoft.NetCore.Analyzers.3.0.0/
17+
Packages/Microsoft.NetFramework.Analyzers.3.0.0/
18+
1319
# MemoryCaptures can get excessive in size.
1420
# They also could contain extremely sensitive data
1521
/[Mm]emoryCaptures/
@@ -67,4 +73,6 @@ crashlytics-build.properties
6773

6874
# Temporary auto-generated Android Assets
6975
/[Aa]ssets/[Ss]treamingAssets/aa.meta
70-
/[Aa]ssets/[Ss]treamingAssets/aa/*
76+
/[Aa]ssets/[Ss]treamingAssets/aa/*
77+
.editorconfig
78+
packages.config

Assets/Scripts/Common.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Common/IIntegrator.cs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface IIntegrator
2+
{
3+
void StepOneFrame(PhysicalScene scene);
4+
}

Assets/Scripts/Common/IIntegrator.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/PhysicalScene.cs renamed to Assets/Scripts/Common/PhysicalScene.cs

+15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class PhysicalScene : MonoBehaviour
1212
public GameObject particlePrefab;
1313
public GameObject linePrefab;
1414

15+
[System.NonSerialized]
16+
public string integratorType = "";
1517
[System.NonSerialized]
1618
public int objectCount = 0;
1719

@@ -157,6 +159,19 @@ public void Load(TextAsset scene)
157159
edgeList.Add(lineRenderer);
158160
edgeIndexList.Add(new Vector2Int(i, j));
159161
}
162+
163+
// Frame Rate
164+
XmlNodeList integrators = xmlDoc.GetElementsByTagName("integrator");
165+
if(integrators.Count > 0)
166+
{
167+
XmlNode integrator = integrators[0];
168+
float dt = float.Parse(integrator.Attributes["dt"]?.InnerText);
169+
integratorType = integrator.Attributes["type"]?.InnerText;
170+
171+
Debug.Log("Target Delta Time Is " + dt.ToString());
172+
Debug.Log("Integrator Type Is " + integratorType);
173+
Application.targetFrameRate = (int)(1.0f / dt);
174+
}
160175
}
161176

162177
public void Dispose()

Assets/Scripts/Runner.cs renamed to Assets/Scripts/Common/Runner.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,25 @@
1010
public class Runner : MonoBehaviour
1111
{
1212
PhysicalScene scene;
13-
ExplicitEulerIntegrator explicitEuler = new ExplicitEulerIntegrator();
13+
IIntegrator integrator;
1414

1515
void Awake()
1616
{
1717
scene = GetComponent<PhysicalScene>();
1818
scene.Load();
19+
20+
if (scene.integratorType == "explicit-euler")
21+
{
22+
integrator = new ExplicitEulerIntegrator();
23+
}
24+
else if (scene.integratorType == "symplectic-euler")
25+
{
26+
integrator = new SymplecticEulerIntegrator();
27+
}
28+
else
29+
{
30+
Debug.Assert(false, "Can Not Find Any Suitable Integrator.");
31+
}
1932
}
2033

2134
void OnDestroy()
@@ -26,7 +39,7 @@ void OnDestroy()
2639
// Update is called once per frame
2740
void Update()
2841
{
29-
explicitEuler.StepOneFrame(scene);
42+
integrator.StepOneFrame(scene);
3043
scene.Frame();
3144
}
3245

File renamed without changes.

Assets/Scripts/Integrator.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/ExplicitEulerIntegrator.cs renamed to Assets/Scripts/Integrator/ExplicitEulerIntegrator.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
3-
using UnityEngine;
1+
using UnityEngine;
42
using Unity.Jobs;
53
using UnityEngine.Jobs;
64
using Unity.Collections;
7-
using System.Xml;
85

9-
public class ExplicitEulerIntegrator
6+
public class ExplicitEulerIntegrator: IIntegrator
107
{
118
// By Order
129
SimpleGravityJob m_GravityGradiantJob;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using UnityEngine;
2+
using Unity.Jobs;
3+
using UnityEngine.Jobs;
4+
using Unity.Collections;
5+
6+
public class SymplecticEulerIntegrator : IIntegrator
7+
{
8+
// By Order
9+
SimpleGravityJob m_GravityGradiantJob;
10+
SymplecticEulerJob m_SymplecticEulerJob;
11+
UpdateJob m_UpdateJob;
12+
13+
JobHandle m_GravityGradiantJobHandle;
14+
JobHandle m_SymplecticEulerJobHandle;
15+
JobHandle m_UpdateJobHandle;
16+
17+
public void StepOneFrame(PhysicalScene scene)
18+
{
19+
m_GravityGradiantJob = new SimpleGravityJob
20+
{
21+
gravity = scene.m_Gravity,
22+
gradiant = scene.m_gradiants,
23+
mass = scene.m_masses,
24+
fixes = scene.m_fixes,
25+
};
26+
27+
m_SymplecticEulerJob = new SymplecticEulerJob
28+
{
29+
deltaTime = Time.deltaTime,
30+
velocity = scene.m_Velocities,
31+
fixes = scene.m_fixes,
32+
position = scene.m_Positions,
33+
gradiant = scene.m_gradiants,
34+
mass = scene.m_masses,
35+
};
36+
37+
m_UpdateJob = new UpdateJob
38+
{
39+
position = scene.m_Positions,
40+
gradiant = scene.m_gradiants,
41+
};
42+
43+
m_GravityGradiantJobHandle = m_GravityGradiantJob.Schedule(scene.objectCount, 64);
44+
45+
m_SymplecticEulerJobHandle = m_SymplecticEulerJob.Schedule(scene.objectCount, 64, m_GravityGradiantJobHandle);
46+
47+
m_UpdateJobHandle = m_UpdateJob.Schedule(scene.m_TransformsAccessArray, m_SymplecticEulerJobHandle);
48+
49+
m_UpdateJobHandle.Complete();
50+
}
51+
}

Assets/Scripts/Integrator/SymplecticEulerIntegrator.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using UnityEngine;
2+
using Unity.Jobs;
3+
using Unity.Collections;
4+
5+
public struct SymplecticEulerJob : IJobParallelFor
6+
{
7+
[ReadOnly]
8+
public NativeArray<bool> fixes;
9+
[ReadOnly]
10+
public NativeArray<float> mass;
11+
[ReadOnly]
12+
public NativeArray<Vector3> gradiant;
13+
14+
public NativeArray<Vector3> position;
15+
public NativeArray<Vector3> velocity;
16+
17+
public float deltaTime;
18+
19+
public void Execute(int index)
20+
{
21+
if (fixes[index])
22+
{
23+
return;
24+
}
25+
26+
position[index] = position[index] + velocity[index] * deltaTime;
27+
28+
Vector3 force = -gradiant[index];
29+
velocity[index] = velocity[index] + deltaTime * force / mass[index];
30+
}
31+
}

Assets/Scripts/Jobs/SymplecticEulerJob.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)