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.