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

Author Topic: Smoothest methods for movement?  (Read 2630 times)

0 Members and 1 Guest are viewing this topic.

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
Smoothest methods for movement?
« on: August 23, 2011, 02:02:04 am »
Inside of main.cpp file:
Code: [Select]

App.SetFramerateLimit(60);


Inside of my player.cpp file:
Code: [Select]

//speed = 1.0f;
//fric = 1.1f;
void Player::update()
{
App.Draw(self);

grav++;
y += grav;

if (right == true && left == false)
{
self.FlipX(false);
xs += speed;
}

if (right == false && left == true)
{
self.FlipX(true);
xs -= speed;
}

xs /= fric;
x += xs;

while (y >= 500)
{
y--;
grav = 0;
canJump = true;
}

self.SetPosition(x, y);
}


Currently the movement feels very choppy.

I've also tried experimenting around with not setting a Framerate Limit, and multiplying ElapsedTime with the values, but I've received different results on different machines.

I'm also aware that the while loop is most likely causing some of the choppy movement as well.  What other methods could I implement that would be more optimal?  It's also just meant for a pseudo ground at the moment.

I'm sure that what I'm currently doing isn't too practical, but I'd certainly like to learn how to make it more practical!  How do you guys like to setup your movement to make it smooth?

CodeCriminal

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Smoothest methods for movement?
« Reply #1 on: August 23, 2011, 04:17:29 am »
Im not sure if this is what your after, or what you mean by choppy but in platformers I generally add an element of acceleration/deceleration to lessen the binary feel of movement ( i.e. moving at constant speeds and stopping instantaneously)

It goes a little something like this

Code: [Select]

if ( key left is pressed and not key left is pressed )
  velocity -= acceleration factor
else if ( velocity < 0 )
  velocity += deceleration factor
  if ( velocity > 0 )
     velocity = 0

// ^ same for the right key only in the opposite direction
// ...


player x position += clamp( velocity, -4, 4 )


the acceleration does not have to be too slow, infact even a quick acceleration ( reaching max speed in a few frames ) helps, but that depends on your taste for the game.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Smoothest methods for movement?
« Reply #2 on: August 24, 2011, 02:09:07 am »
What CodeCriminal mentioned is what you probably will find in most platform games.
Your approach with the while-loop is nonsense since lowering your Y position step by step without drawing the changes or doing anything else equals to setting it directly to 0 and would gain again some time to do diffrent things.
So your while loop actually 'equals':
Code: [Select]
if(y >= 500)
{
   y = 499;
   grav = 0;
   canJump = true;
}


Moving in virtual space should still have some touch of moving in the real world, so you do go to add some of Newton's first law of motion.
But just use the idea of the law and don't go way into physics and maths, yes even make it unnatural. The players will like it more!  :wink:

eXpl0it3r
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/