Advertisement

optimizing Breakout ball-bouncing

Started by September 04, 2002 07:22 PM
-1 comments, last by sofsenint 22 years, 2 months ago
I'm working on a Breakout clone and had actually made a lot of progress until I decided to make my ball-bouncing algorithm a little more accurate. The way that I had it, the program just checked to see if adding the velocity per frame of the ball would make it hit a side, and, if so, it reversed the velocity. However, when the velocity gets high at all you can tell that the ball isn't actually hitting the wall before it rebounds. So I decided to try a different approach. First, it used the same check as before. If it did hit a wall, it would go into further checking to see how many individual pixels it could move before it actually touched anything. To do this, I use a for loop that continually subtracts one from the number of pixels to be moved until it could be done without overshooting a side. As you may have guessed, this isn't exactly very speedy. In fact, it is incredibly, horrendously, unbelievably slow. I was wondering if anyone had any different ideas on how to do this or optimizations I could implement. Here is the code I am using:
  int MoveBall()
{
	if(ballx+dx<0)//ball hit left wall

	{/*the ball hit the left wall, so we can assume that it had a negative dx*/
		for(int pixels = dx+1; pixels <= 0; pixels++)
		{/*Here, we start by testing to see if moving the ball one less pixel than dx
		 will still put us off the screen.  If it will, we continue subtracting one until 
		 it stays onscreen or until we discover that the ball is already in contact with
		 the wall and must rebound*/
			if(ballx+pixels<0)
			{
			}
			else
			{
				ballx += pixels;
				break;
			}
		}
		dx = -dx;
	}
	else if(ballx+BALL_WIDTH+dx>=1024)//ball hit right wall
	{/*the ball hit the right wall, so we can assume that it had a positive dx*/
		for(int pixels = dx -1; pixels>0; pixels--)
		{//for an explanation of why this part should work(in theory), see above
			if(ballx+pixels+BALL_WIDTH>1024)
			{
			}
			else
			{
				ballx += pixels;
				break;
			}
		}
		dx = -dx;
	}
	else
	{
		ballx += dx;
	}
	if(bally+dy<0)//ball hit top
	{/*since the ball hit the top, we can assume that dy has a negative value*/
		for(int pixels = dy + 1; pixels <= 0; pixels++)
		{
			if(bally+pixels<0)
			{
			}
			else
			{
				bally+=pixels;
				break;
			}
		}
		dy = -dy;
	}
	else if(bally+BALL_HEIGHT+dy>740)//ball hit line parrallel to top of paddle

	{//since the ball hit the bottom, dx is positive

		for(int pixels = dy -1; pixels>=0; pixels--)
		{
			if(bally+pixels+BALL_HEIGHT>740)
			{
			}
			else
			{
				bally+=pixels;
				break;
			}
		}
		dy = -dy;
	}
	else
	{
		bally+=dy;
	}

	return 1;
}  
Thanks in advance for your help! [edited by - sofsenint on September 4, 2002 8:23:52 PM] [edited by - sofsenint on September 4, 2002 8:25:13 PM]
-----------------------------Weeks of programming can save you hours of planning.There are 10 kinds of people in this world-- those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement