inertialrush-game-test
Version:
This package enables the integration of the Inertial Rush game into any React application, making it easy to showcase the game.
83 lines (68 loc) • 3.25 kB
JavaScript
var bkcore = bkcore || {};
bkcore.hexgl = bkcore.hexgl || {};
bkcore.hexgl.FirstAidKitPlusControl = function () {
this.mesh = null;
this.epsilon = 0.00000001;
this.rotation = new THREE.Vector3(0, 0, 0);
this.quaternion = new THREE.Quaternion();
this.dummy = new THREE.Object3D();
this.rollLerp = 0.08;
this.rollDirection = new THREE.Vector3(0, 0, 1);
this.gradient = 0.0;
this.gradientTarget = 0.0;
this.gradientLerp = 0.05;
this.gradientAxis = new THREE.Vector3(1, 0, 0);
this.tilt = 0.0;
this.tiltTarget = 0.0;
this.tiltLerp = 0.05;
this.tiltAxis = new THREE.Vector3(0, 0, 1);
this.savedPos = null;
}
bkcore.hexgl.FirstAidKitPlusControl.prototype.control = function (threeMesh) {
this.mesh = threeMesh;
this.mesh.martixAutoUpdate = false;
this.mesh.matrixAutoUpdate = false;
// this.dummy.position = this.mesh.position;
// this.dummy.position.x = parseInt(this.dummy.position.x);
// this.dummy.position.y = parseInt(this.dummy.position.y);
// this.dummy.position.z = parseInt(this.dummy.position.z);
this.dummy.position.set(parseInt(this.mesh.position.x), parseInt(this.mesh.position.y), parseInt(this.mesh.position.z));
this.savedPos = this.dummy.position.x;
};
bkcore.hexgl.FirstAidKitPlusControl.prototype.update = function (position) {
this.dummy.position.set(position.x, position.y, position.z)
this.quaternion.set(0, 0.05, 0, 1).normalize();
this.dummy.quaternion.multiply(this.quaternion);
this.dummy.matrix.makeRotationFromQuaternion(this.dummy.quaternion);
this.dummy.matrix.setPosition(this.dummy.position);
if (this.mesh != null) {
this.mesh.matrix.identity();
var tempMatrixRotation = new THREE.Matrix4();
// Gradient (Mesh only, no dummy physics impact)
var gradientDelta = (this.gradientTarget - (this.gradient)) * this.gradientLerp;
if (Math.abs(gradientDelta) > this.epsilon) this.gradient += gradientDelta;
if (Math.abs(this.gradient) > this.epsilon) {
this.gradientAxis.set(1, 0, 0);
tempMatrixRotation.makeRotationAxis(this.gradientAxis, this.gradient);
this.mesh.matrix.multiply( tempMatrixRotation );
}
// Tilting (Idem)
var tiltDelta = (this.tiltTarget - this.tilt) * this.tiltLerp;
if (Math.abs(tiltDelta) > this.epsilon) this.tilt += tiltDelta;
if (Math.abs(this.tilt) > this.epsilon) {
this.tiltAxis.set(0, 0, 1);
tempMatrixRotation.makeRotationAxis(this.tiltAxis, this.tilt);
this.mesh.matrix.multiply( tempMatrixRotation );
}
// Rolling (Idem)
var rollDelta = (this.roll) * this.rollLerp;
if (Math.abs(rollDelta) > this.epsilon) this.roll += rollDelta;
if (Math.abs(this.roll) > this.epsilon) {
this.rollAxis.copy(this.rollDirection);
tempMatrixRotation.makeRotationAxis(this.rollAxis, this.roll);
this.mesh.matrix.multiply( tempMatrixRotation );
}
this.mesh.applyMatrix4(this.dummy.matrix);
this.mesh.updateMatrixWorld(true);
}
};