Sign in to follow this  
ajm113

Can someone suggest a better "logic" of my code?

Recommended Posts

ajm113    355
Ok, well I know some parts are unorthodox, but I keep getting a ball that won't bounce back, but would instead go along the walls or simply will freeze when near a paddle. Can anyone suggest some fix ups or what I should remove or add? I am making a pong game so the ball needs to atleast reflect into a another direction, instead of going back or forth or getting stuck along walls or the paddle. (C++/OpenGL) Here is my code:

bool isWithinDelta(float x, float y, float delta){
return abs(x - y) <= delta;
	
}

	//Ball Physics Processing

	  //PAD_LENGT
		

	//Ball Physics Processing


		
		//If the ball hits the top or bottom sides.
	  if(ballY >= 2.4){ 
		  
		  ballY += 0.02f; ballX +=0.02f;
	  }
	  else if(ballY <= -2.4){ 
		  
		  ballY -= ballX; ballX -=0.02f;
	  }

		//If the player trys to block the ball we must process the size of the paddles and make the ball change X dir.
		
	   
		else if (isWithinDelta(ballY, player1pos, 0.79)){
		ballX -=0.02f; ballY -=0.02f;
		}
		


		else if (isWithinDelta(ballY, player2pos, 0.79)){
		ballX +=0.02f; ballY +=0.02f;
		}
		

		//If a player misted lets figure out where and what should happen if it hits one of the X positions on the screen past the paddles
		else if(ballX >= 3.5) {
		ballX = 0.0f; ballY = 0.0f;
		}

		else if(ballX <= -3.5) {
		ballX = 0.0f; ballY = 0.0f;

		}else{
                //If nothing returned then make the ball move around.

		if(ballX < 0) ballX -= 0.02;
		if(ballX > 0) ballX += 0.02;
		

		if(ballY < 0) ballY -= 0.02;
		if(ballY > 0) ballY += 0.02;
		}


Can someone look at the code and please tell me what they think? I commented some of the stuff. [Edited by - ajm113 on March 23, 2008 2:17:32 AM]

Share this post


Link to post
Share on other sites
ajm113    355
Well I am updating it by position by position with 0.02 so I thought that would count as a velocity. But does it matter if the ball is moving fast the way you want it, but isn't moving the directions you want even if you did add velocity variable?

Share this post


Link to post
Share on other sites
flounder    100
If you keep adding a constant to the ball's position, it'll only move in one direction. How does the ball know it bounced off a wall if all you do is add .02 to it's x/y position every frame?

Share this post


Link to post
Share on other sites
ajm113    355
Well I return the ball's X or Y cord when it's in a specific X position then I tell it to revers its adding. Thats what I am wounder how can I tell it to keep going backwards or etc.

So whats a more proper way to do this then?

Share this post


Link to post
Share on other sites
ajm113    355
I changed it to float. Now it seems to work better, but the ball seems to shake and freeze when it gets close. I updated the code uptop.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this