示例#1
0
        // Check collision with paddle
        public bool Collide(WidgetPaddle paddle)
        {
            bool result = false;

            // This isn't normally how you check if a circle intersects with a line, I'm cheating for now
            if (dPosY + m_dRadius >= paddle.dPosY && dPosY <= paddle.dPosY)
            {
                if (dPosX >= paddle.dPosX - paddle.m_dWidth * 0.5 && dPosX <= paddle.dPosX + paddle.m_dWidth * 0.5)
                    result = true;
                else if (Math.Sqrt(Math.Pow(dPosX - (paddle.dPosX - paddle.m_dWidth * 0.5), 2) + Math.Pow(0, 2)) <= m_dRadius)
                    result = true;
                else if (Math.Sqrt(Math.Pow(dPosX - (paddle.dPosX + paddle.m_dWidth * 0.5), 2) + Math.Pow(0, 2)) <= m_dRadius)
                    result = true;
            }
            return result;
        }
示例#2
0
        // Update ball's position and velocity, checking collision with the current paddle
        public void Update(WidgetPaddle paddle)
        {
            // Apply gravity
            m_dVelY += m_dGravity;

            // Update position
            dPosX += m_dVelX;
            dPosY += m_dVelY;

            if (dPosY > m_dMaxY)
            {
                // Check and handle falling below play area
                Reset();

                if (m_OnFall != null)
                    m_OnFall(this, this);
            }
            else if (Collide(paddle))
            {
                // Check and handle collision with the paddle

                // Reverse vertical velocity
                m_dVelY *= -1;
                dPosY = paddle.dPosY - m_dRadius;

                // Make the ball bounce off a bit based on distance from centre of paddle to make things interesting
                m_dVelX += (dPosX - paddle.dPosX) * paddle.m_dAngle;

                // Bring the ball's velocity closer to the paddle's
                //m_dVelX = paddle.m_dVelX * paddle.m_dFriction + m_dVelX * (paddle.m_dFriction - 1);

                if (m_OnCollide != null)
                    m_OnCollide(this, this);
            }

            // Enforce horizontal bounds, bouncing off if violated
            if (dPosX > m_dMaxX)
            {
                dPosX = m_dMaxX;
                m_dVelX *= -1;
            }
            else if (dPosX < m_dMinX)
            {
                dPosX = m_dMinX;
                m_dVelX *= -1;
            }
        }