-
Bouncing ball
I can make a ball bounce around the screen and it's not too difficult. The way I do it though the angles are always the same, by simply adding or subtracting 1 to the X and Y coordinates. What I am wondering is how I would be able to make the ball bounce at different angles. Correct angles. I know I would have to use the SIN, or COSINE function. Can someone shed a little light on this for me please. Thank you.
-
the angle of incidence is always the same as the angle of reflection. if you take your motion, and break it into horizontal and vertical force components... [in a real gravity simulation]... you'll note that there is no force applied in the horizontal direction, and acceleration is always negative [back down to earth]... so if you have a constant horizontal motion, and an accelerating vertical speed... you get correct, and realistic motion.
in order to get the correct angles, you have to calculate the initial vertical speed of the ball, and the angle of incidence it makes on the plane it attacks. also, if that plane is sloped, you also need to account for that.
is there anything specific you wanted to know besides the physics of it? do you have any examples?
hth
PS i like your signature. :)
-
#define PI 3.14159
#define DEGTORAD(x) (double)((double)x*PI/(double)180)
x+=cos(DEGTORAD(ballangle))*ballspeed;
y+=sin(DEGTORAD(ballangle))*ballspeed;
-
For the right wall
xvel = -xvel;
yvel = yvel;
For the left wall
xvel = -xvel;
yvel = yvel;
For the top wall
xvel = xvel;
yvel = -yvel;
And so on ...
-
Actually, to get the ball to bounce off at the correct angle you have to do more than that.
You have to keep track of the balls last position on the screen. When it hits a wall you then create a triangle of sorts and do the standard trig to get the return angle.
Code:
struct ball
{
double x;
double y;
double lastx;
double lasty;
double spd;
double xinc;
double yinc;
void Update(void) {x+=(xinc*spd);y+=(yinc*spd);};
}Ball;
void CheckWallHit(void)
{
if (ball.y<0)
{
//Ball in 2nd quadrant -,+
double diffy=ball.lasty-ball.y;
double diffx=ball.lastx-ball.x;
diffx*=diffx;
diffy*=diffy;
double hyp=sqr(diffx+diffy);
if (diffx==0.0f)
{
//Ball returns at 90 or 180 - depending on your orientation
ball.yinc=1;
ball.xinc=0;
}
else
{
ball.yinc=(diffy/hyp);
ball.xinc=-(diffx/hyp);
}
ball.lastx=ball.x;
ball.lasty=ball.y;
}
}
Now to check for the rest of the walls, you use the same algo and check for x<=left x>=right and y>=bottom. Just change the signs to get the ball to bounce correctly.
1st quad -,- Lower left of screen
2nd quad -,+ Upper left of screen
3rd quad +,- Upper right of screen
4th quad +,+ Lower right of screen
This is not using cos and sin per se like my first example. It breaks the x,y into sep components. Actually all that is happening is that you are taking the cos and sin of the angle when you divide by the hyp.
cos=opp/hyp
sin=adj/hyp
tan=opp/adj
or
(O)scar (H)ad (A) (H)old (O)n (A)rthur
cos - O/H
sin - A/H
tan -O/A
It does use the sqrt() function which is dreadfully slow, but it's ok for pong and balls bouncing. My program has over 1000 balls bouncing at a time and it is very fast. Should be no problem for 1 ball. My floating point calcs are handled by the FPU, though, so you may not be able to get as many balls pinging off of the walls.
I believe this example's orientation is:
Code:
270
|
180 <- * -> 0
|
90
(Had to use code section to preserve tabs)
At the corners this algo will perform 2 square roots since both x and y will have hit the walls. But you should not notice it.
For ball acceleration and projectile stuff, check out www.programmersheaven.com and about a million other sites. Look it up on a search engine. Hey DA why didn't you give him the projectile formula? I've forgotten it.:)
-
Ok you have a ball going at m velocity which hits the wall on
the right at an angle of incidence of theta. Then decomposing
the velocity
m_x = sin(theta)
m_y = cos(theta)
After it hits the wall the angle of incidence is the same at the magnitude is assumed to be the same.
m'_x = -m sin(theta)
m'_y = m cos(theta)
-
Thank you for all the help, I will try messing with it when i get back to school and I will tell you how it turns out. Thank you again. I love this forum.
-
Quick question for you..are you just adding and/or subtracting a value from the y coordinate of the ball when bouncing??
coz if you are, then the ball will not look like it's bouncing naturally (ie, with gravity :)).
U.
-
The algo that I gave will produce a pong ball effect. For projectile bouncing or projectile motion I referred to www.programmersheaven.com. This is not projectile motion.
Also the angle of incidence is not equal to the angle that it hits the wall. This is why I broke the velocity into two component x and y vectors. I've tried this simply by taking the sin cos of the angle, but you must find the return angle, not the angle it hit the wall at. Just inversing the x and y vectors when it hits left,top,right,bottom repesctively will not work. The ball will not bounce off the walls at the correct angles.
-
Yes I like my signature as well. I got it from my brother, who I believe got it from Dr Seuss. As of now I am just adding and subtracting from the x and y coordinates. When I thought about how to do it that is the only way i came up with. I will keep trying it this way until I get it, maybe after I'll try it the other way. How would you suggest doing the other way? Let me know so I can start to think about it. Good bye.
-
The way that I showed in my first post will work for all angles and all return angles.