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

Author Topic: Inconsistent movement?  (Read 2071 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
Inconsistent movement?
« on: November 05, 2011, 04:29:12 am »
Currently I reference the App.GetFrameTime() anytime I move a sprite around on the screen.  
App is an sf::RenderWindow object.

For moving on the x axis, I have a float that I append to x depending on the direction that the sprite is supposed to go.  It looks something like:
Code: [Select]

offset = App.GetFrameTime() * 0.25f;


And this works fine!  It seems to be consistent for all of the test cases.  However, currently for jumping I'm trying:

Code: [Select]

grav -= ((App.GetFrameTime() * 1.5f) + 0.5f) / factor;


Also, every iteration I add grav to y if you're not touching the ground.

This isn't very consistent, occasionally the character jumps very high as the App.GetFrameTime() is constantly changing.  Initially I was trying:
Code: [Select]

grav -= ((jMax * 3) + 0.5f) / factor;
//jMax = 1.3f;


Which worked fine for myself, but for other test cases the jump is very tiny.

I cap the FPS around 400 frames per second.  The game runs very smooth for me, but for some of my friends it runs around 100 FPS and the jumping is very tiny.  The x axis movement with the offset variable defined above seems to be fine though.

What could I do to possibly make it consistent for everyone?  Thanks for any help!

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Inconsistent movement?
« Reply #1 on: November 07, 2011, 11:13:16 pm »
You should use a fixed timestep.

I do it something like this:

Code: [Select]

const float UPDATE_INTERVAL = 10f; //10ms -> 100 ticks per second

float restTime = 0f;

while(app.IsOpened())
{
    restTime += app.GetFrameTime();
    while(restTime >= UPDATE_INTERVAL)
    {
         restTime -= UPDATE_INTERVAL;
         Update(UPDATE_INTERVAL); //here you calculate the offset and gravity and so on with a fixed timestep.
    }
    Draw();
}


The problem might be if you have functions which are proportional to time^2 and not to time then the interval matters and depending on the pc the interval can change. So you just need to set the update interval to a fixed time step (I usually do 100 ticks per second, you can adjust it to your needs).

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
Inconsistent movement?
« Reply #2 on: November 08, 2011, 07:10:05 pm »
Quote from: "P@u1"
You should use a fixed timestep.

I do it something like this:

Code: [Select]

const float UPDATE_INTERVAL = 10f; //10ms -> 100 ticks per second

float restTime = 0f;

while(app.IsOpened())
{
    restTime += app.GetFrameTime();
    while(restTime >= UPDATE_INTERVAL)
    {
         restTime -= UPDATE_INTERVAL;
         Update(UPDATE_INTERVAL); //here you calculate the offset and gravity and so on with a fixed timestep.
    }
    Draw();
}


The problem might be if you have functions which are proportional to time^2 and not to time then the interval matters and depending on the pc the interval can change. So you just need to set the update interval to a fixed time step (I usually do 100 ticks per second, you can adjust it to your needs).


Thank you!  That's exactly what I was looking for, it seems to work well for each machine that I've tested.

 

anything