Jump to content
  • Advertisement
Sign in to follow this  
ajm113

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

This topic is 3774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!