Skip to content

Commit 119ae11

Browse files
committed
Using perf.now and tidying it all up.
1 parent 4d142ad commit 119ae11

1 file changed

Lines changed: 11 additions & 21 deletions

File tree

v3/src/boot/VariableTimeStep.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var VariableTimeStep = function (game, framerate)
1010
this.started = false;
1111
this.running = false;
1212

13+
// For fixed-step physics
1314
this.fps = framerate;
1415

1516
this.callback = NOOP;
@@ -30,11 +31,6 @@ VariableTimeStep.prototype.constructor = VariableTimeStep;
3031

3132
VariableTimeStep.prototype = {
3233

33-
toString: function ()
34-
{
35-
return 'time: ' + this.time + ' delta: ' + this.delta;
36-
},
37-
3834
start: function (useRAF, callback)
3935
{
4036
if (this.started)
@@ -45,9 +41,11 @@ VariableTimeStep.prototype = {
4541
this.started = true;
4642
this.running = true;
4743

48-
this.time = Date.now();
49-
this.startTime = Date.now();
50-
this.lastTime = Date.now();
44+
var now = window.performance.now();
45+
46+
this.time = now;
47+
this.startTime = now;
48+
this.lastTime = now;
5149

5250
// Pre-populate smoothing array
5351

@@ -83,7 +81,7 @@ VariableTimeStep.prototype = {
8381
// clamp delta to 0.0001 to 0.5 range
8482
dt = Math.max(Math.min(dt, 0.5), 0.0001);
8583

86-
// Smooth out the delta over the previous 10 frames
84+
// Smooth out the delta over the previous X frames
8785

8886
var idx = this.deltaIndex;
8987
var history = this.deltaHistory;
@@ -99,7 +97,7 @@ VariableTimeStep.prototype = {
9997
// average
10098
var avg = 0;
10199

102-
// Loop the array, adding the delta values together
100+
// Loop the history array, adding the delta values together
103101
for (var i = 0; i < max; i++)
104102
{
105103
avg += history[i];
@@ -120,10 +118,9 @@ VariableTimeStep.prototype = {
120118
this.lastTime = time;
121119
},
122120

123-
/*
124121
tick: function ()
125122
{
126-
this.step(true);
123+
this.step(window.performance.now());
127124
},
128125

129126
sleep: function ()
@@ -144,25 +141,19 @@ VariableTimeStep.prototype = {
144141
}
145142
else if (seamless)
146143
{
147-
this.startTime += -this.lastUpdate + (this.lastUpdate = Date.now());
148-
}
149-
else if (this.frame > 10)
150-
{
151-
this.lastUpdate = Date.now() - this.lagThreshold + 5;
144+
this.startTime += -this.lastTime + (this.lastTime = window.performance.now());
152145
}
153146

154147
this.raf.start(this.step.bind(this), this.useRAF);
155148

156149
this.running = true;
157150

158-
this.step(true);
151+
this.step(window.performance.now());
159152
},
160153

161154
setFps: function (value)
162155
{
163156
this.fps = value;
164-
this.gap = 1 / (value || 60);
165-
this.nextTime = this.time + this.gap;
166157

167158
this.wake();
168159
},
@@ -171,7 +162,6 @@ VariableTimeStep.prototype = {
171162
{
172163
return this.fps;
173164
},
174-
*/
175165

176166
stop: function ()
177167
{

0 commit comments

Comments
 (0)