forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvalidPhysicsJointDemo.cs
More file actions
71 lines (58 loc) · 3.08 KB
/
Copy pathInvalidPhysicsJointDemo.cs
File metadata and controls
71 lines (58 loc) · 3.08 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
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
public class InvalidPhysicsJointDemoScene : SceneCreationSettings {}
public class InvalidPhysicsJointDemo : SceneCreationAuthoring<InvalidPhysicsJointDemoScene> {}
public class InvalidPhyiscsJointDemoSystem : SceneCreationSystem<InvalidPhysicsJointDemoScene>
{
public override void CreateScene(InvalidPhysicsJointDemoScene sceneSettings)
{
BlobAssetReference<Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry
{
Center = float3.zero,
Orientation = quaternion.identity,
Size = new float3(0.25f),
BevelRadius = 0.0f
});
CreatedColliders.Add(collider);
// Add a dynamic body constrained to the world that will die
// Once the dynamic body is destroyed the joint will be invalid
{
// Create a dynamic body
float3 pivotWorld = new float3(-2f, 0, 0);
Entity body = CreateDynamicBody(pivotWorld, quaternion.identity, collider, float3.zero, float3.zero, 1.0f);
// create extra dynamic body to trigger Havok sync after the first one is destroyed
CreateDynamicBody(pivotWorld * 2.0f, quaternion.identity, collider, float3.zero, float3.zero, 1.0f);
// add timeout on dynamic body after 15 frames.
EntityManager.AddComponentData(body, new LifeTime { Value = 15 });
// Create the joint
float3 pivotLocal = float3.zero;
var joint = PhysicsJoint.CreateBallAndSocket(pivotLocal, pivotWorld);
var jointEntity = CreateJoint(joint, body, Entity.Null);
// add timeout on joint entity after 30 frames.
EntityManager.AddComponentData(jointEntity, new LifeTime { Value = 30 });
}
// Add two static bodies constrained together
// The joint is invalid immediately
{
// Create a body
Entity bodyA = CreateStaticBody(new float3(0, 0.0f, 0), quaternion.identity, collider);
Entity bodyB = CreateStaticBody(new float3(0, 1.0f, 0), quaternion.identity, collider);
// Create the joint
float3 pivotLocal = float3.zero;
var joint = PhysicsJoint.CreateBallAndSocket(pivotLocal, pivotLocal);
var jointEntity = CreateJoint(joint, bodyA, bodyB);
// add timeout on joint entity after 15 frames.
EntityManager.AddComponentData(jointEntity, new LifeTime { Value = 15 });
}
// Add two dynamic bodies constrained together with 0 dimension
{
// Create a body
Entity bodyA = CreateDynamicBody(new float3(0, 5.0f, 0), quaternion.identity, collider, float3.zero, float3.zero, 1.0f);
Entity bodyB = CreateDynamicBody(new float3(0, 6.0f, 0), quaternion.identity, collider, float3.zero, float3.zero, 1.0f);
// Create the joint
var joint = PhysicsJoint.CreateLimitedDOF(RigidTransform.identity, new bool3(false), new bool3(false));
CreateJoint(joint, bodyA, bodyB);
}
}
}