Progress: Interpolation & Smoothing

This commit is contained in:
Jeremiah Billmann
2013-06-26 21:29:45 -04:00
parent e321a24909
commit ad79f1a67a
2 changed files with 10 additions and 10 deletions
+7 -3
View File
@@ -24,6 +24,7 @@ window.GarageServerIO = (function (window, socketio) {
inputs = [],
currentState = {},
currentTime,
currentFrameTime = new Date().getTime(),
currentDelta,
currentPlayerId,
options = null,
@@ -81,6 +82,7 @@ window.GarageServerIO = (function (window, socketio) {
updateState = function (data) {
currentTime = data.time - pingDelay / 2;
currentFrameTime = new Date().getTime();
currentDelta = data.delta;
updatePlayerState(data);
@@ -197,10 +199,12 @@ window.GarageServerIO = (function (window, socketio) {
var target = playerUpdates[updateIdx + 1];
if(previous && target && currentTime > previous.time && currentTime < target.time) {
var frameDiff = new Date().getTime() - currentFrameTime;
range = target.time - previous.time;
difference = currentTime - previous.time;
difference = currentTime - previous.time + frameDiff;
amount = parseFloat((difference / range).toFixed(3));
positions.previousState = previous.state;
positions.targetState = target.state;
positions.amount = amount;
@@ -220,7 +224,7 @@ window.GarageServerIO = (function (window, socketio) {
if (options.interpolation && options.onInterpolation) {
var positions = getPositions(players[playerIdx].updates);
if (positions.previousState && positions.targetState) {
stateCallback(options.onInterpolation(players[playerIdx].updates[maxUpdate].state, positions.previousState, positions.targetState, positions.amount, 0.015));
stateCallback(options.onInterpolation(players[playerIdx].updates[maxUpdate].state, positions.previousState, positions.targetState, positions.amount));
}
else {
stateCallback(players[playerIdx].updates[maxUpdate].state);
+3 -7
View File
@@ -22,17 +22,13 @@ $(function () {
}
return state;
},
onInterpolation: function (currentState, previousState, targetState, amount, delta) {
var interpolationState = {},
smoothState = {};
onInterpolation: function (currentState, previousState, targetState, amount) {
var interpolationState = {};
interpolationState.x = (previousState.x + amount * (targetState.x - previousState.x));
interpolationState.y = (previousState.y + amount * (targetState.y - previousState.y));
smoothState.x = (currentState.x + (20 * delta) * (interpolationState.x - currentState.x));
smoothState.y = (currentState.y + (20 * delta) * (interpolationState.y - currentState.y));
return smoothState;
return interpolationState;
}
});