-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathSpringNode3D.cs
More file actions
56 lines (38 loc) · 1.01 KB
/
SpringNode3D.cs
File metadata and controls
56 lines (38 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpringNode3D : SpringNode
{
//The state of the node
public Vector3 pos;
public Vector3 vel;
//The total force on this node from the springs attached to it
public Vector3 force;
//Gravity
private readonly Vector3 g = new(0f, -9.81f, 0f);
public SpringNode3D(Vector3 pos, bool isFixed = false) : base (isFixed)
{
this.pos = pos;
}
public void UpdateNodeState(float dt)
{
if (isFixed)
{
return;
}
float m = 1f;
//Add gravity
Vector3 F_gravity = m * g;
force += F_gravity;
//Calculate the acceleration on this node
//F = m*a -> a = F/m
Vector3 a = force / m;
//Move the simulation forward one step
this.vel += dt * a;
this.pos += dt * this.vel;
//Add some damping
//this.vel *= 0.99f;
//Reset F
force = Vector3.zero;
}
}