예제 #1
0
        /// <inheritdoc />
        public RayCastResults IntersectRay(MapId mapId, CollisionRay ray, float maxLength = 50, IEntity ignoredEnt = null)
        {
            RayCastResults result = default;

            this[mapId].Query((ref IPhysBody body, in Vector2 point, float distFromOrigin) => {
                if (distFromOrigin > maxLength)
                {
                    return(true);
                }

                if (body.Owner == ignoredEnt)
                {
                    return(true);
                }

                if (!body.CollisionEnabled)
                {
                    return(true);
                }

                if ((body.CollisionLayer & ray.CollisionMask) == 0x0)
                {
                    return(true);
                }

                // ReSharper disable once CompareOfFloatsByEqualityOperator
                if (result.Distance != 0f && distFromOrigin > result.Distance)
                {
                    return(true);
                }

                result = new RayCastResults(distFromOrigin, point, body.Owner);

                return(true);
            }, ray.Position, ray.Direction);
예제 #2
0
        /// <inheritdoc />
        public RayCastResults IntersectRay(MapId mapId, Ray ray, float maxLength = 50, IEntity ignoredEnt = null)
        {
            RayCastResults rayResults = default;

            bool Callback(int proxy, RayCastResults results)
            {
                if (results.HitEntity == ignoredEnt)
                {
                    return(false);
                }

                rayResults = results;
                return(true);
            }

            _broadphase.RayCast(Callback, mapId, ray, maxLength);

            DebugDrawRay?.Invoke(new DebugRayData(ray, maxLength, rayResults));
            return(rayResults);
        }
예제 #3
0
        /// <inheritdoc />
        public RayCastResults IntersectRayWithPredicate(MapId mapId, CollisionRay ray, float maxLength = 50, Func <IEntity, bool> predicate = null, bool ignoreNonHardCollidables = false)
        {
            RayCastResults result = default;

            this[mapId].Query((ref IPhysBody body, in Vector2 point, float distFromOrigin) => {
                if (distFromOrigin > maxLength)
                {
                    return(true);
                }

                if (predicate.Invoke(body.Owner))
                {
                    return(true);
                }

                if (!body.CollisionEnabled)
                {
                    return(true);
                }

                if ((body.CollisionLayer & ray.CollisionMask) == 0x0)
                {
                    return(true);
                }

                if (ignoreNonHardCollidables && !body.IsHardCollidable)
                {
                    return(true);
                }

                // ReSharper disable once CompareOfFloatsByEqualityOperator
                if (result.Distance != 0f && distFromOrigin > result.Distance)
                {
                    return(true);
                }

                result = new RayCastResults(distFromOrigin, point, body.Owner);

                return(true);
            }, ray.Position, ray.Direction);
예제 #4
0
        /// <inheritdoc />
        public IEnumerable <RayCastResults> IntersectRayWithPredicate(MapId mapId, CollisionRay ray,
                                                                      float maxLength = 50F,
                                                                      Func <IEntity, bool>?predicate = null, bool returnOnFirstHit = true)
        {
            List <RayCastResults> results = new List <RayCastResults>();

            this[mapId].Query((ref IPhysBody body, in Vector2 point, float distFromOrigin) =>
            {
                if (returnOnFirstHit && results.Count > 0)
                {
                    return(true);
                }

                if (distFromOrigin > maxLength)
                {
                    return(true);
                }

                if (!body.CanCollide)
                {
                    return(true);
                }

                if ((body.CollisionLayer & ray.CollisionMask) == 0x0)
                {
                    return(true);
                }

                if (predicate != null && predicate.Invoke(body.Entity))
                {
                    return(true);
                }

                var result = new RayCastResults(distFromOrigin, point, body.Entity);
                results.Add(result);
                DebugDrawRay?.Invoke(new DebugRayData(ray, maxLength, result));
                return(true);
            }, ray.Position, ray.Direction);
예제 #5
0
        /// <inheritdoc />
        public RayCastResults IntersectRay(Ray ray, float maxLength = 50, IEntity ignoredEnt = null)
        {
            IEntity entity      = null;
            var     hitPosition = Vector2.Zero;
            var     minDist     = maxLength;

            foreach (var body in _bodies)
            {
                if ((ray.CollisionMask & body.CollisionLayer) == 0x0)
                {
                    continue;
                }
                if (ray.Intersects(body.WorldAABB, out var dist, out var hitPos) && dist < minDist)
                {
                    if (!body.IsHardCollidable || ignoredEnt != null && ignoredEnt == body.Owner)
                    {
                        continue;
                    }

                    entity      = body.Owner;
                    minDist     = dist;
                    hitPosition = hitPos;
                }
            }

            RayCastResults results = default;

            if (entity != null)
            {
                results = new RayCastResults(minDist, hitPosition, entity);
            }

            DebugDrawRay?.Invoke(new DebugRayData(ray, maxLength, results));

            return(results);
        }