Created
June 19, 2020 20:02
-
-
Save DragoniteSpam/788c0f53a9c2fdcf805504b9412f770c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function NVec2(_x, _y) { | |
x = _x; | |
y = _y; | |
Add = function(_val) { | |
return new NVec2(x + _val.x, y + _val.y); | |
} | |
Sub = function(_val) { | |
return new NVec2(x - _val.x, y - _val.y); | |
} | |
Mul = function(_val) { | |
if (is_real(_val)) { | |
return new NVec2(x * _val.x, y * _val.y); | |
} else { | |
return new NVec2(x * _val, y * _val); | |
} | |
} | |
Equals = function(_val) { | |
return (x == _val.x && y == _val.y); | |
} | |
Dot = function(_val) { | |
return dot_product(x, y, _val.x, _val.y); | |
} | |
Magnitude = function() { | |
return sqrt(Dot(self)); | |
} | |
Magnitude2 = function() { | |
return Dot(self); | |
} | |
Distance = function() { | |
return point_distance(0, 0, x, y); | |
} | |
Normalize = function() { | |
var n = Mul(1 / Magnitude(self)); | |
x = n.x; | |
y = n.y; | |
} | |
Normalized = function() { | |
return Mul(1 / Magnitude(self)); | |
} | |
Angle = function(_val) { | |
return point_direction(x, y, _val.x, _val.y); | |
} | |
Project = function(_dir) { | |
return _dir.Mul(Dot(_dir) / _dir.Magnitude2()); | |
} | |
Perpendicular = function(_dir) { | |
return Sub(Project(_dir)); | |
} | |
Reflection = function(_normal) { | |
return Sub(_normal.Mul(Dot(_normal).Mul(2))); | |
} | |
} | |
function NVec3(_x, _y, _z) { | |
x = _x; | |
y = _y; | |
z = _z; | |
Add = function(_val) { | |
return new NVec3(x + _val.x, y + _val.y, z + _val.z); | |
} | |
Sub = function(_val) { | |
return new NVec3(x - _val.x, y - _val.y, z - _val.z); | |
} | |
Mul = function(_val) { | |
if (is_real(_val)) { | |
return new NVec3(x * _val.x, y * _val.y, z * _val.z); | |
} else { | |
return new NVec3(x * _val, y * _val, z * _val); | |
} | |
} | |
Equals = function(_val) { | |
return (x == _val.x && y == _val.y && z == _val.z); | |
} | |
Dot = function(_val) { | |
return dot_product_3d(x, y, z, _val.x, _val.y, _val.z); | |
} | |
Magnitude = function() { | |
return sqrt(Dot(self)); | |
} | |
Magnitude2 = function() { | |
return Dot(self); | |
} | |
Distance = function() { | |
return point_distance_3d(0, 0, 0, x, y, z); | |
} | |
Normalize = function() { | |
var n = Mul(1 / Magnitude(self)); | |
x = n.x; | |
y = n.y; | |
z = n.z; | |
} | |
Normalized = function() { | |
return Mul(1 / Magnitude(self)); | |
} | |
Cross = function(_val) { | |
return new NVec3(y * _val.z - z * _val.y, z * _val.x - x * _val.z, x * _val.y - y * _val.x); | |
} | |
Angle = function(_val) { | |
return arccos(Dot(_val) / sqrt(Magnitude2() * _val.Magnitude2())); | |
} | |
Project = function(_dir) { | |
return _dir.Mul(Dot(_dir) / _dir.Magnitude2()); | |
} | |
Perpendicular = function(_dir) { | |
return Sub(Project(_dir)); | |
} | |
Reflection = function(_normal) { | |
return Sub(_normal.Mul(Dot(_normal).Mul(2))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment