Skip to content

Commit d479e4e

Browse files
Merge pull request #3 from sandalconsumer/spring-damping
Spring damping
2 parents ca96347 + 0c94f21 commit d479e4e

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

Assets/Scenes/SampleScene.unity

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,31 @@ MonoBehaviour:
231231
recordData: 1
232232
masses:
233233
- position: {x: 5, y: 0, z: 0}
234+
acceleration: {x: 0, y: 0, z: 0}
235+
velocity: {x: 0, y: 0, z: 0}
234236
mass: 1
235237
leadForce: {x: 0, y: 0, z: 0}
236238
- position: {x: 10, y: 0, z: 0}
239+
acceleration: {x: 0, y: 0, z: 0}
240+
velocity: {x: 0, y: 0, z: 0}
237241
mass: 1
238242
leadForce: {x: 0, y: 0, z: 0}
239243
springs:
240244
- springConstant: 1
241245
restLength: 6
242246
rootLinkedIndex: 0
243247
endLinkedIndex: 1
248+
dampingCoefficient: 0
244249
rootLinked:
245250
position: {x: 0, y: 0, z: 0}
251+
acceleration: {x: 0, y: 0, z: 0}
252+
velocity: {x: 0, y: 0, z: 0}
246253
mass: 0
247254
leadForce: {x: 0, y: 0, z: 0}
248255
endLinked:
249256
position: {x: 0, y: 0, z: 0}
257+
acceleration: {x: 0, y: 0, z: 0}
258+
velocity: {x: 0, y: 0, z: 0}
250259
mass: 0
251260
leadForce: {x: 0, y: 0, z: 0}
252261
restorativeForce: {x: 0, y: 0, z: 0}

Assets/Scripts/Physics/PhysicsController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ void FixedUpdate()
5757

5858
spring.rootLinked.leadForce -= spring.restorativeForce;
5959
spring.endLinked.leadForce += spring.restorativeForce;
60+
61+
Vector3 relativeVelocity = spring.rootLinked.velocity - spring.endLinked.velocity;
62+
float velocityOnSpringDirection = Vector3.Dot(relativeVelocity, springDirection);
63+
Vector3 dampingForce = -spring.dampingCoefficient * velocityOnSpringDirection * springDirection;
64+
65+
spring.rootLinked.leadForce += dampingForce;
66+
spring.endLinked.leadForce -= dampingForce;
6067

6168
if(visualizeSprings) {Debug.DrawLine(spring.rootLinked.position, spring.endLinked.position, Color.yellow);}
6269
if(recordData) {simulationEndData.stretches.Add(extension);}

Assets/Scripts/Physics/PhysicsObjects.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class Spring
1212
public int rootLinkedIndex;
1313
public int endLinkedIndex;
1414

15+
public float dampingCoefficient;
16+
1517
[HideInInspector]public Pointmass rootLinked;
1618
[HideInInspector]public Pointmass endLinked;
1719

@@ -22,8 +24,8 @@ public class Spring
2224
public class Pointmass
2325
{
2426
public Vector3 position;
25-
Vector3 acceleration;
26-
Vector3 velocity;
27+
[HideInInspector] public Vector3 acceleration;
28+
[HideInInspector] public Vector3 velocity;
2729

2830
public float mass = 100;
2931
[HideInInspector]public Vector3 leadForce;

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Harmonic Motion in Unity.
55
- This project is still in its **very** early phase. I plan to make this project into a cloth engine to be used in games and for industrial purposes.
66

77
## Planned Features
8-
- Spring Damping
98
- Higher-Order ODE Integrator (RK4) For more accurate Velocity, Position, and Acceleration calculations with more computation
109
- Lower-Order ODE Integrator (Euler) for less computation but less accuracy
1110
- Collisions with geometric meshes
@@ -55,6 +54,7 @@ Every element in springs is an individual instance of class **Spring**:
5554
- Rest Length is the length of the spring `x` in Hooke's law `Fs = -kx` where if x = 0 there is no restorative force `Fs = 0`.
5655
- Root linked index is the pointmass that is linked to the spring from its root.
5756
- End linked index is the pointmass that is linked to the spring from its end.
57+
- Damping Coefficient determines the amount of damping on the spring. 0 means no damping (perfect oscillation).
5858

5959
Note: If either of the "linked indices" do not exist, the spring is considered to come from the origin.
6060

0 commit comments

Comments
 (0)