SFML community forums

Help => Graphics => Topic started by: PapaSmurf on March 23, 2015, 12:14:48 pm

Title: Getting projectiles to move according to time
Post by: PapaSmurf on March 23, 2015, 12:14:48 pm
Hi,

I need to get my projectiles moving relevant to time. As well as sprite animation.

The code that I have for my bullets at the moment is as follows. They do not seem to animate with relevance to time. To be honest I am pretty baffled by this.



struct BulletData
{
        sf::Vector2f direction;
        sf::Vector2f startpos;  
        float        speed;    
        bool         active;   
        BulletData()
        {
                direction = sf::Vector2f(0.0f,0.0f);
                startpos = sf::Vector2f(0.0f,0.0f);
                speed    = 600000.0f;
                active   = false;
        }
};
        sf::Time            dt; // delta time
        sf::Time            elapsedTime;

        sf::Clock clock;

        elapsedTime += dt;
        int timeAsMs = elapsedTime.asMilliseconds();
        dt = clock.restart();


 void Game::updateBulletCollisions(sf::Time dt)
{

        for (int i = 0; i < NO_BULLETS; i++)
        {
                if (this->_arr_bullet_data[i].active)
                {
                        sf::Vector2f tbulletvel = this->_arr_bullet_data[i].direction * this->_arr_bullet_data[i].speed * dt.asSeconds();
                        this->_arr_bullet_spr[i].move(tbulletvel);
       }
}
 

Regards
Andy
Title: Re: Getting projectiles to move according to time
Post by: TheNess on March 24, 2015, 08:42:32 pm
its unclear from the code you added - are you calling dt = clock.restart(); every frame? or just once? (you suppose to call it every frame)

Edit: see this page http://www.sfml-dev.org/tutorials/2.0/system-time.php
at the bottom you have an example.
Title: Re: Getting projectiles to move according to time
Post by: Jesper Juhl on March 24, 2015, 08:55:46 pm
You should read this: http://gafferongames.com/game-physics/fix-your-timestep/