Advertisement
Guest User

Quaternion Math Library

a guest
Apr 2nd, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // --- Quaternions
  2. function Quaternion() {
  3.     this.x = 0;
  4.     this.y = 0;
  5.     this.z = 0;
  6.     this.w = 1;
  7. }
  8.  
  9. Quaternion.prototype = {
  10.     makeIdentity: function() {
  11.         return this.make(0, 0, 0, 1);
  12.     },
  13.  
  14.     make: function(x, y, z, w) {
  15.         this.x = x;
  16.         this.y = y;
  17.         this.z = z;
  18.         this.w = w;
  19.  
  20.         return this;
  21.     },
  22.  
  23.     copy: function(quat) {
  24.         this.x = quat.x;
  25.         this.y = quat.y;
  26.         this.z = quat.z;
  27.         this.w = quat.w;
  28.  
  29.         return this;
  30.     },
  31.  
  32.     add: function(quat) {
  33.         this.x += quat.x;
  34.         this.y += quat.y;
  35.         this.z += quat.z;
  36.         this.w += quat.w;
  37.  
  38.         return this;
  39.     },
  40.  
  41.     sub: function(quat) {
  42.         this.x -= quat.x;
  43.         this.y -= quat.y;
  44.         this.z -= quat.z;
  45.         this.w -= quat.w;
  46.  
  47.         return this;
  48.     },
  49.  
  50.     length: function() {
  51.         return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  52.     },
  53.  
  54.     length2: function() {
  55.         return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
  56.     },
  57.  
  58.     conjugate: function() {
  59.         this.x = -this.x;
  60.         this.y = -this.y;
  61.         this.z = -this.z;
  62.  
  63.         this.normalize();
  64.  
  65.         return this;
  66.     },
  67.  
  68.     normalize: function() {
  69.         var quatLenght = this.length();
  70.  
  71.         if (quatLenght === 0) {
  72.             this.x = 0;
  73.             this.y = 0;
  74.             this.z = 0;
  75.             this.w = 0;
  76.         }
  77.         else {
  78.             this.x /= quatLenght;
  79.             this.y /= quatLenght;
  80.             this.z /= quatLenght;
  81.             this.w /= quatLenght;
  82.         }
  83.  
  84.         return this;
  85.     },
  86.  
  87.     multiply: function(quat) {
  88.         this.x = this.x*quat.w + this.w*quat.x + this.y*quat.z - this.z*quat.y;
  89.         this.y = this.y*quat.w + this.z*quat.x + this.w*quat.y - this.x*quat.z;
  90.         this.z = this.z*quat.w + this.x*quat.y + this.w*quat.z - this.y*quat.x;
  91.         this.w = this.w*quat.w - this.x*quat.x - this.y*quat.y - this.z*quat.z;
  92.  
  93.         return this;
  94.     },
  95.  
  96.     quaternionToMatrix: function() {
  97.         var aMatrix = new Matrix4x3();
  98.  
  99.         aMatrix.element[0] = 1 - 2 * (this.y * this.y - this.z * this.z);
  100.         aMatrix.element[1] = 2 * (this.x * this.y + this.w * this.z);
  101.         aMatrix.element[2] = 2 * (this.x * this.z - this.w * this.y);
  102.  
  103.         aMatrix.element[4] = 2 * (this.x * this.y - this.w * this.z);
  104.         aMatrix.element[5] = 1 - 2 * (this.x * this.x - this.z * this.z);
  105.         aMatrix.element[6] = 2 * (this.y * this.z + this.w * this.x);
  106.  
  107.         aMatrix.element[8] = 2 * (this.x * this.z + this.w * this.y);
  108.         aMatrix.element[9] = 2 * (this.y * this.z - this.w * this.x);
  109.         aMatrix.element[10] = 1 - 2 * (this.x * this.x - this.y * this.y);
  110.  
  111.         return aMatrix;
  112.     },
  113.  
  114.     axisToQuaternion: function(angle, x, y, z) {
  115.         var quatRotation = new Quaternion();
  116.         var sinA = Math.sin(angle/2);
  117.         var length = Math.sqrt(x*x + y*y + z*z);
  118.        
  119.         x = x/length;
  120.         y = y/length;
  121.         z = z/length;
  122.  
  123.         quatRotation.x = (x * sinA);
  124.         quatRotation.y = (y * sinA);
  125.         quatRotation.z = (z * sinA);
  126.         quatRotation.w = Math.cos(angle/2);
  127.  
  128.         return quatRotation;
  129.     },
  130.  
  131.     matrixToQuaternion: function(m) {
  132.         function copySign(a, b) {
  133.             return b < 0 ? -Math.abs(a) : Math.abs(a);
  134.         }
  135.         var absQ = Math.pow(m.determinant(), 1.0 / 3.0);
  136.  
  137.         this.w = Math.sqrt(Math.max(0, absQ + m.element[0] + m.element[5] + m.element[10])) / 2;
  138.         this.x = Math.sqrt(Math.max(0, absQ + m.element[0] - m.element[5] - m.element[10])) / 2;
  139.         this.y = Math.sqrt(Math.max(0, absQ - m.element[0] + m.element[5] - m.element[10])) / 2;
  140.         this.z = Math.sqrt(Math.max(0, absQ - m.element[0] - m.element[5] + m.element[10])) / 2;
  141.         this.x = copySign(this.x, (m.element[9] - m.element[6]));
  142.         this.y = copySign(this.y, (m.element[2] - m.element[8]));
  143.         this.z = copySign(this.z, (m.element[4] - m.element[1]));
  144.         this.normalize();
  145.  
  146.         return this;
  147.     },
  148. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement