forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftJointDemo.cs
More file actions
141 lines (124 loc) · 6.5 KB
/
Copy pathSoftJointDemo.cs
File metadata and controls
141 lines (124 loc) · 6.5 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using static Unity.Physics.Math;
public class SoftJointDemoScene : SceneCreationSettings {}
public class SoftJointDemo : SceneCreationAuthoring<SoftJointDemoScene> {}
public class SoftJointDemoCreationSystem : SceneCreationSystem<SoftJointDemoScene>
{
public override void CreateScene(SoftJointDemoScene sceneSettings)
{
// Make soft ball and sockets
{
BlobAssetReference<Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry
{
Center = float3.zero,
Orientation = quaternion.identity,
Size = new float3(0.2f, 0.2f, 0.2f),
BevelRadius = 0.0f
});
CreatedColliders.Add(collider);
// Make joints with different spring frequency. The leftmost joint should oscillate at 0.5hz, the next at 1hz, the next at 1.5hz, etc.
for (int i = 0; i < 10; i++)
{
// Create a body
float3 position = new float3((i - 4.5f) * 1.0f, 0, 0);
float3 velocity = new float3(0, -10.0f, 0);
Entity body = CreateDynamicBody(
position, quaternion.identity, collider, velocity, float3.zero, 1.0f);
// Create the ball and socket joint
float3 pivotLocal = float3.zero;
float3 pivotInWorld = math.transform(GetBodyTransform(body), pivotLocal);
var jointData = PhysicsJoint.CreateBallAndSocket(pivotLocal, pivotInWorld);
var constraints = jointData.GetConstraints();
var constraint = constraints[0];
// Choose a small damping value instead of 0 to improve stability of the joints
constraint.SpringDamping = 0.05f;
constraint.SpringFrequency = 0.5f * (float)(i + 1);
constraints[0] = constraint;
jointData.SetConstraints(constraints);
CreateJoint(jointData, body, Entity.Null);
}
}
//Make soft limited hinges
{
BlobAssetReference<Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry
{
Center = float3.zero,
Orientation = quaternion.identity,
Size = new float3(0.4f, 0.1f, 0.6f),
BevelRadius = 0.0f
});
CreatedColliders.Add(collider);
// First row has soft limit with hard hinge + pivot, second row has everything soft
for (int j = 0; j < 2; j++)
{
for (int i = 0; i < 10; i++)
{
// Create a body
float3 position = new float3((i - 4.5f) * 1.0f, 0, (j + 1) * 3.0f);
float3 velocity = new float3(0, -10.0f, 0);
float3 angularVelocity = new float3(0, 0, -10.0f);
Entity body = CreateDynamicBody(
position, quaternion.identity, collider, velocity, angularVelocity, 1.0f);
// Create the limited hinge joint
float3 pivotLocal = new float3(0, 0, 0);
float3 pivotInWorld = math.transform(GetBodyTransform(body), pivotLocal);
float3 axisLocal = new float3(0, 0, 1);
float3 axisInWorld = axisLocal;
float3 perpendicularLocal = new float3(0, 1, 0);
float3 perpendicularInWorld = perpendicularLocal;
var frameLocal = new BodyFrame { Axis = axisLocal, PerpendicularAxis = perpendicularLocal, Position = pivotLocal };
var frameWorld = new BodyFrame { Axis = axisInWorld, PerpendicularAxis = perpendicularInWorld, Position = pivotInWorld };
var jointData = PhysicsJoint.CreateLimitedHinge(frameLocal, frameWorld, default);
// First constraint is the limit, next two are the hinge and pivot
var constraints = jointData.GetConstraints();
for (int k = 0; k < 1 + 2 * j; k++)
{
var constraint = constraints[k];
// Choose a small damping value instead of 0 to improve stability of the joints
constraint.SpringDamping = 0.05f;
constraint.SpringFrequency = 0.5f * (i + 1);
constraints[k] = constraint;
}
jointData.SetConstraints(constraints);
CreateJoint(jointData, body, Entity.Null);
}
}
}
// Make a soft prismatic
{
BlobAssetReference<Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry
{
Center = float3.zero,
Orientation = quaternion.identity,
Size = new float3(0.2f, 0.2f, 0.2f),
BevelRadius = 0.0f
});
CreatedColliders.Add(collider);
// Create a body
float3 position = new float3(0, 0, 9.0f);
float3 velocity = new float3(50.0f, 0, 0);
Entity body = CreateDynamicBody(
position, quaternion.identity, collider, velocity, float3.zero, 1.0f);
// Create the prismatic joint
float3 pivotLocal = float3.zero;
float3 pivotInWorld = math.transform(GetBodyTransform(body), pivotLocal);
float3 axisLocal = new float3(1, 0, 0);
float3 axisInWorld = axisLocal;
float3 perpendicularLocal = new float3(0, 1, 0);
float3 perpendicularInWorld = perpendicularLocal;
var localFrame = new BodyFrame { Axis = axisLocal, PerpendicularAxis = perpendicularLocal, Position = pivotLocal };
var worldFrame = new BodyFrame { Axis = axisInWorld, PerpendicularAxis = perpendicularInWorld, Position = pivotInWorld };
var jointData = PhysicsJoint.CreatePrismatic(localFrame, worldFrame, new FloatRange(-2f, 2f));
var constraints = jointData.GetConstraints();
var constraint = constraints[0];
// Choose a small damping value instead of 0 to improve stability of the joints
constraint.SpringDamping = 0.05f;
constraint.SpringFrequency = 5.0f;
constraints[0] = constraint;
jointData.SetConstraints(constraints);
CreateJoint(jointData, body, Entity.Null);
}
}
}