Radial-Controls

Hello Guys

Right now im coding on a rocket game. The player is supposed to rotate the rocket and press space to accelerate in the chosen direction. Right now the rocket seems to randomly go into random directions. Does anybody know how i can control the rocket?

Here ist the code, interesting lines start at line 42.
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

#include <stdlib.h>
#include <iostream.h>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>


int main(int argc, char** argv) {
    sf::RenderWindow Game(sf::VideoMode(800, 600, 0), "Gravitation");
    sf::WindowSettings::WindowSettings(24, 8, 4);
    sf::Blend::Alpha;


    sf::Image IRocketP1;
    if (!IRocketP1.LoadFromFile("wrocket.png")) {
        // Error...
    }
    sf::Sprite rocketP1;
    rocketP1.SetImage(IRocketP1);
    rocketP1.SetColor(sf::Color(255, 255, 255, 255));
    rocketP1.SetX(0.f);
    rocketP1.SetY(0.f);
    rocketP1.SetPosition(350.f, 500.f);
    rocketP1.SetRotation(0.f);
    rocketP1.SetCenter(rocketP1.GetSize().x / 2 + 20, rocketP1.GetSize().y / 2 + 20);

    double speedR = 100;
    double widthR = rocketP1.GetSize().x;
    double heightR = rocketP1.GetSize().y;
    int speedRocket = 3;
    float direction = 0;
    double speedRotate = 2;
    int resX = 1280;
    int resY = 800;

    while (Game.IsOpened()) {
        Game.UseVerticalSync(true);

        // CONTROL
        float ElapsedTime = Game.GetFrameTime();
        if (Game.GetInput().IsKeyDown(sf::Key::Left) && rocketP1.GetPosition().x > 0)
            direction += speedRotate;
        if (Game.GetInput().IsKeyDown(sf::Key::Right) && rocketP1.GetPosition().x + widthR < resX)
            direction -= speedRotate;
        rocketP1.SetRotation(direction);
        if (Game.GetInput().IsKeyDown(sf::Key::Space)) {
            rocketP1.Move(speedRocket * sin(direction), speedRocket * cos(direction));
        }
        cout << direction;
        


        sf::Event Close;
        while (Game.GetEvent(Close)) {

            if (Close.Type == sf::Event::Closed)
                Game.Close();
            if ((Close.Type == sf::Event::KeyPressed) && (Close.Key.Code == sf::Key::Escape))
                Game.Close();
        }

        Game.Draw(rocketP1);
        Game.Display();
        Game.Clear();

    }
    return (EXIT_SUCCESS);
}


I'm not sure this is the problem, since I don't know how does "going randomly" look like, but it could be that your speedRotate is too big. sin() function takes the angle in radians. 2 radians is about 115 degrees. Rotating 115 degrees 60 times every second may look random. Try something more like 0.01
Thank you hamsterman. That was the solution!
Topic archived. No new replies allowed.