Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// @function quat4d()
- /// @description creates an identity quaternion
- function quat4d() constructor{
- X = 0;
- Y = 0;
- Z = 0;
- W = 1;
- /// @function copy()
- /// @description creates a copy of the quaternion
- static copy = function(){
- var n = new quat4d();
- n.X = X;
- n.y = Y;
- n.Z = Z;
- n.W = W;
- return n;
- }
- /// @function normalize_self()
- /// @description normalizes the quaternion
- static normalize_self = function(){
- var n = sqrt(X * X + Y * Y + Z * Z + W * W);
- X /= n;
- Y /= n;
- Z /= n;
- W /= n;
- }
- /// @function conjugate_self()
- /// @description converts the quaternion to its conjugate
- static conjugate_self = function(){
- X = -X;
- Y = -Y;
- Z = -Z;
- }
- /// @function conjugate()
- /// @description returns the conjugate of the quaternion
- static conjugate = function(){
- var n = new quat4d();
- n.X = -X;
- n.Y = -Y;
- n.Z = -Z;
- n.W = W;
- return n;
- }
- /// @function add_self(_other)
- /// @description adds two quaternions, changing the original quaternion
- /// @param {pointer} _other the other quaternion
- static add_self = function(_other){
- X += _other.X;
- Y += _other.Y;
- Z += _other.Z;
- W += _other.W;
- }
- /// @function add(_other)
- /// @description adds two quaternions, returning their sum
- /// @param {pointer} _other the other quaternion
- static add = function(_other){
- var n = new quat4d();
- n.X = X + _other.X;
- n.Y = Y + _other.Y;
- n.Z = Z + _other.Z;
- n.W = W + _other.W;
- return n;
- }
- /// @function subtract_self(_other)
- /// @description subtracts two quaternions, changing the original quaternion
- /// @param {pointer} _other the other quaternion
- static subtract_self = function(_other){
- X -= _other.X;
- Y -= _other.Y;
- Z -= _other.Z;
- W -= _other.W;
- }
- /// @function subtract(_other)
- /// @description subtracts two quaternions, returning their difference
- /// @param {pointer} _other the other quaternion
- static subtract = function(_other){
- var n = new quat4d();
- n.X = X - _other.X;
- n.Y = Y - _other.Y;
- n.Z = Z - _other.Z;
- n.W = W - _other.W;
- return n;
- }
- /// @function multiply_self(_other)
- /// @description multplies 2 quaternions, changing the original quaternion
- /// @param {pointer} _other the other quaternion
- static multiply_self = function(_other){
- var XX = X * _other.W + W * _other.X + Y * _other.Z - Z * _other.Y;
- var YY = W * _other.Y - X * _other.Z + Y * _other.W + Z * _other.X;
- var ZZ = W * _other.Z + X * _other.Y - Y * _other.X + Z * _other.W;
- var WW = W * _other.W - X * _other.X - Y * _other.Y - Z * _other.Z;
- X = XX;
- Y = YY;
- Z = ZZ;
- W = WW;
- }
- /// @function multiply(_other)
- /// @description multplies 2 quaternions, returns the product
- /// @param {pointer} _other the other quaternion
- static multiply = function(_other){
- var XX = X * _other.W + W * _other.X + Y * _other.Z - Z * _other.Y;
- var YY = W * _other.Y - X * _other.Z + Y * _other.W + Z * _other.X;
- var ZZ = W * _other.Z + X * _other.Y - Y * _other.X + Z * _other.W;
- var WW = W * _other.W - X * _other.X - Y * _other.Y - Z * _other.Z;
- var n = new quat4d();
- n.X = XX;
- n.Y = YY;
- n.Z = ZZ;
- n.W = WW;
- return n;
- }
- /// @function xrotate_self(_a)
- /// @description sets the quaternion to rotate a vector _a degrees around the X axis
- /// @param {real} _a the angle to rotate by
- static xrotate_self = function(_a){
- var _other = new quat4d()
- _other.X = dsin(_a/2);
- _other.W = dcos(_a/2);
- self.multiply_self(_other);
- delete _other;
- }
- /// @function yrotate_self(_a)
- /// @description sets the quaternion to rotate a vector _a degrees around the Y axis
- /// @param {real} _a the angle to rotate by
- static yrotate_self = function(_a){
- var _other = new quat4d()
- _other.Y = dsin(_a/2);
- _other.W = dcos(_a/2);
- self.multiply_self(_other);
- delete _other;
- }
- /// @function zrotate_self(_a)
- /// @description sets the quaternion to rotate a vector _a degrees around the Z axis
- /// @param {real} _a the angle to rotate by
- static zrotate_self = function(_a){
- var _other = new quat4d()
- _other.Z = dsin(_a/2);
- _other.W = dcos(_a/2);
- self.multiply_self(_other);
- delete _other;
- }
- /// @function rotate_self(_a, _v)
- /// @description sets the quaternion to rotate _a degrees around the vector _v
- /// @param {real} _a the angle to rotate by
- /// @param {pointer} a vec3d for the rotation axis
- static rotate_self = function(_a, _v){
- var _other = new quat4d()
- _other.X = _v.X * dsin(_a/2);
- _other.Y = _v.Y * dsin(_a/2);
- _other.Z = _v.Z * dsin(_a/2);
- _other.W = dcos(_a/2);
- self.multiply_self(_other);
- delete _other;
- }
- /// @function rotate_vec(_v)
- /// @description rotates the vector _v by the quaternion, returns the rotated vector
- /// @param {pointer} _v the vector to rotate
- static rotate_vec = function(_v){
- var v_quat = new quat4d();
- v_quat.X = _v.X;
- v_quat.Y = _v.Y;
- v_quat.Z = _v.Z;
- v_quat.W = 0;
- var conj = self.conjugate();
- v_quat = self.multiply(v_quat);
- v_quat = v_quat.multiply(conj);
- delete conj;
- return new vec3d(v_quat.X, v_quat.Y, v_quat.Z);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment