Advertisement
Kendy999

Vector Functions for GMS2

Jun 13th, 2022
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* -----------------------------------
  2.   Macros for referencing vector axes
  3.  ---------------------------------- */
  4.  
  5. #macro vec2_x 0
  6. #macro vec2_y 1
  7.  
  8. /// @description vec2 [x [, y]] - Creates a 2d vector.
  9. /// @param [x]
  10. /// @param [y]
  11. /* ------------------------------------------------
  12.     Usage Example
  13.     vec2()     - returns [0, 0]
  14.     vec2(1)    - returns [1, 1]
  15.     vec2(2, 5) - returns [2, 5]
  16.  ------------------------------------------------ */
  17. function vec2()
  18. {
  19.     switch (argument_count)
  20.     {
  21.         case 0:
  22.             return [0, 0];
  23.         case 1:
  24.             return [argument0, argument0];
  25.         case 2:
  26.             return [argument0, argument1];
  27.         default:
  28.             show_error("Error: vec2 can only hold 2 values but " + string(argument_count) + "were given", true);
  29.     }
  30. }
  31.  
  32. /// @description vec2_addf vector, value - Adds numeric value to vector.
  33. /// @param vec2
  34. /// @param value
  35. /* --------------------------------------------------------------
  36.     Usage Example
  37.     var vector = vec2(5, 4)             - creates vector [5, 4]
  38.     vec2_addf(vector, 9) => [5+9, 4+9]  - returns [14, 13]
  39.  -------------------------------------------------------------- */
  40. function vec2_addf(vector, value)
  41. {
  42.     if not is_array(vector) show_error("vec2_addf expected vec2 as the first argument.", true);
  43.     if not is_numeric(value) show_error("vec2_addf expected a numeric value as the second argument.", true);
  44.    
  45.     return vec2(vector[vec2_x] + value,
  46.                 vector[vec2_y] + value);
  47. }
  48.  
  49. /// @description vec2_add vector_1, vector_2 - Adds two vectors together.
  50. /// @param vector_1
  51. /// @param vector_2
  52. /* ----------------------------------------------------------------------
  53.     Usage Example
  54.     var vector_1 = vec2(5, 4)                   - creates vector [5, 4]
  55.     var vector_2 = vec2(4, 5)                   - creates vector [4, 5]
  56.     vec2_add(vector_1, vector_2) => [5+4, 4+5]  - returns [9, 9]
  57.  ---------------------------------------------------------------------- */
  58. function vec2_add(vector_1, vector_2)
  59. {
  60.     if not is_array(vector_1) show_error("vec2_add expected vec2 as the first argument.", true);
  61.     if not is_array(vector_2) show_error("vec2_add expected vec2 as the second argument.", true);
  62.    
  63.     return vec2(vector_1[vec2_x] + vector_2[vec2_x],
  64.                 vector_1[vec2_y] + vector_2[vec2_y]);
  65. }
  66.  
  67. /// @description vec2_subf vector, value - Subtracts numeric value from a vector.
  68. /// @param vec2
  69. /// @param value
  70. /* --------------------------------------------------------------
  71.     Usage Example
  72.     var vector = vec2(5, 4)             - creates vector [5, 4]
  73.     vec2_subf(vector, 3) => [5-3, 4-3]  - returns [2, 1]
  74.  -------------------------------------------------------------- */
  75. function vec2_subf(vector, value)
  76. {
  77.     if not is_array(vector) show_error("vec2_subf expected vec2 as the first argument.", true);
  78.     if not is_numeric(value) show_error("vec2_subf expected a numeric value as the second argument.", true);
  79.    
  80.     return vec2(vector[vec2_x] - value,
  81.                 vector[vec2_y] - value);
  82. }
  83.  
  84. /// @description vec2_sub vector_1, vector_2 - Subtracts vector_2 from vector_1.
  85. /// @param vector_1
  86. /// @param vector_2
  87. /* ----------------------------------------------------------------------
  88.     Usage Example
  89.     var vector_1 = vec2(5, 4)                   - creates vector [5, 4]
  90.     var vector_2 = vec2(4, 5)                   - creates vector [4, 5]
  91.     vec2_sub(vector_1, vector_2) => [5-4, 4-5]  - returns [1, -1]
  92.  ---------------------------------------------------------------------- */
  93. function vec2_sub(vector_1, vector_2)
  94. {
  95.     if not is_array(vector_1) show_error("vec2_sub expected vec2 as the first argument.", true);
  96.     if not is_array(vector_2) show_error("vec2_sub expected vec2 as the second argument.", true);
  97.    
  98.     return vec2(vector_1[vec2_x] - vector_2[vec2_x],
  99.                 vector_1[vec2_y] - vector_2[vec2_y]);
  100. }
  101.  
  102. /// @description vec2_mulf vector, value - Multiplies vector by a numeric value.
  103. /// @param vec2
  104. /// @param value
  105. /* ---------------------------------------------------------------
  106.     Usage Example
  107.     var vector = vec2(5, 4)             - creates vector [5, 4]
  108.     vec2_mulf(vector, 3) => [5*3, 4*3]  - returns [15, 12]
  109.  --------------------------------------------------------------- */
  110. function vec2_mulf(vector, value)
  111. {
  112.     if not is_array(vector) show_error("vec2_mulf expected vec2 as the first argument.", true);
  113.     if not is_numeric(value) show_error("vec2_mulf expected a numeric value as the second argument.", true);
  114.    
  115.     return vec2(vector[vec2_x] * value,
  116.                 vector[vec2_y] * value);
  117. }
  118.  
  119. /// @description vec2_mul vector_1, vector_2 - Multiplies two vectors.
  120. /// @param vector_1
  121. /// @param vector_2
  122. /* ----------------------------------------------------------------------
  123.     Usage Example
  124.     var vector_1 = vec2(5, 4)                   - creates vector [5, 4]
  125.     var vector_2 = vec2(4, 5)                   - creates vector [4, 5]
  126.     vec2_mul(vector_1, vector_2) => [5*4, 4*5]  - returns [20, 20]
  127.  ---------------------------------------------------------------------- */
  128. function vec2_mul(vector_1, vector_2)
  129. {
  130.     if not is_array(vector_1) show_error("vec2_mul expected vec2 as the first argument.", true);
  131.     if not is_array(vector_2) show_error("vec2_mul expected vec2 as the second argument.", true);
  132.    
  133.     return vec2(vector_1[vec2_x] * vector_2[vec2_x],
  134.                 vector_1[vec2_y] * vector_2[vec2_y]);
  135. }
  136.  
  137. /// @description vec2_divf vector, value - Divides vector by a numeric value.
  138. /// @param vec2
  139. /// @param value
  140. /* --------------------------------------------------------------------
  141.     Usage Example
  142.     var vector = vec2(69, 12)               - creates vector [69, 12]
  143.     vec2_divf(vector, 3) => [69/3, 12/3]    - returns [23, 4]
  144.  -------------------------------------------------------------------- */
  145. function vec2_divf(vector, value)
  146. {
  147.     if not is_array(vector) show_error("vec2_divf expected vec2 as the first argument.", true);
  148.     if not is_numeric(value) show_error("vec2_divf expected a numeric value as the second argument.", true);
  149.    
  150.     return vec2(vector[vec2_x] / value,
  151.                 vector[vec2_y] / value);
  152. }
  153.  
  154. /// @description vec2_div vector_1, vector_2 - Divides two vectors.
  155. /// @param vector_1
  156. /// @param vector_2
  157. /* -------------------------------------------------------------------------
  158.     Usage Example
  159.     var vector_1 = vec2(12)                      - creates vector [12, 12]
  160.     var vector_2 = vec2(3, 4)                    - creates vector [3, 4]
  161.     vec2_div(vector_1, vector_2) => [12/3, 12/4] - returns [4, 3]
  162.  ------------------------------------------------------------------------- */
  163. function vec2_div(vector_1, vector_2)
  164. {
  165.     if not is_array(vector_1) show_error("vec2_div expected vec2 as the first argument.", true);
  166.     if not is_array(vector_2) show_error("vec2_div expected vec2 as the second argument.", true);
  167.    
  168.     return vec2(vector_1[vec2_x] / vector_2[vec2_x],
  169.                 vector_1[vec2_y] / vector_2[vec2_y]);
  170. }
  171.  
  172. /// @description vec2_dot vector_1, vector_2 [, normalized] - Returns dot product of two vectors.
  173. /// @param vector_1
  174. /// @param vector_2
  175. /// @param [normalized]
  176. function vec2_dot(vector_1, vector_2, normalized = false)
  177. {
  178.     if not is_array(vector_1) show_error("vec2_dot expected vec2 as the first argument.", true);
  179.     if not is_array(vector_2) show_error("vec2_dot expected vec2 as the second argument.", true);
  180.    
  181.     if normalized return dot_product_normalized(vector_1[vec2_x], vector_1[vec2_y],
  182.                                                 vector_2[vec2_x], vector_2[vec2_y]);
  183.    
  184.     return dot_product(vector_1[vec2_x], vector_1[vec2_y],
  185.                        vector_2[vec2_x], vector_2[vec2_y]);
  186. }
  187.  
  188. /// @description vec2_mag vector - Returns magnitude of a vector.
  189. /// @param vector
  190. function vec2_mag(vector)
  191. {
  192.     if not is_array(vector) show_error("vec2_mag expected vec2 as the first argument.", true);
  193.    
  194.     return sqrt(sqr(vector[vec2_x])+sqr(vector[vec2_y]));
  195. }
  196.  
  197. /// @description vec2_dir vector - Returns direction of a vector.
  198. /// @param vector
  199. /// @param [result_in_degrees]
  200. function vec2_dir(vector, result_in_degrees = false)
  201. {
  202.     if not is_array(vector) show_error("vec2_dir expected vec2 as the first argument.", true);
  203.    
  204.     if result_in_degrees return darctan(vector[vec2_y] / vector [vec2_x]);
  205.     return arctan(vector[vec2_y] / vector [vec2_x]);
  206. }
  207.  
  208. /// @description vec2_angdiff vector_1, vector_2 - Returns angle difference between two vectors.
  209. /// @param vector_1
  210. /// @param vector_2
  211. function vec2_angdiff(vector_1, vector_2)
  212. {
  213.     if not is_array(vector_1) show_error("vec2_angdiff expected vec2 as the first argument.", true);
  214.     if not is_array(vector_2) show_error("vec2_angdiff expected vec2 as the second argument.", true);
  215.    
  216.     return angle_difference(vec2_dir(vector_1, true),
  217.                             vec2_dir(vector_2, true));
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement