public void TestPhysicsCapsuleColliderCalculateAabbLocalTranslation() { { var geometry = new CapsuleGeometry { Vertex0 = new float2(-23f, -2f), Vertex1 = new float2(2.1f, 3f), Radius = 1.5f }; Aabb expectedAabb = new Aabb { Min = new float2(-24.5f, -3.5f), Max = new float2(3.6f, 4.5f) }; using (var colliderBlob = PhysicsCapsuleCollider.Create(geometry)) { var aabb = colliderBlob.Value.CalculateAabb(); Assert.AreEqual(expectedAabb.Min, aabb.Min); Assert.AreEqual(expectedAabb.Max, aabb.Max); } } }
public void TestPhysicsCapsuleColliderMassProperties() { var geometry = new CapsuleGeometry { Vertex0 = new float2(0f, -2f), Vertex1 = new float2(0f, 3f), Radius = 1.5f }; const uint UserData = 0xDEADBEEF; using (var colliderBlob = PhysicsCapsuleCollider.Create(geometry, CollisionFilter.Default, PhysicsMaterial.Default, UserData)) { ref var collider = ref colliderBlob.GetColliderRef <PhysicsCapsuleCollider>(); Assert.AreEqual(ColliderType.Capsule, collider.ColliderType); Assert.AreEqual(CollisionType.Convex, collider.CollisionType); Assert.AreEqual(UserData, collider.UserData); Assert.AreEqual(CollisionFilter.Default, collider.Filter); Assert.AreEqual(PhysicsMaterial.Default, collider.Material); // The following assumptions are made for the MassProperties: var radiusSqr = geometry.Radius * geometry.Radius; var bodyLength = math.distance(geometry.Vertex0, geometry.Vertex1); var bodyArea = bodyLength * geometry.Radius * 2.0f; var bodyMass = bodyArea; var bodyInertia = bodyMass * (bodyLength * bodyLength + radiusSqr) / 12.0f; var capsArea = math.PI * radiusSqr; var capsMass = capsArea; var capsInertia = capsMass * (0.5f * radiusSqr + bodyLength * bodyLength * 0.25f); var mass = bodyMass + capsArea; var localCenterOfMass = 0.5f * (geometry.Vertex0 + geometry.Vertex1); var area = bodyArea + capsArea; var inertia = bodyInertia + capsInertia + mass * math.dot(localCenterOfMass, localCenterOfMass); var angularExpansionFactor = math.length(geometry.Vertex1 - geometry.Vertex0) * 0.5f; var massProperties = collider.MassProperties; Assert.AreEqual(localCenterOfMass, massProperties.MassDistribution.LocalCenterOfMass); Assert.AreEqual(1.0f / inertia, massProperties.MassDistribution.InverseInertia); Assert.AreEqual(area, massProperties.Area); Assert.AreEqual(angularExpansionFactor, massProperties.AngularExpansionFactor); }
public void PhysicsBodyCalculateAabb_CapsuleColliderTest() { var geometry = new CapsuleGeometry { Vertex0 = new float2(0f, -2f), Vertex1 = new float2(0f, 3f), Radius = 1.5f }; var collider1 = PhysicsCapsuleCollider.Create(geometry); var collider2 = PhysicsCapsuleCollider.Create(geometry); var physicsBody = new PhysicsBody(collider1); var aabb = physicsBody.CalculateAabb(); Assert.IsTrue(aabb.Equals(collider2.Value.CalculateAabb())); collider1.Dispose(); collider2.Dispose(); }
public void TestPhysicsCapsuleColliderCreate() { var geometry = new CapsuleGeometry { Vertex0 = new float2(0f, -2f), Vertex1 = new float2(0f, 3f), Radius = 1.5f }; using (var colliderBlob = PhysicsCapsuleCollider.Create(geometry)) { var collider = colliderBlob.GetColliderRef <PhysicsCapsuleCollider>(); Assert.AreEqual(ColliderType.Capsule, collider.ColliderType); Assert.AreEqual(CollisionType.Convex, collider.CollisionType); Assert.AreEqual(geometry.Vertex0, collider.Vertex0); Assert.AreEqual(geometry.Vertex0, collider.Geometry.Vertex0); Assert.AreEqual(geometry.Vertex1, collider.Vertex1); Assert.AreEqual(geometry.Vertex1, collider.Geometry.Vertex1); Assert.AreEqual(geometry.Radius, collider.Radius); Assert.AreEqual(geometry.Radius, collider.Geometry.Radius); } }
protected override void OnUpdate() { Entities.ForEach((UnityEngine.CapsuleCollider2D collider) => { // Convert the collider if it's valid. if (ConversionUtilities.CanConvertCollider(collider)) { try { UnityEngine.Vector3 vertex0; UnityEngine.Vector3 vertex1; float radius; var halfSize = new float2(collider.size) * 0.5f; if (collider.direction == UnityEngine.CapsuleDirection2D.Vertical) { radius = halfSize.x; vertex0 = new UnityEngine.Vector3(0.0f, halfSize.y - radius, 0.0f); vertex1 = new UnityEngine.Vector3(0.0f, -halfSize.y + radius, 0.0f); } else { radius = halfSize.y; vertex0 = new UnityEngine.Vector3(halfSize.x - radius, 0.0f, 0.0f); vertex1 = new UnityEngine.Vector3(-halfSize.x + radius, 0.0f, 0.0f); } // Add offset to capsule. var colliderOffset = (UnityEngine.Vector3)collider.offset; vertex0 += colliderOffset; vertex1 += colliderOffset; var lossyScale = new float3(collider.transform.lossyScale).xy; if (math.any(!math.isfinite(lossyScale)) || math.any(lossyScale <= 0.0f)) { throw new ArgumentException("Transform XY scale cannot be zero or Infinite/NaN.", "Transform XY scale."); } var localToWorld = ConversionUtilities.GetColliderLocalToWorld(collider); var geometry = new CapsuleGeometry { Vertex0 = new float3(localToWorld.MultiplyPoint(vertex0)).xy, Vertex1 = new float3(localToWorld.MultiplyPoint(vertex1)).xy, Radius = math.max(PhysicsSettings.Constants.MinimumConvexRadius, math.cmax(lossyScale) * radius), }; var colliderBlob = PhysicsCapsuleCollider.Create( geometry, ConversionUtilities.GetCollisionFilterFromCollider(collider), ConversionUtilities.GetPhysicsMaterialFromCollider(collider) ); // Submit the collider for conversion. m_ColliderConversionSystem.SubmitCollider(collider, ref colliderBlob); } catch (ArgumentException exception) { UnityEngine.Debug.LogWarning($"{collider.name}: {exception.Message}", collider); } } }); }
public void TestPhysicsCapsuleColliderCreateInvalid() { var geometry = new CapsuleGeometry { Vertex0 = new float2(0f, -2f), Vertex1 = new float2(0f, 3f), Radius = 1.5f }; // Invalid vertex0, positive infinity { var invalidGeometry = geometry; invalidGeometry.Vertex0 = new float2(float.PositiveInfinity); Assert.Throws <ArgumentException>(() => PhysicsCapsuleCollider.Create(invalidGeometry)); } // Invalid vertex0, negative infinity { var invalidGeometry = geometry; invalidGeometry.Vertex0 = new float2(float.NegativeInfinity); Assert.Throws <ArgumentException>(() => PhysicsCapsuleCollider.Create(invalidGeometry)); } // Invalid vertex0, nan { var invalidGeometry = geometry; invalidGeometry.Vertex0 = new float2(float.NaN); Assert.Throws <ArgumentException>(() => PhysicsCapsuleCollider.Create(invalidGeometry)); } // Invalid vertex1, positive infinity { var invalidGeometry = geometry; invalidGeometry.Vertex1 = new float2(float.PositiveInfinity); Assert.Throws <ArgumentException>(() => PhysicsCapsuleCollider.Create(invalidGeometry)); } // Invalid vertex1, negative infinity { var invalidGeometry = geometry; invalidGeometry.Vertex1 = new float2(float.NegativeInfinity); Assert.Throws <ArgumentException>(() => PhysicsCapsuleCollider.Create(invalidGeometry)); } // Invalid vertex1, nan { var invalidGeometry = geometry; invalidGeometry.Vertex1 = new float2(float.NaN); Assert.Throws <ArgumentException>(() => PhysicsCapsuleCollider.Create(invalidGeometry)); } // Negative radius { var invalidGeometry = geometry; invalidGeometry.Radius = -1.0f; Assert.Throws <ArgumentException>(() => PhysicsCapsuleCollider.Create(invalidGeometry)); } // Invalid radius, positive inf { var invalidGeometry = geometry; invalidGeometry.Radius = float.PositiveInfinity; Assert.Throws <ArgumentException>(() => PhysicsCapsuleCollider.Create(invalidGeometry)); } // Invalid radius, negative inf { var invalidGeometry = geometry; invalidGeometry.Radius = float.NegativeInfinity; Assert.Throws <ArgumentException>(() => PhysicsCapsuleCollider.Create(invalidGeometry)); } // Invalid radius, nan { var invalidGeometry = geometry; invalidGeometry.Radius = float.NaN; Assert.Throws <ArgumentException>(() => PhysicsCapsuleCollider.Create(invalidGeometry)); } }
public void Execute() => PhysicsCapsuleCollider.Create(new CapsuleGeometry { Vertex0 = new float2(0f, -1f), Vertex1 = new float2(0f, 1f), Radius = 0.5f }).Dispose();