# The infamous pong

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

## Recommended Posts

So, as you could imagine, I'm writing a game of pong. So far, I have everything drawn, albeit crudely, but drawing and user input is fine, and collision detection is done. Now, here's my slight problem. Since this is 2d, the ball (currently a white box), is set up with an angle, and each time everything is drawn, the box is advanced in its path, by however far, using trig. Whenever a collision with the top or bottom walls takes place, I have it bouncing back at a random angle using this code:

if( /* top wall collision */ ){
// bounce
// generate random angles until one is between 90 and 270 (downwards)
float tempangle = ((float)(rand()/(float)RAND_MAX))*360.0f;

while( tempangle < 90.0f && tempangle > 270.0f )
tempangle = ((float)(rand()/(float)RAND_MAX))*360.0f;

((CBall*)game_items[ball_index])->setAngle(tempangle);		// set new angle
}

else if( /* bottom wall collision */ ){
// bounce
// generate random angles until one is between 270 and 90 (upwards)
float tempangle = ((float)(rand()/(float)RAND_MAX))*360.0f;

while( tempangle > 90.0f && tempangle < 270.0f )
tempangle = ((float)(rand()/(float)RAND_MAX))*360.0f;

((CBall*)game_items[ball_index])->setAngle(tempangle);		// set new angle
}


And with that, sometimes the ball will hit the top at an angle of 45 degrees and bounce backwards. I'm looking for a better way to generate the angle than by looking again the the incoming angle and make sure that, instead of the angle being in the right half, the angle being in the right quadrant. It seems a little inelegant. Is there a better way to do it that won't result in completely predicatable angles? Also, is there a better function to use than rand()? Even though I'm seeding it with 'time(NULL)', it practically always has the same first 2 or 3 bounces.

##### Share on other sites
Store your velocity as a vector, or two variables xSpeed and ySpeed. When you hit the top or bottom wall, negate ySpeed. That's all there is to it.

##### Share on other sites
Hey,

I'd do what wendigo23 suggested, but if you want to have some randomness to the bounce, add or subtract some angle from the ball after you have its new direction.

CJM

##### Share on other sites
Just for some efficiency you could use this to get a random number between min and max.

min+float(max-min)*rand()/RAND_MAX

##### Share on other sites
naturally if your using rand() youll nees to seed it first so it is actualy always random..

srand((unsigned int)time(0));

then you can call rand() % (high-low) + low

i suggest if youre going to make random angles, to cap what values you generate so you cant hit at 45 degs and bounc back....or handle it as two vectors, negate the y movement and add do x movement + rand() % (3-0) - 1.5

that way you get a random number between -1.5 and 1.5 which will change your angle....but beware....this method without capping the x movement could eventually lead to the ball bouncing at rediculous angles...

either way you do it...youll need to set a cap...either a max angle or max speed etc...hope this helps

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 96
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009646
• ### Who's Online (See full list)

There are no registered users currently online

×