Hello,

I recently took up a challenge to make a full screen pong game in java. So far it is full screen, it does the basics (bouncing off floor/roof/paddles) with a minor point system. I also added support for every 20 seconds it speeds up the ball by 1pixel per 0.05 seconds (50ms is the time I used for an update "delay"), there is also a ball color changer that changes the ball's color randomly every 2 seconds after the 1 minute mark. The 2 mini-challenges have a timer where if one of the 2 paddles misses the ball (aka scoring a point) then their timers are reset.

What I want to do is add gravity, and "reverse" gravity. These will be "power-ups" for the user to try and get while playing the game. I already know how to code the display part and how to make them only work on click. I just can't seem to figure out the code to make gravity work.

The ball has 5 properties:

  • ballX - X Position of the ball
  • ballY - Y Position of the ball
  • ballVelocityX - The rate at which the ball travels per pixel per update
  • ballVelocityY - The rate at which the ball travels per pixel per update
  • ballColor - Ball Color

The velocity is based off of this calculation:
rand is a Random number generator.

//Sets Ball Pos
		ballX = s.getWidth()/2-15;
		ballY = s.getHeight()/2-15;
		
		//Changes color back
		ballColor = Color.RED;
		
		//Ball Velocity
		ballVelocityY = Math.round(rand.nextInt(5));
		ballVelocityX = Math.round(rand.nextInt(5));
		while(ballVelocityY == ballVelocityX){
			ballVelocityY = Math.round(rand.nextInt(10));
		}
		while(ballVelocityX <= 5){
			ballVelocityX = Math.round(rand.nextInt(10));
		}
		while(ballVelocityY <= 5){
			ballVelocityY = Math.round(rand.nextInt(10));
		}
		
		//Ball Direction Handler
		int dir = rand.nextInt(4);
		if(dir==1){
			ballVelocityY = ballVelocityY*-1;
		}else if(dir==2){
			ballVelocityX = ballVelocityX*-1;
		}else if(dir==3){
			ballVelocityX = ballVelocityX*-1;
		}else if(dir==4){
			ballVelocityY = ballVelocityY*-1;
		}

The paddles have 2 properties, X and Y, both represent a position on screen.

There is one paddle for the computer to use, and one for the player.
the computer's paddle is set to follow the ball's Y position so it never misses.

The player's paddle is tracked through the mouse's Y position

I did make this at 4am local time so I might have included 20x the information I actually need to pass down.

Any help is good help :) (And well appreciated help)

--Turt2Live

Dani AI

Generated

Nice progress so far — building on ’s direction, make gravity a temporary acceleration field rather than a one‑off velocity change. Convert position/velocity to floating‑point and use time-based units so the effect is stable across frame rates (your current 50 ms tick => dt = 0.05s). Keep a small gravity state (direction, accel, remainingDuration) and apply acceleration to the Y velocity each tick; when the power‑up ends clear the state. Don’t instantly flip vy on activation — apply acceleration (or a small impulse) so motion stays smooth.

Example pattern (Java-style pseudocode):

double posY, velY;          // pixels, pixels/second
double gravityAccel = 400;  // pixels/sec^2 (tweak)
int gravityDir = 0;         // 0 = off, 1 = normal, -1 = reverse
double gravityTime = 0.0;   // seconds remaining

// inside your update loop, with dt in seconds (0.05)
if (gravityDir != 0) {
  velY += gravityDir * gravityAccel * dt;
  gravityTime -= dt;
  if (gravityTime <= 0) gravityDir = 0;
}
posY += velY * dt;

Troubleshooting & tips:

  • Round only for rendering; keep physics in double to avoid cumulative rounding error.
  • Cap velY or use sub‑stepping if the ball tunnels through paddles/walls at high speed.
  • On bounce, invert vy and multiply by a bounceLoss factor (0.8–0.95) for a realistic, lossy bounce.
  • For reverse gravity, set gravityDir = -1 instead of negating velY so the change is gradual.
  • If the CPU paddle currently “teleports” to ball Y, either keep that behavior (makes power-ups feel harder) or update the AI to predict under constant acceleration so difficulty stays fair.

Tune gravityAccel and duration to taste (try 200–800 px/s^2 and 2–6 seconds) and adjust bounceLoss to control how bouncy the game feels.

Recommended Answers

All 2 Replies

The principle is easy - on each "tick" of your clock add a small downwards increment to the Y velocity. This simulates the effect of gravity accelerating things downwards. Eg if you start with an upwards velocity it will slowly reduce to zero and then go negative.
You may also want to simulate a bit of air resistance by multiplying the velocity by a float just a bit smaller than 1 on each tick, and simulate lossy bouncing by losing a few percent of the velocity when reversing direction on the bounce.

ps When you start to do this kind of thing you will run into integer arithmetic limitations - you'll probably have to have the position & velocity variables as floats and only convert to int when actually painting pixels

The principle is easy - on each "tick" of your clock add a small downwards increment to the Y velocity. This simulates the effect of gravity accelerating things downwards. Eg if you start with an upwards velocity it will slowly reduce to zero and then go negative.
You may also want to simulate a bit of air resistance by multiplying the velocity by a float just a bit smaller than 1 on each tick, and simulate lossy bouncing by losing a few percent of the velocity when reversing direction on the bounce.

That makes more sense than trying to actually calculate physics, thank you so much :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.