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

Author Topic: Getting projectiles to move according to time  (Read 1066 times)

0 Members and 1 Guest are viewing this topic.

PapaSmurf

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Getting projectiles to move according to time
« 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

TheNess

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Getting projectiles to move according to time
« Reply #1 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.
« Last Edit: March 24, 2015, 08:45:24 pm by TheNess »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Getting projectiles to move according to time
« Reply #2 on: March 24, 2015, 08:55:46 pm »