mercredi 6 mai 2015

C# collision: Bounce ball at 4 sides of a rectangle

I'm trying to make the an epic ping pong game, but there is one thing I don't get right.

PHOTO: http://ift.tt/1IfXHkg

If you look to the image, you can see that I have multiple rectangles in the middle of the canvas. The ball should bounce of it at all 4 sides. But whatever i am trying, i cant get it right.

I also want to change the following code with an IntersecWith code.

(yPos + bal.Width > recShapes.yPos && yPos < recShapes.yPos + recShapes.LandScapeRectangle.Width + 5)
                    &&
                    (xPos + bal.Height > recShapes.xPos && xPos < recShapes.xPos + recShapes.LandScapeRectangle.Height)

But I cant get the InterSect working, I get an error that says that there is no definition for IntersectsWith.

When the color of the ball is the same as the rectangle, the ball will go through the rectangle.

Don't throw to difficult things to me without explaining, i'm programming for not that long now :). I'm using WPF.

Thanks!

 protected void UpdateBall()
        {
            UpdatePlayers(mousePositionPlayerOne, AIposition);
            UpdateLandscape();

            // yPos and xPos are coordinates of the bal.
            yPos += yChange;
            xPos += xChange;

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

            if (doubleBallSpeedEnable)
            {
                UpdatePlayers(mousePositionPlayerOne, AIposition);
                UpdateLandscape();

                yPos += yChange;
                xPos += xChange;

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


            PlayerDied();
        }

    if ((yPos + bal.Width > recShapes.yPos && yPos < recShapes.yPos + recShapes.LandScapeRectangle.Width + 5)
                        &&
                        (xPos + bal.Height > recShapes.xPos && xPos < recShapes.xPos + recShapes.LandScapeRectangle.Height)
                    && ((!(ballPlayerBrush.Color.Equals(recShapes.LandScapeBrush.Color))) && (!(ballPlayerBrush.Color.Equals(recShapes.LandScapeBrush.Color))) || recShapes.LandScapeBrush.Color.Equals(Colors.LimeGreen)))
                {
                    double left = Double.MaxValue;
                    double right = Double.MaxValue;
                    double top = Double.MaxValue;
                    double bot = Double.MaxValue;



                    if (yChange > 0)
                    {
                        left = recShapes.xPos - xPos + bal.Width / 2;
                        if (left < 0)
                            left = -left;
                    }
                    if (xChange > 0)
                    {
                        top = recShapes.yPos - yPos + bal.Height / 2 + 5;
                        if (top < 0)
                            top = -top;
                    }

                    if (yChange < 0)
                    {
                        right = xPos - recShapes.xPos +                           recShapes.LandScapeRectangle.Width + bal.Height / 2;
                        if (right < 0)
                            right = -right;
                    }
                    if (xChange < 0)
                    {
                        bot = yPos - recShapes.yPos + recShapes.LandScapeRectangle.Height + bal.Height / 2 + 5 ;
                        if (bot < 0)
                            bot = -bot;
                    }


                    Console.WriteLine("top: \t" + top);
                    Console.WriteLine("left: \t" + left);
                    Console.WriteLine("right: \t" + right);
                    Console.WriteLine("bot: \t" + bot);

                    double lowestNumber = Math.Min(Math.Min(Math.Min(bot, top), left), right);
                    Console.WriteLine("bigge: \t" + lowestNumber);

                    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 = new SolidColorBrush(Colors.Blue);
                        recShapes.LandScapeBrush = new SolidColorBrush(Colors.Blue);
                    }else 
                    {
                        recShapes.LandScapeRectangle.Fill = new SolidColorBrush(Colors.Red);
                        recShapes.LandScapeBrush = new SolidColorBrush(Colors.Red);
                    }
                    //xChange = -xChange;

                } //<-- goede reset code!

Aucun commentaire:

Enregistrer un commentaire