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

Author Topic: Smooth Movement  (Read 23571 times)

0 Members and 1 Guest are viewing this topic.

Samyboy

  • Newbie
  • *
  • Posts: 19
    • View Profile
Smooth Movement
« on: February 01, 2010, 09:32:39 pm »
Hello,

I am currently trying to write my second game using SFML (my first one was a simple chess -> no action ;)).

Well, I have walked into a problem, that I've tried to solve for a quite a while now.

I am trying to move my Player, once the User presses the Left key. Well, the moving actually does work (God bless SFML!), but my problem is, that my movement is not smooth at all, I think I am doing something very wrong!

Here is the piece of my code that handles the movement:

if (App.GetInput().IsKeyDown(sf::Key::Left)) m_sprite.Move(6, 0);

Is there anyway to move my Player exactly that fast, but smooth?

Thanks for your time!

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Smooth Movement
« Reply #1 on: February 01, 2010, 10:24:44 pm »
Yeah, well, action games are all about physics  :lol:

Instead of changing position with a constant value, use a variable for velocity, and update your position later with it:

Something like:

Code: [Select]
game loop:
     if (leftKeyDown && velocity < maxVel) // max vel is something like 6
          velocity += acceleration; // something like 0.1
    sprite.Move( velocity, 0 );
Pluma - Plug-in Management Framework

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Smooth Movement
« Reply #2 on: February 02, 2010, 01:17:41 am »
You need something called Acceleration and Speed (or Velocity) :-)

Every step you should do this:
Code: [Select]
// Edit: 02/02/2010
accelerationV = 0; // If no key is pressed, we won't accelerate
if KeyUp pressed, accelerationV = 6; // A constant value
else if KeyDown pressed, accelerationV = -6;

speedV += accelerationV;
if(speedV > maxSpeedV) speedV = maxSpeedV;
else if(speedV < -maxSpeedV) speedV = -maxSpeedV;

sprite.x += speedV;


As you see, when you move when your acceleration is different than zero. Now you can use 2D vectors and so!

Samyboy

  • Newbie
  • *
  • Posts: 19
    • View Profile
Smooth Movement
« Reply #3 on: February 02, 2010, 08:47:43 am »
Thanks for you fast answers!

I've tried to implement velocity with an acceleration of 0.3, but it still doesn't work as I want it to:

- The player is still somewhat not smooth when velocity reaches 6
- The player is not smooth when changing his direction to move (like from right to left)

Code: [Select]
// MOVEMENT
if (theWindow.GetInput().IsKeyDown(sf::Key::Left)) {
    if (velocity > -6)
        velocity -= acceleration;
     player.Move(velocity, 0);
}

if (theWindow.GetInput().IsKeyDown(sf::Key::Right)) {
    if (velocity < 6)
        velocity += acceleration;
    player.Move(velocity, 0);
}


I really appreciate your help!

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Smooth Movement
« Reply #4 on: February 02, 2010, 12:55:02 pm »
Instead of
Code: [Select]

if (velocity > -6) velocity -= acceleration;

try
Code: [Select]

velocity -= acceleration; if(velocity < -6) velocity = -6;


Now maybe you want some kind of friction in velocity. Just make your velocity decrease (or increase) to zero every step, no matter you accelerate or not.

Luinechor

  • Guest
Smooth Movement
« Reply #5 on: February 02, 2010, 02:14:09 pm »
Where are you updating the player's position?
It's not smooth if you update it on the Event-loop:
Code: [Select]

while (Window.GetEvent(Event)) {
   // Updating the position here
}


Instead try updating it at the Window-Opened-loop:
Code: [Select]
// Start game loop
while (Window.IsOpened()) {
   //  Update position here.
}

Samyboy

  • Newbie
  • *
  • Posts: 19
    • View Profile
Smooth Movement
« Reply #6 on: February 02, 2010, 03:55:34 pm »
Still not working correctly -.-

Could someone please write me a simple program with a smooth moving rectangle, that the user can move?


Would be really appreciated...

Luinechor

  • Guest
Smooth Movement
« Reply #7 on: February 02, 2010, 04:25:41 pm »
This works perfect for me:
Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
sf::Shape Rectangle = sf::Shape::Rectangle(150, 150, 300, 300, sf::Color::Blue, 1.0f, sf::Color::White);

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            // A key has been pressed
            if (Event.Type == sf::Event::KeyPressed)
            {
                // Escape key : exit
                if (Event.Key.Code == sf::Key::Escape)
                    App.Close();
            }
        }

        // Clear the screen
        App.Clear(sf::Color(0, 0, 0));

if (App.GetInput().IsKeyDown(sf::Key::Left))  {
Rectangle.Move(150 * App.GetFrameTime() * -1, 0);
} else if (App.GetInput().IsKeyDown(sf::Key::Right)) {
Rectangle.Move(150 * App.GetFrameTime(), 0);
}

App.Draw(Rectangle);

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


Don't know what you really mean with 'Smooth', but it is.

Samyboy

  • Newbie
  • *
  • Posts: 19
    • View Profile
Smooth Movement
« Reply #8 on: February 02, 2010, 04:42:32 pm »
Quote from: "Luinechor"
This works perfect for me:
Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
sf::Shape Rectangle = sf::Shape::Rectangle(150, 150, 300, 300, sf::Color::Blue, 1.0f, sf::Color::White);

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            // A key has been pressed
            if (Event.Type == sf::Event::KeyPressed)
            {
                // Escape key : exit
                if (Event.Key.Code == sf::Key::Escape)
                    App.Close();
            }
        }

        // Clear the screen
        App.Clear(sf::Color(0, 0, 0));

if (App.GetInput().IsKeyDown(sf::Key::Left))  {
Rectangle.Move(150 * App.GetFrameTime() * -1, 0);
} else if (App.GetInput().IsKeyDown(sf::Key::Right)) {
Rectangle.Move(150 * App.GetFrameTime(), 0);
}

App.Draw(Rectangle);

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


Don't know what you really mean with 'Smooth', but it is.


Thank you very much!

 

anything