forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasePhysicsDemo.cs
More file actions
166 lines (142 loc) · 6.13 KB
/
Copy pathBasePhysicsDemo.cs
File metadata and controls
166 lines (142 loc) · 6.13 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System.Collections.Generic;
using Unity.Physics;
using Unity.Physics.Extensions;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
using Collider = Unity.Physics.Collider;
using Material = UnityEngine.Material;
using Mesh = UnityEngine.Mesh;
/// <summary>
/// Helper for demos set up in C# rather than in the editor
/// </summary>
public class BasePhysicsDemo : MonoBehaviour
{
public static EntityManager EntityManager => World.Active.EntityManager;
protected Entity stepper;
public Material dynamicMaterial;
public Material staticMaterial;
public SimulationType StepType = SimulationType.UnityPhysics;
protected void init(float3 gravity)
{
// Camera control
GameObject mainCamera = GameObject.FindGameObjectWithTag("MainCamera");
CameraControl cameraControl = mainCamera.AddComponent<CameraControl>();
cameraControl.lookSpeedH = 0.6f;
cameraControl.lookSpeedV = 0.6f;
cameraControl.zoomSpeed = 1.0f;
cameraControl.dragSpeed = 4.0f;
// Create the stepper
EntityManager entityManager = EntityManager;
ComponentType[] componentTypes = {
typeof(PhysicsStep),
typeof(Unity.Physics.Authoring.PhysicsDebugDisplayData),
typeof(MousePick)
};
stepper = entityManager.CreateEntity(componentTypes);
entityManager.SetComponentData(stepper, new PhysicsStep
{
SimulationType = StepType,
Gravity = gravity,
SolverIterationCount = PhysicsStep.Default.SolverIterationCount,
ThreadCountHint = PhysicsStep.Default.ThreadCountHint
});
// Add options for visually debugging physics information
entityManager.SetComponentData(stepper, new Unity.Physics.Authoring.PhysicsDebugDisplayData { });
// Load assets
//dynamicMaterial = (Material)Resources.Load("Materials/PhysicsDynamicMaterial");
//staticMaterial = (Material)Resources.Load("Materials/PhysicsStaticMaterial");
}
protected virtual void Start()
{
init(new float3(0, -9.81f, 0));
}
//
// Object creation
//
private Entity CreateBody(float3 position, quaternion orientation, BlobAssetReference<Collider> collider,
float3 linearVelocity, float3 angularVelocity, float mass, bool isDynamic)
{
EntityManager entityManager = EntityManager;
Entity entity = entityManager.CreateEntity(new ComponentType[] { });
entityManager.AddComponentData(entity, new LocalToWorld { });
entityManager.AddComponentData(entity, new Translation { Value = position });
entityManager.AddComponentData(entity, new Rotation { Value = orientation });
var colliderComponent = new PhysicsCollider { Value = collider };
entityManager.AddComponentData(entity, colliderComponent);
Mesh mesh = new Mesh();
List<Unity.Physics.Authoring.DisplayBodyColliders.DrawComponent.DisplayResult> meshes;
unsafe { meshes = Unity.Physics.Authoring.DisplayBodyColliders.DrawComponent.BuildDebugDisplayMesh(colliderComponent.ColliderPtr); }
CombineInstance[] instances = new CombineInstance[meshes.Count];
for (int i = 0; i < meshes.Count; i++)
{
instances[i] = new CombineInstance
{
mesh = meshes[i].Mesh,
transform = Matrix4x4.TRS(meshes[i].Position, meshes[i].Orientation, meshes[i].Scale)
};
}
mesh.CombineMeshes(instances);
entityManager.AddSharedComponentData(entity, new RenderMesh
{
mesh = mesh,
material = isDynamic ? dynamicMaterial : staticMaterial
});
if (isDynamic)
{
entityManager.AddComponentData(entity, PhysicsMass.CreateDynamic(colliderComponent.MassProperties, mass));
float3 angularVelocityLocal = math.mul(math.inverse(colliderComponent.MassProperties.MassDistribution.Transform.rot), angularVelocity);
entityManager.AddComponentData(entity, new PhysicsVelocity()
{
Linear = linearVelocity,
Angular = angularVelocityLocal
});
entityManager.AddComponentData(entity, new PhysicsDamping()
{
Linear = 0.01f,
Angular = 0.05f
});
}
return entity;
}
protected Entity CreateStaticBody(float3 position, quaternion orientation, BlobAssetReference<Collider> collider)
{
return CreateBody(position, orientation, collider, float3.zero, float3.zero, 0.0f, false);
}
protected Entity CreateDynamicBody(float3 position, quaternion orientation, BlobAssetReference<Collider> collider,
float3 linearVelocity, float3 angularVelocity, float mass)
{
return CreateBody(position, orientation, collider, linearVelocity, angularVelocity, mass, true);
}
protected unsafe Entity CreateJoint(BlobAssetReference<JointData> jointData, Entity entityA, Entity entityB, bool enableCollision = false)
{
EntityManager entityManager = World.Active.EntityManager;
ComponentType[] componentTypes = new ComponentType[1];
componentTypes[0] = typeof(PhysicsJoint);
Entity jointEntity = entityManager.CreateEntity(componentTypes);
entityManager.SetComponentData(jointEntity, new PhysicsJoint
{
JointData = jointData,
EntityA = entityA,
EntityB = entityB,
EnableCollision = (enableCollision ? 1 : 0)
});
return jointEntity;
}
//
// Helper methods
//
protected void SetDebugDisplay(Unity.Physics.Authoring.PhysicsDebugDisplayData debugDisplay)
{
EntityManager.SetComponentData<Unity.Physics.Authoring.PhysicsDebugDisplayData>(stepper, debugDisplay);
}
protected RigidTransform GetBodyTransform(Entity entity)
{
EntityManager entityManager = EntityManager;
return new RigidTransform(
entityManager.GetComponentData<Rotation>(entity).Value,
entityManager.GetComponentData<Translation>(entity).Value);
}
}