Example #1
0
        /// <summary>
        /// Check if a collision happened against the given collider
        /// </summary>
        /// <param name="collidable">Collider</param>
        /// <param name="bounceX">Whether we should bounce on X axis as result of the collision</param>
        /// <param name="bounceY">Whether we should bounce on Y axis as result of the collision</param>
        private void CheckCollision(Collidable collidable, ref bool bounceX, ref bool bounceY)
        {
            var game = (BrickBustGame)RB.Game;

            bool hit      = false;
            bool isPaddle = collidable is Paddle;

            for (int pass = 0; pass < 2; pass++)
            {
                // If we should only collide with top side then ignore collision if already past top side
                if (isPaddle && Rect.y > collidable.Rect.y)
                {
                    continue;
                }

                var depth = collidable.CollideRect.IntersectionDepth(Rect);

                if (depth.x != 0 || depth.y != 0)
                {
                    // Resolve shallow axis first
                    if (Mathf.Abs(depth.x) != 0 && Mathf.Abs(depth.x) < Mathf.Abs(depth.y))
                    {
                        hit = true;

                        if (!mHitCollidable.Contains(collidable))
                        {
                            mHitCollidable.Add(collidable);
                        }

                        Rect.x -= (int)depth.x;
                        bounceX = true;
                    }
                    else
                    {
                        Rect.y -= (int)depth.y;

                        hit = true;

                        if (!mHitCollidable.Contains(collidable))
                        {
                            mHitCollidable.Add(collidable);
                        }

                        // Paddle bouncing in Y axis gets special treatment where we want to control the bounce angle.
                        if (isPaddle)
                        {
                            PaddleBounce();
                        }
                        else
                        {
                            bounceY = true;
                        }
                    }
                }
            }

            if (hit)
            {
                mPos.x = Rect.x;
                mPos.y = Rect.y;
            }
        }