Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

23.12.2011, 09:54

Die Steuerung bei Asteroids

Hi,
ich programmiere zur Zeit das Spiel Asteroids nach. Habe allerdings ein Problem mit der Steuerung.
Ich möchte die Steuerung so haben wie sie auch im Originalem Spiel ist, d.h. man kann das Raumschiff jederzeit drehen ohne das dies
die Richtung beeinträchtigt, erst wenn ich den UP-Key drücke fliegt das Raumschiff in die Richtung in der das Raumschiff zeigt.
Zudem soll es eine Art Beschleunigung geben.

Ich habe schon etwas rumm probiert und eine Halbwegs brauchbaren Ansatz. Jedoch hab ich 2 Probleme:

Mein Raumschiff wird in bestimmten fällen (erst nach unten Links fliegen und dann nach oben Rechts) mega schnell und irgendwann sieht man das Sprite kaum noch^^.
Und in manchen Fällen fliegt das Raumschiff erst garnicht in eine andere Richtung da aufeinmal die Abfragen nicht mehr passen.

Hier der QuellCode:

C-/C++-Quelltext

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
void Spieler::ProcessMove (sf::RenderWindow &app)
{
    ///////////////////////////////////////////
    // Tasten abfragen ^, <-, -> ob gedrückt //
    ///////////////////////////////////////////

    if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Up))
    {       
        

        //erst ab bestimmter Geschwindigkeit Trägheit einsetzen damit bei start nach recht das dingens nich nach oben fliegt
        //bei Geschwindigkeit 0 oder kleinen wert muss bei rotation auch Richtungsvektor gesetzt werden

        /*
         * DIE OBEREN QUADRANTEN RECHTS UND LINKS
         */
            // Rechts
            //
            // oben
            if (GetVSicht().x > 0 && GetVSicht().y > 0)
            {   
                sf::Vector2f tmpV;

                // SichtX > RichtungX
                // SichtY < RichtungY
                if (GetVSicht().x > GetVRichtung().x && GetVSicht().y < GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x + ((GetVSicht().x - GetVRichtung().x) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y - GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                    //cout << "sX > rX ; sY < rY" << endl;

                }
                // SichtX > RichtungX
                // SichtY > RichtungY
                else if (GetVRichtung().x > GetVRichtung().x && GetVSicht().y > GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x + (( GetVSicht().x - GetVRichtung().x) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y + GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                    //cout << "sX > rX ; sY < rY" << endl;
    
                }
                // SichtX < RichtungX
                // SichtY > RichtungY
                else if (GetVSicht().x < GetVRichtung().x && GetVSicht().y > GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x - ((GetVSicht().x + GetVRichtung().x) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y + GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                //  cout << "sX > rX ; sY < rY" << endl;
    
                }
                // SichtX < RichtungX
                // SichtY < RichtungY
                else if (GetVSicht().x < GetVRichtung().x && GetVSicht().y < GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x - ((GetVSicht().x + GetVRichtung().x) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y + GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                //  cout << "sX > rX ; sY < rY" << endl;

                }
            //  ShowVector();
            }

            // Links
            //
            // oben
            if (GetVSicht().x < 0 && GetVSicht().y > 0)
            {
                sf::Vector2f tmpV;

                // SichtX < RichtungX
                // SichtY < RichtungY
                if (GetVSicht().x < GetVRichtung().x && GetVSicht().y < GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x + ((GetVSicht().x - GetVRichtung().x) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y - GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                    //cout << "sX < rX ; sY < rY" << endl;
    
                }
                // SichtX < RichtungX
                // SichtY > RichtungY
                else if (GetVSicht().x < GetVRichtung().x && GetVSicht().y > GetVRichtung().y) 
                {
                    tmpV.x = GetVRichtung().x + ((GetVSicht().x - GetVRichtung().x) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y + GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                    //cout << "sX < rX ; sY > rY" << endl;
                }

                // SichtX > RichtungX 
                // SichtY < RichtungY
                else if (GetVSicht().x > GetVRichtung().x && GetVSicht().y < GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x + ((GetVSicht().x + GetVRichtung().x) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y - GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                    //cout << "sX > rX ; sY < rY" << endl;
                }

                // SichtX > RichtungX
                // SichtY > RichtungY
                else if (GetVSicht().x > GetVRichtung().x && GetVSicht().y > GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x + ((GetVSicht().x + GetVRichtung().x) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y + GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                    //cout << "sX > rX ; sY > rY" << endl;
                }
                //ShowVector();
            }

            // Rechts
            //
            // unten
            if (GetVSicht().x > 0 && GetVSicht().y < 0)
            {
                sf::Vector2f tmpV;

                // SichtX < RichtungX
                // SichtY < RichtungY
                if (GetVSicht().x < GetVRichtung().x && GetVSicht().y < GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x + (((GetVSicht().x - GetVRichtung().x)*-1) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y + GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                    //cout << "sX < rX ; sY < rY" << endl;
                }
                
                // SichtX < RichtungX
                // SichtY > RichtungY
                else if (GetVSicht().x < GetVRichtung().x && GetVSicht().y > GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x + (((GetVSicht().x + GetVRichtung().x)*-1) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y + GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                    //cout << "sX < rX ; sY > rY" << endl;
                }
                // SichtX > RichtungX
                // SichtY > RichtungY
                else if (GetVSicht().x > GetVRichtung().x && GetVSicht().y > GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x - (((GetVSicht().x + GetVRichtung().x)*-1) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y + GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                    //cout << "sX > rX ; sY > rY" << endl;
                }
                // SichtX > RichtungX
                // SichtY < RichtungY
                else if (GetVSicht().x > GetVRichtung().x && GetVSicht().y < GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x - (((GetVSicht().x + GetVRichtung().x)*-1) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y + GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                    //cout << "sX > rX ; sY < rY" << endl;
                }
                //ShowVector();
            }

            // Links
            //
            // unten
            if (GetVSicht().x < 0 && GetVSicht().y < 0)
            {
                sf::Vector2f tmpV;

                // SichtX < RichtungX
                // SichtY < RichtungY
                if (GetVSicht().x < GetVRichtung().x && GetVSicht().y < GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x - (((GetVSicht().x - GetVRichtung().x)*-1) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y + GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                    //cout << "sX < rX ; sY < rY" << endl;
                }

                // SichtX < RichtungX
                // SichtY > RichtungY
                else if (GetVSicht().x < GetVRichtung().x && GetVSicht().y > GetVRichtung().y)
                {
                    tmpV.x = GetVRichtung().x - (((GetVSicht().x - GetVRichtung().x)*-1) / m_MoveSpeed);
                    tmpV.y = GetVRichtung().y - GetVSicht().y / m_MoveSpeed;
                    SetVRichtung(tmpV);
                //  cout << "sX < rX ; sY > rY" << endl;
                }

                // SichtX > RichtungX
                // SichtY > RichtungY
                else if (GetVSicht().x > GetVRichtung().x && GetVSicht().y > GetVRichtung().y)
                {
                    //cout << "sX > rX ; sY > rY" << endl;
                }

                // SichtX > RichtungX
                // SichtY < RichtungY
                else if (GetVSicht().x > GetVRichtung().x && GetVSicht().y < GetVRichtung().y)
                {
                    //cout << "sX > rX ; sY < rY" << endl;
                }
            //  ShowVector();
            }

            CheckSpeed();
            m_SpielerSprite.Move(GetVRichtung().x*m_fSpeed*app.GetFrameTime(), GetVRichtung().y*-m_fSpeed*app.GetFrameTime());
    }

    if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Left))
    {
        m_SpielerSprite.Rotate(-0.4*app.GetFrameTime());
        m_vSicht.x = cos((m_SpielerSprite.GetRotation()-90)*(PI/180));
        m_vSicht.y = sin((m_SpielerSprite.GetRotation()+90)*(PI/180));
    }

    if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Right))
    {
        m_SpielerSprite.Rotate(0.4*app.GetFrameTime());
        m_vSicht.x = cos((m_SpielerSprite.GetRotation()-90)*(PI/180));
        m_vSicht.y = sin((m_SpielerSprite.GetRotation()+90)*(PI/180));
    }

    //////////////////////////////////////////////////////////
    // Prüfen ob Taste losgelassen wurde um Beschleunigung  //
    // ins negative zu setzen!                              //
    //////////////////////////////////////////////////////////

    if (!sf::Keyboard::IsKeyPressed(sf::Keyboard::Up) && m_fSpeed > 0.005)
    {
            m_fSpeed *= 0.9999f;
            m_SpielerSprite.Move(GetVRichtung().x*m_fSpeed*app.GetFrameTime(), GetVRichtung().y*-m_fSpeed*app.GetFrameTime());

    }

}


Zur Erläuterung:

SichtVektor: Ein 2D-Vektor der die Richtung angibt in welche man schaut.
RichtungsVektor: Ein 2D-Vektor der die Richtung angibt in welche man zZ fliegt.

Der Richtungsvektor soll sich dem SichtVektor immer Stück für Stück annähern damit die Lenkung nicht zu abrupt ist. Jeder der Androids schonmal gespielt hat weiss ja ungefähr wie die Steuerung dort ist. Ein gewisse Trägheit soll erreicht werden.

Mich würde es sehr freuen wenn ihr mir ein paar Tipps geben könntet oder irgendwie Ideen habt. Vllt. hat ja auch jemand von euch schonmal etwas ähnliches Programmiert und kann mir zumindest sagen ob der Ansatz so halbwegs in Ordnunng ist oder ob ich etwas komplett anderes machen sollte^^

Gruss
AK

EDIT: m_MoveSpeed ist einfach nur eine Konstante und wurde im Konstruktor auf 1000 gesetzt.

2

24.12.2011, 19:38

Hallo,
wegen den unterschiedlichen Geschwindigkeiten hätte ich mal app.SetFrameLimit(60) oder niedriger gemacht und bei der Tastenabfrage die vergangene Zeit mitberechnet (Tipp: sf::Clock).

PS: Ich denk niemand hat Lust 250 Zeilen Code zu lesen. :)

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »C++-Coder« (24.12.2011, 19:44)


3

05.01.2012, 14:28

Zitat

"Mein Raumschiff wird in bestimmten fällen (erst nach unten Links fliegen und dann nach oben Rechts) mega schnell und irgendwann sieht man das Sprite kaum noch^^.
Und in manchen Fällen fliegt das Raumschiff erst garnicht in eine andere Richtung da aufeinmal die Abfragen nicht mehr passen."

das hört sich aber noch nicht einmal nache einem halbwegs brauchbaren ansatz an, da stimmt was megamäßig nicht. du hast einen drehwinkel vom raumschiff und daraus berechnet man megaeinfach x und y komponente. was du da mit den 4 quadranten willst versteh ich auch nicht lieber acidkiller.

C-/C++-Quelltext

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
    _fAngle += _rotSpeed * gTimer._ElapsedSec;

    if (gBase._KeyDown (_KeySpeed))
    {
        int speedX = _SpeedX + _nSpeedUpSens * gTimer._ElapsedSec * sin(RADPERDEG*_fAngle);
        int speedY = _SpeedY + _nSpeedUpSens * gTimer._ElapsedSec * cos(RADPERDEG*_fAngle);
        int speed = sqrt(pow((double)speedX,2)+pow((double)speedY,2));
        if(speed < 600)
        {
            _SpeedX = speedX;
            _SpeedY = speedY;
            _Speed = speed;
            _MaxSpeedX = _SpeedX;
            _MaxSpeedY = _SpeedY;
        }
        else
        {
            _SpeedX *= 0.99f;//huch
            _SpeedY *= 0.99f;
        }
        _fSlowDownTimer = 0.0f;
    }
    else 
    {
        if(_Speed <= 0.0001)
        {
            _SpeedX = _SpeedY = _Speed = 0;
            _fSlowDownTimer = 0.0f;
        }
        else
        {
            _SpeedX = _MaxSpeedX / pow(2,(double)_fSlowDownTimer);
            _SpeedY = _MaxSpeedY / pow(2,(double)_fSlowDownTimer);
                _fSlowDownTimer += gTimer._ElapsedSec * _fSlowDownSens;

        }
    }

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »dr.hallo« (05.01.2012, 14:33)


Werbeanzeige