boxcastBroadphaseExcludingSelf() public static method

returns all colliders that are intersected by bounds excluding the passed-in collider (self). this method is useful if you want to create the swept bounds on your own for other queries
public static boxcastBroadphaseExcludingSelf ( Collider collider, RectangleF &rect, int layerMask = allLayers ) : IEnumerable
collider Collider Collider.
rect RectangleF
layerMask int
return IEnumerable
Ejemplo n.º 1
0
        public bool collidesWithAnyExcept(Collider exceptCollider, out CollisionResult result)
        {
            result = new CollisionResult();

            // fetch anything that we might collide with at our new position
            var neighbors = Physics.boxcastBroadphaseExcludingSelf(this, collidesWithLayers);

            foreach (var neighbor in neighbors)
            {
                if (neighbor == exceptCollider)
                {
                    continue;
                }

                // skip triggers
                if (neighbor.isTrigger)
                {
                    continue;
                }

                if (collidesWith(neighbor, out result))
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// moves the entity taking collisions into account
        /// </summary>
        /// <returns><c>true</c>, if move actor was newed, <c>false</c> otherwise.</returns>
        /// <param name="motion">Motion.</param>
        /// <param name="collisionResult">Collision result.</param>
        public bool move(Vector2 motion, out CollisionResult collisionResult)
        {
            collisionResult = new CollisionResult();

            // no collider? just move and forget about it
            if (entity.getComponent <Collider>() == null || _triggerHelper == null)
            {
                entity.transform.position += motion;
                return(false);
            }

            // 1. move all non-trigger Colliders and get closest collision
            var colliders = entity.getComponents <Collider>();

            for (var i = 0; i < colliders.Count; i++)
            {
                var collider = colliders[i];

                // skip triggers for now. we will revisit them after we move.
                if (collider.isTrigger)
                {
                    continue;
                }

                // fetch anything that we might collide with at our new position
                var bounds = collider.bounds;
                bounds.x += motion.X;
                bounds.y += motion.Y;
                var neighbors = Physics.boxcastBroadphaseExcludingSelf(
                    collider,
                    ref bounds,
                    collider.collidesWithLayers);

                foreach (var neighbor in neighbors)
                {
                    // skip triggers for now. we will revisit them after we move.
                    if (neighbor.isTrigger)
                    {
                        continue;
                    }

                    if (collider.collidesWith(neighbor, motion, out collisionResult))
                    {
                        // hit. back off our motion
                        motion -= collisionResult.minimumTranslationVector;
                    }
                }
            }

            ListPool <Collider> .free(colliders);

            // 2. move entity to its new position if we have a collision else move the full amount. motion is updated when a collision occurs
            entity.transform.position += motion;

            // 3. do an overlap check of all Colliders that are triggers with all broadphase colliders, triggers or not.
            // Any overlaps result in trigger events.
            _triggerHelper.update();

            return(collisionResult.collider != null);
        }
Ejemplo n.º 3
0
        void IUpdatable.update()
        {
            if (isImmovable)
            {
                velocity = Vector2.Zero;
                return;
            }

            if (shouldUseGravity)
            {
                velocity += Physics.gravity * Time.deltaTime;
            }

            entity.transform.position += velocity * Time.deltaTime;

            CollisionResult collisionResult;

            // fetch anything that we might collide with at our new position
            var neighbors = Physics.boxcastBroadphaseExcludingSelf(_collider, _collider.collidesWithLayers);

            foreach (var neighbor in neighbors)
            {
                // if the neighbor collider is of the same entity, ignore it
                if (neighbor.entity == entity)
                {
                    continue;
                }

                if (_collider.collidesWith(neighbor, out collisionResult))
                {
                    // if the neighbor has an ArcadeRigidbody we handle full collision response. If not, we calculate things based on the
                    // neighbor being immovable.
                    var neighborRigidbody = neighbor.entity.getComponent <ArcadeRigidbody>();
                    if (neighborRigidbody != null)
                    {
                        processOverlap(neighborRigidbody, ref collisionResult.minimumTranslationVector);
                        processCollision(neighborRigidbody, ref collisionResult.minimumTranslationVector);
                    }
                    else
                    {
                        // neighbor has no ArcadeRigidbody so we assume its immovable and only move ourself
                        entity.transform.position -= collisionResult.minimumTranslationVector;
                        var relativeVelocity = velocity;
                        calculateResponseVelocity(
                            ref relativeVelocity,
                            ref collisionResult.minimumTranslationVector,
                            out relativeVelocity);
                        velocity += relativeVelocity;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// caculates the movement modifying the motion vector to take into account any collisions that will
        /// occur when moving
        /// </summary>
        /// <returns><c>true</c>, if movement was calculated, <c>false</c> otherwise.</returns>
        /// <param name="motion">Motion.</param>
        /// <param name="collisionResult">Collision result.</param>
        public bool calculateMovement(ref Vector2 motion, out CollisionResult collisionResult)
        {
            collisionResult = new CollisionResult();

            // no collider? just move and forget about it
            if (entity.getComponent <Collider>() == null || _triggerHelper == null)
            {
                entity.transform.position += motion;
                return(false);
            }

            // 1. move all non-trigger Colliders and get closest collision
            var colliders = entity.getComponents <Collider>();

            for (var i = 0; i < colliders.Count; i++)
            {
                var collider = colliders[i];

                // skip triggers for now. we will revisit them after we move.
                if (collider.isTrigger)
                {
                    continue;
                }

                // fetch anything that we might collide with at our new position
                var bounds = collider.bounds;
                bounds.x += motion.X;
                bounds.y += motion.Y;
                var neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, ref bounds, collider.collidesWithLayers);

                foreach (var neighbor in neighbors)
                {
                    // skip triggers for now. we will revisit them after we move.
                    if (neighbor.isTrigger)
                    {
                        continue;
                    }

                    if (collider.collidesWith(neighbor, motion, out collisionResult))
                    {
                        // hit. back off our motion
                        motion -= collisionResult.minimumTranslationVector;
                    }
                }
            }
            ListPool <Collider> .free(colliders);

            return(collisionResult.collider != null);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// checks to see if this Collider with motion applied (delta movement vector) collides with any collider. If it does, true will be
        /// returned and result will be populated with collision data. Motion will be set to the maximum distance the Collider can travel
        /// before colliding.
        /// </summary>
        /// <returns><c>true</c>, if with was collidesed, <c>false</c> otherwise.</returns>
        /// <param name="motion">Motion.</param>
        /// <param name="result">Result.</param>
        public bool collidesWithAny(ref Vector2 motion, out CollisionResult result)
        {
            result = new CollisionResult();

            // fetch anything that we might collide with at our new position
            var colliderBounds = bounds;

            colliderBounds.x += motion.X;
            colliderBounds.y += motion.Y;
            var neighbors = Physics.boxcastBroadphaseExcludingSelf(this, ref colliderBounds, collidesWithLayers);

            // alter the shapes position so that it is in the place it would be after movement so we can check for overlaps
            var oldPosition = shape.position;

            shape.position += motion;

            var didCollide = false;

            foreach (var neighbor in neighbors)
            {
                // skip triggers
                if (neighbor.isTrigger)
                {
                    continue;
                }

                if (collidesWith(neighbor, out result))
                {
                    // hit. back off our motion and our Shape.position
                    motion         -= result.minimumTranslationVector;
                    shape.position -= result.minimumTranslationVector;
                    didCollide      = true;
                }
            }

            // return the shapes position to where it was before the check
            shape.position = oldPosition;

            return(didCollide);
        }