SFML rotation of an object while angle is 180 degree

Apr 3, 2020 at 9:23am
In my move function, I want to rotate an object in specific regions. When I want to rotate the object 180 degree, it only rotates 1 degree and stops rotation.

This is my move function:

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
void Vehicle::move(float &rotation, int &c, sf::Vector2f &direction, sf::Vector2f &currentVelocity, float &maxVelocity, float &acceleration, float &tempSpeed, int &increment, int &increment2, float &x, float &y, sf::Sprite &mySprite)
{
    x += increment; //increment = 1
    y += increment2; //increment2 = 0

    mySprite.setOrigin(sf::Vector2f(mySprite.getGlobalBounds().width / 2, mySprite.getGlobalBounds().height / 2));

    direction = sf::Vector2f(0.f, 0.f);

    if((x >= 130 && x < 239*2 + 100) && (y >= 130 && y < 239*2 + 100))
    {
	rotation = mySprite.getRotation();

        if(rotation > 0)
        {
            rotation -= 1;
            cout << "rotation in first 0: " << rotation;
            cout << endl;

            mySprite.setRotation(rotation);
            mySprite.move(10.f * tempSpeed / 1000, 0);
            increment = 1;
            increment2 = 0;

            //Acceleration
            direction.x = 1.f;

            if(currentVelocity.x < maxVelocity)
                currentVelocity.x += acceleration * direction.x * tempSpeed;
        }
    }

    else if((x >= 239*2 + 100 && x < 239*4 + 100) && (y >= 130 && y < 239*2 + 100))
    {
        rotation = mySprite.getRotation();

        if(rotation >= 0 && rotation < 90)
        {
            rotation += 1;
            cout << "rotation in first 90: " << rotation;
            cout << endl;

            mySprite.setRotation(rotation);
            mySprite.move(0, 10.f * tempSpeed / 1000);
            increment = 0;
            increment2 = 1;

            //Acceleration
            direction.y = 1.f;

            if(currentVelocity.y < maxVelocity)
                currentVelocity.y += acceleration * direction.y * tempSpeed;
        }
    }

    else if((x >= 239*2 + 100 && x < 239*4 + 100) && (y >= 239*2 + 100 && y < 239*4 + 100))
    {
        rotation = mySprite.getRotation();

        if(rotation > 0)
        {
            rotation -= 1;
            cout << "rotation in second 0: " << rotation;
            cout << endl;

            mySprite.setRotation(rotation);

            mySprite.move(10.f * tempSpeed / 1000, 0);
            increment = 1;
            increment2 = 0;

            //Acceleration
            direction.x = 1.f;

            if(currentVelocity.x < maxVelocity)
                currentVelocity.x += acceleration * direction.x * tempSpeed;
        }
    }

    else if((x >= 239*4 + 100 && x < 239*5 + 100) && (y >= 239*2 + 100 && y < 239*4 + 100))
    {
	rotation = mySprite.getRotation();

        if(rotation < 90)
        {
            rotation += 1;
            cout << "rotation in second 90: " << rotation;
            cout << endl;

            mySprite.setRotation(rotation);
            mySprite.move(0, 10.f * tempSpeed / 1000);
            increment = 0;
            increment2 = 1;

            //Acceleration
            direction.y = 1.f;

            if(currentVelocity.y < maxVelocity)
                currentVelocity.y += acceleration * direction.y * tempSpeed;
        }
    }

    else if((x >= 239*4 + 100 && x < 239*5 + 100) && (y >= 239*4 + 100 && y < 239*5 + 100))
    {
	rotation = mySprite.getRotation();

        if(rotation >= 90 && rotation < 180)
        {
            rotation += 1;
            cout << "rotation in 180: " << rotation;
            cout << endl;

            mySprite.setRotation(rotation);
            mySprite.move(10.f * -tempSpeed / 1000, 0);
            increment = -1;
            increment2 = 0;

            //Acceleration
            direction.x = -1.f;

            if(currentVelocity.x > -maxVelocity)
                currentVelocity.x += acceleration * direction.x * tempSpeed;
        }
    }

    else if((x >= 130 && x < 239*2 + 100) && (y >= 239*4 + 100 && y < 239*5 + 100))
    {
	rotation = mySprite.getRotation();

        if(rotation >= 180 && rotation < 270)
        {
            rotation += 1;
            cout << "rotation in 270: " << rotation;
            cout << endl;

            mySprite.setRotation(rotation);
            mySprite.move(0, 10.f * -tempSpeed / 1000);
            increment = 0;
            increment2 = -1;

            //Acceleration
            direction.y = -1.f;

            if(currentVelocity.y > -maxVelocity)
                currentVelocity.y += acceleration * direction.y * tempSpeed;
        }
    }
}



The output of the code:

https://imgur.com/CI1QrmV


It stops at 91 degree. It does not increase until 180 degree.

How can I do that rotation correctly?
Last edited on Apr 3, 2020 at 9:31am
Apr 3, 2020 at 11:07am
Are you sure it enters the if statement lines 103-124 more than once?
Apr 3, 2020 at 11:12am
I'd advise stepping through your code with your debugger. That way, you'll be able to see which lines of your code are being executed, and to examine the contents of memory to find out why they are or aren't.
Apr 3, 2020 at 4:31pm
@Peter87, yes, it enters the if more than one in 103-124. I checked it by adding:
cout << "fifth if" << endl;

@MikeyBoy, as stated above, I added cout to the all if statements to check if the program enters that statement or not. And, program enters all of the statement. Maybe, I will debug the function.
Apr 3, 2020 at 4:54pm
TBH, you should look at the 6 times you copy/pasted a very similar looking piece of code, and figure out a more general function you can just call 6 times with different parameters.

You might think you made all the necessary edits with each ctrl-v, but my guess is you've got things like an x where there should be a y, or a - where there should be a +.

Apr 3, 2020 at 5:09pm
could also have a degree/radian issue as it seems to work in degrees and may have missed converting somewhere.
Apr 4, 2020 at 6:52am
Utku wrote:
@Peter87, yes, it enters the if more than one in 103-124. I checked it by adding: cout << "fifth if" << endl;

Then the next step is to check the value of x and y and verify that they actually satisfy the condition.
Last edited on Apr 4, 2020 at 6:53am
Apr 5, 2020 at 1:32am
@Peter87,

I then checked x and y coordinates. I made a small calculation mistake. I corrected the rotation.

Thank you all for your contributing.
Topic archived. No new replies allowed.