GameLib.D3.API.Quaternion = function ApiQuaternion(x, y, z, w, axis, angle) { if (GameLib.D3.Utils.UndefinedOrNull(x)) { x = 0; } this.x = x; if (GameLib.D3.Utils.UndefinedOrNull(y)) { y = 0; } this.y = y; if (GameLib.D3.Utils.UndefinedOrNull(z)) { z = 0; } this.z = z; if (GameLib.D3.Utils.UndefinedOrNull(w)) { w = 1; } this.w = w; if (GameLib.D3.Utils.UndefinedOrNull(axis)) { axis = new GameLib.D3.API.Vector3(0, 0, 0); } this.axis = axis; if (GameLib.D3.Utils.UndefinedOrNull(angle)) { angle = 0; } this.angle = angle; }; GameLib.D3.API.Quaternion.prototype.translate = function (v) { this.x += v.x; this.y += v.y; this.z += v.z; return this; }; GameLib.D3.API.Quaternion.prototype.copy = function () { return new GameLib.D3.API.Quaternion( this.x, this.y, this.z, this.w ); }; /** * Note, this normalize function leaves 'w' component untouched */ GameLib.D3.API.Quaternion.prototype.normalize = function () { var EPSILON = 0.000001; var v2 = this.x * this.x + this.y * this.y + this.z * this.z; if (v2 < EPSILON) { return this; //do nothing for zero vector } var invLength = 1 / Math.sqrt(v2); this.x *= invLength; this.y *= invLength; this.z *= invLength; }; GameLib.D3.API.Quaternion.prototype.multiply = function (s) { if (s instanceof GameLib.D3.API.Vector3) { this.x *= s.x; this.y *= s.y; this.z *= s.z; } else if (s instanceof GameLib.D3.API.Matrix4) { var x = s.rows[0].x * this.x + s.rows[0].y * this.y + s.rows[0].z * this.z + s.rows[0].w * this.w; var y = s.rows[1].x * this.x + s.rows[1].y * this.y + s.rows[1].z * this.z + s.rows[1].w * this.w; var z = s.rows[2].x * this.x + s.rows[2].y * this.y + s.rows[2].z * this.z + s.rows[2].w * this.w; var w = s.rows[3].x * this.x + s.rows[3].y * this.y + s.rows[3].z * this.z + s.rows[3].w * this.w; this.x = x; this.y = y; this.z = z; this.w = w; } else { console.log("functionality not implemented - please do this"); throw new Error("not implemented"); } }; GameLib.D3.API.Quaternion.prototype.setFromAngle = function (angle) { this.instance.setFromAxisAngle(this.axis.instance, angle); this.x = this.instance.x; this.y = this.instance.y; this.z = this.instance.z; this.w = this.instance.w; return this; }; GameLib.D3.API.Quaternion.prototype.subtract = function (v) { if (v instanceof GameLib.D3.API.Vector3) { this.x -= v.x; this.y -= v.y; this.z -= v.z; } if (v instanceof GameLib.D3.API.Quaternion) { this.x -= v.x; this.y -= v.y; this.z -= v.z; this.w -= v.w; } return this; };