-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRigidBody.cs
More file actions
60 lines (51 loc) · 1.45 KB
/
RigidBody.cs
File metadata and controls
60 lines (51 loc) · 1.45 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
57
58
59
60
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Boatanator
{
class RigidBody
{
public Verlet[] vertices;
float[,] Lengths;
public void Init(Verlet[] vs)
{
vertices = vs;
Lengths = new float[vs.Length, vs.Length];
for (int i = 0; i < vs.Length; i++)
{
for (int j = 0; j < vs.Length; j++)
{
Lengths[i, j] = Vector3.Distance(vs[i].Pos, vs[j].Pos);
}
}
}
public void Step()
{
for (int i = 0; i < vertices.Length; i++)
{
vertices[i].Step();
}
ApplyConstraints();
}
public void ApplyConstraints()
{
for (int i = 0; i < vertices.Length-1; i++)
{
for (int j = i+1; j < vertices.Length; j++)
{
ApplyConstraints(ref vertices[i].Pos, ref vertices[j].Pos, Lengths[i, j]);
}
}
}
void ApplyConstraints(ref Vector3 p1, ref Vector3 p2, float distance)
{
Vector3 dPos = p2 - p1;
float dLength = dPos.Length();
Vector3 correction = (dLength - distance) / dLength * 0.5f * dPos;
p1 += correction;
p2 -= correction;
}
}
}