Skip to content

Commit 270b0f0

Browse files
Merge pull request #2 from sandalconsumer/spring-mass-modularity
System Refactor for ease of understanding and use.
2 parents f61fd0d + 3805361 commit 270b0f0

File tree

12 files changed

+252
-477
lines changed

12 files changed

+252
-477
lines changed

Assets/Scenes/SampleScene.unity

Lines changed: 53 additions & 352 deletions
Large diffs are not rendered by default.

Assets/Scripts/Physics/Body.cs

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using UnityEngine;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using Color = UnityEngine.Color;
5+
6+
[RequireComponent(typeof(SimulationDataCompiler))]
7+
public class PhysicsController : MonoBehaviour
8+
{
9+
private SimulationEndData simulationEndData;
10+
11+
public bool visualizeSprings = true;
12+
public bool recordData = true;
13+
14+
public List<Pointmass> masses = new List<Pointmass>();
15+
public List<Spring> springs = new List<Spring>();
16+
17+
private float timestep;
18+
private float totalTime;
19+
20+
void Start()
21+
{
22+
simulationEndData = GetComponent<SimulationDataCompiler>().simulationEndData;
23+
timestep = Time.fixedDeltaTime;
24+
25+
foreach (Spring spring in springs)
26+
{
27+
int rootLinkedIndex = spring.rootLinkedIndex;
28+
int endLinkedIndex = spring.endLinkedIndex;
29+
30+
if (spring.rootLinkedIndex < masses.Count)
31+
{
32+
spring.rootLinked = masses[rootLinkedIndex];
33+
}
34+
35+
if (spring.endLinkedIndex < masses.Count)
36+
{
37+
spring.endLinked = masses[endLinkedIndex];
38+
}
39+
}
40+
}
41+
42+
void FixedUpdate()
43+
{
44+
foreach (Pointmass mass in masses)
45+
{
46+
mass.VerletHalfstep(timestep);
47+
}
48+
49+
foreach (Spring spring in springs)
50+
{
51+
Vector3 springVector = spring.endLinked.position - spring.rootLinked.position;
52+
float currentLength = springVector.magnitude;
53+
float extension = currentLength - spring.restLength;
54+
55+
Vector3 springDirection = springVector.normalized;
56+
spring.restorativeForce = -spring.springConstant * extension * springDirection;
57+
58+
spring.rootLinked.leadForce -= spring.restorativeForce;
59+
spring.endLinked.leadForce += spring.restorativeForce;
60+
61+
if(visualizeSprings) {Debug.DrawLine(spring.rootLinked.position, spring.endLinked.position, Color.yellow);}
62+
if(recordData) {simulationEndData.stretches.Add(extension);}
63+
}
64+
65+
foreach (Pointmass mass in masses)
66+
{
67+
mass.VerletFinalstep(timestep);
68+
69+
mass.leadForce = Vector3.zero;
70+
if(recordData) {simulationEndData.positions.Add(mass.position);}
71+
}
72+
73+
totalTime += timestep;
74+
if(recordData) {simulationEndData.timeStamps.Add(totalTime);}
75+
}
76+
}

Assets/Scripts/Physics/PhysicsController.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using UnityEngine;
2+
using System.Collections.Generic;
3+
using Unity.Mathematics;
4+
5+
[System.Serializable]
6+
public class Spring
7+
{
8+
public float springConstant = 100;
9+
10+
public float restLength;
11+
12+
public int rootLinkedIndex;
13+
public int endLinkedIndex;
14+
15+
[HideInInspector]public Pointmass rootLinked;
16+
[HideInInspector]public Pointmass endLinked;
17+
18+
[HideInInspector]public Vector3 restorativeForce = Vector3.zero;
19+
}
20+
21+
[System.Serializable]
22+
public class Pointmass
23+
{
24+
public Vector3 position;
25+
Vector3 acceleration;
26+
Vector3 velocity;
27+
28+
public float mass = 100;
29+
[HideInInspector]public Vector3 leadForce;
30+
31+
public void VerletHalfstep(float deltaTime)
32+
{
33+
position += velocity * deltaTime + acceleration * (0.5f * deltaTime * deltaTime);
34+
}
35+
36+
public void VerletFinalstep(float deltaTime)
37+
{
38+
Vector3 nA = leadForce / mass;
39+
velocity += (acceleration + nA) * (0.5f * deltaTime);
40+
acceleration = leadForce / mass;
41+
}
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
[CustomEditor(typeof(Pointmass))]
5+
public class PointmassEditor : Editor
6+
{
7+
private SerializedProperty mass;
8+
private SerializedProperty position;
9+
10+
void OnEnable()
11+
{
12+
mass = serializedObject.FindProperty("mass");
13+
position = serializedObject.FindProperty("position");
14+
}
15+
16+
public override void OnInspectorGUI()
17+
{
18+
serializedObject.Update();
19+
20+
EditorGUILayout.LabelField("Pointmass Properties", EditorStyles.boldLabel);
21+
EditorGUILayout.PropertyField(mass);
22+
EditorGUILayout.PropertyField(position);
23+
24+
serializedObject.ApplyModifiedProperties();
25+
}
26+
}

Assets/Scripts/Utilities/PointmassEditor.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Utilities/SimulationDataCompiler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ public class SimulationEndData
88
public List<float> stretches = new List<float>();
99
}
1010

11+
[RequireComponent(typeof(PhysicsController))]
1112
public class SimulationDataCompiler : MonoBehaviour // to be used as a singleton
1213
{
1314
public SimulationEndData simulationEndData = new SimulationEndData();
1415

1516
public float plotScale = 1f;
1617
public Vector3 plotOrigin = new Vector3(10, 0, 0);
1718

18-
void Update()
19+
void FixedUpdate()
1920
{
2021
for (int i = 1; i < simulationEndData.timeStamps.Count; i++)
2122
{
@@ -24,4 +25,6 @@ void Update()
2425
Debug.DrawLine(p1, p2, Color.cyan);
2526
}
2627
}
28+
29+
2730
}

Assets/Scripts/Utilities/SpringEditor.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
[CustomEditor(typeof(Spring))]
55
public class SpringEditor : Editor
66
{
7-
private SerializedProperty rootPosition;
7+
private SerializedProperty rootLinked;
8+
private SerializedProperty endLinked;
89
private SerializedProperty springConstant;
910
private SerializedProperty restLength;
1011

1112
void OnEnable()
1213
{
13-
rootPosition = serializedObject.FindProperty("rootPosition");
14+
rootLinked = serializedObject.FindProperty("rootLinkedIndex");
15+
endLinked = serializedObject.FindProperty("endLinkedIndex");
1416
springConstant = serializedObject.FindProperty("springConstant");
1517
restLength = serializedObject.FindProperty("restLength");
1618
}
@@ -20,7 +22,8 @@ public override void OnInspectorGUI()
2022
serializedObject.Update();
2123

2224
EditorGUILayout.LabelField("Spring Properties", EditorStyles.boldLabel);
23-
EditorGUILayout.PropertyField(rootPosition);
25+
EditorGUILayout.PropertyField(rootLinked);
26+
EditorGUILayout.PropertyField(endLinked);
2427
EditorGUILayout.PropertyField(springConstant);
2528
EditorGUILayout.PropertyField(restLength);
2629

0 commit comments

Comments
 (0)