SFML community forums

Help => General => Topic started by: 5gum on November 11, 2013, 10:21:40 pm

Title: Acceleration of a Sprite?
Post by: 5gum on November 11, 2013, 10:21:40 pm
Hello, it's me again,

at first: Sorry for my bad English, I usually speak German so please answer in "Simple English" if it's possible ;)
Second: I began SFML yesterday so... yeah I'm a beginner ;D

Here I show you a code of a little program from me:

#include <iostream>
#include <SFML\Graphics.hpp>

int main()
{
        sf::RenderWindow mMainWindow(sf::VideoMode(800,600,32),"Test",sf::Style::Titlebar);
        sf::Event mMainEvent;

        sf::Image car;
        car.loadFromFile("car.png");
        car.createMaskFromColor(sf::Color::White);

        sf::Texture tCar;
        tCar.loadFromImage(car);

        sf::Sprite sCar(tCar);

        while(mMainWindow.isOpen())
        {

                while(mMainWindow.pollEvent(mMainEvent))
                {
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                                mMainWindow.close();
                }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        sCar.move(1,0);
                }

                mMainWindow.clear();
                mMainWindow.draw(sCar);
                mMainWindow.display();
        }

        system("pause");
}

I've got a sprite and if I press the Right-Key it moves to the right 1 pixel every frame. Nothing special.
But now I want that when I pressed the Key e.g. 0.5 seconds, my Sprite moves faster.
sCar.move(3,0);
for example...
So I want something like an acceleration for the sprite. I tried it with
sf::Clock timer;
but if I write that out from the "Keypress-Loop" the timer works always and if I write it in the loop the timer reset every frame.

Is there any solution for that problem or am I just a little bit dumb?
Please don't be too strict because of my English and my knowledge about SFML.

Thanks,

5gum

[edit]If I've got big missktakes in English grammar, please tell that :)[/edit]

Title: Re: Acceleration of a Sprite?
Post by: G. on November 12, 2013, 12:17:25 am
First, use a variable velocity (speed) instead of a fixed speed of 1 pixel per second.
Every frame, add the sprite velocity to the sprite position.
When the right key is pressed, increase the velocity.
When the right key isn't pressed, decrease the velocity until it reach 0.

float speedX = 0;
...
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
   speedX += 0.1f;
}
else
{
  //if your speed is close to 0, stop
   if (abs(speedX) <= 0.2f)
  {
      speedX = 0;
  }
  else
  {
     //decrease the speed
     speedX -= 0.2f;
  }
}
sCar.move(speedX, 0);

Then replace 0.1f by a variable named acceleration, 0.2f by deceleration (or friction), tweak the values, limit your speed to a max speed, adapt it to the other directions, make your speed frame independent, etc. :p
Title: Re: Acceleration of a Sprite?
Post by: 5gum on November 12, 2013, 01:30:45 pm
Simple and it works!

Thanks  ;D
Title: Re: Acceleration of a Sprite?
Post by: Ditto on December 01, 2013, 10:56:26 pm
Often it feels even better using Lerp(Linear Interpolation) to accelerate speed etc.

The code below lerps from X to Y, using Z as a speed-modifier.

float Lerp(float x, float y, float z) {
        return ((1.0f - z) * x) + (z * y);      
}

So for your example you would input "currentSpeed = Lerp(currentSpeed, maxSpeed, Z);" where Z is a value between 0 and 1, often 0.1f is a good starting point, but it's up to you to tweak it to make it feel good! :D