Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // --- Quaternions
- function Quaternion() {
- this.x = 0;
- this.y = 0;
- this.z = 0;
- this.w = 1;
- }
- Quaternion.prototype = {
- makeIdentity: function() {
- return this.make(0, 0, 0, 1);
- },
- make: function(x, y, z, w) {
- this.x = x;
- this.y = y;
- this.z = z;
- this.w = w;
- return this;
- },
- copy: function(quat) {
- this.x = quat.x;
- this.y = quat.y;
- this.z = quat.z;
- this.w = quat.w;
- return this;
- },
- add: function(quat) {
- this.x += quat.x;
- this.y += quat.y;
- this.z += quat.z;
- this.w += quat.w;
- return this;
- },
- sub: function(quat) {
- this.x -= quat.x;
- this.y -= quat.y;
- this.z -= quat.z;
- this.w -= quat.w;
- return this;
- },
- length: function() {
- return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
- },
- length2: function() {
- return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
- },
- conjugate: function() {
- this.x = -this.x;
- this.y = -this.y;
- this.z = -this.z;
- this.normalize();
- return this;
- },
- normalize: function() {
- var quatLenght = this.length();
- if (quatLenght === 0) {
- this.x = 0;
- this.y = 0;
- this.z = 0;
- this.w = 0;
- }
- else {
- this.x /= quatLenght;
- this.y /= quatLenght;
- this.z /= quatLenght;
- this.w /= quatLenght;
- }
- return this;
- },
- multiply: function(quat) {
- this.x = this.x*quat.w + this.w*quat.x + this.y*quat.z - this.z*quat.y;
- this.y = this.y*quat.w + this.z*quat.x + this.w*quat.y - this.x*quat.z;
- this.z = this.z*quat.w + this.x*quat.y + this.w*quat.z - this.y*quat.x;
- this.w = this.w*quat.w - this.x*quat.x - this.y*quat.y - this.z*quat.z;
- return this;
- },
- quaternionToMatrix: function() {
- var aMatrix = new Matrix4x3();
- aMatrix.element[0] = 1 - 2 * (this.y * this.y - this.z * this.z);
- aMatrix.element[1] = 2 * (this.x * this.y + this.w * this.z);
- aMatrix.element[2] = 2 * (this.x * this.z - this.w * this.y);
- aMatrix.element[4] = 2 * (this.x * this.y - this.w * this.z);
- aMatrix.element[5] = 1 - 2 * (this.x * this.x - this.z * this.z);
- aMatrix.element[6] = 2 * (this.y * this.z + this.w * this.x);
- aMatrix.element[8] = 2 * (this.x * this.z + this.w * this.y);
- aMatrix.element[9] = 2 * (this.y * this.z - this.w * this.x);
- aMatrix.element[10] = 1 - 2 * (this.x * this.x - this.y * this.y);
- return aMatrix;
- },
- axisToQuaternion: function(angle, x, y, z) {
- var quatRotation = new Quaternion();
- var sinA = Math.sin(angle/2);
- var length = Math.sqrt(x*x + y*y + z*z);
- x = x/length;
- y = y/length;
- z = z/length;
- quatRotation.x = (x * sinA);
- quatRotation.y = (y * sinA);
- quatRotation.z = (z * sinA);
- quatRotation.w = Math.cos(angle/2);
- return quatRotation;
- },
- matrixToQuaternion: function(m) {
- function copySign(a, b) {
- return b < 0 ? -Math.abs(a) : Math.abs(a);
- }
- var absQ = Math.pow(m.determinant(), 1.0 / 3.0);
- this.w = Math.sqrt(Math.max(0, absQ + m.element[0] + m.element[5] + m.element[10])) / 2;
- this.x = Math.sqrt(Math.max(0, absQ + m.element[0] - m.element[5] - m.element[10])) / 2;
- this.y = Math.sqrt(Math.max(0, absQ - m.element[0] + m.element[5] - m.element[10])) / 2;
- this.z = Math.sqrt(Math.max(0, absQ - m.element[0] - m.element[5] + m.element[10])) / 2;
- this.x = copySign(this.x, (m.element[9] - m.element[6]));
- this.y = copySign(this.y, (m.element[2] - m.element[8]));
- this.z = copySign(this.z, (m.element[4] - m.element[1]));
- this.normalize();
- return this;
- },
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement