boxcastBroadphase() public static method

returns all colliders with bounds that are intersected by collider.bounds. Note that this is a broadphase check so it only checks bounds and does not do individual Collider-to-Collider checks!
public static boxcastBroadphase ( RectangleF rect, int layerMask = allLayers ) : IEnumerable
rect RectangleF
layerMask int Layer mask.
return IEnumerable
        /// <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>
        public bool move(Vector2 motion)
        {
            if (_collider == null)
            {
                return(false);
            }

            var didCollide = false;

            // fetch anything that we might collide with at our new position
            entity.transform.position += motion;

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

            foreach (var neighbor in neighbors)
            {
                if (_collider.overlaps(neighbor))
                {
                    didCollide = true;
                    notifyTriggerListeners(_collider, neighbor);
                }
            }

            return(didCollide);
        }
Example #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)
        {
            var collider = entity.colliders.mainCollider;

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

            // remove ourself from the physics system until after we are done moving
            entity.colliders.unregisterAllCollidersWithPhysicsSystem();
            var didCollide = false;

            // fetch anything that we might collide with at our new position
            entity.transform.position += motion;

            // fetch anything that we might collide with us at our new position
            var neighbors = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);

            foreach (var neighbor in neighbors)
            {
                if (collider.overlaps(neighbor))
                {
                    didCollide = true;
                    notifyTriggerListeners(collider, neighbor);
                }
            }

            // let Physics know about our new position
            entity.colliders.registerAllCollidersWithPhysicsSystem();

            return(didCollide);
        }
Example #3
0
        void IUpdatable.update()
        {
            if (isImmovable)
            {
                velocity = Vector2.Zero;
                return;
            }

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



            entity.colliders.unregisterAllCollidersWithPhysicsSystem();
            entity.transform.position += velocity * Time.deltaTime;

            CollisionResult collisionResult;
            // fetch anything that we might collide with at our new position
            var neighbors = Physics.boxcastBroadphase(entity.colliders.mainCollider.bounds, entity.colliders.mainCollider.collidesWithLayers);

            foreach (var neighbor in neighbors)
            {
                if (entity.colliders.mainCollider.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;
                    }
                }
            }

            entity.colliders.registerAllCollidersWithPhysicsSystem();
        }
        /// <summary>
        /// update should be called AFTER Entity is moved. It will take care of any ITriggerListeners that the Collider overlaps.
        /// </summary>
        public void update()
        {
            // 3. do an overlap check of all entity.colliders that are triggers with all broadphase colliders, triggers or not.
            // Any overlaps result in trigger events.
            var colliders = _entity.getComponents <Collider>();

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

                // fetch anything that we might collide with us at our new position
                var neighbors = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
                foreach (var neighbor in neighbors)
                {
                    // we need at least one of the colliders to be a trigger
                    if (!collider.isTrigger && !neighbor.isTrigger)
                    {
                        continue;
                    }

                    if (collider.overlaps(neighbor))
                    {
                        var pair = new Pair <Collider>(collider, neighbor);

                        // if we already have this pair in one of our sets (the previous or current trigger intersections) dont call the enter event
                        var shouldReportTriggerEvent = !_activeTriggerIntersections.Contains(pair) &&
                                                       !_previousTriggerIntersections.Contains(pair);
                        if (shouldReportTriggerEvent)
                        {
                            notifyTriggerListeners(pair, true);
                        }

                        _activeTriggerIntersections.Add(pair);
                    }
                    // overlaps
                }
                // end foreach
            }

            ListPool <Collider> .free(colliders);

            checkForExitedColliders();
        }
Example #5
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.colliders.Count == 0 || _triggerHelper == null)
            {
                entity.transform.position += motion;
                return(false);
            }

            // remove ourself from the physics system until after we are done moving
            entity.colliders.unregisterAllCollidersWithPhysicsSystem();

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

            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.boxcastBroadphase(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();

            // let Physics know about our new position
            entity.colliders.registerAllCollidersWithPhysicsSystem();

            return(collisionResult.collider != null);
        }
Example #6
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.colliders.Count == 0)
            {
                entity.transform.position += motion;
                return(false);
            }

            // remove ourself from the physics system until after we are done moving
            entity.colliders.unregisterAllCollidersWithPhysicsSystem();

            // 1. move all non-trigger entity.colliders and get closest collision
            for (var i = 0; i < entity.colliders.Count; i++)
            {
                var collider = entity.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.boxcastBroadphase(ref bounds, collider.collidesWithLayers);

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

                    CollisionResult tempCollisionResult;
                    if (collider.collidesWith(neighbor, motion, out tempCollisionResult))
                    {
                        // hit. compare with the previous hit (if we have one) and choose the one that is closest (smallest MTV)
                        if (collisionResult.collider == null ||
                            collisionResult.minimumTranslationVector.LengthSquared() > tempCollisionResult.minimumTranslationVector.LengthSquared())
                        {
                            collisionResult = tempCollisionResult;
                        }
                    }
                }
            }

            // 2. move entity to its new position if we have a collision else move the full amount
            if (collisionResult.collider != null)
            {
                entity.transform.position += motion - collisionResult.minimumTranslationVector;
            }
            else
            {
                entity.transform.position += motion;
            }

            // 3. do an overlap check of all entity.colliders that are triggers with all broadphase colliders, triggers or not.
            //    Any overlaps result in trigger events.
            for (var i = 0; i < entity.colliders.Count; i++)
            {
                var collider = entity.colliders[i];

                // fetch anything that we might collide with us at our new position
                var neighbors = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
                foreach (var neighbor in neighbors)
                {
                    // we need at least one of the colliders to be a trigger
                    if (!collider.isTrigger && !neighbor.isTrigger)
                    {
                        continue;
                    }

                    if (collider.overlaps(neighbor))
                    {
                        var pair = new Pair <Collider>(collider, neighbor);

                        // if we already have this pair in one of our sets (the previous or current trigger intersections) dont call the enter event
                        var shouldReportTriggerEvent = !_activeTriggerIntersections.Contains(pair) && !_previousTriggerIntersections.Contains(pair);
                        if (shouldReportTriggerEvent)
                        {
                            notifityTriggerListeners(pair, true);
                        }

                        _activeTriggerIntersections.Add(pair);
                    }             // overlaps
                }                 // end foreach
            }

            // let Physics know about our new position
            entity.colliders.registerAllCollidersWithPhysicsSystem();

            checkForExitedColliders();

            return(collisionResult.collider != null);
        }