First Step CSharp - Sprite Collisions

From SwinGame

Although it is fun watching a ball bounce aimlessly around the screen it would be nice if we could interact with it. To do this we need to tell our program to look out for a collision. Namely a collision between the ball and the paddle. If this collision occurs then obviously we need to tell the program to bounce the ball off in the other direction the same as if we had hit the bottom or top by reversing the Y axis of the path of the ball.

The code to test if there is a collision between the two sprites is:

if (Physics.HaveSpritesCollided(ball, paddle)) //if the ball and paddle have collided
     ball.Movement.Y = -ball.Movement.Y; //reverse the y axis of the path 

Add this to your game loop.

Now you do this...

  • Make the "paddlehit" sound play when there is a collision between the ball and the paddle (easy)
  • Add a second paddle to the game so you can play against a friend in a pong style game. You will need to follow the same process you did for the first paddle.
    1. Download the RedPaddle
    2. Add the new image to Gameresources in the code
    3. Now back to GameLogic to declare the new Sprite also set position (x and y) of red paddle and set the buttons to make the paddle move (perhaps z and x)
    4. In Game Loop draw the red paddle and set up the collision, stop it being able to move off the screen. Then test it out against a friend.
    5. Figure out how to add Scoring to your game
Tip: You can of course copy, paste and edit the from the code for the first paddle for the red paddle.

Advanced Sprite Collisions

As you play the game for a while you will notice something is not right with what happens when the ball hits the side of the paddle. It seems to get stuck to the paddle, work its way across and then get free. This is to do with the fact that the game does not understand side collisions. In order to make it understand this we need to add the following code to GameLogic inside the class GameLogic:

private static void PerformCollision(Sprite ball, Sprite paddle)
{
        LineSegment hitLine;
        LineSegment[] lines;
        Vector vectTemp;
        double shortest;
        Point2D temp, ballCenter;
 
        //Get rectangles that surround the paddle and ball
        Rectangle pdlRect = Shapes.CreateRectangle(paddle);
        Rectangle ballRect = Shapes.CreateRectangle(ball);
 
        //Get the 4 lines that make up the paddle - top, bottom, left and right
        lines = Shapes.LinesFromRect(pdlRect);
 
        //Get the vector to move the ball sprite out of the paddle sprite - they have collided
        // This vector indicates how to move the ball to back it out of the paddle
        Vector mv = Physics.VectorOutOfRectFromRect(ballRect,pdlRect, ball.Movement.AsVector());
 
        //Now use the vector to move the ball out of the paddle
        Graphics.MoveSprite(ball, mv);
 
        //Get the center point of the ball
        ballCenter = Shapes.CenterPoint(ball);
 
        //Find the side of the paddle that is closest to the ball 
        // this is the side that it hit... so we will bounce off this side
        // it must be < than width as we did collide... so start shortest at 
        // the width of the ball
        shortest = ball.Width;
        hitLine = lines[0];
 
        //find the closest point
        for (int i = 0; i < 4; i++)
        {
            //Find the point on the line closest to the center of the ball
            temp = Shapes.ClosestPointOnLine(ballCenter, lines[i]);
            vectTemp = Physics.VectorFromPoints(ballCenter, temp);
 
            //If this is shorter than the current shortest - change shortest
            if (Physics.Magnitude(vectTemp) < shortest)
            {
                shortest = Physics.Magnitude(vectTemp);
                hitLine = lines[i];
            }
        }
 
        //Get the ball to bounce off the line that it hit...
        //  SwinGame will take care of calculating the angles etc.
        Physics.CircleCollisionWithLine(ball, hitLine);
    }

Then change the collision code down in your game loop to the following (oh and don't forget to add the line for the "paddlehit" sound to this as well):

//ball collision with paddles
            if (Physics.HaveSpritesCollided(ball, paddle))
            {
                PerformCollision(ball, paddle);
            }
 
            if (Physics.HaveSpritesCollided(ball, redPaddle))
            {
                PerformCollision(ball, redPaddle);
            }

Next let's get a Scoring feature for our game so you can keep track with who is winning.