Space game rotation

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.
Last edited on
HELP
Translate the object as it is rotating.
can i have an example?
1
2
3
4
object.rotate(delta_theta);
object.move(object_spd*cos(object.getRotation())*delta_t, object_spd*sin(object.getRotation())*delta_t);
//Delta_t is change in time
//Delta_theta is change in angle 


Basic trigonometry/mechanical physics. I haven't worked with SFML that much, so you should double and triple check my code.

Edit:
And I guess you could account for time depending on your frame rate.
Last edited on
what is object_spd?
The speed the object is travelling.

Remember:
x = v*t
Where:
x ~ The distance travelled
v ~ The speed
t ~ The time it takes

Then you take the cosine or sine of the angle depending on which axis you're travelling along: x or y.
i still don't understand how to implement it.
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.

1
2
changeX = cos(rotation)*speed;
changeY = sin(rotation)*speed;


sin and cos return a value 0-1, so you need to multiply their return values by the desired speed to make the changeX and changeY values what you need.
1
2
3
4
5
object.rotate(delta_theta);
object.move(
    object_spd*cos(object.getRotation())*delta_t,
    object_spd*sin(object.getRotation())*delta_t
);


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.
what i need is someone to tell me how to implement delta_theta and delta_t. my compiler is telling me it doesn't exist so how do i calculate it.
Last edited on
delta_theta is how many radians (or degrees) your object will rotate. I can't tell you how much your game will rotate an object.

delta_t... You don't really need it I suppose, but I would just take the inverse of the frame rate. So 30 fps --> 30-1 seconds per frame.
I need code showing me an implementation. as in full code.
here is my code tell me how to fix it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>

int main()
{
    sf::RenderWindow window(sf::VideoMode (800,600), "movement based on rotation");

    sf::RectangleShape rect(sf::Vector2f(40, 60));
    rect.setFillColor(sf::Color::Green);
    rect.setPosition(400, 300);

    float distance, speed = 1, time = 1;
    distance = speed * time;

    float rect_spd = distance;

    while(window.isOpen())
    {
        sf::Event Event;
        while(window.pollEvent(Event))
        {
            switch (Event.type)
            {
                case sf::Event::Closed:
                window.close();
                break;
            }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            rect.rotate(0.01);

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            rect.rotate(-0.01);

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
        {
            system("cls");
            std::cout << rect.getRotation() << std::endl;
            rect.move(rect_spd*cos(rect.getRotation())*-10, rect_spd*sin(rect.getRotation())*-10);
        }

        window.clear(sf::Color::Cyan);
        window.draw(rect);
        window.display();
    }
}
Like I said, I haven't worked with SFML that much, so check my code snippet carefully for errors.

Lines 13 to 16 seem a little circuitous when you could just have:
 
float distance(1), speed(1), time(1), rect_spd(1);

But then, I am not entirely sure what you're planning for those variables.
I don't really get why you need to multiply by -10 on line 41.

Why do you need lines 39 and 40? Are they there just for debugging purposes?
closed account (D80DSL3A)
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.
closed account (zb0S216C)
Cronnoc wrote:
"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."

What I would do is use quaternions[1] for rotation and vectors to represent and implement directional movement. For example, here's some pseudo-code:

1
2
3
4
5
6
New_Quaternion Rotate_and_Move( Old_Rotation, Radians, Distance_to_Move )
{
  Output_Quaternion = Rotate( Old_Quaternion, Radians );
  Move_Vector( Distance_to_Move );
  return( Output_Quaternion );
}

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.

[1]http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

Wazzak
Last edited on
fun2code's solution worked thank you
Last edited on
Topic archived. No new replies allowed.