public override void CreateScene(CreateSimpleTerrainScene sceneSettings) { int2 size = new int2(2, 2); float3 scale = new float3(10, 1.0f, 10); NativeArray <float> heights = new NativeArray <float>(size.x * size.y * UnsafeUtility.SizeOf <float>(), Allocator.Temp); { heights[0] = 0; heights[1] = 0; heights[2] = 0; heights[3] = 0; } var collider = TerrainCollider.Create(heights, size, scale, TerrainCollider.CollisionMethod.VertexSamples); CreatedColliders.Add(collider); float3 position = new float3(15.0f, -1.0f, -5.0f); CreateTerrainBody(position, collider); // Mark this one CollisionResponse.None collider = TerrainCollider.Create(heights, size, scale, TerrainCollider.CollisionMethod.VertexSamples); collider.As <TerrainCollider>().Material.CollisionResponse = CollisionResponsePolicy.None; CreatedColliders.Add(collider); position = new float3(15.0f, -1.0f, 10.0f); CreateTerrainBody(position, collider); }
public override void CreateScene(CompoundDemoScene sceneSettings) { // // Floor // { // BlobAssetReference<Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new float3(0, -0.1f, 0), Quaternion.identity, new float3(10.0f, 0.1f, 10.0f), 0.05f); // CreatedColliders.Add(collider); // CreateStaticBody(float3.zero, quaternion.identity, collider); // } // Dynamic compound { var box = new BoxGeometry { Center = float3.zero, Orientation = quaternion.identity, Size = new float3(1), BevelRadius = 0.05f }; var sphere = new SphereGeometry { Center = float3.zero, Radius = 0.5f }; var children = new NativeArray <CompoundCollider.ColliderBlobInstance>(3, Allocator.Temp) { [0] = new CompoundCollider.ColliderBlobInstance { CompoundFromChild = new RigidTransform(quaternion.identity, new float3(-1, 0, 0)), Collider = Unity.Physics.BoxCollider.Create(box) }, [1] = new CompoundCollider.ColliderBlobInstance { CompoundFromChild = RigidTransform.identity, Collider = Unity.Physics.SphereCollider.Create(sphere) }, [2] = new CompoundCollider.ColliderBlobInstance { CompoundFromChild = new RigidTransform(quaternion.identity, new float3(1, 0, 0)), Collider = Unity.Physics.BoxCollider.Create(box) } }; foreach (var child in children) { CreatedColliders.Add(child.Collider); } BlobAssetReference <Unity.Physics.Collider> collider = CompoundCollider.Create(children); CreatedColliders.Add(collider); children.Dispose(); CreateDynamicBody(new float3(0, 1, 0), quaternion.identity, collider, float3.zero, float3.zero, 1.0f); } }
public override void CreateScene(InvalidPhysicsJointExcludeDemoScene sceneSettings) { float colliderSize = 0.25f; BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry { Center = float3.zero, Orientation = quaternion.identity, Size = new float3(colliderSize), BevelRadius = 0.0f }); CreatedColliders.Add(collider); float timeToSwap = sceneSettings.TimeToSwap; var bodyAPos = new float3(2f, 5.0f, 2); var bodyBPos = new float3(2f, 6.0f, 2); // Add constrained dynamic/dynamic body pair that will have their bodies excluded bool buildThisSection = true; if (buildThisSection) { bodyAPos += new float3(1, 0, 0); bodyBPos += new float3(1, 0, 0); // Create a body Entity bodyA = CreateDynamicBody(bodyAPos, quaternion.identity, collider, float3.zero, float3.zero, 1.0f); Entity bodyB = CreateDynamicBody(bodyBPos, quaternion.identity, collider, float3.zero, float3.zero, 1.0f); for (int i = 0; i < 2; i++) { // Create the joint var joint = PhysicsJoint.CreateBallAndSocket(new float3(0, colliderSize, 0), new float3(0, -colliderSize, 0)); var jointEntity = CreateJoint(joint, bodyA, bodyB); var pair = EntityManager.GetComponentData <PhysicsConstrainedBodyPair>(jointEntity); pair.EnableCollision = 1; EntityManager.SetComponentData(jointEntity, pair); } // add exclude components. EntityManager.AddComponentData(bodyA, new InvalidPhysicsJointExcludeBodies { }); EntityManager.AddComponentData(bodyA, new InvalidPhysicsJointExcludeTimerEvent { TimeLimit = timeToSwap, Timer = timeToSwap }); EntityManager.AddComponentData(bodyB, new InvalidPhysicsJointExcludeBodies { }); EntityManager.AddComponentData(bodyB, new InvalidPhysicsJointExcludeTimerEvent { TimeLimit = timeToSwap, Timer = timeToSwap }); } }
public override void CreateScene(RagdollGridDemoScene sceneSettings) { BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry { Center = float3.zero, Orientation = quaternion.identity, Size = new float3(0.2f, 1.0f, 0.2f), BevelRadius = 0.0f }); CreatedColliders.Add(collider); // Make some ragdoll joints 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 = new float3(0, 0.5f, 0); float3 pivotInWorld = math.transform(GetBodyTransform(body), pivotLocal); float3 axisLocal = new float3(0, 1, 0); float3 perpendicularLocal = new float3(0, 0, 1); quaternion worldFromLocal = Quaternion.AngleAxis((i - 4.5f) * 20.0f, new float3(0, 0, 1)); float3 axisWorld = math.mul(worldFromLocal, axisLocal); float3 perpendicularWorld = math.mul(worldFromLocal, perpendicularLocal); float maxConeAngle = (float)math.PI / 4.0f; var perpendicularAngle = new FloatRange(-math.PI / 2f, math.PI / 2f); var twistAngle = new FloatRange(-math.PI / 8f, math.PI / 8f); var localFrame = new BodyFrame { Axis = axisLocal, PerpendicularAxis = perpendicularLocal, Position = pivotLocal }; var worldFrame = new BodyFrame { Axis = axisWorld, PerpendicularAxis = perpendicularWorld, Position = pivotInWorld }; PhysicsJoint.CreateRagdoll(localFrame, worldFrame, maxConeAngle, perpendicularAngle, twistAngle, out var ragdoll0, out var ragdoll1); CreateJoint(ragdoll0, body, Entity.Null); CreateJoint(ragdoll1, body, Entity.Null); } }
public override void CreateScene(InvalidPhysicsJointDemoScene sceneSettings) { float colliderSize = 0.25f; BlobAssetReference <Collider> collider = BoxCollider.Create(new BoxGeometry { Center = float3.zero, Orientation = quaternion.identity, Size = new float3(colliderSize), 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); } }
private void CreateRagdoll(Mesh torsoMesh, Mesh renderMesh, float3 positionOffset, quaternion rotationOffset, float3 initialVelocity, int ragdollIndex = 1, bool internalCollisions = false, float rangeGain = 1.0f) { var entities = new NativeList <Entity>(Allocator.Temp); var rangeModifier = new float2(math.max(0, math.min(rangeGain, 1))); // Head float headRadius = 0.1f; float3 headPosition = new float3(0, 1.8f, headRadius); Entity head; { CollisionFilter filter = internalCollisions ? layerFilter(layer.Head, layer.Torso) : groupFilter(-ragdollIndex); BlobAssetReference <Collider> headCollider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = new float3(0, 0, 0), Vertex1 = new float3(0, 0, headRadius / 4), Radius = headRadius }, filter); CreatedColliders.Add(headCollider); head = CreateDynamicBody(headPosition, quaternion.identity, headCollider, float3.zero, float3.zero, 5.0f); } entities.Add(head); // Torso float3 torsoSize; float3 torsoPosition; Entity torso; { //UnityEngine.Mesh torsoMesh = (UnityEngine.Mesh)Resources.Load("torso", typeof(UnityEngine.Mesh)); torsoSize = torsoMesh.bounds.size; torsoPosition = headPosition - new float3(0, headRadius * 3.0f / 4.0f + torsoSize.y, 0); CollisionFilter filter = internalCollisions ? layerFilter(layer.Torso, layer.Thigh | layer.Head | layer.UpperArm | layer.Pelvis) : groupFilter(-ragdollIndex); NativeArray <float3> points = new NativeArray <float3>(torsoMesh.vertices.Length, Allocator.TempJob); for (int i = 0; i < torsoMesh.vertices.Length; i++) { points[i] = torsoMesh.vertices[i]; } BlobAssetReference <Collider> collider = ConvexCollider.Create( points, ConvexHullGenerationParameters.Default, CollisionFilter.Default ); CreatedColliders.Add(collider); points.Dispose(); collider.Value.Filter = filter; torso = CreateDynamicBody(torsoPosition, quaternion.identity, collider, float3.zero, float3.zero, 20.0f); } entities.Add(torso); // Neck { float3 pivotHead = new float3(0, -headRadius, 0); float3 pivotTorso = math.transform(math.inverse(GetBodyTransform(torso)), math.transform(GetBodyTransform(head), pivotHead)); float3 axisHead = new float3(0, 0, 1); float3 perpendicular = new float3(1, 0, 0); FloatRange coneAngle = new FloatRange(math.radians(0), math.radians(45)) * rangeModifier; FloatRange perpendicularAngle = new FloatRange(math.radians(-30), math.radians(+30)) * rangeModifier; FloatRange twistAngle = new FloatRange(math.radians(-5), math.radians(5)) * rangeModifier; var axisTorso = math.rotate(math.inverse(GetBodyTransform(torso).rot), math.rotate(GetBodyTransform(head).rot, axisHead)); axisTorso = math.rotate(quaternion.AxisAngle(perpendicular, math.radians(10)), axisTorso); var headFrame = new BodyFrame { Axis = axisHead, PerpendicularAxis = perpendicular, Position = pivotHead }; var torsoFrame = new BodyFrame { Axis = axisTorso, PerpendicularAxis = perpendicular, Position = pivotTorso }; PhysicsJoint.CreateRagdoll(headFrame, torsoFrame, coneAngle.Max, perpendicularAngle, twistAngle, out var ragdoll0, out var ragdoll1); CreateJoint(ragdoll0, head, torso); CreateJoint(ragdoll1, head, torso); } // Arms { float armLength = 0.25f; float armRadius = 0.05f; CollisionFilter armUpperFilter = internalCollisions ? layerFilter(layer.UpperArm, layer.Torso | layer.Forearm) : groupFilter(-ragdollIndex); CollisionFilter armLowerFilter = internalCollisions ? layerFilter(layer.Forearm, layer.UpperArm | layer.Hand) : groupFilter(-ragdollIndex); BlobAssetReference <Collider> upperArmCollider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = new float3(-armLength / 2, 0, 0), Vertex1 = new float3(armLength / 2, 0, 0), Radius = armRadius }, armUpperFilter); BlobAssetReference <Collider> foreArmCollider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = new float3(-armLength / 2, 0, 0), Vertex1 = new float3(armLength / 2, 0, 0), Radius = armRadius }, armLowerFilter); float handLength = 0.025f; float handRadius = 0.055f; CollisionFilter handFilter = internalCollisions ? layerFilter(layer.Hand, layer.Forearm) : groupFilter(-ragdollIndex); BlobAssetReference <Collider> handCollider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = new float3(-handLength / 2, 0, 0), Vertex1 = new float3(handLength / 2, 0, 0), Radius = handRadius }, handFilter); CreatedColliders.Add(upperArmCollider); CreatedColliders.Add(foreArmCollider); CreatedColliders.Add(handCollider); for (int i = 0; i < 2; i++) { float s = i * 2 - 1.0f; float3 upperArmPosition = torsoPosition + new float3(s * (torsoSize.x + armLength) / 2.0f, 0.9f * torsoSize.y - armRadius, 0.0f); Entity upperArm = CreateDynamicBody(upperArmPosition, quaternion.identity, upperArmCollider, float3.zero, float3.zero, 10.0f); float3 foreArmPosition = upperArmPosition + new float3(armLength * s, 0, 0); Entity foreArm = CreateDynamicBody(foreArmPosition, quaternion.identity, foreArmCollider, float3.zero, float3.zero, 5.0f); float3 handPosition = foreArmPosition + new float3((armLength + handLength) / 2.0f * s, 0, 0); Entity hand = CreateDynamicBody(handPosition, quaternion.identity, handCollider, float3.zero, float3.zero, 2.0f); entities.Add(upperArm); entities.Add(foreArm); entities.Add(hand); // shoulder { float3 pivotArm = new float3(-s * armLength / 2.0f, 0, 0); float3 pivotTorso = math.transform(math.inverse(GetBodyTransform(torso)), math.transform(GetBodyTransform(upperArm), pivotArm)); float3 axisArm = new float3(-s, 0, 0); float3 perpendicularArm = new float3(0, 1, 0); FloatRange coneAngle = new FloatRange(math.radians(0), math.radians(80)) * rangeModifier; FloatRange perpendicularAngle = new FloatRange(math.radians(-70), math.radians(20)) * rangeModifier; FloatRange twistAngle = new FloatRange(math.radians(-5), math.radians(5)) * rangeModifier; var axisTorso = math.rotate(math.inverse(GetBodyTransform(torso).rot), math.rotate(GetBodyTransform(upperArm).rot, axisArm)); axisTorso = math.rotate(quaternion.AxisAngle(perpendicularArm, math.radians(-s * 45.0f)), axisTorso); var armFrame = new BodyFrame { Axis = axisArm, PerpendicularAxis = perpendicularArm, Position = pivotArm }; var bodyFrame = new BodyFrame { Axis = axisTorso, PerpendicularAxis = perpendicularArm, Position = pivotTorso }; PhysicsJoint.CreateRagdoll(armFrame, bodyFrame, coneAngle.Max, perpendicularAngle, twistAngle, out var ragdoll0, out var ragdoll1); CreateJoint(ragdoll0, upperArm, torso); CreateJoint(ragdoll1, upperArm, torso); } // elbow { float3 pivotUpper = new float3(s * armLength / 2.0f, 0, 0); float3 pivotFore = -pivotUpper; float3 axis = new float3(0, -s, 0); float3 perpendicular = new float3(-s, 0, 0); var lowerArmFrame = new BodyFrame { Axis = axis, PerpendicularAxis = perpendicular, Position = pivotFore }; var upperArmFrame = new BodyFrame { Axis = axis, PerpendicularAxis = perpendicular, Position = pivotUpper }; var hingeRange = new FloatRange(math.radians(0), math.radians(100)); hingeRange = (hingeRange - new float2(hingeRange.Mid)) * rangeModifier + hingeRange.Mid; PhysicsJoint hinge = PhysicsJoint.CreateLimitedHinge(lowerArmFrame, upperArmFrame, hingeRange); CreateJoint(hinge, foreArm, upperArm); } // wrist { float3 pivotFore = new float3(s * armLength / 2.0f, 0, 0); float3 pivotHand = new float3(-s * handLength / 2.0f, 0, 0); float3 axis = new float3(0, 0, -s); float3 perpendicular = new float3(0, 0, 1); var handFrame = new BodyFrame { Axis = axis, PerpendicularAxis = perpendicular, Position = pivotHand }; var forearmFrame = new BodyFrame { Axis = axis, PerpendicularAxis = perpendicular, Position = pivotFore }; var hingeRange = new FloatRange(math.radians(0), math.radians(135)) * rangeModifier; PhysicsJoint hinge = PhysicsJoint.CreateLimitedHinge(handFrame, forearmFrame, hingeRange); CreateJoint(hinge, hand, foreArm); } } } // Pelvis float pelvisRadius = 0.08f; float pelvisLength = 0.22f; float3 pelvisPosition = torsoPosition - new float3(0, pelvisRadius * 0.75f, 0.0f); Entity pelvis; { CollisionFilter filter = internalCollisions ? layerFilter(layer.Pelvis, layer.Torso | layer.Thigh) : groupFilter(-ragdollIndex); BlobAssetReference <Collider> collider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = new float3(-pelvisLength / 2, 0, 0), Vertex1 = new float3(pelvisLength / 2, 0, 0), Radius = pelvisRadius }, filter); pelvis = CreateDynamicBody(pelvisPosition, quaternion.identity, collider, float3.zero, float3.zero, 15.0f); CreatedColliders.Add(collider); } entities.Add(pelvis); // Waist { float3 pivotTorso = float3.zero; float3 pivotPelvis = math.transform(math.inverse(GetBodyTransform(pelvis)), math.transform(GetBodyTransform(torso), pivotTorso)); float3 axis = new float3(0, 1, 0); float3 perpendicular = new float3(0, 0, 1); FloatRange coneAngle = new FloatRange(math.radians(0), math.radians(5)) * rangeModifier; FloatRange perpendicularAngle = new FloatRange(math.radians(-5), math.radians(90)) * rangeModifier; FloatRange twistAngle = new FloatRange(-math.radians(-5), math.radians(5)) * rangeModifier; var pelvisFrame = new BodyFrame { Axis = axis, PerpendicularAxis = perpendicular, Position = pivotPelvis }; var torsoFrame = new BodyFrame { Axis = axis, PerpendicularAxis = perpendicular, Position = pivotTorso }; PhysicsJoint.CreateRagdoll(pelvisFrame, torsoFrame, coneAngle.Max, perpendicularAngle, twistAngle, out var ragdoll0, out var ragdoll1); CreateJoint(ragdoll0, pelvis, torso); CreateJoint(ragdoll1, pelvis, torso); } // Legs { float thighLength = 0.32f; float thighRadius = 0.08f; CollisionFilter thighFilter = internalCollisions ? layerFilter(layer.Thigh, layer.Pelvis | layer.Calf) : groupFilter(-ragdollIndex); BlobAssetReference <Collider> thighCollider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = new float3(0, -thighLength / 2, 0), Vertex1 = new float3(0, thighLength / 2, 0), Radius = thighRadius }, thighFilter); float calfLength = 0.32f; float calfRadius = 0.06f; CollisionFilter calfFilter = internalCollisions ? layerFilter(layer.Calf, layer.Thigh | layer.Foot) : groupFilter(-ragdollIndex); BlobAssetReference <Collider> calfCollider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = new float3(0, -calfLength / 2, 0), Vertex1 = new float3(0, calfLength / 2, 0), Radius = calfRadius }, calfFilter); float footLength = 0.08f; float footRadius = 0.06f; CollisionFilter footFilter = internalCollisions ? layerFilter(layer.Foot, layer.Calf) : groupFilter(-ragdollIndex); BlobAssetReference <Collider> footCollider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = new float3(0), Vertex1 = new float3(0, 0, footLength), Radius = footRadius }, footFilter); CreatedColliders.Add(thighCollider); CreatedColliders.Add(calfCollider); CreatedColliders.Add(footCollider); for (int i = 0; i < 2; i++) { float s = i * 2 - 1.0f; float3 thighPosition = pelvisPosition + new float3(s * pelvisLength / 2.0f, -thighLength / 2.0f, 0.0f); Entity thigh = CreateDynamicBody(thighPosition, quaternion.identity, thighCollider, float3.zero, float3.zero, 10.0f); float3 calfPosition = thighPosition + new float3(0, -(thighLength + calfLength) / 2.0f, 0); Entity calf = CreateDynamicBody(calfPosition, quaternion.identity, calfCollider, float3.zero, float3.zero, 5.0f); float3 footPosition = calfPosition + new float3(0, -calfLength / 2.0f, 0); Entity foot = CreateDynamicBody(footPosition, quaternion.identity, footCollider, float3.zero, float3.zero, 2.0f); entities.Add(thigh); entities.Add(calf); entities.Add(foot); // hip { float3 pivotThigh = new float3(0, thighLength / 2.0f, 0); float3 pivotPelvis = math.transform(math.inverse(GetBodyTransform(pelvis)), math.transform(GetBodyTransform(thigh), pivotThigh)); float3 axisLeg = new float3(0, -1, 0); float3 perpendicularLeg = new float3(-s, 0, 0); FloatRange coneAngle = new FloatRange(math.radians(0), math.radians(60)) * rangeModifier; FloatRange perpendicularAngle = new FloatRange(math.radians(-10), math.radians(40)) * rangeModifier; FloatRange twistAngle = new FloatRange(-math.radians(5), math.radians(5)) * rangeModifier; var axisPelvis = math.rotate(math.inverse(GetBodyTransform(pelvis).rot), math.rotate(GetBodyTransform(thigh).rot, axisLeg)); axisPelvis = math.rotate(quaternion.AxisAngle(perpendicularLeg, math.radians(s * 45.0f)), axisPelvis); var upperLegFrame = new BodyFrame { Axis = axisLeg, PerpendicularAxis = perpendicularLeg, Position = pivotThigh }; var pelvisFrame = new BodyFrame { Axis = axisPelvis, PerpendicularAxis = perpendicularLeg, Position = pivotPelvis }; PhysicsJoint.CreateRagdoll(upperLegFrame, pelvisFrame, coneAngle.Max, perpendicularAngle, twistAngle, out var ragdoll0, out var ragdoll1); CreateJoint(ragdoll0, thigh, pelvis); CreateJoint(ragdoll1, thigh, pelvis); } // knee { float3 pivotThigh = new float3(0, -thighLength / 2.0f, 0); float3 pivotCalf = math.transform(math.inverse(GetBodyTransform(calf)), math.transform(GetBodyTransform(thigh), pivotThigh)); float3 axis = new float3(-1, 0, 0); float3 perpendicular = new float3(0, 0, 1); var lowerLegFrame = new BodyFrame { Axis = axis, PerpendicularAxis = perpendicular, Position = pivotCalf }; var upperLegFrame = new BodyFrame { Axis = axis, PerpendicularAxis = perpendicular, Position = pivotThigh }; var hingeRange = new FloatRange(math.radians(-90), math.radians(0)); hingeRange = (hingeRange - new float2(hingeRange.Mid)) * rangeModifier + hingeRange.Mid; PhysicsJoint hinge = PhysicsJoint.CreateLimitedHinge(lowerLegFrame, upperLegFrame, hingeRange); CreateJoint(hinge, calf, thigh); } // ankle { float3 pivotCalf = new float3(0, -calfLength / 2.0f, 0); float3 pivotFoot = float3.zero; float3 axis = new float3(-1, 0, 0); float3 perpendicular = new float3(0, 0, 1); var footFrame = new BodyFrame { Axis = axis, PerpendicularAxis = perpendicular, Position = pivotFoot }; var lowerLegFrame = new BodyFrame { Axis = axis, PerpendicularAxis = perpendicular, Position = pivotCalf }; var hingeRange = new FloatRange(math.radians(-5), math.radians(5)) * rangeModifier; PhysicsJoint hinge = PhysicsJoint.CreateLimitedHinge(footFrame, lowerLegFrame, hingeRange); CreateJoint(hinge, foot, calf); } } } // reposition with offset information if (entities.Length > 0) { for (int i = 0; i < entities.Length; i++) { var e = entities[i]; bool isTorso = (i == 1); SwapRenderMesh(e, isTorso, torsoMesh, renderMesh); Translation positionComponent = EntityManager.GetComponentData <Translation>(e); Rotation rotationComponent = EntityManager.GetComponentData <Rotation>(e); PhysicsVelocity velocityComponent = EntityManager.GetComponentData <PhysicsVelocity>(e); float3 position = positionComponent.Value; quaternion rotation = rotationComponent.Value; float3 localPosition = position - pelvisPosition; localPosition = math.rotate(rotationOffset, localPosition); position = localPosition + pelvisPosition + positionOffset; rotation = math.mul(rotation, rotationOffset); positionComponent.Value = position; rotationComponent.Value = rotation; velocityComponent.Linear = initialVelocity; EntityManager.SetComponentData <PhysicsVelocity>(e, velocityComponent); EntityManager.SetComponentData <Translation>(e, positionComponent); EntityManager.SetComponentData <Rotation>(e, rotationComponent); } } }
public override void CreateScene(TerrainDemoScene sceneSettings) { // Make heightfield data NativeArray <float> heights; int2 size; float3 scale; bool simple = false; #if UNITY_ANDROID || UNITY_IOS simple = true; #endif bool flat = false; bool mountain = false; if (simple) { size = new int2(2, 2); scale = new float3(25, 0.1f, 25); heights = new NativeArray <float>(size.x * size.y * UnsafeUtility.SizeOf <float>(), Allocator.Temp); heights[0] = 1; heights[1] = 0; heights[2] = 0; heights[3] = 1; } else { size = new int2(sceneSettings.SizeX, sceneSettings.SizeZ); scale = new float3(sceneSettings.ScaleX, sceneSettings.ScaleY, sceneSettings.ScaleZ); float period = 50.0f; heights = new NativeArray <float>(size.x * size.y * UnsafeUtility.SizeOf <float>(), Allocator.Temp); for (int j = 0; j < size.y; j++) { for (int i = 0; i < size.x; i++) { float a = (i + j) * 2.0f * (float)math.PI / period; heights[i + j * size.x] = flat ? 0.0f : math.sin(a); if (mountain) { float fractionFromCenter = 1.0f - math.min(math.length(new float2(i - size.x / 2, j - size.y / 2)) / (math.min(size.x, size.y) / 2), 1.0f); float mountainHeight = math.smoothstep(0.0f, 1, fractionFromCenter) * 25.0f; heights[i + j * size.x] += mountainHeight; } } } } // static terrain Entity staticEntity; { var collider = sceneSettings.BuildTerrainMesh ? CreateMeshTerrain(heights, new int2(sceneSettings.SizeX, sceneSettings.SizeZ), new float3(sceneSettings.ScaleX, sceneSettings.ScaleY, sceneSettings.ScaleZ)) : TerrainCollider.Create(heights, size, scale, sceneSettings.Method); CreatedColliders.Add(collider); if (sceneSettings.BuildCompoundTerrain) { var numChildren = sceneSettings.NumChildren; var instances = new NativeArray <CompoundCollider.ColliderBlobInstance>(numChildren, Allocator.Temp); for (int i = 0; i < numChildren; i++) { instances[i] = new CompoundCollider.ColliderBlobInstance { Collider = collider, CompoundFromChild = new RigidTransform { pos = new float3((i % 2) * scale.x * (size.x - 1), 0.0f, (i / 2) * scale.z * (size.y - 1)), rot = quaternion.identity } }; } collider = Unity.Physics.CompoundCollider.Create(instances); CreatedColliders.Add(collider); instances.Dispose(); } float3 position = new float3(size.x - 1, 0.0f, size.y - 1) * scale * -0.5f; staticEntity = CreateStaticBody(position, quaternion.identity, collider); } }
public override void CreateScene(InvalidPhysicsJointSwapDemoScene sceneSettings) { float colliderSize = 0.25f; BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry { Center = float3.zero, Orientation = quaternion.identity, Size = new float3(colliderSize), BevelRadius = 0.0f }); CreatedColliders.Add(collider); float timeToSwap = 0.25f; // Add two constrained dynamic bodies that will have their bodies swapped bool buildThisSection = true; if (buildThisSection) { // Create a body Entity bodyA = CreateDynamicBody(new float3(2f, 5.0f, 0), quaternion.identity, collider, float3.zero, float3.zero, 1.0f); Entity bodyB = CreateDynamicBody(new float3(2f, 6.0f, 0), quaternion.identity, collider, float3.zero, float3.zero, 1.0f); for (int i = 0; i < 2; i++) { // Create the joint var joint = PhysicsJoint.CreateBallAndSocket(new float3(0, colliderSize, 0), new float3(0, -colliderSize, 0)); var jointEntity = CreateJoint(joint, bodyA, bodyB); var pair = EntityManager.GetComponentData <PhysicsConstrainedBodyPair>(jointEntity); pair.EnableCollision = 1; EntityManager.SetComponentData(jointEntity, pair); if (1 == i) { // add swap and timer components. EntityManager.AddComponentData(jointEntity, new InvalidPhysicsJointSwapBodies { }); EntityManager.AddComponentData(jointEntity, new InvalidPhysicsJointSwapTimerEvent { TimeLimit = timeToSwap, Timer = timeToSwap }); } } EntityManager.AddComponentData(bodyA, new InvalidPhysicsJointKillBodies { }); EntityManager.AddComponentData(bodyA, new InvalidPhysicsJointSwapTimerEvent { TimeLimit = timeToSwap * 2.0f, Timer = timeToSwap * 2.0f }); } // Add constrained static/dynamic body pair that will have their bodies swapped buildThisSection = true; if (buildThisSection) { // Create a body var staticTransform = new RigidTransform(quaternion.identity, new float3(3f, 5.0f, 0)); Entity bodyS = CreateStaticBody(staticTransform.pos, staticTransform.rot, collider); Entity bodyD = CreateDynamicBody(new float3(3f, 6.0f, 0), quaternion.identity, collider, float3.zero, float3.zero, 1.0f); for (int i = 0; i < 2; i++) { // Create the joint var joint = PhysicsJoint.CreateBallAndSocket(new float3(0, colliderSize, 0), new float3(0, -colliderSize, 0)); var jointEntity = CreateJoint(joint, bodyS, bodyD); var pair = EntityManager.GetComponentData <PhysicsConstrainedBodyPair>(jointEntity); pair.EnableCollision = 1; EntityManager.SetComponentData(jointEntity, pair); } // add swap and timer components. EntityManager.AddComponentData(bodyS, new InvalidPhysicsJointSwapMotionType { OriginalTransform = staticTransform }); EntityManager.AddComponentData(bodyS, new InvalidPhysicsJointSwapTimerEvent { TimeLimit = timeToSwap, Timer = timeToSwap }); } }
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); } }
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); } }
public override void CreateScene(ChangeCompoundFilterScene sceneSettings) { // Common params float3 groundSize = new float3(5.0f, 1.0f, 5.0f); float3 boxSize = new float3(1.0f, 1.0f, 1.0f); float mass = 1.0f; // Compound ground which will have child 0 change filter { var compoundCollider = CreateGroundCompoundWith2Children(groundSize); CreateStaticBody(new float3(-20.0f, 0.0f, 0.0f), quaternion.identity, compoundCollider); var boxCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = boxSize }); CreateDynamicBody(new float3(-20.0f, 1.0f, 0.0f), quaternion.identity, boxCollider, float3.zero, float3.zero, mass); CreatedColliders.Add(compoundCollider); CreatedColliders.Add(boxCollider); } // Compound ground which will have child 1 change filter { var compoundCollider = CreateGroundCompoundWith2Children(groundSize); CreateStaticBody(new float3(-10.0f, 0.0f, 0.0f), quaternion.identity, compoundCollider); var boxCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = boxSize }); CreateDynamicBody(new float3(-10.0f, 1.0f, 0.0f), quaternion.identity, boxCollider, float3.zero, float3.zero, mass); CreatedColliders.Add(compoundCollider); CreatedColliders.Add(boxCollider); } // Compound ground which will have both children change filter { var compoundCollider = CreateGroundCompoundWith2Children(groundSize); CreateStaticBody(new float3(0.0f, 0.0f, 0.0f), quaternion.identity, compoundCollider); var boxCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = boxSize }); CreateDynamicBody(new float3(0.0f, 1.0f, 0.0f), quaternion.identity, boxCollider, float3.zero, float3.zero, mass); CreatedColliders.Add(compoundCollider); CreatedColliders.Add(boxCollider); } // Compound ground which will have its filter changed through root collider { var compoundCollider = CreateGroundCompoundWith2Children(groundSize); CreateStaticBody(new float3(10.0f, 0.0f, 0.0f), quaternion.identity, compoundCollider); var boxCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = boxSize }); CreateDynamicBody(new float3(10.0f, 1.0f, 0.0f), quaternion.identity, boxCollider, float3.zero, float3.zero, mass); CreatedColliders.Add(compoundCollider); CreatedColliders.Add(boxCollider); } }
public override void CreateScene(VerifyActivationScene sceneSettings) { // Common params float3 groundSize = new float3(5.0f, 1.0f, 5.0f); float3 boxSize = new float3(1.0f, 1.0f, 1.0f); float mass = 1.0f; Entity e = GetSingletonEntity <VerifyActivationScene>(); VerifyActivationData data = GetComponent <VerifyActivationData>(e); // Ground to do nothing on (other than filter change) and dynamic box over it if (data.PureFilter == 1) { var groundCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = groundSize }); CreateStaticBody(new float3(-30.0f, 0.0f, 0.0f), quaternion.identity, groundCollider); var boxCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = boxSize }); CreateDynamicBody(new float3(-30.0f, 1.0f, 0.0f), quaternion.identity, boxCollider, float3.zero, float3.zero, mass); CreatedColliders.Add(groundCollider); CreatedColliders.Add(boxCollider); } // Ground to remove and dynamic box over it if (data.Remove == 1) { var groundCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = groundSize }); CreateStaticBody(new float3(-20.0f, 0.0f, 0.0f), quaternion.identity, groundCollider); var boxCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = boxSize }); CreateDynamicBody(new float3(-20.0f, 1.0f, 0.0f), quaternion.identity, boxCollider, float3.zero, float3.zero, mass); CreatedColliders.Add(groundCollider); CreatedColliders.Add(boxCollider); } // Ground to convert to dynamic and dynamic box over it if (data.MotionChange == 1) { var groundCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = groundSize }); CreateStaticBody(new float3(-10.0f, 0.0f, 0.0f), quaternion.identity, groundCollider); var boxCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = boxSize }); CreateDynamicBody(new float3(-10.0f, 1.0f, 0.0f), quaternion.identity, boxCollider, float3.zero, float3.zero, mass); CreatedColliders.Add(groundCollider); CreatedColliders.Add(boxCollider); } // Ground to teleport and dynamic box over it if (data.Teleport == 1) { var groundCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = groundSize }); CreateStaticBody(new float3(0.0f, 0.0f, 0.0f), quaternion.identity, groundCollider); var boxCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = boxSize }); CreateDynamicBody(new float3(0.0f, 1.0f, 0.0f), quaternion.identity, boxCollider, float3.zero, float3.zero, mass); CreatedColliders.Add(groundCollider); CreatedColliders.Add(boxCollider); } // Ground to change collider of and dynamic box over it if (data.ColliderChange == 1) { var groundCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = groundSize }); CreateStaticBody(new float3(10.0f, 0.0f, 0.0f), quaternion.identity, groundCollider); var boxCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = boxSize }); CreateDynamicBody(new float3(10.0f, 1.0f, 0.0f), quaternion.identity, boxCollider, float3.zero, float3.zero, mass); CreatedColliders.Add(groundCollider); CreatedColliders.Add(boxCollider); } // Ground to set new collider on and dynamic box over it if (data.NewCollider == 1) { var groundCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = groundSize }); CreateStaticBody(new float3(20.0f, 0.0f, 0.0f), quaternion.identity, groundCollider); var boxCollider = BoxCollider.Create(new BoxGeometry { Orientation = quaternion.identity, Size = boxSize }); CreateDynamicBody(new float3(20.0f, 1.0f, 0.0f), quaternion.identity, boxCollider, float3.zero, float3.zero, mass); CreatedColliders.Add(groundCollider); CreatedColliders.Add(boxCollider); } }