public static BlobAssetReference <JointData> CreateLimitDOFJoint( RigidTransform offset, bool3 linearLocks, bool3 angularLocks) { var constraintCount = (math.any(linearLocks) ? 1 : 0) + (math.any(angularLocks) ? 1 : 0); var constraints = new NativeArray <Constraint>(constraintCount, Allocator.Temp); int index = 0; if (math.any(linearLocks)) { constraints[index++] = new Constraint { ConstrainedAxes = linearLocks, Type = ConstraintType.Linear, Min = 0, Max = 0, SpringFrequency = Constraint.DefaultSpringFrequency, SpringDamping = Constraint.DefaultSpringDamping }; } if (math.any(angularLocks)) { constraints[index++] = new Constraint { ConstrainedAxes = angularLocks, Type = ConstraintType.Angular, Min = 0, Max = 0, SpringFrequency = Constraint.DefaultSpringFrequency, SpringDamping = Constraint.DefaultSpringDamping }; } return(JointData.Create(RigidTransform.identity, offset, constraints)); }
void ConvertSpringJoint(LegacySpring joint) { var constraints = new NativeList <Constraint>(Allocator.Temp); constraints.Add(new Constraint { ConstrainedAxes = new bool3(true), Type = ConstraintType.Linear, Min = joint.minDistance, Max = joint.maxDistance, SpringFrequency = 1f, // ? SpringDamping = 0.1f // ? }); var jointFrameA = JointFrame.Identity; jointFrameA.Position = joint.anchor; var connectedEntity = GetPrimaryEntity(joint.connectedBody); var isConnectedBodyConverted = joint.connectedBody == null || connectedEntity != Entity.Null; RigidTransform bFromBSource = isConnectedBodyConverted ? RigidTransform.identity : Math.DecomposeRigidBodyTransform(joint.connectedBody.transform.localToWorldMatrix); var jointFrameB = JointFrame.Identity; jointFrameB.Position = math.mul(bFromBSource, new float4(joint.connectedAnchor, 1f)).xyz; var jointData = JointData.Create(jointFrameA, jointFrameB, constraints); CreateJointEntity(joint.gameObject, jointData, GetPrimaryEntity(joint.gameObject), joint.connectedBody == null ? Entity.Null : connectedEntity, joint.enableCollision); constraints.Dispose(); }
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 override unsafe void Start() { init(float3.zero); // Enable the joint viewer SetDebugDisplay(new Unity.Physics.Authoring.PhysicsDebugDisplayData { DrawJoints = 1 }); BlobAssetReference <Collider> collider = BoxCollider.Create(new BoxGeometry { Center = float3.zero, Orientation = quaternion.identity, Size = new float3(0.2f, 0.2f, 0.2f), BevelRadius = 0.0f }); // Make some 1d angular limit joints const int size = 6; const float speed = 1.0f; int iDbg = 0; int jDbg = 3; for (int i = 0; i < size; i++) { quaternion q1 = quaternion.AxisAngle(new float3(1, 0, 0), (float)math.PI * 2.0f * i / size); for (int j = 0; j < size; j++) { if (iDbg >= 0 && jDbg >= 0 && (i != iDbg || j != jDbg)) { continue; } // Choose the limited axis quaternion q2 = quaternion.AxisAngle(math.mul(q1, new float3(0, 1, 0)), (float)math.PI * ((float)j / size)); // Create a body with some angular velocity about the axis float3 pos = new float3(i - (size - 1) / 2.0f, 0, j - (size - 1) / 2.0f); Entity body = CreateDynamicBody(pos, quaternion.identity, collider, float3.zero, math.mul(q2, new float3(speed, 0, 0)), 1.0f); // Create a 1D angular limit about the axis float3x3 rotationB = float3x3.identity; float3x3 rotationA = math.mul(new float3x3(q2), rotationB); MTransform transformA = new MTransform(rotationA, new float3(0, 0, 0)); MTransform transformB = new MTransform(rotationB, pos); BlobAssetReference <JointData> jointData = JointData.Create( transformA, transformB, new[] { Constraint.Twist(0, -(float)math.PI / 4.0f, (float)math.PI / 4.0f) } ); CreateJoint(jointData, body, Entity.Null); } } }
public void JointDataCreateTest() { var aFromJoint = MTransform.Identity; var bFromJoint = MTransform.Identity; var constraints = new Constraint[0]; var jointDataRef = JointData.Create(aFromJoint, bFromJoint, constraints); var jointData = jointDataRef.Value; Assert.AreEqual(MTransform.Identity, jointData.AFromJoint); Assert.AreEqual(MTransform.Identity, jointData.BFromJoint); Assert.AreEqual(1, jointData.Version); }
public static BlobAssetReference <JointData> CreateLimitDOFJoint( RigidTransform offset, bool3 linearLocks, bool3 angularLocks) { var constraintCount = (math.any(linearLocks) ? 1 : 0) + (math.any(angularLocks) ? 1 : 0); Constraint[] constraints = new Constraint[constraintCount]; int index = 0; if (math.any(linearLocks)) { constraints[index++] = new Constraint { ConstrainedAxes = linearLocks, Type = ConstraintType.Linear, Min = 0, Max = 0, SpringFrequency = Constraint.DefaultSpringFrequency, SpringDamping = Constraint.DefaultSpringDamping }; } if (math.any(angularLocks)) { constraints[index++] = new Constraint { ConstrainedAxes = angularLocks, Type = ConstraintType.Angular, Min = 0, Max = 0, SpringFrequency = Constraint.DefaultSpringFrequency, SpringDamping = Constraint.DefaultSpringDamping }; } return(JointData.Create( new Math.MTransform(float3x3.identity, float3.zero), new Math.MTransform(offset), constraints )); }
private void CreatePlayer() { // Instantiate a prefab entity var prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(playerPrefab, GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, assetStore)); var player = entityManager.Instantiate(prefab); entityManager.SetName(player, "Player"); // Trying to create a limited joint to limit rotation along certain axes // Creating Constraints var angularConstraint = new Constraint { ConstrainedAxes = new bool3(true, false, true), Type = ConstraintType.Angular, Min = 0, Max = 0, SpringFrequency = Constraint.DefaultSpringFrequency, SpringDamping = Constraint.DefaultSpringDamping }; Constraint[] constraints = new Constraint[] { angularConstraint }; // Creating JointData var jointData = JointData.Create( new Math.MTransform(float3x3.identity, float3.zero), new Math.MTransform(float3x3.identity, float3.zero), constraints ); // Creating a PhysicsJoint this will constrain EntityA and EntityB from rotating or moving // along certain Axes based on the above defined constraints. var componentData = new PhysicsJoint { JointData = jointData, EntityA = player, EntityB = Entity.Null, EnableCollision = 0 }; // Create the ComponentType for the PhysicsJoint ComponentType[] componentTypes = new ComponentType[1]; componentTypes[0] = typeof(PhysicsJoint); // Create an entity to hold the PhysicsJoint component var jointEntity = entityManager.CreateEntity(componentTypes); // TODO: Check if this is needed // Setting the name of the jointEntity entityManager.SetName(jointEntity, "Joint Entity"); // Add the component data to the jointEntity entityManager.AddComponentData(jointEntity, componentData); //// Adding buffer to entity //entityManager.AddBuffer<BufferCollisionDetails>(player); //entityManager.AddComponent<ColAngle>(player); //entityManager.AddComponent<BaseSpeed>(player); //entityManager.SetComponentData(player, new BaseSpeed { Value = 8f }); //entityManager.AddComponent<JumpHeight>(player); //entityManager.SetComponentData(player, new JumpHeight { Value = 15f }); //var entity = entityManager.Instantiate(player); }
BlobAssetReference <JointData> CreateConfigurableJoint( quaternion jointFrameOrientation, LegacyJoint joint, bool3 linearLocks, bool3 linearLimited, SoftJointLimit linearLimit, SoftJointLimitSpring linearSpring, bool3 angularFree, bool3 angularLocks, bool3 angularLimited, SoftJointLimit lowAngularXLimit, SoftJointLimit highAngularXLimit, SoftJointLimitSpring angularXLimitSpring, SoftJointLimit angularYLimit, SoftJointLimit angularZLimit, SoftJointLimitSpring angularYZLimitSpring) { var constraints = new NativeList <Constraint>(Allocator.Temp); // TODO: investigate mapping PhysX spring and damping to Unity Physics SpringFrequency and SpringDamping var springFrequency = Constraint.DefaultSpringFrequency; var springDamping = Constraint.DefaultSpringDamping; if (angularLimited[0]) { constraints.Add(Constraint.Twist(0, math.radians(new FloatRange(-highAngularXLimit.limit, -lowAngularXLimit.limit)), springFrequency, springDamping)); } if (angularLimited[1]) { constraints.Add(Constraint.Twist(1, math.radians(new FloatRange(-angularYLimit.limit, angularYLimit.limit)), springFrequency, springDamping)); } if (angularLimited[2]) { constraints.Add(Constraint.Twist(2, math.radians(new FloatRange(-angularZLimit.limit, angularZLimit.limit)), springFrequency, springDamping)); } if (math.any(linearLimited)) { constraints.Add(new Constraint { ConstrainedAxes = linearLimited, Type = ConstraintType.Linear, Min = math.csum((int3)linearLimited) == 1 ? -linearLimit.limit : 0f, Max = linearLimit.limit, SpringFrequency = springFrequency, SpringDamping = springDamping }); } if (math.any(linearLocks)) { constraints.Add(new Constraint { ConstrainedAxes = linearLocks, Type = ConstraintType.Linear, Min = 0, Max = 0, SpringFrequency = springFrequency, SpringDamping = springDamping }); } if (math.any(angularLocks)) { constraints.Add(new Constraint { ConstrainedAxes = angularLocks, Type = ConstraintType.Angular, Min = 0, Max = 0, SpringFrequency = springFrequency, SpringDamping = springDamping }); } RigidTransform worldFromBodyA = Math.DecomposeRigidBodyTransform(joint.transform.localToWorldMatrix); RigidTransform worldFromBodyB = joint.connectedBody == null ? RigidTransform.identity : Math.DecomposeRigidBodyTransform(joint.connectedBody.transform.localToWorldMatrix); var legacyWorldFromJointA = math.mul( new RigidTransform(joint.transform.rotation, joint.transform.position), new RigidTransform(jointFrameOrientation, joint.anchor) ); var bodyAFromJoint = new JointFrame(math.mul(math.inverse(worldFromBodyA), legacyWorldFromJointA)); var connectedEntity = GetPrimaryEntity(joint.connectedBody); var isConnectedBodyConverted = joint.connectedBody == null || connectedEntity != Entity.Null; RigidTransform bFromA = isConnectedBodyConverted ? math.mul(math.inverse(worldFromBodyB), worldFromBodyA) : worldFromBodyA; RigidTransform bFromBSource = isConnectedBodyConverted ? RigidTransform.identity : worldFromBodyB; var bodyBFromJoint = new JointFrame { Axis = math.mul(bFromA.rot, bodyAFromJoint.Axis), PerpendicularAxis = math.mul(bFromA.rot, bodyAFromJoint.PerpendicularAxis), Position = math.mul(bFromBSource, new float4(joint.connectedAnchor, 1f)).xyz }; var jointData = JointData.Create(bodyAFromJoint, bodyBFromJoint, constraints); constraints.Dispose(); return(jointData); }