ACSUtils Wiki

An ACS library for ZDoom-based ports

User Tools

Site Tools


new:math

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
new:math [2018/02/18 18:44] korshunnew:math [2018/02/21 13:11] korshun
Line 23: Line 23:
   * ''num cmp(num a, num b)'' -- returns 1 if A is greater than B, -1 if lesser, 0 if they are equal.   * ''num cmp(num a, num b)'' -- returns 1 if A is greater than B, -1 if lesser, 0 if they are equal.
  
-==== Other useful functions ====+==== Other functions ====
  
-  * ''num mod(num x, num period)'' -- just like ''% period'', but performs "true" modulo division, without negative results or mirrorings.+  * ''fixed IntDiv(int x, int y)'' -- divides two integers and returns fixed-point result. Supports any pair of integers whose ratio is less than 32767. 
 +  * ''fixed AngleDiff(fixed a, fixed b)'' -- returns the shortest signed rotation angle needed to go from angle A to angle B. Result is always in [-0.5, 0.5]. Both A and B may be outside [0.0, 1.0]. 
 +  * ''num gcf(num a, num b)'' -- returns the greatest common factor of two numbers. 
 +  * ''fixed fract(fixed x)'' -- returns the fractional part of X. For example''fract(12.345)'' returns ''0.345''. ''fract(-1234.56789) == 0.56789''.
  
-Example of practical use of ''mod'' that wouldn't work with ''%'': 
  
-<code> +==== Powers ====
-int NUM_MENU_ITEMS 10; +
-int selected_item 0;+
  
-if (KeyPressed(BT_FORWARD)+  * ''num ipow(num x, int power)'' -- returns x^power. Supports int and fixed X, but only integer powers. Uses a multiplication loop. 
-    selected_item--; +  * ''fixed fpow(fixed xfixed power)'' -- returns x^power. Supports only fixed-point powers and fixed-point X. Uses multiplication loops.
-if (KeyPressed(BT_BACK)) +
-    selected_item++; +
-     +
-// wrap around +
-selected_item = mod(selected_itemNUM_MENU_ITEMS)+
-</code> +
- +
-==== Bit math ====+
  
-  * ''int npo2(int x)'' -- returns the lowest power of 2 greater or equal to X. 
-  * ''int bitlog2(int x)'' -- returns the index of the highest used bit, aka integer binary logarithm. Useful to convert flags like ''1<<8 = 256'' back into ''8''. 
  
 ==== Trigonometry ==== ==== Trigonometry ====
Line 65: Line 55:
  
 Since some functions need to return a vector or a pair of angles, they use the ACSUtils [[multiple return values]] convention. Since some functions need to return a vector or a pair of angles, they use the ACSUtils [[multiple return values]] convention.
 +
 +<note important>ZDoom [[zdoom>GetActorPitch]] returns actor pitch with the wrong sign (multiplied by -1). Make sure to fix the sign before using actor pitch in the below vector functions.</note>
  
 Functions returning one value: Functions returning one value:
  
   * ''fixed VectorLength3D(fixed x, fixed y, fixed z)'' -- a 3D version of [[zdoom>VectorLength]]. Shorthand for ''VectorLength(z, VectorLength(x, y))''.   * ''fixed VectorLength3D(fixed x, fixed y, fixed z)'' -- a 3D version of [[zdoom>VectorLength]]. Shorthand for ''VectorLength(z, VectorLength(x, y))''.
-  * ''fixed SqVectorLength(fixed x, fixed y)'' -- squared length of 2D vector. May be faster for length comparisons.+  * ''fixed SqVectorLength(fixed x, fixed y)'' -- squared length of 2D vector.
   * ''fixed SqVectorLength3D(fixed x, fixed y, fixed z)'' -- squared length of 3D vector.   * ''fixed SqVectorLength3D(fixed x, fixed y, fixed z)'' -- squared length of 3D vector.
   * ''fixed dot2(fixed x1, fixed y1, fixed x2, fixed y2)'' -- dot product of two 2D vectors.   * ''fixed dot2(fixed x1, fixed y1, fixed x2, fixed y2)'' -- dot product of two 2D vectors.
Line 79: Line 71:
   * ''fixed, fixed VectorToAngles(fixed x, fixed y, fixed z)'' -- converts vector to a pair of angles (angle and pitch)   * ''fixed, fixed VectorToAngles(fixed x, fixed y, fixed z)'' -- converts vector to a pair of angles (angle and pitch)
   * ''fixed, fixed RotateVector(fixed x, fixed y, fixed angle)'' -- returns vector rotated by angle   * ''fixed, fixed RotateVector(fixed x, fixed y, fixed angle)'' -- returns vector rotated by angle
-  * ''fixed, fixed RotateVectorSC(fixed x, fixed y, fixed sin, fixed cos)'' -- returns vector rotated by sin and cos of some angle (aka rotation vector)This is useful if you already have the sine and cosine of the rotation angle.+  * ''fixed, fixed RotateVectorCS(fixed x, fixed y, fixed rx, fixed ry)'' -- returns vector rotated by another vector's angleResult is multipled by the second vector's length. If the second vector is normalized, it's a (cos(angle), sin(angle)) vector of rotation angle.
   * ''fixed, fixed RotatePoint(fixed x, fixed y, fixed originX, fixed originY, fixed angle)'' -- returns 2D point rotated by angle around the origin.   * ''fixed, fixed RotatePoint(fixed x, fixed y, fixed originX, fixed originY, fixed angle)'' -- returns 2D point rotated by angle around the origin.
  
Line 87: Line 79:
   * ''fixed, fixed, fixed AnglesToVector(fixed angle, fixed pitch)'' -- converts a pair of angles to a vector of length 1.   * ''fixed, fixed, fixed AnglesToVector(fixed angle, fixed pitch)'' -- converts a pair of angles to a vector of length 1.
  
-<note important>ZDoom [[zdoom>GetActorPitch]] returns actor pitch with the wrong sign (multiplied by -1)Make sure to fix the sign before using actor pitch in the above vector functions.</note>+==== Other useful functions ==== 
 + 
 +=== mod === 
 + 
 +''num mod(num x, num period)'' -- just like ''x % period'', but performs "true" modulo division, without negative results or mirrorings. It's as if you began at 0 in space [0, period) with wraparound, and moved X steps to the right. 
 + 
 +Example of practical use of ''mod'' that wouldn't work with ''%'': 
 + 
 +<code> 
 +int NUM_MENU_ITEMS = 10; 
 +int selected_item = 0; 
 + 
 +if (KeyPressed(BT_FORWARD)) 
 +    selected_item--; 
 +if (KeyPressed(BT_BACK)) 
 +    selected_item++; 
 +     
 +// wrap around 
 +selected_item = mod(selected_item, NUM_MENU_ITEMS); 
 +</code> 
 + 
 +=== lerp === 
 + 
 +''fixed lerp(fixed a, fixed b, fixed alpha)'' 
 + 
 +Performs [[wp>Linear interpolation|linear interpolation]] (or extrapolation) between two numbers and returns the result. 
 + 
 +Simple animation of movement between two points: 
 +<code> 
 +for (int time = 0; time < 1.0; time += 0.05) 
 +
 +    int x = lerp(x1, x2, time);  
 +    int y = lerp(y1, y2, time); 
 +    DrawSomething(x, y); 
 +    Delay(1); 
 +
 +</code> 
  
  
new/math.txt · Last modified: 2019/04/21 01:25 by korshun