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.
The other problem is that the rocket does not fly in smooth curves but when i accelerate and at the same time rotate it starts to stutter and wont move.
#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;
double speedRocket = 3;
double 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);
}
[/quote][/code]