コード例 #1
0
ファイル: CollisionDetector.cs プロジェクト: BNHeadrick/Bros
        /// <summary>
        /// Moves a collider in the direction provided until it hits something
        /// </summary>
        /// <param name="mover">Collider which is moving</param>
        /// <param name="deltaPosition">Change in position the collider is trying to make</param>
        public void handleMovement(Collider mover, Vector2 deltaPosition)
        {
            double areaOfMovementX1 = Math.Min(mover.Bounds.X, mover.Bounds.X + deltaPosition.X);
            double areaOfMovementY1 = Math.Min(mover.Bounds.Y, mover.Bounds.Y + deltaPosition.Y);
            double areaOfMovementX2 = Math.Max(mover.Bounds.X + mover.Bounds.Width, mover.Bounds.X + mover.Bounds.Width + deltaPosition.X);
            double areaOfMovementY2 = Math.Max(mover.Bounds.Y + mover.Bounds.Height, mover.Bounds.Y + mover.Bounds.Height + deltaPosition.Y);

            DoubleRect areaOfMovement = new DoubleRect(
                areaOfMovementX1,
                areaOfMovementY1,
                areaOfMovementX2 - areaOfMovementX1,
                areaOfMovementY2 - areaOfMovementY1);

            //DoubleRect newbounds = mover.Bounds + deltaPosition;

            List<Collider> collisions = m_tree.Query(areaOfMovement);
            List<Collider>.Enumerator i = collisions.GetEnumerator();
            Vector2 allowedMovement = deltaPosition;
            Vector2 temp;
            while (i.MoveNext())
            {
                // we will usually collide with our old position - ignore that case
                if (i.Current != mover)
                {
                    bool canMove = CollisionHandler.handleMovement(mover, i.Current, deltaPosition, out temp);
                    if (!canMove)
                    {
                        return; // don't allow movement
                    }
                    if (allowedMovement.X > 0.0f)
                        allowedMovement.X = Math.Min(allowedMovement.X, temp.X);
                    else if (allowedMovement.X < 0.0f)
                        allowedMovement.X = Math.Max(allowedMovement.X, temp.X);
                    if (allowedMovement.Y > 0.0f)
                        allowedMovement.Y = Math.Min(allowedMovement.Y, temp.Y);
                    else if (allowedMovement.Y < 0.0f)
                        allowedMovement.Y = Math.Max(allowedMovement.Y, temp.Y);
                }
            }

            mover.move(allowedMovement);
        }