Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: asteroids-like ship movement..so close  (Read 2084 times)

0 Members and 1 Guest are viewing this topic.

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
asteroids-like ship movement..so close
« on: August 15, 2011, 07:27:26 pm »
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

Code: [Select]

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;
}


Code: [Select]

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.

h4tt3n

  • Newbie
  • *
  • Posts: 7
    • View Profile
asteroids-like ship movement..so close
« Reply #1 on: August 28, 2011, 10:06:55 pm »
I might be able to help. What is the problem exactly? What do you want to achieve, and what is happening?

Cheers,
Mike

Sui

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://www.suisoft.co.uk/
asteroids-like ship movement..so close
« Reply #2 on: August 29, 2011, 09:55:51 pm »
You need to constrain the directional velocity, rather than the x and y components.

Calculate the hypotenuse (wikipedia has the calc for this if you can't remember your maths class) and check if it exceeds your velocity limit.

If it does you can scale down your x and y components accordingly.

E.g.
Component = component * max velocity / hypotenuse

Hope this helps.
Gary Marples, Developer of games and other stuff.
www.suisoft.co.uk/games/BlogTwitter