I don't see how the dot product helps here. In order to use it he needs the direction vector, but the problem here is that he is trying to
find the direction vector.
Crash course on sin/cos:
Get a piece of paper and draw a circle on it. This circle is on a "grid", where the center point is at (0,0). The circle has a radius of 1, so the right-most point of the circle is (1,0), the top is (0,1), the left side is (-1,0), and the bottom is (0,-1).
With me so far?
now put your finger at the right most point. With your finger, start tracing the circle counter clockwise.
Your finger is now forming sin and cos waves. The Y position of your finger forms a sin wave, and the X position of your finger forms a cos wave.
so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
// assuming 'sin' and 'cos' take degrees:
// right-most point:
cos( 0 ); // <- 1
sin( 0 ); // <- 0
// top-most point:
cos( 90 ); // <- 0
sin( 90 ); // <- 1
// left-most point:
cos( 180 ); // <- -1
sin( 180 ); // <- 0
// bottom-most point:
cos( 270 ); // <- 0
sin( 270 ); // <- -1
}
|
So basically, if you have a target angle "theta" and want to find out the desired X,Y position on that circle, you can just use sin/cos:
1 2
|
y = sin(theta);
x = cos(theta);
|
That will form a "unit vector" (a vector with a length of 1) which moves in the direction of the given angle theta.
From there, you can multiply x and y by a speed. So say you want to move an object 6 pixels/units instead of 1:
1 2
|
y = sin(theta) * 6;
x = cos(theta) * 6;
|
Now there are are a few things to be concerned with:
1) sin/cos in the c library take
radians not degrees. So if you have the angle in degrees, you'll need to convert. Converting is simple: 180 degrees = pi radians, so to convert:
|
angle_in_radians = angle_in_degrees * pi / 180;
|
2) Remember that with sin/cos, the circle is formed counter-clockwise, not clockwise.
3) With sin/cos, positive Y is up and negative Y is down. In SFML it's the other way around, so you might have to negate your Y.
4) With sin/cos, the circle starts (ie: angle 0) is the right-most point, not the top-most point. So if you want it to originate at the top most point, you'll need to adjust (add 90 degrees to theta)