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
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 |
include <SFML/Graphics.hpp> #include <iostream> #include <math.h> #pragma comment ( lib , "sfml-system-s-d.lib" ) #pragma comment ( lib , "sfml-window-s-d.lib" ) #pragma comment ( lib , "sfml-graphics-s-d.lib" ) int main ( ) { class sf::RenderWindow Window ( sf::VideoMode ( 800 , 600 ) , "Test" , sf::Style::Resize | sf::Style::Close , sf::WindowSettings ( ) ) ; class sf::Event Event ; class sf::Shape Player ; Player.AddPoint ( 0 , 0 , sf::Color ( 255 , 0 , 0 , 255 ) , sf::Color ( 0 , 0 , 0 , 255 ) ) ; Player.AddPoint ( 0 , 50 , sf::Color ( 255 , 0 , 0 , 255 ) , sf::Color ( 0 , 0 , 0 , 255 ) ) ; Player.AddPoint ( 50 , 50 , sf::Color ( 255 , 0 , 0 , 255 ) , sf::Color ( 0 , 0 , 0 , 255 ) ) ; Player.AddPoint ( 50 , 0 , sf::Color ( 255 , 0 , 0 , 255 ) , sf::Color ( 0 , 0 , 0 , 255 ) ) ; Player.SetPosition ( 0 , 0 ) ; Player.SetRotation ( 0.0f ) ; Player.SetCenter ( 25 , 25 ) ; Player.SetScale ( 1.0f , 1.0f ) ; class sf::Shape Bot ; Bot.AddPoint ( 0 , 0 , sf::Color ( 255 , 0 , 0 , 255 ) , sf::Color ( 0 , 0 , 0 , 255 ) ) ; Bot.AddPoint ( 0 , 50 , sf::Color ( 255 , 0 , 0 , 255 ) , sf::Color ( 0 , 0 , 0 , 255 ) ) ; Bot.AddPoint ( 50 , 50 , sf::Color ( 255 , 0 , 0 , 255 ) , sf::Color ( 0 , 0 , 0 , 255 ) ) ; Bot.AddPoint ( 50 , 0 , sf::Color ( 255 , 0 , 0 , 255 ) , sf::Color ( 0 , 0 , 0 , 255 ) ) ; Bot.SetPosition ( 800 , 0 ) ; Bot.SetRotation ( 0.0f ) ; Bot.SetCenter ( 25 , 25 ) ; Bot.SetScale ( 1.0f , 1.0f ) ; while ( Window.IsOpened ( ) ) { while ( Window.GetEvent ( Event ) ) { switch ( Event.Type ) { case Event.Closed : { Window.Close ( ) ; } } } float Speed = 0 ; if ( Window.GetInput ( ).IsKeyDown ( sf::Key::Up ) ) { Speed = 100 ; } if ( Window.GetInput ( ).IsKeyDown ( sf::Key::Left ) ) { Player.SetRotation ( Player.GetRotation ( ) + 0.5f ) ; } if ( Window.GetInput ( ).IsKeyDown ( sf::Key::Right ) ) { Player.SetRotation ( Player.GetRotation ( ) - 0.5f ) ; } float PlayerRotation ( Player.GetRotation ( ) * 3.14159265f / 180.0f ) ; float PlayerXPosition ( sin ( PlayerRotation ) * Speed * Window.GetFrameTime ( ) ) ; float PlayerYPosition ( cos ( PlayerRotation ) * Speed * Window.GetFrameTime ( ) ) ; Player.Move ( PlayerXPosition , PlayerYPosition ) ; float BotRotation ( Bot.GetRotation ( ) * 3.14159265f / 180.0f ) ; float BotXPosition ( sin ( BotRotation ) * 50 * Window.GetFrameTime ( ) ) ; float BotYPosition ( cos ( BotRotation ) * 50 * Window.GetFrameTime ( ) ) ; Bot.Move ( BotXPosition , BotYPosition ) ; Window.Clear ( sf::Color ( 255 , 255 , 255 , 255 ) ) ; Window.Draw ( Player ) ; Window.Draw ( Bot ) ; Window.Display ( ) ; } return 0 ; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 |
class Vector2f { public: float x, y; Vector2f (float _x, float _y) : x (_x), y (_y) { } }; |
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 |
Vector2f vRichtung (Player.GetCenter().x - Bot.GetCenter().x, Player.GetCenter().y - Bot.GetCenter().y); // An dieser Stelle musst du dir die Frage stellen: // Wo hat das Viereck des Bots denn seine Vorderseite? // Ein Viereck besteht ja aus vier gleichberechtigten Seiten. // Beispielhaft gehen wir davon aus, dass die Seite, die nach rechts zeigt, die Vorderseite des Bots markiert. // Nun berechnen wir, in welche Richtung der Bot unter Berücksichtigung seiner Rotation gerade guckt. // Eventuell musst du bei der folgenden Berechnungsvorschrift noch Winkelangaben in Grad und Radianten angleichen: Vector2f vVorne (cos (Bot.GetRotation()), sin (Bot.GetRotation()); // Nun müssen wir nur noch den Winkel zwischen vRichtung und vVorne berechnen. // Dafür normieren wir zuerst vRichtung. vRichtung /= sqrt (vRichtung.x*vRichtung.x + vRichtung.y*vRichtung+y); // An dieser Stelle solltest du eigentlich noch unter Berücksichtigung einer gewissen Toleranz prüfen, ob vRichtung und vVorne gleich sind. float fRot = acos (vVorne.x * vRichtung.x + vVorne.y * vRichtung.y); // Abschließend brauchen wir die Rotation nur noch anzuwenden: Bot.SetRotation (Bot.GetRotation () + fRot) |
Werbeanzeige