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 (
|
2016-12-07 15:20:41 +01:00
|
|
|
graphics,
|
2016-12-12 17:24:05 +01:00
|
|
|
apiQuaternion,
|
2017-01-17 13:24:45 +01:00
|
|
|
parentObject,
|
2016-12-07 15:20:41 +01:00
|
|
|
grain
|
|
|
|
) {
|
2016-12-01 18:37:57 +01:00
|
|
|
this.graphics = graphics;
|
|
|
|
this.graphics.isNotThreeThrow();
|
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
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,
|
2017-01-17 13:24:45 +01:00
|
|
|
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;
|
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
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 {
|
2017-01-17 13:24:45 +01:00
|
|
|
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);
|
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
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
|
|
|
};
|