Skip to content

Commit

Permalink
fix interpolate
Browse files Browse the repository at this point in the history
  • Loading branch information
namel committed Jul 28, 2018
1 parent e018a7b commit 180aeb4
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 427 deletions.
4 changes: 3 additions & 1 deletion es5/ClientEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ var ClientEngine = function () {
}, {
key: 'doInputLocal',
value: function doInputLocal(message) {
if (this.gameEngine.passive) {

// some synchronization strategies (interpolate) ignore inputs on client side
if (this.gameEngine.ignoreInputsOnClient) {
return;
}

Expand Down
55 changes: 1 addition & 54 deletions es5/serialize/DynamicObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ var _GameObject2 = require('./GameObject');

var _GameObject3 = _interopRequireDefault(_GameObject2);

var _Serializer = require('./Serializer');

var _Serializer2 = _interopRequireDefault(_Serializer);

var _BaseTypes = require('./BaseTypes');

var _BaseTypes2 = _interopRequireDefault(_BaseTypes);
Expand Down Expand Up @@ -272,7 +268,7 @@ var DynamicObject = function (_GameObject) {
this.incrementScale = percent / increments;
this.bendingPositionDelta = original.position.getBendingDelta(this.position, positionBending);
this.bendingVelocityDelta = original.velocity.getBendingDelta(this.velocity, velocityBending);
this.bendingAngleDelta = _MathUtils2.default.interpolateDeltaWithWrapping(original.angle, this.angle, angleBending.percent, 0, 2 * Math.PI) / increments;
this.bendingAngleDelta = _MathUtils2.default.interpolateDeltaWithWrapping(original.angle, this.angle, angleBending.percent, 0, 360) / increments;

this.bendingTarget = new this.constructor();
this.bendingTarget.syncTo(this);
Expand Down Expand Up @@ -302,55 +298,6 @@ var DynamicObject = function (_GameObject) {

this.bendingIncrements--;
}
}, {
key: 'interpolate',
value: function interpolate(nextObj, playPercentage, worldSettings) {

var px = this.position.x;
var py = this.position.y;
var angle = this.angle;

// TODO allow netscheme to designate interpolatable attribute (power, shield, etc)
// first copy all the assignable attributes
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = Object.keys(this.constructor.netScheme)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var k = _step.value;

var val = nextObj[k];
if (_Serializer2.default.typeCanAssign(this.constructor.netScheme[k].type)) this[k] = val;else if (typeof val.clone === 'function') this[k] = val.clone();
}

// update other objects with interpolation
// TODO interpolate using TwoVector methods, including wrap-around
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}

function calcInterpolate(start, end, wrap, p) {
if (Math.abs(end - start) > wrap / 2) return end;
return (end - start) * p + start;
}
this.position.x = calcInterpolate(px, nextObj.position.x, worldSettings.width, playPercentage);
this.position.y = calcInterpolate(py, nextObj.position.y, worldSettings.height, playPercentage);

var shortestAngle = ((nextObj.angle - angle) % 360 + 540) % 360 - 180;
this.angle = angle + shortestAngle * playPercentage;
}
}, {
key: 'getAABB',
value: function getAABB() {
Expand Down
65 changes: 0 additions & 65 deletions es5/syncStrategies/ExtrapolateStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _SyncStrategy2 = require('./SyncStrategy');
Expand Down Expand Up @@ -43,8 +41,6 @@ var ExtrapolateStrategy = function (_SyncStrategy) {
_this.lastSync = null;
_this.needFirstSync = true;
_this.recentInputs = {};
_this.gameEngine = _this.clientEngine.gameEngine;
_this.gameEngine.on('client__postStep', _this.extrapolate.bind(_this));
_this.gameEngine.on('client__processInput', _this.clientInputSave.bind(_this));
return _this;
}
Expand All @@ -63,22 +59,6 @@ var ExtrapolateStrategy = function (_SyncStrategy) {
this.recentInputs[inputData.step].push(inputData);
}

// add an object to our world

}, {
key: 'addNewObject',
value: function addNewObject(objId, newObj, options) {

var curObj = new newObj.constructor(this.gameEngine, {
id: objId
});
curObj.syncTo(newObj);
this.gameEngine.addObjectToWorld(curObj);
console.log('adding new object ' + curObj);

return curObj;
}

// clean up the input buffer

}, {
Expand Down Expand Up @@ -367,51 +347,6 @@ var ExtrapolateStrategy = function (_SyncStrategy) {
}
}
}

// Perform client-side extrapolation.

}, {
key: 'extrapolate',
value: function extrapolate(stepDesc) {
var _this3 = this;

// apply incremental bending
this.gameEngine.world.forEachObject(function (id, o) {
if (typeof o.applyIncrementalBending === 'function') {
o.applyIncrementalBending(stepDesc);
o.refreshToPhysics();
}
});

// apply all pending required syncs

var _loop5 = function _loop5() {

var requiredStep = _this3.requiredSyncs[0].stepCount;

// if we haven't reached the corresponding step, it's too soon to apply syncs
if (requiredStep > _this3.gameEngine.world.stepCount) return {
v: void 0
};

_this3.gameEngine.trace.trace(function () {
return 'applying a required sync ' + requiredStep;
});
_this3.applySync(_this3.requiredSyncs.shift());
};

while (this.requiredSyncs.length) {
var _ret5 = _loop5();

if ((typeof _ret5 === 'undefined' ? 'undefined' : _typeof(_ret5)) === "object") return _ret5.v;
}

// if there is a sync from the server, from the past or present, apply it now
if (this.lastSync && this.lastSync.stepCount <= this.gameEngine.world.stepCount) {
this.applySync(this.lastSync);
this.lastSync = null;
}
}
}]);

return ExtrapolateStrategy;
Expand Down
Loading

0 comments on commit 180aeb4

Please sign in to comment.