From e22b9d4535f34e9dabfbf15fa0c847ac4f4c3887 Mon Sep 17 00:00:00 2001 From: BKcore Date: Mon, 15 Oct 2012 17:58:19 +0200 Subject: [PATCH] Added bkcore.hexgl.RaceData class to store ship states. --- bkcore/hexgl/RaceData.js | 72 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 bkcore/hexgl/RaceData.js diff --git a/bkcore/hexgl/RaceData.js b/bkcore/hexgl/RaceData.js new file mode 100644 index 0000000..a32cefe --- /dev/null +++ b/bkcore/hexgl/RaceData.js @@ -0,0 +1,72 @@ + /* + * HexGL + * @author Thibaut 'BKcore' Despoulain + * @license This work is licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported License. + * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/. + */ + +var bkcore = bkcore || {}; +bkcore.hexgl = bkcore.hexgl || {}; + +bkcore.hexgl.RaceData = function(track, mode, shipControls) +{ + this.track = track; + this.mode = mode; + this.shipControls = shipControls; + + this.data = []; + this.last = 0; + this.seek = 0; + + this._p = new THREE.Vector3(); + this._q = new THREE.Quaternion(); +} + +bkcore.hexgl.RaceData.prototype.load = function(data) +{ + this.data = data; + this.last = data.length - 1; +} + +bkcore.hexgl.RaceData.prototype.tick = function(time) +{ + this.data.push(new bkcore.hexgl.RaceTick( + time, + this.shipControls.getPosition(), + this.shipControls.getQuaternion() + )); + ++this.last; +} + +bkcore.hexgl.RaceData.prototype.applyInterpolated = function(time) +{ + while(this.seek < this.last && this.data[this.seek+1].time < time) + ++this.seek; + + var prev = this.data[this.seek]; + + // no interpolation + if(this.seek == this.last && time >= prev.time + || this.seek == 0) + this.apply(prev.position, prev.quaternion); + + // interpolation + var next = this.data[this.seek+1]; + var t = (time-prev.time) / (next.time-prev.time); + this._p.copy(prev.position).lerpSelf(next.position, t); + this._q.copy(prev.quaternion).slerpSelf(next.quaternion, t); + + this.shipControls.teleport(this._p, this._q); +} + +bkcore.hexgl.RaceData.prototype.reset = function() +{ + this.seek = 0; +} + +bkcore.hexgl.RaceTick = function(time, position, quaternion) +{ + this.time = time; + this.position = position; + this.quaternion = quaternion; +} \ No newline at end of file