Created
February 28, 2024 19:23
-
-
Save AlexiyOrlov/3cf86ab6a453472a6c357478841c85b4 to your computer and use it in GitHub Desktop.
Vector 2D for Game maker studio (unlike original, this one is immutable)
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 vec2() constructor | |
{ | |
#region Initialize Vector | |
__IS_VEC_3 = false; | |
#region Setup | |
/// Set available parameters | |
var paramArray = array_create(2,0); | |
for ( var i=0; i<argument_count; i++ ) | |
{ | |
paramArray[@i] = argument[i]; | |
} | |
#endregion | |
#region Position[0] Handling | |
var arg = argument[0]; | |
if ( argument_count==1 ) | |
{ | |
if ( is_struct(arg) ) | |
{ | |
paramArray = [ arg.x, arg.y ]; | |
} | |
else | |
{ | |
paramArray = [ arg, arg ]; | |
} | |
} | |
#endregion | |
#region Set values | |
x=paramArray[0]; | |
y=paramArray[1]; | |
#endregion | |
#endregion | |
#region Math Functions | |
#region Arithmetical functions | |
static multiply=function(val) | |
{ | |
///@func mul(value_or_vec) | |
var v = new vec2(val); | |
return new vec2(x*v.x,y*v.y) ; | |
} | |
static add=function(val) | |
{ | |
///@func add(value_or_vec) | |
var v = new vec2(val); | |
var v2=new vec2(x+v.x,y+v.y) | |
return v2; | |
} | |
static subtract=function(val) | |
{ | |
///@func sub(value_or_vec) | |
var v = new vec2(val); | |
var v2=new vec2(x-v.x,y-v.y) | |
return v2; | |
} | |
static divide=function(val) | |
{ | |
///@func divide(value_or_vec) | |
var v = new vec2(val); | |
var v2=new vec2(x/v.x,y/v.y) | |
return v2; | |
} | |
static modulo=function(val) | |
{ | |
///@func modulo(value_or_vec) | |
var v = new vec2(val); | |
var v2=new vec2(x % v.x,y % v.y) | |
return v2; | |
} | |
#endregion | |
#region Advanced functions | |
static length_squared = function() | |
{ | |
///@func length_squared() | |
return x*x+y*y; | |
} | |
static length = function() | |
{ | |
///@func length() | |
return sqrt(length_squared()); | |
} | |
static normalize = function() | |
{ | |
///@func normalize() | |
var mag = length_squared(); | |
if (mag == 0 ) { | |
return new vec2(0,0) | |
} | |
else | |
{ | |
return divide(mag); | |
} | |
} | |
static is_normalized = function() | |
{ | |
///@func is_normalized() | |
return ( length_squared() == 1 ); | |
} | |
static inverse = function() | |
{ | |
///@func inverse() | |
return new vec2(1/x, 1/y); | |
} | |
static dot = function(val) | |
{ | |
///@func dot(value_or_vec) | |
var v = new vec2(val); | |
return dot_product(x,y,v.x,v.y); | |
} | |
static dot_normalized = function(val) | |
{ | |
///@func dot_normalized(value_or_vec) | |
var v = new vec2(val); | |
return dot_product_normalized(x,y,v.x,v.y); | |
} | |
static distance_to = function(val) | |
{ | |
///@func distance_to(value_or_vec) | |
var v = new vec2(val); | |
var v2 = new vec2(x,y); | |
v.subtract(v2); | |
return v.length(); | |
} | |
static angle_to = function(val) | |
{ | |
///@func angle_to(value_or_vec) | |
var v = new vec2(val); | |
return point_direction(x,y,v.x,v.y); | |
} | |
static lerp_to = function(val,amt) | |
{ | |
///@func lerp_to(value_or_vec, amount) | |
var v = new vec2(val); | |
var v2=new vec2(lerp(x,v.x,amt),lerp(y,v.y,amt)) | |
return v2; | |
} | |
static reflect = function(val) | |
{ | |
///@func reflect(normal); | |
var v = new vec2(val); | |
v=v.normalize(); | |
var d = dot(v); | |
v=v.multiply(2*d); | |
v=v.subtract(new vec2(x,y)); | |
return v; | |
} | |
static clamp_to = function(v1, v2) | |
{ | |
///@func clamp_to(min, max) | |
return new vec2(clamp(x,v1,v2),clamp(y,v1,v2)); | |
} | |
static vec_abs = function() | |
{ | |
return new vec2(abs(x),abs(y)); | |
} | |
static vec_floor = function() | |
{ | |
return new vec2(floor(x),floor(y)); | |
} | |
static vec_ceil = function() | |
{ | |
return new vec2(ceil(x),ceil(y)); | |
} | |
static vec_sign = function() | |
{ | |
return new vec2(sign(x),sign(y)); | |
} | |
static vec_round = function() | |
{ | |
return new vec2(round(x),round(y)); | |
} | |
#endregion | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment