public unsafe bool DistanceLeaf <T>(ColliderDistanceInput input, int rigidBodyIndex, ref T collector)
                where T : struct, ICollector <DistanceHit>
            {
                rigidBodyIndex += BaseRigidBodyIndex;
                RigidBody body = m_Bodies[rigidBodyIndex];

                // Transform the input into body space
                var        worldFromBody = new MTransform(body.WorldFromBody);
                MTransform bodyFromWorld = Inverse(worldFromBody);
                var        inputLs       = new ColliderDistanceInput
                {
                    Collider  = input.Collider,
                    Transform = new RigidTransform(
                        math.mul(math.inverse(body.WorldFromBody.rot), input.Transform.rot),
                        Mul(bodyFromWorld, input.Transform.pos)),
                    MaxDistance  = input.MaxDistance,
                    QueryContext = new QueryContext
                    {
                        RigidBodyIndex          = rigidBodyIndex,
                        ColliderKey             = ColliderKey.Empty,
                        NumColliderKeyBits      = 0,
                        WorldFromLocalTransform = worldFromBody,
                        Entity        = body.Entity,
                        IsInitialized = true
                    }
                };

                return(body.CalculateDistance(inputLs, ref collector));
            }
示例#2
0
 public unsafe bool CalculateDistance <T>(ColliderDistanceInput input, ref T collector) where T : struct, ICollector <DistanceHit>
 {
     fixed(TerrainCollider *target = &this)
     {
         return(DistanceQueries.ColliderCollider(input, (Collider *)target, ref collector));
     }
 }
示例#3
0
            public unsafe bool DistanceLeaf <T>(ColliderDistanceInput input, int rigidBodyIndex, ref T collector)
                where T : struct, ICollector <DistanceHit>
            {
                rigidBodyIndex += BaseRigidBodyIndex;
                RigidBody body = m_Bodies[rigidBodyIndex];

                // Transform the input into body space
                var        worldFromBody = new MTransform(body.WorldFromBody);
                MTransform bodyFromWorld = Inverse(worldFromBody);
                var        inputLs       = new ColliderDistanceInput
                {
                    Collider  = input.Collider,
                    Transform = new RigidTransform(
                        math.mul(math.inverse(body.WorldFromBody.rot), input.Transform.rot),
                        Mul(bodyFromWorld, input.Transform.pos)),
                    MaxDistance = input.MaxDistance
                };

                float fraction = collector.MaxFraction;
                int   numHits  = collector.NumHits;

                if (body.CalculateDistance(inputLs, ref collector))
                {
                    // Transform results back into world space
                    collector.TransformNewHits(numHits, fraction, worldFromBody, rigidBodyIndex);
                    return(true);
                }
                return(false);
            }
示例#4
0
        public static bool OverlapCapsuleCustom <T, C>(ref T target, float3 point1, float3 point2, float radius, ref C collector, CollisionFilter filter, QueryInteraction queryInteraction = QueryInteraction.Default)
            where T : struct, ICollidable
            where C : struct, ICollector <DistanceHit>
        {
            Assert.IsTrue(collector.MaxFraction == 0);

            CapsuleCollider collider = default;
            float3          center   = (point1 + point2) / 2;

            CapsuleGeometry geometry = new CapsuleGeometry
            {
                Radius  = radius,
                Vertex0 = point1 - center,
                Vertex1 = point2 - center
            };

            collider.Initialize(geometry, filter, Material.Default);
            ColliderDistanceInput input;

            unsafe
            {
                input = new ColliderDistanceInput
                {
                    Collider    = (Collider *)UnsafeUtility.AddressOf(ref collider),
                    MaxDistance = 0.0f,
                    Transform   = new RigidTransform
                    {
                        pos = center,
                        rot = quaternion.identity
                    }
                };
            }

            if (queryInteraction == QueryInteraction.Default)
            {
                return(target.CalculateDistance(input, ref collector));
            }
            else
            {
                unsafe
                {
                    var interactionCollector = new QueryInteractionCollector <DistanceHit, C>
                    {
                        Collector = collector,
                    };

                    bool returnValue = target.CalculateDistance(input, ref interactionCollector);

                    collector = interactionCollector.Collector;
                    return(returnValue);
                }
            }
        }
示例#5
0
            public unsafe bool DistanceLeaf <T>(ColliderDistanceInput input, int rigidBodyIndex, ref T collector)
                where T : struct, ICollector <DistanceHit>
            {
                rigidBodyIndex += BaseRigidBodyIndex;

                input.QueryContext.IsInitialized  = true;
                input.QueryContext.RigidBodyIndex = rigidBodyIndex;

                RigidBody body = m_Bodies[rigidBodyIndex];

                return(body.CalculateDistance(input, ref collector));
            }
示例#6
0
        public static bool OverlapBoxCustom <T, C>(ref T target, float3 center, quaternion orientation, float3 halfExtents, ref C collector, CollisionFilter filter, QueryInteraction queryInteraction = QueryInteraction.Default)
            where T : struct, ICollidable
            where C : struct, ICollector <DistanceHit>
        {
            Assert.IsTrue(collector.MaxFraction == 0);

            BoxCollider collider = default;
            BoxGeometry geometry = new BoxGeometry
            {
                BevelRadius = 0.0f,
                Center      = float3.zero,
                Size        = halfExtents * 2,
                Orientation = quaternion.identity
            };

            collider.Initialize(geometry, filter, Material.Default);

            ColliderDistanceInput input;

            unsafe
            {
                input = new ColliderDistanceInput
                {
                    Collider    = (Collider *)UnsafeUtility.AddressOf(ref collider),
                    MaxDistance = 0.0f,
                    Transform   = new RigidTransform
                    {
                        pos = center,
                        rot = orientation
                    }
                };
            }

            if (queryInteraction == QueryInteraction.Default)
            {
                return(target.CalculateDistance(input, ref collector));
            }
            else
            {
                unsafe
                {
                    var interactionCollector = new QueryInteractionCollector <DistanceHit, C>
                    {
                        Collector = collector,
                    };

                    bool returnValue = target.CalculateDistance(input, ref interactionCollector);

                    collector = interactionCollector.Collector;
                    return(returnValue);
                }
            }
        }
示例#7
0
        public static unsafe bool ColliderCollider <T>(ColliderDistanceInput input, Collider *target, ref T collector) where T : struct, ICollector <DistanceHit>
        {
            if (!CollisionFilter.IsCollisionEnabled(input.Collider->Filter, target->Filter))
            {
                return(false);
            }

            switch (input.Collider->CollisionType)
            {
            case CollisionType.Convex:
                switch (target->Type)
                {
                case ColliderType.Convex:
                case ColliderType.Sphere:
                case ColliderType.Capsule:
                case ColliderType.Triangle:
                case ColliderType.Quad:
                case ColliderType.Box:
                case ColliderType.Cylinder:
                    MTransform targetFromQuery = new MTransform(input.Transform);
                    Result     result          = ConvexConvex(target, input.Collider, targetFromQuery);
                    if (result.Distance < collector.MaxFraction)
                    {
                        collector.AddHit(new DistanceHit
                        {
                            Fraction      = result.Distance,
                            SurfaceNormal = -result.NormalInA,
                            Position      = result.PositionOnAinA,
                            ColliderKey   = ColliderKey.Empty
                        });
                        return(true);
                    }
                    return(false);

                case ColliderType.Mesh:
                    return(ConvexMesh(input, (MeshCollider *)target, ref collector));

                case ColliderType.Compound:
                    return(ConvexCompound(input, (CompoundCollider *)target, ref collector));

                default:
                    throw new NotImplementedException();
                }

            case CollisionType.Composite:
                // no support for composite query shapes
                throw new NotImplementedException();

            default:
                throw new NotImplementedException();
            }
        }
示例#8
0
        public static bool CalculateDistance <T>(ref T target, ColliderDistanceInput input, out DistanceHit result) where T : struct, ICollidable
        {
            var collector = new ClosestHitCollector <DistanceHit>(input.MaxDistance);

            if (target.CalculateDistance(input, ref collector))
            {
                result = collector.ClosestHit;  // TODO: would be nice to avoid this copy
                return(true);
            }

            result = new DistanceHit();
            return(false);
        }
示例#9
0
        public bool CalculateDistance <T>(ColliderDistanceInput input, ref T collector) where T : struct, ICollector <DistanceHit>
        {
            // Transform the input into body space
            MTransform worldFromBody = new MTransform(WorldFromBody);
            MTransform bodyFromWorld = Inverse(worldFromBody);

            input.Transform = new RigidTransform(
                math.mul(math.inverse(WorldFromBody.rot), input.Transform.rot),
                Mul(bodyFromWorld, input.Transform.pos));

            SetQueryContextParameters(ref input.QueryContext, ref worldFromBody);

            return(Collider.IsCreated && Collider.Value.CalculateDistance(input, ref collector));
        }
示例#10
0
            public bool DistanceLeaf <T>(ColliderDistanceInput input, int primitiveKey, ref T collector)
                where T : struct, ICollector <DistanceHit>
            {
                m_Mesh->GetPrimitive(primitiveKey, out float3x4 vertices, out Mesh.PrimitiveFlags flags, out CollisionFilter filter);

                if (!CollisionFilter.IsCollisionEnabled(input.Collider->Filter, filter)) // TODO: could do this check within GetPrimitive()
                {
                    return(false);
                }

                int  numPolygons = Mesh.GetNumPolygonsInPrimitive(flags);
                bool isQuad      = Mesh.IsPrimitveFlagSet(flags, Mesh.PrimitiveFlags.IsQuad);

                float3 *v         = stackalloc float3[4];
                bool    acceptHit = false;

                ref ConvexHull inputHull       = ref ((ConvexCollider *)input.Collider)->ConvexHull;
示例#11
0
        public unsafe bool CalculateDistance <T>(ColliderDistanceInput input, NativeSlice <RigidBody> rigidBodies, ref T collector)
            where T : struct, ICollector <DistanceHit>
        {
            Assert.IsTrue(input.Collider != null);
            if (input.Collider->Filter.IsEmpty)
            {
                return(false);
            }
            var leafProcessor = new BvhLeafProcessor(rigidBodies);

            leafProcessor.BaseRigidBodyIndex = m_DynamicTree.NumBodies;
            bool hasHit = m_StaticTree.BoundingVolumeHierarchy.Distance(input, ref leafProcessor, ref collector);

            leafProcessor.BaseRigidBodyIndex = 0;
            hasHit |= m_DynamicTree.BoundingVolumeHierarchy.Distance(input, ref leafProcessor, ref collector);

            return(hasHit);
        }
 public bool CalculateDistance <T>(ColliderDistanceInput input, ref T collector) where T : struct, ICollector <DistanceHit>
 {
     return(CollisionWorld.CalculateDistance(input, ref collector));
 }
示例#13
0
 public bool CalculateDistance(ColliderDistanceInput input, ref NativeList <DistanceHit> allHits) => QueryWrappers.CalculateDistance(ref this, input, ref allHits);
示例#14
0
        public static bool CalculateDistance <T>(ref T target, ColliderDistanceInput input) where T : struct, ICollidable
        {
            var collector = new AnyHitCollector <DistanceHit>(input.MaxDistance);

            return(target.CalculateDistance(input, ref collector));
        }
示例#15
0
 public bool CalculateDistance(ColliderDistanceInput input) => QueryWrappers.CalculateDistance(ref this, input);
示例#16
0
        public static bool CalculateDistance <T>(ref T target, ColliderDistanceInput input, ref NativeList <DistanceHit> allHits) where T : struct, ICollidable
        {
            var collector = new AllHitsCollector <DistanceHit>(input.MaxDistance, ref allHits);

            return(target.CalculateDistance(input, ref collector));
        }
示例#17
0
 public bool CalculateDistance(ColliderDistanceInput input, out DistanceHit closestHit) => QueryWrappers.CalculateDistance(ref this, input, out closestHit);
示例#18
0
 public bool CalculateDistance <T>(ColliderDistanceInput input, ref T collector) where T : struct, ICollector <DistanceHit>
 {
     return(Collider != null && Collider->CalculateDistance(input, ref collector));
 }
示例#19
0
 public bool CalculateDistance <T>(ColliderDistanceInput input, ref T collector) where T : struct, ICollector <DistanceHit>
 {
     return(Broadphase.CalculateDistance(input, m_Bodies, ref collector));
 }
示例#20
0
 public bool CalculateDistance <T>(ColliderDistanceInput input, ref T collector) where T : struct, ICollector <DistanceHit>
 {
     return(Collider.IsCreated && Collider.Value.CalculateDistance(input, ref collector));
 }