Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framerate independent movement speed #89

Open
patrikturi opened this issue Aug 16, 2015 · 2 comments
Open

Framerate independent movement speed #89

patrikturi opened this issue Aug 16, 2015 · 2 comments

Comments

@patrikturi
Copy link

  1. Moving a sprite
    This doesn't work:
public class PlayState extends FlxState ...
@Override
public void update() {
    sprite.x += FlxG.elapsed*60;
}

because FlxG.elapsed is just a constant, not the actual elapsed time.
This doesn't produce the same speed either in my test:

sprite.x += Gdx.graphics.getDeltaTime()*60;
  1. Box2D physics
    When I'm using Box2D and have a state extending B2FlxState, in the update method I call super.update() and the physics also slows down when framerate drops. B2FlxState's update method has the same problem:
_frameTime = FlxG.elapsed;
  1. Side question: how to measure actual FPS?
@moly
Copy link
Contributor

moly commented Aug 16, 2015

Have you tried getting the delta time like this:

private long prevTime;

@Override
public void create()
{
    prevTime = System.currentTimeMillis();
}

@Override
public void update()
{
     long time = System.currentTimeMillis();
     long delta = time - prevTime;

     prevTime = time;
}

@patrikturi
Copy link
Author

Thank you, your suggestion works. Now only the following things remain:
a) B2FlxState.update() shall use this delta time calculated above instead of FlxG.elapsed:

public void update()
{
    _frameTime = FlxG.elapsed;
    _stepsPerformed = 0;
    while((_frameTime > 0.0) && (_stepsPerformed < MAXIMUM_NUMBER_OF_STEPS))
    {
        _deltaTime = FlxU.min(_frameTime, FIXED_TIMESTEP);
        _frameTime -= _deltaTime;
    ...

Because FlxG.elapsed is just a constant not the actual delta time. (I can write some workaround code when calling B2FlxState.update() for now but it should be fixed.)
b) I think it would be good to have this delta time available in FlxG.
edit: I've tried sprite.velocity.x = 30; and it is also not framerate independent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants