Bouncing ball?

Started by
5 comments, last by DevLiquidKnight 20 years, 6 months ago
I have been trying to figure this out. I made collsion detection detect so that if the ball x or y is less then 0 to go other direction and if x or y is greater then the width of the window go in the other direction but it seems as if the ball movement is not realistic its only doing like 90 and 45 degree angles I was wondering how do I make it so that it is more realistic and does not only go 90 and 45 degree angles. eg.

	double x = ball.x
	double y = ball.y
	double speed = ball.speed;
	if(x >= 450)	{
		ball.xdir = -ball.xdir;
	}
	if(y <= 0)// Player scores
	{
		user_score += 1;
		ball.speed+=computer_score/10;
		ball.ydir = -ball.ydir;
	}
	if(x < 0) 
	{
		ball.xdir = -ball.xdir;
	}
	if(y >= 400) // Computer scores
	{
		computer_score+=1;
		ball.speed+=computer_score/10;
		ball.ydir = -ball.ydir;
	}
 
coder requires 0xf00d before continue().
Killer Eagle Software [edited by - DevLiquidKnight on October 22, 2003 5:53:15 PM]
Advertisement
if you start your ball out going at 90 degrees or 45 degrees... its logical that it will continue with those angles..
You could try applying spin to your ball. That would most likely affect the trajectory when it comes in contact with a solid surface. Though I''m not entirely sure how that would be coded.
Anyone know how to spin the ball?
You can fake the spin. Just keep a float variable for the spin on the ball. negative spin would be counterclockwise, positive could be clockwise spin. Basically, if the paddle is moving one direction when the ball hits it, just add or subtract a value to/from that variable. Then when the ball strikes the wall, use that variable to add some bias to the ball''s angle.

fOriginalAngle = CalculateReturnAngle ();
fOriginalAngle += SPIN_BIAS_COEFFICIENT * fSpinBias;
check this site out :

http://www.cs.unc.edu/~ehmann/RigidTutorial/

this is probably a bit than you bargained for, i haven''t read it myself yet (bookmarked it for later)... i think its relevant though...

if you are just dealing with circles and flat surfaces you can simplify stuff quite a bit... for example, the distance from the contact point to the center of mass will always be the same.
If your rebound surfaces are at angles non-orthogonal to the ball''s trajectory you can also make it non-45.

This topic is closed to new replies.

Advertisement