in pong, what is normally done is that the point of the ball hitting is compared to the center. the closer to the center, the closer to the 90 degree angle the ball is deflected. farther means an angle closer to 30 (or whatever you pick as the low end)
2D angles and Paddle-ball sliding...
Get off my lawn!
1) how to i figure out the new angle the ball should move in
2) how to convert that angle to x,y velocities
For the ball i want to have various speeds so i set up a speed variable that i multiply by the xv and yv variables to get the x and y increments. so I need the xv and yv variables to be 1 or below or -1 and above (obviously)
please help me. I've been trying to figure this out and i know trigonomitry is involved, but i haven't learned trig yet...
Bill(c)
that's simple:
let's say your ball has the position x/y and the angle ang (in degree!).
0° is up, 90° is right and so on
Now you can move your ball this way:
int ang, v;
float x,y;
do {
//move
x += v*sin(ang*0.0174);
y -= v*cos(ang*0.0174);
//display or whatever ...
}
this is simple trigonometry:
/|
c/ |
/ |b
/w |
-----
a
sin(w) = b/c cos(w) = a/c
and as you can see, a is your dx, and b is dy.
last thing: I multiplied w with 0.0174 to convert it from degree to radiants.
I know, this code must be optimized, because
it has many floating point numbers in it
(even x and y are floats), but I think, it
answers your question.
good luck ...
Philippo
------------------
Philippo
s_buschmann@gmx.de
http://privat.schlund.de/Gerio