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

Author Topic: Best way to get smooth input  (Read 3453 times)

0 Members and 1 Guest are viewing this topic.

Leandro

  • Newbie
  • *
  • Posts: 8
    • MSN Messenger - leostera@hotmail.com
    • View Profile
    • http://www.elrincondelea.com.ar
Best way to get smooth input
« on: November 16, 2008, 08:05:09 pm »
HI there guys, it's the first time I use this API. I come from SDL (3 years, ppff, lotta time) and got bored of doing "low-level" stuff...(not really low, but lower than this for sure).

It's been no more than 12 hours since I've installed SFML and I'm making a Scrolling Shooter, and this API is damn easy to use...I got used really fast. If you aimed that L, then two thumbs up for you  :D

Ok, right to the question:

I ran a while loop with the GetEvent(mEvent) thing, "similar" to SDL's way

Code: [Select]

    while (mApp.GetEvent(mEvent))
        {
        if ((mEvent.Type == sf::Event::Closed) || ( (mEvent.Type == sf::Event::KeyPressed) && (mEvent.Key.Code == sf::Key::Escape) ) )
            Quit();

        //Step functions calls
        mPlayer.Step(mEvent);

        //Drawing stuff
        mApp.Draw(mPlayer.Get());

        mApp.Display();
        }


As you can see, the Step method of the mPlayer instance (actually it's a "Player" class instance) asks for a mEvent.  The Step source is right below:

Code: [Select]

//LEFT
if ( (Ev.Type==sf::Event::KeyPressed)  && (Ev.Key.Code == sf::Key::A) )
    {
    mCurrentImg = 1;
    mMotionX = -5.0f;
    }

//RIGHT
else if ( (Ev.Type==sf::Event::KeyPressed)  && (Ev.Key.Code == sf::Key::D))
    {
    mCurrentImg = 2;
    mMotionX = 5.0f;
    }

//DOWN
else if ( (Ev.Type==sf::Event::KeyPressed)  && (Ev.Key.Code == sf::Key::S) )
    {
    mMotionY = -1.0f;
    }

//UP
else if ( (Ev.Type==sf::Event::KeyPressed)  && (Ev.Key.Code == sf::Key::W))
    {
    mMotionY = 2.5f;
    }
else
    {
    mCurrentImg = 0;
    mMotionX = 0.0f;
    mMotionY = 0.0f;
    mScaleFactor = 0.0f;
    }

mSprite.Move(mMotionX,mMotionY);
mSprite.Scale(mScaleFactor,mScaleFactor);


But the movement I get is not "smooth". By "smooth" I mean that once you press the key you have to wait for less than a second for the Sprite to start moving and you can't just get a nice bidirectional movement when you press A and W or A and S and so on...

So my question is what the heck I'm doing wrong that doesnt let me get that smooth, nice and sweet movement I'm trying to get?

Thanks beforehand

L

PD: Nevermind the Sprite.Scale & mCurrentImg things, I am planning to do some multidirectional-like effect...and I will upload progress =)
Leandro Ostera
My Blags
Who Am I

Imbue

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Best way to get smooth input
« Reply #1 on: November 16, 2008, 10:20:04 pm »
I think the KeyPressed event fires only once when a key is pressed, and then again if it's held down long enough. For example, hold down the a key in a text box. Every time you see it type an a the event would fire.

In the tutorial read the section Real Time Inputs for info on how to test if a key is down.

For example, you may want to try:
Code: [Select]
const sf::Input& Input = App.GetInput();
bool LeftKeyDown = Input.IsKeyDown(sf::Key::Left);

Leandro

  • Newbie
  • *
  • Posts: 8
    • MSN Messenger - leostera@hotmail.com
    • View Profile
    • http://www.elrincondelea.com.ar
Best way to get smooth input
« Reply #2 on: November 17, 2008, 02:23:07 am »
Thanks for the answer

What I was doing after using KeyPressed was just what you did, but got the same laggy result. However, if I comment the while thing in the GetEvent part (meaning, calling GetEvent just one time per loop cycle) there IS SMOOTHNESS IN THE MOVEMENT (not yelling, but emphasising)

The thing is that I'm not pretty sure how will this make other game input react like.

As I am moving on with this I will probably have more input issues, but anyway...I'll stick to the smoothness by now.

Thanks Imbue  :)

PD: By the way, yes, I love reading technical documents or API documentation such as the tutorials you mentioned (which I've already read).
Leandro Ostera
My Blags
Who Am I

Imbue

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Best way to get smooth input
« Reply #3 on: November 17, 2008, 02:29:12 am »
Have you tried keeping the while loop but moving all the drawing/stepping stuff outside of it? It's probably best to redraw everything every frame.

I think your problem is that you are only updating/drawing while there is an event. Most if the time there is no event.

Leandro

  • Newbie
  • *
  • Posts: 8
    • MSN Messenger - leostera@hotmail.com
    • View Profile
    • http://www.elrincondelea.com.ar
Best way to get smooth input
« Reply #4 on: November 17, 2008, 03:04:54 am »
Point taken.

Yep, I just double checked it with some SDL stuff and there was the problem.

So, the source code should be like this:

Main loop
Code: [Select]
   //Event parsing
    while(mApp.GetEvent(mEvent))
        {
        //EVENT STUFF
        }
    //Some things that need to be done every cycle
    mMouseSprite.SetPosition(mApp.GetInput().GetMouseX(),mApp.GetInput().GetMouseX());

    //Step functions calls (basically the game logic)
    mPlayer.Step(mApp);

    //Drawing stuff
    mApp.Draw(mPlayer.Get());

    mApp.Display();


And in wherever you need to use the IsKeyDown thing I am asking for a sf::RenderWindow& and that's all:
Code: [Select]

void Player::Step(sf::RenderWindow& App)
{
bool KA = App.GetInput().IsKeyDown(sf::Key::A);
bool KD = App.GetInput().IsKeyDown(sf::Key::D);
//...and so on


Well, I think this problem is solved. And where I got my moving smothness in movement maybe someone else will get an answer.

Thanks Imbue  :D
Leandro Ostera
My Blags
Who Am I