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

Author Topic: diagonal movement = little shaking  (Read 1602 times)

0 Members and 2 Guests are viewing this topic.

Ileev

  • Newbie
  • *
  • Posts: 3
    • View Profile
diagonal movement = little shaking
« on: January 23, 2014, 04:30:20 am »
hello everyone!
I have a problem with my movement system.

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                m_sprite.setTextureRect(sf::IntRect(42, 0, 42, 42));
                m_vel.y = -4;
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                m_sprite.setTextureRect(sf::IntRect(0, 0, 42, 42));
                m_vel.y = 4;
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                m_sprite.setTextureRect(sf::IntRect(84, 0, 42, 42));
                m_vel.x = -4;

        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                m_sprite.setTextureRect(sf::IntRect(126, 0, 42, 42));
                m_vel.x = 4;
        }


        if ((m_vel.x != 0) && (m_vel.y != 0)) m_vel /= sqrt(2.f);
        m_sprite.move(m_vel.x, m_vel.y);

        if (m_vel.x != 0) m_vel.x = 0;
        if (m_vel.y != 0) m_vel.y = 0;
        }

My movement is very smooth when I move in straight lines but in other cases my character starts shaking.
I believe this line
if ((m_vel.x != 0) && (m_vel.y != 0)) m_vel /= sqrt(2.f);
is responsible for this issue. However I can't just delete it but I also want to get rid of this little shaking. Any ideas how can I do that?


« Last Edit: January 23, 2014, 04:33:48 am by Ileev »

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: diagonal movement = little shaking
« Reply #1 on: January 23, 2014, 05:00:09 am »
I don't think, a more complete code would be welcome. Specialy the part you are drawing your sprite.
« Last Edit: January 23, 2014, 05:14:09 am by math1992 »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: diagonal movement = little shaking
« Reply #2 on: January 23, 2014, 05:09:20 am »
A couple things, first I would recommend that you don't use real time events in this situation. Instead handle key down/up events in the event loop and set a flag accordingly. Second take into consideration delta time when you do your movements. For example, move(velocity * deltatime.asSeconds()). Third you may want to consider a fixed time step for updates (helps remove some issues especially with high frame rates). And fourth of all, if you decide to stick with real time events make sure it isn't in the other event loop.  ;)

Specialy the part you are drawing your sprite.

Makes no difference....   :P

I don't think, a more complete code would be welcome.

But this might  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Ileev

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: diagonal movement = little shaking
« Reply #3 on: January 23, 2014, 06:37:04 am »
hello again.

I should have said this before but I have class Engine which has "update" method.

I have changed few things and now it looks like this:
void CEngine::run()
{
        while (window->isOpen())
        {
                handleEvents()
                update(m_deltatime.getElapsedTime());
                m_deltatime.restart();
                render();
        }
}

void CEngine::update(sf::Time deltatime)
{
        m_player->update(deltatime.asSeconds());
}

Player "update" method:
void CPlayer::update(float deltatime)
{
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                m_sprite.setTextureRect(sf::IntRect(42, 0, 42, 42));
                m_vel.y = -169;
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                m_sprite.setTextureRect(sf::IntRect(0, 0, 42, 42));
                m_vel.y = 169;
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                m_sprite.setTextureRect(sf::IntRect(84, 0, 42, 42));
                m_vel.x = -169;

        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                m_sprite.setTextureRect(sf::IntRect(126, 0, 42, 42));
                m_vel.x = 169;
        }


        if ((m_vel.x != 0) && (m_vel.y != 0)) m_vel /= sqrt(2.f);
        m_sprite.move(m_vel.x*deltatime, m_vel.y*deltatime);

        if (m_vel.x != 0) m_vel.x = 0;
        if (m_vel.y != 0) m_vel.y = 0;


}

Slight difference, maybe I have to live with that :D

 

anything