Skip to content

Commit 571b2de

Browse files
Merge pull request #4 from sandalconsumer/springmass-plane-generation
Springmass plane generation
2 parents b3d286b + 4565aed commit 571b2de

File tree

9 files changed

+171
-6
lines changed

9 files changed

+171
-6
lines changed

Assets/Scenes/SampleScene.unity

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ GameObject:
177177
serializedVersion: 6
178178
m_Component:
179179
- component: {fileID: 1678634227}
180+
- component: {fileID: 1678634229}
180181
- component: {fileID: 1678634226}
181182
- component: {fileID: 1678634228}
182183
m_Layer: 0
@@ -228,7 +229,7 @@ MonoBehaviour:
228229
m_Name:
229230
m_EditorClassIdentifier:
230231
visualizeSprings: 1
231-
recordData: 1
232+
recordData: 0
232233
masses:
233234
- position: {x: 5, y: 0, z: 0}
234235
acceleration: {x: 0, y: 0, z: 0}
@@ -240,12 +241,35 @@ MonoBehaviour:
240241
velocity: {x: 0, y: 0, z: 0}
241242
mass: 1
242243
leadForce: {x: 0, y: 0, z: 0}
244+
- position: {x: 7.5, y: 0, z: 3}
245+
acceleration: {x: 0, y: 0, z: 0}
246+
velocity: {x: 0, y: 0, z: 0}
247+
mass: 1
248+
leadForce: {x: 0, y: 0, z: 0}
243249
springs:
244250
- springConstant: 1
245-
restLength: 6
251+
restLength: 5
246252
rootLinkedIndex: 0
247253
endLinkedIndex: 1
248-
dampingCoefficient: 0
254+
dampingCoefficient: 0.1
255+
rootLinked:
256+
position: {x: 0, y: 0, z: 0}
257+
acceleration: {x: 0, y: 0, z: 0}
258+
velocity: {x: 0, y: 0, z: 0}
259+
mass: 0
260+
leadForce: {x: 0, y: 0, z: 0}
261+
endLinked:
262+
position: {x: 0, y: 0, z: 0}
263+
acceleration: {x: 0, y: 0, z: 0}
264+
velocity: {x: 0, y: 0, z: 0}
265+
mass: 0
266+
leadForce: {x: 0, y: 0, z: 0}
267+
restorativeForce: {x: 0, y: 0, z: 0}
268+
- springConstant: 1
269+
restLength: 3.905125
270+
rootLinkedIndex: 1
271+
endLinkedIndex: 2
272+
dampingCoefficient: 0.1
249273
rootLinked:
250274
position: {x: 0, y: 0, z: 0}
251275
acceleration: {x: 0, y: 0, z: 0}
@@ -259,6 +283,39 @@ MonoBehaviour:
259283
mass: 0
260284
leadForce: {x: 0, y: 0, z: 0}
261285
restorativeForce: {x: 0, y: 0, z: 0}
286+
- springConstant: 1
287+
restLength: 5
288+
rootLinkedIndex: 0
289+
endLinkedIndex: 2
290+
dampingCoefficient: 0.1
291+
rootLinked:
292+
position: {x: 0, y: 0, z: 0}
293+
acceleration: {x: 0, y: 0, z: 0}
294+
velocity: {x: 0, y: 0, z: 0}
295+
mass: 0
296+
leadForce: {x: 0, y: 0, z: 0}
297+
endLinked:
298+
position: {x: 0, y: 0, z: 0}
299+
acceleration: {x: 0, y: 0, z: 0}
300+
velocity: {x: 0, y: 0, z: 0}
301+
mass: 0
302+
leadForce: {x: 0, y: 0, z: 0}
303+
restorativeForce: {x: 0, y: 0, z: 0}
304+
--- !u!114 &1678634229
305+
MonoBehaviour:
306+
m_ObjectHideFlags: 0
307+
m_CorrespondingSourceObject: {fileID: 0}
308+
m_PrefabInstance: {fileID: 0}
309+
m_PrefabAsset: {fileID: 0}
310+
m_GameObject: {fileID: 1678634225}
311+
m_Enabled: 1
312+
m_EditorHideFlags: 0
313+
m_Script: {fileID: 11500000, guid: 3e27b847016d0be4a9b8690e2cd23087, type: 3}
314+
m_Name:
315+
m_EditorClassIdentifier:
316+
detail: 10
317+
meshSize: 1
318+
meshDamping: 0.5
262319
--- !u!1660057539 &9223372036854775807
263320
SceneRoots:
264321
m_ObjectHideFlags: 0
File renamed without changes.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using UnityEngine;
2+
using System.Collections.Generic;
3+
4+
[RequireComponent(typeof(PhysicsController))]
5+
public class ClothmeshGeneration : MonoBehaviour
6+
{
7+
public int detail = 10; //10 means the mesh will be a 10x10 square grid (200 triangles)
8+
public float meshSize = 0.1f;
9+
public float meshDamping = 0.5f;
10+
11+
private List<Spring> edges;
12+
private List<Pointmass> vertices;
13+
14+
private PhysicsController physicsController;
15+
16+
void Start()
17+
{
18+
physicsController = GetComponent<PhysicsController>();
19+
20+
for (int i = 0; i < detail; i++)
21+
{
22+
for (int j = 0; j < detail; j++)
23+
{
24+
Pointmass currentPointmass = new Pointmass();
25+
26+
vertices.Add(currentPointmass);
27+
currentPointmass.position = new Vector3(i * meshSize, 0, j * meshSize);
28+
29+
//check neighbors and make springs (right and down are not used because they will always not be created yet. j - 1 will always exist (other than index 0).
30+
31+
if (j > 0)
32+
{
33+
Pointmass ln = vertices[i * detail + j - 1];
34+
35+
edges.Add(new Spring
36+
{
37+
rootLinkedIndex = i * detail + j,
38+
endLinkedIndex = i * detail + j - 1,
39+
restLength = (currentPointmass.position - ln.position).magnitude,
40+
dampingCoefficient = meshDamping,
41+
springConstant = 1f,
42+
});
43+
}
44+
45+
if (i > 0)
46+
{
47+
Pointmass un = vertices[i * detail + j - detail];
48+
49+
edges.Add(new Spring
50+
{
51+
rootLinkedIndex = i * detail + j,
52+
endLinkedIndex = i * detail + j - detail,
53+
restLength = (currentPointmass.position - un.position).magnitude,
54+
dampingCoefficient = meshDamping,
55+
springConstant = 1f,
56+
});
57+
}
58+
}
59+
}
60+
61+
for (int i = 0; i < detail; i++)
62+
{
63+
for (int j = 0; j < detail; j++)
64+
{
65+
Pointmass currentPointmass = vertices[i * detail + j];
66+
int currentIndex = i * detail + j;
67+
68+
if (i > 0 && j > 0)
69+
{
70+
int topLeftNeighborIndex = (i - 1) * detail + (j - 1);
71+
Pointmass topLeftNeighbor = vertices[topLeftNeighborIndex];
72+
73+
edges.Add(new Spring
74+
{
75+
rootLinkedIndex = currentIndex,
76+
endLinkedIndex = topLeftNeighborIndex,
77+
restLength = (currentPointmass.position - topLeftNeighbor.position).magnitude,
78+
dampingCoefficient = meshDamping,
79+
springConstant = 1f,
80+
});
81+
}
82+
83+
if (i > 0 && j < detail - 1)
84+
{
85+
int topRightNeighborIndex = (i - 1) * detail + (j + 1);
86+
Pointmass topRightNeighbor = vertices[topRightNeighborIndex];
87+
88+
edges.Add(new Spring
89+
{
90+
rootLinkedIndex = currentIndex,
91+
endLinkedIndex = topRightNeighborIndex,
92+
restLength = (currentPointmass.position - topRightNeighbor.position).magnitude,
93+
dampingCoefficient = meshDamping,
94+
springConstant = 1f,
95+
});
96+
}
97+
}
98+
}
99+
100+
physicsController.masses.Clear();
101+
physicsController.springs.Clear();
102+
103+
physicsController.masses = vertices;
104+
physicsController.springs = edges;
105+
}
106+
}

Assets/Scripts/Core/ClothmeshGeneration.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/Physics/PhysicsObjects.cs renamed to Assets/Scripts/Core/PhysicsObjects.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Pointmass
2727
[HideInInspector] public Vector3 acceleration;
2828
[HideInInspector] public Vector3 velocity;
2929

30-
public float mass = 100;
30+
public float mass = 0.05f;
3131
[HideInInspector]public Vector3 leadForce;
3232

3333
public void VerletHalfstep(float deltaTime)

ProjectSettings/TimeManager.asset

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ TimeManager:
1010
m_Denominator: 1
1111
m_Numerator: 141120000
1212
Maximum Allowed Timestep: 0.33333334
13-
m_TimeScale: 2
14-
Maximum Particle Timestep: 0.03
13+
m_TimeScale: 1
14+
Maximum Particle Timestep: 0.07

0 commit comments

Comments
 (0)