Guest User

vector script

a guest
Dec 12th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Script assets have changed for v2.3.0 see
  2. // https://help.YoYogames.com/hc/en-us/articles/360005277377 for more information
  3.  
  4.  
  5. /// @function vec3d(_X, _Y, _Z)
  6. /// @description a 3D vector
  7. /// @param {real} _X the X coordinate
  8. /// @param {real} _Y the Y coordinate
  9. /// @param {real} _Z the Z coordinate
  10. function vec3d(_X, _Y, _Z) constructor{
  11.     X = _X;
  12.     Y = _Y;
  13.     Z = _Z;
  14.     /// @function copy()
  15.     /// @description creates a copy of a vector
  16.     static copy = function(){
  17.         return new vec3d(X, Y, Z);
  18.     }
  19.     /// @function set(_X, _Y, _Z)
  20.     /// @description sets the values of the vector
  21.     /// @param {real} _X the X coordinate
  22.     /// @param {real} _Y the Y coordinate
  23.     /// @param {real} _Z the Z coordinate
  24.     static set = function(_X, _Y, _Z){
  25.         X = _X;
  26.         Y = _Y;
  27.         Z = _Z;
  28.     }
  29.     /// @function from_array(array)
  30.     /// @description sets the vector values according to a 3 membered array
  31.     /// @param {pointer} array the array
  32.     static from_array = function(array){
  33.         X = array[0];
  34.         Y = array[1];
  35.         Z = array[2];
  36.     }
  37.     /// @function add_self(_other)
  38.     /// @description adds a vector to the vector, changing the original vector
  39.     /// @param {pointer} _other the vector to add to this vector
  40.     static add_self = function(_other){
  41.         X += _other.X;
  42.         Y += _other.Y;
  43.         Z += _other.Z;
  44.     }
  45.     /// @function add(_other)
  46.     /// @description adds two vectors, creating a new vector
  47.     /// @param {pointer} _other the other vector to add to this vector
  48.     static add = function(_other){
  49.         return new vec3d(X + _other.X, Y + _other.Y, Z + _other.Z);
  50.     }
  51.     /// @function subtract_self(_other)
  52.     /// @description subtracts a vector from the vector, changing the original vector
  53.     /// @param {pointer} _other the other vector to subtract from this vector
  54.     static subtract_self = function(_other){
  55.         X -= _other.X;
  56.         Y -= _other.Y;
  57.         Z -= _other.Z;
  58.     }
  59.     /// @function subtract(_other)
  60.     /// @description subtracts a vector from the vector, creating a new vector
  61.     /// @param {pointer} _other the other vector to subtract from this vector
  62.     static subtract = function(_other){
  63.         return new vec3d(X - _other.X, Y - _other.Y, Z - _other.Z);
  64.     }
  65.     /// @function multiply_self(_s)
  66.     /// @description scalar multiplication, changing the original vector
  67.     /// @param {real} _s the scalar
  68.     static multiply_self = function(_s){
  69.         X *= _s;
  70.         Y *= _s;
  71.         Z *= _s;
  72.     }
  73.     /// @function mutliply(_s)
  74.     /// @description scalar multiplication, creating a new vector
  75.     /// @param {real} _s the scalar
  76.     static multiply = function(_s){
  77.         return new vec3d( X * _s, Y * _s, Z * _s);
  78.     }
  79.     /// @function divide_self(_s)
  80.     /// @description scalar division, changing the original vector
  81.     /// @param {real} _s the scalar
  82.     static divide_self = function(_s){
  83.         X /= _s;
  84.         Y /= _s;
  85.         Z /= _s;
  86.     }
  87.     /// @function divide(_s)
  88.     /// @description scalar division, creating a new vector
  89.     /// @param {real} _s the scalar
  90.     static divide = function(_s){
  91.         return new vec3d(X / _s, Y / _s, Z / _s);
  92.     }
  93.     /// @function square()
  94.     /// @description squares each component and adds them together. Used for magnitude function
  95.     static square = function(){
  96.         return X * X + Y * Y + Z * Z;
  97.     }
  98.     /// @function magnitude()
  99.     /// @description returns the length of the vector
  100.     static magnitude = function(){
  101.         return(sqrt(self.square()));
  102.     }
  103.     /// @function normalize_self()
  104.     /// @description sets the length of the vector to 1
  105.     static normalize_self = function(){
  106.         var mag = self.magnitude();
  107.         if(mag != 0){
  108.             X /= mag;
  109.             Y /= mag;
  110.             Z /= mag;
  111.         }
  112.     }
  113.     /// @function normalize()
  114.     /// @description creates a new vector with same direction, but length of 1
  115.     static normalize = function(){
  116.         var mag = self.magnitude();
  117.         if(mag != 0){
  118.             var XX = X / mag;
  119.             var YY = Y / mag;
  120.             var ZZ = Z / mag;
  121.             return new vec3d(XX, YY, ZZ);
  122.         }else{
  123.             return new vec3d(0, 0, 0);
  124.         }  
  125.     }
  126.     /// @function negate_self()
  127.     /// @description sets the vector to its negative vector
  128.     static negate_self = function(){
  129.         X = -X;
  130.         Y = -Y;
  131.         Z = -Z;
  132.     }
  133.     /// @function negate()
  134.     /// @description creates a new vector, opposite from original vector
  135.     static negate = function(){
  136.         return new vec3d(-X, -Y, -Z);
  137.     }
  138.     /// @function dot(_other)
  139.     /// @description returns the dot product of two vectors
  140.     /// @param {pointer} _other the other vector
  141.     static dot = function(_other){
  142.         return(X * _other.X + Y * _other.Y + Z * _other.Z);
  143.     }
  144.     /// @function cross(_other)
  145.     /// @description returns the cross product (a vec3d) of two vectors
  146.     /// @param {pointer} _other the other vector
  147.     static cross = function(_other){
  148.         return new vec3d(Y * _other.Z - Z * _other.Y, Z * _other.X - X * _other.Z, X * _other.Y - Y * _other.X);
  149.     }
  150.     /// @function toString()
  151.     /// @description returns a string with the vector data
  152.     static toString = function(){
  153.         return "X: " + string(X) + " Y: " + string(Y) + " Z: " + string(Z);
  154.     }
  155.    
  156.     /// @function rotate_self(_a, _b, _c)
  157.     /// @description uses quaternion math to rotate the vector around X, Y, and Z axes
  158.     /// @param {real} _a the angle around the X axis
  159.     /// @param {real} _b the angle around the Y axis
  160.     /// @param {real} _c the angle around the Z axis
  161.     static rotate_self = function(_a, _b, _c){
  162.         var q = new quat4d();
  163.         q.xrotate_self(_a);
  164.         q.yrotate_self(_b);
  165.         q.zrotate_self(_c);
  166.         var temp = q.rotate_vec(self);
  167.         delete q;
  168.         self.set(temp.X, temp.Y, temp.Z);
  169.         delete temp;
  170.     }
  171.     /// @function rotate(_a, _b, _c)
  172.     /// @description uses quaternion math to return a rotated vector around X, Y, and Z axes
  173.     /// @param {real} _a the angle around the X axis
  174.     /// @param {real} _b the angle around the Y axis
  175.     /// @param {real} _c the angle around the Z axis
  176.     static rotate = function(_a, _b, _c){
  177.         var q = new quat4d();
  178.         q.xrotate_self(_a);
  179.         q.yrotate_self(_b);
  180.         q.zrotate_self(_c);
  181.         var temp = q.rotate_vec(self);
  182.         delete q;
  183.         return temp;
  184.     }
  185.     /// @function align(_other)
  186.     /// @description returns an "alignment value". Codirectional vectors will have a value of +1. Opposite vectors will have value of -1. Orthogonal vectors will have value of 0.
  187.     /// @param {pointer} _other the other vector
  188.     static align = function(_other){
  189.         dp = self.dot(_other);
  190.         mag1 = self.magnitude();
  191.         mag2 = _other.magnitude();
  192.         return(dp/(mag1*mag2));
  193.     }
  194.     /// @function conversion_matrix(_other)
  195.     /// @description returns the rotation matrix that will point one vector in the direction of the other vector
  196.     /// @param {pointer} _other the vector we want to convert to
  197.     static conversion_matrix = function(_other){
  198.         var axis = self.cross(_other);
  199.         axis.normalize_self();
  200.         var angle = -darccos(self.dot(_other));
  201.        
  202.         var mat;
  203.         mat[0] = dcos(angle) + axis.X * axis.X * (1 - dcos(angle));
  204.         mat[1] = axis.X * axis.Y * (1-dcos(angle)) - axis.Z * dsin(angle);
  205.         mat[2] = axis.X * axis.Z * (1 - dcos(angle)) + axis.Y * dsin(angle);
  206.         mat[3] = 0;
  207.         mat[4] = axis.Y * axis.X * (1 - dcos(angle)) + axis.Z * dsin(angle);
  208.         mat[5] = dcos(angle) + axis.Y * axis.Y * (1 - dcos(angle));
  209.         mat[6] = axis.Y * axis.Z * (1 - dcos(angle)) - axis.X * dsin(angle);
  210.         mat[7] = 0;
  211.         mat[8] = axis.Z * axis.X * (1 - dcos(angle)) - axis.Y * dsin(angle);
  212.         mat[9] = axis.Z * axis.Y * (1 - dcos(angle)) + axis.X * dsin(angle);
  213.         mat[10] = dcos(angle) + axis.Z * axis.Z * (1 - dcos(angle));
  214.         mat[11] = 0;
  215.         mat[12] = 0;
  216.         mat[13] = 0;
  217.         mat[14] = 0;
  218.         mat[15] = 1;
  219.        
  220.         return(mat);
  221.     }
  222. };
  223.  
Advertisement
Add Comment
Please, Sign In to add comment