0

I'm trying to implement a simple pong game. I want the ball to change X direction or Y direction depending what side of the ball was hit.

The ball moves at 3 pixels per second and has a width of 22 and height of 22. The paddles have a width and height of 32.

For collision detection on the ball, should I just create one rectangle and check for collision with the center point?

Alternatively I could create 4 rectangles around the ball to represent the sides, with an appropriate width, given that the ball moves at 3 pixels per frame.

Another option is to create a method that will check for collision at least 2 pixels in the direction of motion of the ball.

If the ball is moving to the right, and the x-position is 16, the next frame will be 19. Should I create a method that will check x for 16, 17 and 18, to make sure if there is a collision it will hit the right side and not cause the ball to actually go inside the cube?

@Sig

I now have this as my collision detection

if(Rect.intersects(balls.get(j).ball, levelBlocks.blocks.get(i).rect))
            {
            //Bottom
            if(balls.get(j).yPos <= levelBlocks.blocks.get(i).yPos - (32/2))
            {
                balls.get(j).ySpeed = balls.get(j).ySpeed * -1;
            }

            //Top
            if(balls.get(j).yPos >= levelBlocks.blocks.get(i).yPos + (32/2))
            {   
                balls.get(j).ySpeed = balls.get(j).ySpeed * -1;
            }

            //Left
            if(balls.get(j).xPos < levelBlocks.blocks.get(i).xPos)
            {
                balls.get(j).xSpeed = balls.get(j).xSpeed * -1;
            }

            //Right
            if(balls.get(j).xPos > levelBlocks.blocks.get(i).xPos)
            {
                balls.get(j).xSpeed = balls.get(j).xSpeed * -1;
            }

This works, but not 100%, it still seems abit off. if the ball hits two blocks at the same time, it will invert the invert so the direction will go back again.

I then changed it to this

if(Rect.intersects(balls.get(j).ball, levelBlocks.blocks.get(i).rect))
            {
            //Bottom
            if(balls.get(j).yPos <= levelBlocks.blocks.get(i).yPos - (32/2))
            {
                collision = 2;
            }

            //Top
            if(balls.get(j).yPos >= levelBlocks.blocks.get(i).yPos + (32/2))
            {   
                collision = 2;
            }

            //Left
            if(balls.get(j).xPos <= levelBlocks.blocks.get(i).xPos + (32/2))
            {
                collision = 1;
            }

            //Right
            if(balls.get(j).xPos >= levelBlocks.blocks.get(i).xPos + (32/2))
            {
                collision = 1;
            }

            temp = levelBlocks.blocks.get(i);
            levelBlocks.blocks.remove(i);
            combo.blocksDestroied += 1;
            Assets.score += 10 * combo.comboMultiplier;
            }


        }

        if(collision == 1)
        {
            balls.get(j).xSpeed = balls.get(j).xSpeed * -1;
            collision = 0;
        }

        if(collision == 2)
        {
            balls.get(j).ySpeed = balls.get(j).ySpeed * -1;
            collision = 0;
        }

This does work, but every now and then the ball will just start randomly going through blocks, it is very odd to look at, really confused on why it is doing it, but I do feel it is because of the 3 pixels per frame

Canvas
  • 5,779
  • 9
  • 55
  • 98
  • 1
    I'd start with 3 rectangles, one for the ball and one for each paddle. It would then detect if ANY collision has occurred and invert the x/y deltas – MadProgrammer Jun 10 '13 at 01:57
  • Well I have blocks, and if it hits a block from the left, top, right or bottom side I want the X or Y to be inverted depending on the collision, I have a basic version working like the one you stated already. – Canvas Jun 10 '13 at 02:06
  • Take a look at this **[example](http://stackoverflow.com/questions/13261767/java-ball-object-doesnt-bounce-off-of-drawn-rectangles-like-its-supposed-to/13263022#13263022)** then – MadProgrammer Jun 10 '13 at 02:13
  • MadProgrammer, I am looking at your code in the link you gave, you are useing if (collision.intersects(bounds)) I take it this was build using the Android API or what collision a custom method? or a Java API? – Canvas Jun 10 '13 at 02:19
  • Nope, using standard Java libraries. This would basically `Rectangle`, but could be any `Shape` object. – MadProgrammer Jun 10 '13 at 02:20

1 Answers1

0

Because the shapes you're working with are so simple, why not be perfect? Use a rectangle for the paddles and a circle for the ball when performing hit detection. Start by checking for collisions on a frame-by-frame basis; at the speeds you're working with, you shouldn't need to "look into the future" for future collisions.

sigpwned
  • 6,957
  • 5
  • 28
  • 48
  • Well I am trying to implement blocks into the game also, so depending on what side was hit depends on which X or Y speed is then inverted, any sample code would be awesome :) – Canvas Jun 10 '13 at 02:08
  • you may find [this answer on the gamedev stack exchange](http://gamedev.stackexchange.com/questions/22609/breakout-collision-detecting-the-side-of-collision) useful. What you're building sounds a lot like "breakout," which is an old-school break-the-bricks-with-a-ball game. That post talks about hit detection for a breakout-style game. – sigpwned Jun 10 '13 at 02:10
  • Yep I couldn't think of the word/game :D I want to try and mix pong with breakout :), I will take a look at that article – Canvas Jun 10 '13 at 02:16
  • Cool! The game has also been known as "arkanoid" in the past, if that's useful. – sigpwned Jun 10 '13 at 02:20
  • Sigpwned I have updated my code to show you what I added but still doesn't work correctly – Canvas Jun 10 '13 at 02:25