0

I am creating a game similar to pong, but not exactly like it. Unfortunately, my logic is faulty, but I can't figure out what I should be doing. Currently, the ball falls vertically. I have an ellipse and a rectangle. If the ellipse intersects with the rectangle, it SHOULD "bounce" up like in physics without air resistance. Currently instead of doing that, it restarts from some position y rather than bouncing upwards.

To be clear, even just helping me understand how to make a projectile motion of the ball in the window will help. I believe that I can figure out how to make it interact with the "paddle," but I can't get the ball to be set in motion and move as though it's been hit with a "paddle"

Instead of writing the code, I'd really like to understand this because it seems like a fundamental aspect of any game. I will write the parts of the code that seem relevant and then more if needed.

ellipse = new Ellipse2D.Double((int)Ox, (int)Oy, 50, 50);
    protected class timeBall implements ActionListener{


            @Override
            public void actionPerformed(ActionEvent e) {
                    scale = 20.0;
                    speedY += 9.8*10.0/scale;
                    speedX = 50;
                    Oy = Oy + speedY * 10;
                    Ox = Ox + speedX *10;
                    Oy = Oy / scale;
                    Ox = Ox / scale;
                 //   System.out.println(speedX + " " + speedY);
                  //  System.out.println(Ox + " " + Oy);


                    try{
                        System.out.println(paddleRect.getX());
                       if(ellipse.intersects(paddleRect)){
                             Random random = new Random();

                             speedY *= -0.2;
                             Oy = Oy * -(random.nextInt(4));  

                        }

                }catch(RuntimeException NullPointerException){

                }
                    repaint();
            }

Note that this is not the entire code, but for simplicity sake, I've just put out what seems like it should be included.

  • 2
    Calling a variable `NullPointerException` is a really confusing idea. – Andy Turner Dec 04 '15 at 21:55
  • If `Oy` is the y position of the ellipse, why did you think to multiply its position by a negative random number? Why not just leave `Oy` as it is to be updated on the next loop? – Andy Turner Dec 04 '15 at 21:57
  • @AndyTurner I've tried a lot of different combinations. This just happened to be the latest attempt. I can't remember everything I've tried, but one for sure is that I just left Oy alone and changed the speedY to negative. –  Dec 04 '15 at 22:08
  • @AndyTurner I am going to change the name of the variable before I am done. That was just the exception that I wanted to make sure was caught. I don't know why I changed it from e so I will probably just change it back. –  Dec 04 '15 at 22:08
  • Well I can tell you for certain that you don't want to be *multiplying* the y position on a bounce - that certainly doesn't happen in the physical system. I would suggest that you look at moving the collision detection to the top of the method, before where you update the variables. – Andy Turner Dec 04 '15 at 22:10
  • @AndyTurner I appreciate the suggestion, but unfortunately I'm still running into the same issue. –  Dec 04 '15 at 22:13
  • A complete example is examined in possible [duplicate](http://stackoverflow.com/q/13999506/230513). – trashgod Dec 04 '15 at 22:30
  • Seeing that empty catch block and the variable named `NullPointerException` makes me want to say "Oy". – Hovercraft Full Of Eels Dec 04 '15 at 23:19

0 Answers0