0
votes

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

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
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
Take a look at this example then - MadProgrammer
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
Nope, using standard Java libraries. This would basically Rectangle, but could be any Shape object. - MadProgrammer

1 Answers

0
votes

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.