Hi everyone, I've been trying to perfect this and I just can't get it right by myself. I've looked at other code/tutorials and even though I'm doing it the same I can't get the correct behavior.
Basically I can't get the ship to move correctly, well I can, but only without the set of if statements that keep the max velocity in a range. With those if statements turning the ship and using thrust makes it move oddly.
Also, right now I have the frame rate limit set to 60, but I'd like to get this to work without any fps limit, so if anyone could also help with that (without the fps cap it just moves way too fast, even when using frame time, it's just the numbers I can't get right since they are very small).
Below is the relevant code
Ship::Ship(sf::RenderWindow &App)
{
pApp = &App;
ShipSprite.SetImage(GameResource.getImage("ship"));
ShipSprite.SetPosition(512.f, 384.f);
ShipSprite.SetCenter(ShipSprite.GetSize() / 2.f);
xVelocity = 0;
yVelocity = 0;
shipThrust = 25;
maxVelocity = 5;
}
void ship::update()
{
if (pApp->GetInput().IsKeyDown(sf::Key::Up))
{
xVelocity += (-sin(ShipSprite.GetRotation() * (3.14159265 / 180))) * shipThrust * pApp->GetFrameTime();
yVelocity += (-cos(ShipSprite.GetRotation() * (3.14159265 / 180))) * shipThrust * pApp->GetFrameTime();
}
//Keep ship under max velocity
if (xVelocity > maxVelocity)
xVelocity = maxVelocity;
if (xVelocity < -maxVelocity)
xVelocity = -maxVelocity;
if (yVelocity > maxVelocity)
yVelocity = maxVelocity;
if (yVelocity < -maxVelocity)
yVelocity = -maxVelocity;
ShipSprite.Move(xVelocity, yVelocity);
}
If anyone can help me out with this I'd greatly appreciate it.