Ejemplo n.º 1
0
        public static bool OverlapSphereCustom <T, C>(ref T target, float3 position, float radius, ref C collector, CollisionFilter filter, QueryInteraction queryInteraction = QueryInteraction.Default)
            where T : struct, ICollidable
            where C : struct, ICollector <DistanceHit>
        {
            PointDistanceInput input = new PointDistanceInput
            {
                Filter      = filter,
                MaxDistance = radius,
                Position    = position
            };

            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);
                }
            }
        }
Ejemplo n.º 2
0
            public bool DistanceLeaf <T>(PointDistanceInput 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 PointDistanceInput
                {
                    Position     = Mul(bodyFromWorld, input.Position),
                    MaxDistance  = input.MaxDistance,
                    Filter       = input.Filter,
                    QueryContext = new QueryContext
                    {
                        RigidBodyIndex          = rigidBodyIndex,
                        ColliderKey             = ColliderKey.Empty,
                        NumColliderKeyBits      = 0,
                        WorldFromLocalTransform = worldFromBody,
                        Entity        = body.Entity,
                        IsInitialized = true
                    }
                };

                return(body.CalculateDistance(inputLs, ref collector));
            }
Ejemplo n.º 3
0
 public unsafe bool CalculateDistance <T>(PointDistanceInput input, ref T collector) where T : struct, ICollector <DistanceHit>
 {
     fixed(TerrainCollider *target = &this)
     {
         return(DistanceQueries.PointCollider(input, (Collider *)target, ref collector));
     }
 }
Ejemplo n.º 4
0
            public bool DistanceLeaf <T>(PointDistanceInput 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 PointDistanceInput
                {
                    Position    = Mul(bodyFromWorld, input.Position),
                    MaxDistance = input.MaxDistance,
                    Filter      = input.Filter
                };

                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);
            }
Ejemplo n.º 5
0
        public bool CalculateDistance <T>(PointDistanceInput 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.Position = Mul(bodyFromWorld, input.Position);

            SetQueryContextParameters(ref input.QueryContext, ref worldFromBody);

            return(Collider.IsCreated && Collider.Value.CalculateDistance(input, ref collector));
        }
Ejemplo n.º 6
0
            public bool DistanceLeaf <T>(PointDistanceInput 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));
            }
Ejemplo n.º 7
0
        public static bool CalculateDistance <T>(ref T target, PointDistanceInput 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);
        }
Ejemplo n.º 8
0
        public bool CalculateDistance <T>(PointDistanceInput input, NativeSlice <RigidBody> rigidBodies, ref T collector)
            where T : struct, ICollector <DistanceHit>
        {
            if (input.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);
        }
Ejemplo n.º 9
0
            public bool DistanceLeaf <T>(PointDistanceInput 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.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 normalDirection = math.cross(vertices[1] - vertices[0], vertices[2] - vertices[0]);
                bool   acceptHit       = false;

                for (int polygonIndex = 0; polygonIndex < numPolygons; polygonIndex++)
                {
                    Result result;
                    if (isQuad)
                    {
                        result = QuadSphere(
                            vertices[0], vertices[1], vertices[2], vertices[3], normalDirection,
                            input.Position, 0.0f, MTransform.Identity);
                    }
                    else
                    {
                        result = TriangleSphere(
                            vertices[0], vertices[1], vertices[2], normalDirection,
                            input.Position, 0.0f, MTransform.Identity);
                    }

                    if (result.Distance < collector.MaxFraction)
                    {
                        acceptHit |= collector.AddHit(new DistanceHit
                        {
                            Fraction      = result.Distance,
                            Position      = result.PositionOnAinA,
                            SurfaceNormal = -result.NormalInA,
                            ColliderKey   = new ColliderKey(m_NumColliderKeyBits, (uint)(primitiveKey << 1 | polygonIndex))
                        });
                    }
                }

                return(acceptHit);
            }
Ejemplo n.º 10
0
 public bool CalculateDistance <T>(PointDistanceInput input, ref T collector) where T : struct, ICollector <DistanceHit>
 {
     return(CollisionWorld.CalculateDistance(input, ref collector));
 }
Ejemplo n.º 11
0
 public bool CalculateDistance(PointDistanceInput input, ref NativeList <DistanceHit> allHits) => QueryWrappers.CalculateDistance(ref this, input, ref allHits);
Ejemplo n.º 12
0
        public static bool CalculateDistance <T>(ref T target, PointDistanceInput input) where T : struct, ICollidable
        {
            var collector = new AnyHitCollector <DistanceHit>(input.MaxDistance);

            return(target.CalculateDistance(input, ref collector));
        }
Ejemplo n.º 13
0
        public static unsafe bool PointCollider <T>(PointDistanceInput input, Collider *target, ref T collector) where T : struct, ICollector <DistanceHit>
        {
            if (!CollisionFilter.IsCollisionEnabled(input.Filter, target->Filter))
            {
                return(false);
            }

            Result result;

            switch (target->Type)
            {
            case ColliderType.Sphere:
                var sphere = (SphereCollider *)target;
                result = PointPoint(sphere->Center, input.Position, sphere->Radius, sphere->Radius);
                break;

            case ColliderType.Capsule:
                var capsule = (CapsuleCollider *)target;
                result = CapsuleSphere(capsule->Vertex0, capsule->Vertex1, capsule->Radius, input.Position, 0.0f, MTransform.Identity);
                break;

            case ColliderType.Triangle:
                var triangle = (PolygonCollider *)target;
                result = TriangleSphere(
                    triangle->Vertices[0], triangle->Vertices[1], triangle->Vertices[2], triangle->Planes[0].Normal,
                    input.Position, 0.0f, MTransform.Identity);
                break;

            case ColliderType.Quad:
                var quad = (PolygonCollider *)target;
                result = QuadSphere(
                    quad->Vertices[0], quad->Vertices[1], quad->Vertices[2], quad->Vertices[3], quad->Planes[0].Normal,
                    input.Position, 0.0f, MTransform.Identity);
                break;

            case ColliderType.Convex:
            case ColliderType.Box:
            case ColliderType.Cylinder:
                ref ConvexHull hull = ref ((ConvexCollider *)target)->ConvexHull;
                result = ConvexConvex(hull.VerticesPtr, hull.NumVertices, hull.ConvexRadius, &input.Position, 1, 0.0f, MTransform.Identity);
                break;

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

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

            default:
                throw new NotImplementedException();
            }

            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);
        }
Ejemplo n.º 14
0
 public bool CalculateDistance(PointDistanceInput input) => QueryWrappers.CalculateDistance(ref this, input);
Ejemplo n.º 15
0
        public static bool CalculateDistance <T>(ref T target, PointDistanceInput input, ref NativeList <DistanceHit> allHits) where T : struct, ICollidable
        {
            var collector = new AllHitsCollector <DistanceHit>(input.MaxDistance, ref allHits);

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