r3-legacy/src/game-lib-quaternion.js

109 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-12-01 18:37:57 +01:00
/**
2016-12-09 20:32:09 +01:00
* Runtime quaternion for updating instance objects
2016-12-01 18:37:57 +01:00
* @param graphics GameLib.D3.Graphics
* @param parentObject GameLib.D3.*
2016-12-15 14:53:39 +01:00
* @param apiQuaternion GameLib.API.Quaternion
2016-12-01 18:37:57 +01:00
* @param grain Number
* @constructor
*/
2017-01-06 16:53:53 +01:00
GameLib.Quaternion = function (
graphics,
2016-12-12 17:24:05 +01:00
apiQuaternion,
parentObject,
grain
) {
2016-12-01 18:37:57 +01:00
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiQuaternion)) {
apiQuaternion = {};
}
2016-12-15 14:53:39 +01:00
GameLib.API.Quaternion.call(
2016-12-13 15:41:02 +01:00
this,
apiQuaternion.x,
apiQuaternion.y,
apiQuaternion.z,
apiQuaternion.w,
apiQuaternion.axis,
2016-12-13 15:41:02 +01:00
apiQuaternion.angle
);
2016-12-01 18:37:57 +01:00
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
2016-12-13 15:41:02 +01:00
parentObject = null;
}
this.parentObject = parentObject;
this.axis = new GameLib.Vector3(
this.graphics,
this.axis,
this,
this.grain
);
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(grain)) {
2016-12-13 15:41:02 +01:00
grain = 0.001;
}
this.grain = grain;
2016-12-01 18:37:57 +01:00
this.instance = this.createInstance();
};
2016-12-15 14:53:39 +01:00
GameLib.Quaternion.prototype = Object.create(GameLib.API.Quaternion.prototype);
GameLib.Quaternion.prototype.constructor = GameLib.Quaternion;
2016-12-13 15:41:02 +01:00
2016-12-01 18:37:57 +01:00
/**
2016-12-09 20:32:09 +01:00
* Creates an instance quaternion
2016-12-01 18:37:57 +01:00
* @param update
* @returns {*}
*/
2016-12-15 14:53:39 +01:00
GameLib.Quaternion.prototype.createInstance = function(update) {
2016-12-01 18:37:57 +01:00
var instance = null;
if (update) {
instance = this.instance;
instance.x = this.x;
instance.y = this.y;
instance.z = this.z;
instance.w = this.w;
} else {
instance = new THREE.Quaternion(
this.x,
this.y,
this.z,
this.w
);
2016-12-01 18:37:57 +01:00
}
return instance;
};
/**
* Updates the instance vector, calls updateInstance on the parent object
*/
2016-12-15 14:53:39 +01:00
GameLib.Quaternion.prototype.updateInstance = function() {
2016-12-01 18:37:57 +01:00
this.createInstance(true);
if (this.parentObject &&
this.parentObject.updateInstance) {
2016-12-01 18:37:57 +01:00
this.parentObject.updateInstance();
}
};
/**
2016-12-09 20:32:09 +01:00
* Converts runtime quaternion to API quaternion
* @returns {*}
2016-12-01 18:37:57 +01:00
*/
2016-12-15 14:53:39 +01:00
GameLib.Quaternion.prototype.toApiQuaternion = function() {
return new GameLib.API.Quaternion(
2016-12-01 18:37:57 +01:00
this.x,
this.y,
this.z,
2016-12-09 20:32:09 +01:00
this.w,
this.axis.toApiVector(),
this.angle
2016-12-01 18:37:57 +01:00
);
2016-12-02 13:00:56 +01:00
};