Moving a ball at specific angle

Hello,

I'm writing a very simple Pong game in SFML and I'm wondering how to move a ball at specific angle.
Currently I have something like this:

1
2
3
4
5
6
bool move_down,move_right; // to which direction move the ball
// ...
if(move_down)
    ++y;
if(move_right)
    ++x;


But this always moves the ball at angle of 45 degrees. How can I move the ball, for example, at the angle of 55 degrees?

Thanks for help!
Last edited on
If both x and y have the same value, the angle will always be of 45. Just give them different velocities.
Then how can I calculate the velocities of x and y if I want an angle of 55 degrees?
If you don't mind using basic trig....

given an angle 'theta':

1
2
xvel = cos(theta) * ballspeed;
yvel = sin(theta) * ballspeed;


Where angle 0 is moving directly to the right, and rotates counter clock-wise.

EDIT: counter clock-wise if positive Y = upward. If positive Y = downward, then it rotates clock-wise.

EDIT2: also note that sin/cos take radians, not degrees. So convert appropriately.
Last edited on
If you don't mind using basic trig....
also note that sin/cos take radians, not degrees. So convert appropriately.

I have no idea what sin and cos do, plus I have no idea what's the difference between degrees and radians but I'll try to make it work...
I have no idea what sin and cos do,


I'll try to explain at lunch. But I'm at work now so I can't get into it.

I have no idea what's the difference between degrees and radians


There are 360 degrees in a full circle

There are 2*pi radians in a full circle.

To convert degrees to radians: radians = degrees * pi / 180;
sin and cos can be used to find the x and y coordinates of an angle where the hypotenuse is known (in this case the hypotenuse would be the distance the ball moves).

If you visualize a ball moving 1 unit at a forty five degree angle, you can see that it ends up at (x+nx, y+ny). You need trig to find nx and ny, which shift the ball at the correct angles, the equations given by Disch is how to find nx and ny (xvel and yvel as he puts it). The equation will work for any angle.
Last edited on
sin and cos can be used to find the x and y coordinates of an angle

I know that but I don't know anything about trigonometry :-(. Ugh....

Anyway, my first game with SFML is almost finished and the best thing is that I started using SFML only 4 days ago!

Topic archived. No new replies allowed.