protected override void Start() { base.Start(); // Enable the joint viewer SetDebugDisplay(new Unity.Physics.Authoring.PhysicsDebugDisplayData { DrawJoints = 1 }); BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(float3.zero, Quaternion.identity, new float3(0.25f), 0.0f); quaternion orientationA = quaternion.identity; bool identityA = true; if (!identityA) { orientationA = quaternion.AxisAngle(new float3(0, 1, 0), (float)math.PI * 3.0f / 2.0f); } quaternion orientationB = quaternion.identity; bool identityB = true; if (!identityB) { orientationB = quaternion.AxisAngle(math.normalize(new float3(1)), (float)math.PI / 4.0f); } // Make some joints with fixed position, limited 3D angle for (int i = 0; i < 10; i++) { // Create a body Entity body = CreateDynamicBody( new float3((i - 4.5f) * 1.0f, 0, 0), quaternion.identity, collider, float3.zero, float3.zero, 1.0f); // Create the ragdoll joint float3 pivotLocal = float3.zero; float3 pivotInWorld = math.transform(GetBodyTransform(body), pivotLocal); quaternion worldFromLocal = Quaternion.AngleAxis((i - 4.5f) * 20.0f, new float3(0, 0, 1)); BlobAssetReference <JointData> jointData = JointData.Create( new MTransform(orientationA, pivotLocal), new MTransform(orientationB, pivotInWorld), new Constraint[] { Constraint.BallAndSocket(), new Constraint { ConstrainedAxes = new bool3(true, true, true), Type = ConstraintType.Angular, Min = math.max(i - 5, 0) * 0.1f, Max = i * 0.1f, SpringDamping = Constraint.DefaultSpringDamping, SpringFrequency = Constraint.DefaultSpringFrequency } }); CreateJoint(jointData, body, Entity.Null); } }
protected unsafe override void Start() { base.init(); // static body Entity staticEntity; { BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry { Center = float3.zero, Orientation = quaternion.identity, Size = new float3(1.0f), BevelRadius = 0.01f }); staticEntity = CreateStaticBody(float3.zero, quaternion.identity, collider); } // dynamic body Entity dynamicEntity; { BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = new float3(-0.5f, 0, 0), Vertex1 = new float3(0.5f, 0, 0), Radius = 0.05f }); float3 angularVelocity = new float3(0, 1.0f, 0); dynamicEntity = CreateDynamicBody(new float3(2, 0, 0), quaternion.identity, collider, float3.zero, angularVelocity, 1.0f); } // 1D angular limit { var limit = math.radians(45.0f); var worldFromStatic = GetBodyTransform(staticEntity); var worldFromDynamic = GetBodyTransform(dynamicEntity); var StaticFromDynamic = math.mul(math.inverse(worldFromStatic), worldFromDynamic); float3 pivotFromDynamic = new float3(0, 0, 0); float3 pivotFromStatic = math.transform(StaticFromDynamic, pivotFromDynamic); var jointData = new PhysicsJoint { BodyAFromJoint = new RigidTransform(quaternion.identity, pivotFromDynamic), BodyBFromJoint = new RigidTransform(quaternion.identity, pivotFromStatic) }; jointData.SetConstraints(new FixedList128 <Constraint> { new Constraint { ConstrainedAxes = new bool3(true, false, false), Type = ConstraintType.Angular, Min = -limit, Max = limit, SpringFrequency = Constraint.DefaultSpringFrequency, SpringDamping = Constraint.DefaultSpringDamping, }, Constraint.BallAndSocket(), }); CreateJoint(jointData, dynamicEntity, staticEntity); } }
public override void CreateScene(FixedAngleGridScene 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); quaternion orientationA = quaternion.identity; bool identityA = true; if (!identityA) { orientationA = quaternion.AxisAngle(new float3(0, 1, 0), (float)math.PI * 3.0f / 2.0f); } quaternion orientationB = quaternion.identity; bool identityB = true; if (!identityB) { orientationB = quaternion.AxisAngle(math.normalize(new float3(1)), (float)math.PI / 4.0f); } // Make some joints with fixed position, limited 3D angle for (int i = 0; i < 10; i++) { // Create a body Entity body = CreateDynamicBody( new float3((i - 4.5f) * 1.0f, 0, 0), quaternion.identity, collider, float3.zero, float3.zero, 1.0f); // Create the ragdoll joint float3 pivotLocal = float3.zero; float3 pivotInWorld = math.transform(GetBodyTransform(body), pivotLocal); var jointData = new PhysicsJoint { BodyAFromJoint = new RigidTransform(orientationA, pivotLocal), BodyBFromJoint = new RigidTransform(orientationB, pivotInWorld) }; jointData.SetConstraints(new FixedList128Bytes <Constraint> { Length = 2, [0] = Constraint.BallAndSocket(), [1] = new Constraint { ConstrainedAxes = new bool3(true, true, true), Type = ConstraintType.Angular, Min = math.max(i - 5, 0) * 0.1f, Max = i * 0.1f, SpringDamping = Constraint.DefaultSpringDamping, SpringFrequency = Constraint.DefaultSpringFrequency } }); CreateJoint(jointData, body, Entity.Null); } }