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

Update inconsistency #15

Open
heinermann opened this issue Oct 6, 2012 · 6 comments
Open

Update inconsistency #15

heinermann opened this issue Oct 6, 2012 · 6 comments
Assignees
Labels
Milestone

Comments

@heinermann
Copy link
Member

So for example, the ball moves (as per Update function) consistently, but while I am moving the mouse over the webpage, it updates more quickly than it did when I did not move the mouse.

So moving the mouse = ball moves faster.

Is this a bug or something?

@ghost ghost assigned erisco Oct 6, 2012
@heinermann
Copy link
Member Author

This is on Firefox 15.0.1, Win7_64.

@erisco
Copy link
Contributor

erisco commented Oct 6, 2012

This is not a bug. The update for entities and scenes is not called on a fixed time step. Rather, it is called per event. This is why moving the mouse causes a change, because mouse movement events are being fired (probably at 60Hz).

The reason this was done was to simplify update functions.

  • Instead of having to iterate over a list of mouse events, you only have to deal with the current one. Same goes for keyboard events.
  • Event ordering. If event A happens before event B, your update will always react to A before B without having to check event timings.
  • Time accuracy. The time delta between two events occurring on one frame is retained without extra coding to fetch and operate on event times.

Of course, there are down-falls.

  • Some operations seem to require fixed timesteps (ex: box2dweb requires fixed timestep). I say 'seem' because I haven't yet discovered why this would be (ex: integrators use a time step, but only scale in precision if the time step changes, so if you can guarantee a maximum step, you should have a guaranteed maximum error). For these cases, using GFW_Throttle seems well enough, especially since it gives you arbitrary time step control.
  • CPU time can be wasted on redundant updates. My preference would be to break apart the update method so that heavy, time-insensitive operations can be done separately, but how this should be done isn't yet clear and until a performance issue happens, it's a wontfix.

There are two solutions. First, you could use GFW_Throttle(msDelay, predicate, action) to only call the action (in this case, moving the ball) on a fixed time step. The second, better solution for this case, is to move the ball as a function of time. ie, instead of ball.pos += dir * speed, use ball.pos += dir * speed * dt.

You can get the time delta from updateParams.getTime().getDelta().

In another project I'm working on I'm experiencing fishy behaviour even when accounting for time. My theory is that mouse position events are coming in so quickly that there is significant round-off error on the time delta. Although it is ultimately up to the browser, my mouse polls at 1000Hz, which means the delta between mouse events can be as low as 1ms, which is the smallest possible time delta greater than zero (since the browser only reports times to a millisecond accuracy).

Once the ball update method is corrected, we can take a closer look at this issue.

@heinermann
Copy link
Member Author

I tried doing what you suggested but it created some weird results, I logged the value of delta and got this from the console:

[19:44:56.390] delta: 1348353771891 @ file:///C:/Users/A/Documents/GitHub/Pong/pub/entity/Ball.js:76
[19:44:56.395] delta: -1348353771875 @ file:///C:/Users/A/Documents/GitHub/Pong/pub/entity/Ball.js:76
[19:44:56.396] delta: 1348353771881 @ file:///C:/Users/A/Documents/GitHub/Pong/pub/entity/Ball.js:76
[19:44:56.435] delta: -1348353771881 @ file:///C:/Users/A/Documents/GitHub/Pong/pub/entity/Ball.js:76
[19:44:56.435] delta: 16 @ file:///C:/Users/A/Documents/GitHub/Pong/pub/entity/Ball.js:76
[19:44:56.435] delta: 0 @ file:///C:/Users/A/Documents/GitHub/Pong/pub/entity/Ball.js:76
[19:44:56.436] delta: 15 @ file:///C:/Users/A/Documents/GitHub/Pong/pub/entity/Ball.js:76
[19:44:56.436] delta: 0 @ file:///C:/Users/A/Documents/GitHub/Pong/pub/entity/Ball.js:76
[19:44:56.436] delta: 1348353771890 @ file:///C:/Users/A/Documents/GitHub/Pong/pub/entity/Ball.js:76
[19:44:56.444] delta: -1348353771874 @ file:///C:/Users/A/Documents/GitHub/Pong/pub/entity/Ball.js:76
[19:44:56.444] delta: 1348353771883 @ file:///C:/Users/A/Documents/GitHub/Pong/pub/entity/Ball.js:76
[19:44:56.475] delta: -1348353771883 @ file:///C:/Users/A/Documents/GitHub/Pong/pub/entity/Ball.js:76

code (is it right?):

    var delta = updateParams.getTime().getDelta();
    console.log("delta:", delta);

If I use the following, then it is somewhat normalized, but still a barely-noticable inconsistency.

    var delta = updateParams.getTime().getDelta();
    if ( Math.abs(delta) > 1000 )
      return;

@erisco
Copy link
Contributor

erisco commented Oct 7, 2012

Sorry, I thought I posted yesterday. Must have forgot to hit the comment button.

That behaviour is definitely irregular. Please make a new branch for issue_15 and push your code that produces this log. There might be a bug in GFW but I'll have to take a look.

@heinermann
Copy link
Member Author

Also I forgot to mention that this value only appears when I move my mouse. Added issue_15 branch.

EDIT: It also occurs when holding down a keyboard key.

@erisco
Copy link
Contributor

erisco commented Oct 8, 2012

No issue with Chrome Version 22.0.1229.79 m, so might be Firefox specific.

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

No branches or pull requests

2 participants