i am making a space game using sfml 2.0. i want it to move depending on its rotation. so if it is rotated 20 degrees it move in the direction it is facing. how do you do it? I need to know if it is possible.
When an object moves you move it in terms a change in X and a change in Y (meaning the X and Y axis's). The value of these changes depend on the degree of rotation of your object. The trigonometric functions sin and cos will give you the change in X and change in Y that the object should move based on its rotation.
Rotate the object by delta_theta radians (or degrees)
Move the object:
-In the x-direction ~ object's speed * time * cos(angle)
-In the y-direction ~ object's speed * time * sine(angle)
To break it down further:
object's speed * time ~ How far to travel at once
How far to travel at once * cos/sin(angle) ~ Total distance to travel
This happens at nearly the same time, so what you'll see is the object moving on a curved line.
You can also reverse the order, i.e. move first then rotate, but that won't make much of a difference unless your object moves very fast.
Don't forget that the SFML rotation functions take and return an angle in degrees, whereas everything else in the programming universe works with radians (eg. the sin and cos functions) so you'll need to convert back and forth.
Example: cos( rect.getRotation()*PI/180.0f ) where PI has been suitably defined.
The code is simple: first, we rotate the input quaternion by the specified amount of radians. Then, we take the new quaternion and translate it based on the distance to move.
The beauty of quaternions is that they allow for rotation about some arbitrary axis. I would avoid rotating around the origin (0, 0) and then translating. I don't know if SFML uses quaternions internally, though.