Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Script assets have changed for v2.3.0 see
- // https://help.YoYogames.com/hc/en-us/articles/360005277377 for more information
- /// @function vec3d(_X, _Y, _Z)
- /// @description a 3D vector
- /// @param {real} _X the X coordinate
- /// @param {real} _Y the Y coordinate
- /// @param {real} _Z the Z coordinate
- function vec3d(_X, _Y, _Z) constructor{
- X = _X;
- Y = _Y;
- Z = _Z;
- /// @function copy()
- /// @description creates a copy of a vector
- static copy = function(){
- return new vec3d(X, Y, Z);
- }
- /// @function set(_X, _Y, _Z)
- /// @description sets the values of the vector
- /// @param {real} _X the X coordinate
- /// @param {real} _Y the Y coordinate
- /// @param {real} _Z the Z coordinate
- static set = function(_X, _Y, _Z){
- X = _X;
- Y = _Y;
- Z = _Z;
- }
- /// @function from_array(array)
- /// @description sets the vector values according to a 3 membered array
- /// @param {pointer} array the array
- static from_array = function(array){
- X = array[0];
- Y = array[1];
- Z = array[2];
- }
- /// @function add_self(_other)
- /// @description adds a vector to the vector, changing the original vector
- /// @param {pointer} _other the vector to add to this vector
- static add_self = function(_other){
- X += _other.X;
- Y += _other.Y;
- Z += _other.Z;
- }
- /// @function add(_other)
- /// @description adds two vectors, creating a new vector
- /// @param {pointer} _other the other vector to add to this vector
- static add = function(_other){
- return new vec3d(X + _other.X, Y + _other.Y, Z + _other.Z);
- }
- /// @function subtract_self(_other)
- /// @description subtracts a vector from the vector, changing the original vector
- /// @param {pointer} _other the other vector to subtract from this vector
- static subtract_self = function(_other){
- X -= _other.X;
- Y -= _other.Y;
- Z -= _other.Z;
- }
- /// @function subtract(_other)
- /// @description subtracts a vector from the vector, creating a new vector
- /// @param {pointer} _other the other vector to subtract from this vector
- static subtract = function(_other){
- return new vec3d(X - _other.X, Y - _other.Y, Z - _other.Z);
- }
- /// @function multiply_self(_s)
- /// @description scalar multiplication, changing the original vector
- /// @param {real} _s the scalar
- static multiply_self = function(_s){
- X *= _s;
- Y *= _s;
- Z *= _s;
- }
- /// @function mutliply(_s)
- /// @description scalar multiplication, creating a new vector
- /// @param {real} _s the scalar
- static multiply = function(_s){
- return new vec3d( X * _s, Y * _s, Z * _s);
- }
- /// @function divide_self(_s)
- /// @description scalar division, changing the original vector
- /// @param {real} _s the scalar
- static divide_self = function(_s){
- X /= _s;
- Y /= _s;
- Z /= _s;
- }
- /// @function divide(_s)
- /// @description scalar division, creating a new vector
- /// @param {real} _s the scalar
- static divide = function(_s){
- return new vec3d(X / _s, Y / _s, Z / _s);
- }
- /// @function square()
- /// @description squares each component and adds them together. Used for magnitude function
- static square = function(){
- return X * X + Y * Y + Z * Z;
- }
- /// @function magnitude()
- /// @description returns the length of the vector
- static magnitude = function(){
- return(sqrt(self.square()));
- }
- /// @function normalize_self()
- /// @description sets the length of the vector to 1
- static normalize_self = function(){
- var mag = self.magnitude();
- if(mag != 0){
- X /= mag;
- Y /= mag;
- Z /= mag;
- }
- }
- /// @function normalize()
- /// @description creates a new vector with same direction, but length of 1
- static normalize = function(){
- var mag = self.magnitude();
- if(mag != 0){
- var XX = X / mag;
- var YY = Y / mag;
- var ZZ = Z / mag;
- return new vec3d(XX, YY, ZZ);
- }else{
- return new vec3d(0, 0, 0);
- }
- }
- /// @function negate_self()
- /// @description sets the vector to its negative vector
- static negate_self = function(){
- X = -X;
- Y = -Y;
- Z = -Z;
- }
- /// @function negate()
- /// @description creates a new vector, opposite from original vector
- static negate = function(){
- return new vec3d(-X, -Y, -Z);
- }
- /// @function dot(_other)
- /// @description returns the dot product of two vectors
- /// @param {pointer} _other the other vector
- static dot = function(_other){
- return(X * _other.X + Y * _other.Y + Z * _other.Z);
- }
- /// @function cross(_other)
- /// @description returns the cross product (a vec3d) of two vectors
- /// @param {pointer} _other the other vector
- static cross = function(_other){
- return new vec3d(Y * _other.Z - Z * _other.Y, Z * _other.X - X * _other.Z, X * _other.Y - Y * _other.X);
- }
- /// @function toString()
- /// @description returns a string with the vector data
- static toString = function(){
- return "X: " + string(X) + " Y: " + string(Y) + " Z: " + string(Z);
- }
- /// @function rotate_self(_a, _b, _c)
- /// @description uses quaternion math to rotate the vector around X, Y, and Z axes
- /// @param {real} _a the angle around the X axis
- /// @param {real} _b the angle around the Y axis
- /// @param {real} _c the angle around the Z axis
- static rotate_self = function(_a, _b, _c){
- var q = new quat4d();
- q.xrotate_self(_a);
- q.yrotate_self(_b);
- q.zrotate_self(_c);
- var temp = q.rotate_vec(self);
- delete q;
- self.set(temp.X, temp.Y, temp.Z);
- delete temp;
- }
- /// @function rotate(_a, _b, _c)
- /// @description uses quaternion math to return a rotated vector around X, Y, and Z axes
- /// @param {real} _a the angle around the X axis
- /// @param {real} _b the angle around the Y axis
- /// @param {real} _c the angle around the Z axis
- static rotate = function(_a, _b, _c){
- var q = new quat4d();
- q.xrotate_self(_a);
- q.yrotate_self(_b);
- q.zrotate_self(_c);
- var temp = q.rotate_vec(self);
- delete q;
- return temp;
- }
- /// @function align(_other)
- /// @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.
- /// @param {pointer} _other the other vector
- static align = function(_other){
- dp = self.dot(_other);
- mag1 = self.magnitude();
- mag2 = _other.magnitude();
- return(dp/(mag1*mag2));
- }
- /// @function conversion_matrix(_other)
- /// @description returns the rotation matrix that will point one vector in the direction of the other vector
- /// @param {pointer} _other the vector we want to convert to
- static conversion_matrix = function(_other){
- var axis = self.cross(_other);
- axis.normalize_self();
- var angle = -darccos(self.dot(_other));
- var mat;
- mat[0] = dcos(angle) + axis.X * axis.X * (1 - dcos(angle));
- mat[1] = axis.X * axis.Y * (1-dcos(angle)) - axis.Z * dsin(angle);
- mat[2] = axis.X * axis.Z * (1 - dcos(angle)) + axis.Y * dsin(angle);
- mat[3] = 0;
- mat[4] = axis.Y * axis.X * (1 - dcos(angle)) + axis.Z * dsin(angle);
- mat[5] = dcos(angle) + axis.Y * axis.Y * (1 - dcos(angle));
- mat[6] = axis.Y * axis.Z * (1 - dcos(angle)) - axis.X * dsin(angle);
- mat[7] = 0;
- mat[8] = axis.Z * axis.X * (1 - dcos(angle)) - axis.Y * dsin(angle);
- mat[9] = axis.Z * axis.Y * (1 - dcos(angle)) + axis.X * dsin(angle);
- mat[10] = dcos(angle) + axis.Z * axis.Z * (1 - dcos(angle));
- mat[11] = 0;
- mat[12] = 0;
- mat[13] = 0;
- mat[14] = 0;
- mat[15] = 1;
- return(mat);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment