dimanche 10 mai 2015

Bounce ping-pong ball on 4 sides of a Rect

I'm trying for so long now to let a ball bounce of 4 sides from a Rect. My algoritme for bouncing is not that great and if someone could help me would be great :).

A sidequestion: I also wonder how to prevent a ball being stuck in a Rectangle. After intersectWith it keeps changing the yPos, preventing the ball from going out.

private void UpdateBall()
    {
        ballRect.Width = ball.Width;
        ballRect.Height = ball.Height;
        ballRect.X = xPos;
        ballRect.Y = yPos;

        yPos += yChange;
        xPos += xChange;

        ball.Margin = new Thickness(yPos, xPos, 0, 0);

        ResetBallAfterPlayerDied();
    }

private void UpdateLandscape()
    {
        foreach (LandScapeCanvasRectangle recShapes in LandscapeToBallRecs)
        {
            landShapesRect.Width = recShapes.LandScapeRectangle.Width;
            landShapesRect.Height = recShapes.LandScapeRectangle.Height;
            landShapesRect.X = recShapes.xPos;
            landShapesRect.Y = recShapes.yPos;

            if (ballRect.IntersectsWith(landShapesRect)

                && (!(ballPlayerBrush.Color.Equals(recShapes.LandScapeBrush.Color))))
            {

                double left = Double.MaxValue;
                double right = Double.MaxValue;
                double top = Double.MaxValue;
                double bot = Double.MaxValue;

                left = recShapes.yPos - yPos + ball.Width / 2 - 20; // grootste getal eerst
                if (left < 0)
                    left = -left;
                top = recShapes.xPos - xPos + ball.Height / 2;
                if (top < 0)
                    top = -top;
                right = yPos - recShapes.yPos + recShapes.LandScapeRectangle.Width + ball.Height / 2 - 20;
                if (right < 0)
                    right = -right;
                bot = xPos - recShapes.xPos + recShapes.LandScapeRectangle.Height + ball.Height / 2;
                if (bot < 0)
                    bot = -bot;

                double lowestNumber = Math.Min(Math.Min(Math.Min(bot, top), left), right);

                if (lowestNumber == bot)
                {
                    xChange = -xChange;
                }

                if (lowestNumber == top)
                {
                    xChange = -xChange;
                }

                if (lowestNumber == left)
                {
                    yChange = -yChange;
                }

                if (lowestNumber == right)
                {
                    yChange = -yChange;
                }
                delayAfterImpact = 0;

                if (playerTwoHitBall)
                {
                    recShapes.LandScapeRectangle.Fill = landScapeBrush;
                    recShapes.LandScapeBrush = landScapeBrush;
                }

                if (playerOneHitBall)
                {
                    recShapes.LandScapeRectangle.Fill = landScapeBrush;
                    recShapes.LandScapeBrush = landScapeBrush;
                }
                playerOneImpactBall = false;
                playerTwoImpactBall = false;
            }
        }
    }

Aucun commentaire:

Enregistrer un commentaire