Results 1 to 5 of 5

Thread: Pong in 2D - XNA GS2.0

  1. #1

    Thread Starter
    Hyperactive Member BillGeek's Avatar
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    440

    Pong in 2D - XNA GS2.0

    Hi All

    I'm very new to XNA. I installed it last night, and started coding away. I'm busy writing a classic Pong game just to get my head around the "workings" of XNA, and I believe I'm almost done with it.

    Attached is my current project. The scoring is working, as well as the inputs, though I can't seem to get the collission detection running smoothly just yet.

    As it stands, the ball bounces off the top and bottom sides of the window, and the ball also bounces off the paddles, though what I've noticed is that even if you touch the ball with the back of the paddle (by moving your paddle to such a position that you can "graze" the ball) it still bounces back. I know why this happens, though I can't seem to figure out a workaround.

    The code for the collission at the moment:
    Code:
    if (ballPosition.Y <= 0)
        ballDirection = ballDirectionWay.downwardsLeft;
    else if (ballPosition.Intersects(paddleLeft))
        ballDirection=ballDirectionWay.upwardsRight;
    else if (ballPosition.X < (paddleLeft.X + paddleLeft.Width))
        playerDroppedBall(1);
    break;
    I'm making use of the "intersects" function / method in XNA which basically checks whether two Rectangles are intersecting each other. Like I said: It works, though just not perfectly.

    I've uploaded the source code if you guys want to have a look. I'd be grateful to anyone with suggestions!

    (And here's a screenshot of the game in action )
    Attached Images Attached Images  
    Attached Files Attached Files

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Pong in 2D - XNA GS2.0

    Nice! Sounds like you just need a better collision detection routine.

    If I were doing that, I'd just use simple positional information. Is the ball's X position roughly matching the paddle's X position? If so, determine the Y positions of each. If the ball's Y position is anywhere within the paddle's Y position range, perform the deflection. Otherwise, continue on the current trajectory.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Hyperactive Member BillGeek's Avatar
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    440

    Re: Pong in 2D - XNA GS2.0

    I've been considering this, though the problem with this is that the ball might at times move "past" the paddle. (EG: ball X = 34, paddle left + paddle width = 30, ball moves at x - 5, the ball's position will now be 29) This should never happen, though if it does, the ball should bounce back instead of fall off the screen.

    The alternative would be to leave it as it is. In the slightest of times it will seem odd, but I don't think there's much I can do here.

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Pong in 2D - XNA GS2.0

    Have you tried putting your out-of-bounds section of the Else If structure before the part that detects for Intersection?

    You could fine-tune it then since it'll detect and process a missed ball using your given parameters (If ballPosition.X < paddleLeft.X + (paddleLeft.Width * 0.5)) before it processes a rebounded ball that was just barely clipped by the paddle.

    The next step would be to start implementing simple vector math to handle angles and where exactly it intersects on the paddle (closer to the edge rebounds at a steeper angle).

    Then work on ball-speed.

    The hardest thing I had to cope with for my Tetris game I made with XNA was making the game responsive and the controls usable once the speed started to get on the verge of crazy.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  5. #5

    Thread Starter
    Hyperactive Member BillGeek's Avatar
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    440

    Re: Pong in 2D - XNA GS2.0

    That's a very good suggestion. (Can't believe I didn't think of that! ) I'll try swapping the code and see how it works out.

    As for the vector math: I'll probably have a look at that in a couple of days, though right now I'm just using simple 45 degree angles. It's way easier!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width