4

Ping Pong

Hi, I'm making this game called "Ping Pong". I made an algo for the movement of the ball. Now, I want that whenever the ball touches or collides with the player (pictured above), it should bounce off just as it bounces when it touch the edges. Any suggestions??

For ref. here's a snippet from my code till now...

private void Ball()
        {
            int x = 1, y = 1, dx = 8, dy = 8;
            System.Drawing.Point p;

            while (true)
            {
                p = player.Location;

                /*
                           here's where two or more if statements are required
                */

                if (x < 1)
                    dx = dx + 6;

                if (x >= 800 - ball1.Width)
                    dx = dx - 6;

                if (y < 0)
                    dy = dy + 6;

                if (y > 450 - ball1.Height)
                    dy = dy - 6;

                if (x == -3 && y == -2)
                    dy = dy - 6;

                x = x + dx;
                y = y + dy;

                ball1.Invoke
                (
                    (MethodInvoker) delegate {   ball1.Location = new System.Drawing.Point(x,y);   }
                );

                Thread.Sleep(50);
            } 
aayani
  • 333
  • 9
  • 19
  • 4
    This should probably be migrated to http://gamedev.stackexchange.com – Moo-Juice Dec 24 '13 at 19:54
  • thanks. hope that's of some help. – aayani Dec 24 '13 at 19:57
  • 1
    What you are looking for here is called "collision detection". It appears you have hard coded the boundaries of the window. An object oriented form of collision detection would involve all objects inheriting from a base "GameObject" that can check for collisions. For more information on the general topic, see here: http://en.wikipedia.org/wiki/Collision_detection – drew_w Dec 24 '13 at 20:26
  • ^thank you. but unfortunately, that doesn't help. Sorry to say :( – aayani Dec 24 '13 at 20:30
  • I'm confused by `dx`. It's meant to be velocity, but it looks like a value of "8" is forwards, and a value of "2" is backwards? – McGarnagle Dec 24 '13 at 20:39
  • I'm using dx to simply calculate the new position of the ball and it's working fine. – aayani Dec 24 '13 at 20:42

1 Answers1

0

You don't just need the player's location, you need its bounds as well. Then you have several options. The easiest one is simply to check

if (x - ball1.Width > player.Left && x - ball1.Width < player.Right &&
    y - ball1.Height > player.Top && y - ball1.Height < player.Bottom)

If it is, you have a collision, and have to bounce.

Note, that in this simplified calculation, you're actually treating the ball as a rectangle instead of a circle. You're already doing that with your bound detection, though, so I'm betting you won't mind.

Having said that, I do recommend you take @drew_w's advice in the comments. This is going to get ugly and hard to maintain very fast. Find an object oriented solution. (If nothing else, put getBoundingBox on all drawable objects, so you can make an engine that checks for collisions.)

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76