Guest User

quaternion script

a guest
Dec 12th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @function quat4d()
  2. /// @description creates an identity quaternion
  3. function quat4d() constructor{
  4.     X = 0;
  5.     Y = 0;
  6.     Z = 0;
  7.     W = 1;
  8.     /// @function copy()
  9.     /// @description creates a copy of the quaternion
  10.     static copy = function(){
  11.         var n = new quat4d();
  12.         n.X = X;
  13.         n.y = Y;
  14.         n.Z = Z;
  15.         n.W = W;
  16.         return n;
  17.     }
  18.     /// @function normalize_self()
  19.     /// @description normalizes the quaternion
  20.     static normalize_self = function(){
  21.         var n = sqrt(X * X + Y * Y + Z * Z + W * W);
  22.         X /= n;
  23.         Y /= n;
  24.         Z /= n;
  25.         W /= n;
  26.     }
  27.     /// @function conjugate_self()
  28.     /// @description converts the quaternion to its conjugate
  29.     static conjugate_self = function(){
  30.         X = -X;
  31.         Y = -Y;
  32.         Z = -Z;
  33.     }
  34.     /// @function conjugate()
  35.     /// @description returns the conjugate of the quaternion
  36.     static conjugate = function(){
  37.         var n = new quat4d();
  38.         n.X = -X;
  39.         n.Y = -Y;
  40.         n.Z = -Z;
  41.         n.W = W;
  42.         return n;
  43.     }
  44.     /// @function add_self(_other)
  45.     /// @description adds two quaternions, changing the original quaternion
  46.     /// @param {pointer} _other the other quaternion
  47.     static add_self = function(_other){
  48.         X += _other.X;
  49.         Y += _other.Y;
  50.         Z += _other.Z;
  51.         W += _other.W;
  52.     }
  53.     /// @function add(_other)
  54.     /// @description adds two quaternions, returning their sum
  55.     /// @param {pointer} _other the other quaternion
  56.     static add = function(_other){
  57.         var n = new quat4d();
  58.         n.X = X + _other.X;
  59.         n.Y = Y + _other.Y;
  60.         n.Z = Z + _other.Z;
  61.         n.W = W + _other.W;
  62.         return n;
  63.     }
  64.     /// @function subtract_self(_other)
  65.     /// @description subtracts two quaternions, changing the original quaternion
  66.     /// @param {pointer} _other the other quaternion
  67.     static subtract_self = function(_other){
  68.         X -= _other.X;
  69.         Y -= _other.Y;
  70.         Z -= _other.Z;
  71.         W -= _other.W;
  72.     }
  73.     /// @function subtract(_other)
  74.     /// @description subtracts two quaternions, returning their difference
  75.     /// @param {pointer} _other the other quaternion
  76.     static subtract = function(_other){
  77.         var n = new quat4d();
  78.         n.X = X - _other.X;
  79.         n.Y = Y - _other.Y;
  80.         n.Z = Z - _other.Z;
  81.         n.W = W - _other.W;
  82.         return n;
  83.     }
  84.     /// @function multiply_self(_other)
  85.     /// @description multplies 2 quaternions, changing the original quaternion
  86.     /// @param {pointer} _other the other quaternion
  87.     static multiply_self = function(_other){
  88.         var XX = X * _other.W + W * _other.X + Y * _other.Z - Z * _other.Y;
  89.         var YY = W * _other.Y - X * _other.Z + Y * _other.W + Z * _other.X;
  90.         var ZZ = W * _other.Z + X * _other.Y - Y * _other.X + Z * _other.W;
  91.         var WW = W * _other.W - X * _other.X - Y * _other.Y - Z * _other.Z;
  92.        
  93.         X = XX;
  94.         Y = YY;
  95.         Z = ZZ;
  96.         W = WW;
  97.     }
  98.     /// @function multiply(_other)
  99.     /// @description multplies 2 quaternions, returns the product
  100.     /// @param {pointer} _other the other quaternion
  101.     static multiply = function(_other){
  102.         var XX = X * _other.W + W * _other.X + Y * _other.Z - Z * _other.Y;
  103.         var YY = W * _other.Y - X * _other.Z + Y * _other.W + Z * _other.X;
  104.         var ZZ = W * _other.Z + X * _other.Y - Y * _other.X + Z * _other.W;
  105.         var WW = W * _other.W - X * _other.X - Y * _other.Y - Z * _other.Z;
  106.         var n = new quat4d();
  107.         n.X = XX;
  108.         n.Y = YY;
  109.         n.Z = ZZ;
  110.         n.W = WW;
  111.         return n;
  112.     }
  113.     /// @function xrotate_self(_a)
  114.     /// @description sets the quaternion to rotate a vector _a degrees around the X axis
  115.     /// @param {real} _a the angle to rotate by
  116.     static xrotate_self = function(_a){
  117.         var _other = new quat4d()
  118.         _other.X = dsin(_a/2);
  119.         _other.W = dcos(_a/2);
  120.         self.multiply_self(_other);
  121.         delete _other;
  122.     }
  123.     /// @function yrotate_self(_a)
  124.     /// @description sets the quaternion to rotate a vector _a degrees around the Y axis
  125.     /// @param {real} _a the angle to rotate by
  126.     static yrotate_self = function(_a){
  127.         var _other = new quat4d()
  128.         _other.Y = dsin(_a/2);
  129.         _other.W = dcos(_a/2);
  130.         self.multiply_self(_other);
  131.         delete _other;
  132.     }
  133.     /// @function zrotate_self(_a)
  134.     /// @description sets the quaternion to rotate a vector _a degrees around the Z axis
  135.     /// @param {real} _a the angle to rotate by
  136.     static zrotate_self = function(_a){
  137.         var _other = new quat4d()
  138.         _other.Z = dsin(_a/2);
  139.         _other.W = dcos(_a/2);
  140.         self.multiply_self(_other);
  141.         delete _other;
  142.     }
  143.     /// @function rotate_self(_a, _v)
  144.     /// @description sets the quaternion to rotate _a degrees around the vector _v
  145.     /// @param {real} _a the angle to rotate by
  146.     /// @param {pointer} a vec3d for the rotation axis
  147.     static rotate_self = function(_a, _v){
  148.         var _other = new quat4d()
  149.         _other.X = _v.X * dsin(_a/2);
  150.         _other.Y = _v.Y * dsin(_a/2);
  151.         _other.Z = _v.Z * dsin(_a/2);
  152.         _other.W = dcos(_a/2);
  153.         self.multiply_self(_other);
  154.         delete _other;
  155.     }
  156.     /// @function rotate_vec(_v)
  157.     /// @description rotates the vector _v by the quaternion, returns the rotated vector
  158.     /// @param {pointer} _v the vector to rotate
  159.     static rotate_vec = function(_v){
  160.         var v_quat = new quat4d();
  161.         v_quat.X = _v.X;
  162.         v_quat.Y = _v.Y;
  163.         v_quat.Z = _v.Z;
  164.         v_quat.W = 0;
  165.         var conj = self.conjugate();
  166.         v_quat = self.multiply(v_quat);
  167.         v_quat = v_quat.multiply(conj);
  168.         delete conj;
  169.         return new vec3d(v_quat.X, v_quat.Y, v_quat.Z);
  170.     }
  171.  
  172. };
Advertisement
Add Comment
Please, Sign In to add comment