public void PhysicsBodyCalculateAabb_PolygonColliderTest() { var vertices = new NativeArray <float2>( new float2[] { new float2(-1f, -2f), new float2(2f, -3f), new float2(4f, 5f), new float2(-6f, 7f) }, Allocator.Temp ); var geometry = new PolygonGeometry { Vertices = vertices, BevelRadius = 3.0f }; var collider1 = PhysicsPolygonCollider.Create(geometry); var collider2 = PhysicsPolygonCollider.Create(geometry); var physicsBody = new PhysicsBody(collider1); var aabb = physicsBody.CalculateAabb(); Assert.IsTrue(aabb.Equals(collider2.Value.CalculateAabb())); vertices.Dispose(); collider1.Dispose(); collider2.Dispose(); }
public void TestPhysicsPolygonColliderCreate() { const uint UserData = 0xDEADBEEF; using (var colliderBlob = PhysicsPolygonCollider.Create(m_PolygonGeometry, CollisionFilter.Default, PhysicsMaterial.Default, UserData)) { ref var collider = ref colliderBlob.GetColliderRef <PhysicsPolygonCollider>(); Assert.AreEqual(ColliderType.Polygon, 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); Assert.AreEqual(m_PolygonGeometry.BevelRadius, collider.BevelRadius); Assert.AreEqual(m_PolygonGeometry.Vertices.Length, collider.VertexCount); var vertexCount = collider.VertexCount; var giftWrappedIndices = new int[4] { 2, 3, 0, 1 }; for (var i = 0; i < vertexCount; ++i) { Assert.AreEqual(m_PolygonGeometry.Vertices[i], collider.Vertices[giftWrappedIndices[i]]); } }
protected override void OnUpdate() { Entities.ForEach((UnityEngine.PolygonCollider2D collider) => { // Convert the collider if it's valid. if (ConversionUtilities.CanConvertCollider(collider)) { try { // Only single paths with no more than the maximum allowed vertex allowed. // NOTE: Until we implement a convex polygon decomposition, only the convex hull of these points will be used. var colliderPointCount = collider.GetTotalPointCount(); if (collider.pathCount != 1 || colliderPointCount > PhysicsPolygonCollider.Constants.MaxVertexCount) { return; } 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); UnityEngine.Vector3 offset = collider.offset; collider.GetPath(0, m_PolygonVertices); var vertices = new NativeArray <float2>(colliderPointCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory); for (var i = 0; i < colliderPointCount; ++i) { var vertex = localToWorld.MultiplyPoint(offset + (UnityEngine.Vector3)m_PolygonVertices[i]); vertices[i] = new float2(vertex.x, vertex.y); } var geometry = new PolygonGeometry { Vertices = vertices, BevelRadius = PhysicsSettings.Constants.MinimumConvexRadius, }; var colliderBlob = PhysicsPolygonCollider.Create( geometry, ConversionUtilities.GetCollisionFilterFromCollider(collider), ConversionUtilities.GetPhysicsMaterialFromCollider(collider) ); // We finished with the points. vertices.Dispose(); // Submit the collider for conversion. m_ColliderConversionSystem.SubmitCollider(collider, ref colliderBlob); } catch (ArgumentException exception) { UnityEngine.Debug.LogWarning($"{collider.name}: {exception.Message}", collider); } } }); m_PolygonVertices.Clear(); }
public void Execute() => PhysicsPolygonCollider.Create(Geometry).Dispose();