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

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.