Hi people. These days I was working in a little pack of math functions so you can use them in your mods to save some time. Many of the functions here are from ZDoom wiki
- Constants
[spoiler]- MATH_PI: 3.141592654
- MATH_E: 2.718281828
- Conversions
[spoiler]- int mathFixedRound (fixed v): Converts a fixed point value (v) to an integer (round)
- fixed mathIntToFixed (int v): Converts an integer value (v) to a fixed point (This is just a shorthand for "v << 16;")
- Angles to Degree
- fixed mathByteToDegree(fixed byteAngle): Converts a byte angle into a degree angle
- fixed mathFixToDegree(fixed fixVal): Converts a fixed point angle into a degree angle
- fixed mathRadToDegree(fixed radAngle): Converts a radian angle into a degree angle
- Angles to Radians
- fixed mathDegreeToRad(fixed degAngle): Converts a degree angle into a radian angle
- fixed mathByteToRad(fixed byteAngle): Converts a byte angle into a radian angle
- fixed mathFixToRad(fixed fixVal): Converts a fixed point angle into a radian angle
- Angles to Byte Angle
- fixed mathDegreeToByte(fixed degAngle): Converts a degree angle into a byte angle
- fixed mathRadToByte(fixed radAngle): Converts a radian angle into a byte angle
- fixed mathFixToByte(fixed fixAngle): Converts a fixed point angle into a byte angle
- Angles to Fixed Point Angle
- fixed mathDegreeToFix(fixed degAngle): Converts a degree angle into a fixed point angle
- fixed mathRadToFix(fixed radAngle): Converts a radian angle into a fixed point angle
- fixed mathByteToFix(fixed byteAngle): Converts a byte angle into a fixed point angle
- Basic Operations
[spoiler]- int mathAbs(int val) : Returns the absolute value of a number (Can be integer or fixed)
- fixed mathPow(fixed num, int p) : Returns the value of num raised to p
- fixed mathSqrt(fixed number): Returns the square root of number
- fixed mathLogN (fixed x, fixed base): Returns the logarithm of "x" with base "base"
- Trigonometry
- fixed mathTan (fixed x): Returns the tangent of x (fixed point angle)
- Trigonometric functions using radians
- fixed mathRadCos (fixed radAngle): Returns the cosine of a radian angle
- fixed mathRadSin (fixed radAngle): Returns the sine of a radian angle
- fixed mathRadTan (fixed radAngle): Returns the tangent of a radian angle
- Trigonometric functions using Degrees
- fixed mathDegCos (fixed degreeAngle): Returns the cosine of a degree angle
- fixed mathDegSin (fixed degreeAngle): Returns the sine of a degree angle
- fixed mathDegTan (fixed degreeAngle): Returns the tangent of a degree angle
- Trigonometric functions using Byte Angles
- fixed mathByteCos (fixed byteAngle): Returns the cosine of a byte angle
- fixed mathByteSin (fixed byteAngle): Returns the sine of a byte angle
- fixed mathByteTan (fixed byteAngle): Returns the tangent of a byte angle
- Inverse trigonometric functions:
- fixed mathAcos(fixed x): Returns the arc-cosine (in fixed point angle)
- fixed mathAsin(fixed x): Returns the arc-sine (in fixed point angle)
- fixed mathAtan(fixed x): Returns the arc-tangent (in fixed point angle)
- Other Stuff
[spoiler]- int mathMax (int a, int b): Returns the maximum between two values
- int mathMin (int a, int b): Returns the minimum between two values
- fixed math2DDist(fixed vx1, fixed vy1, fixed vx2, fixed vy2): Returns the distance between two points in 2D
- fixed math3DDist(fixed vx1, fixed vy1, fixed vz1, fixed vx2, fixed vy2, fixed vz2): Returns the distance between two points in 3D
- Vectors
[spoiler]This library implements a system to work with vectors.
Example #1: (The long way)You can use vectors with any number of elements (2, 3, 4, 5, 6, etc..). But since the most common number of elements are 2 and 3, I decided to make some shorthand functions to work with these number of elements more easily.Code: Select all
/* * v1 is an integer who contains the index of the vector we're working on. */ int v1 = mathCreateVector(3, MATH_FLOAT); //This create a vector with 3 dimensions and prepared for fixed values //The following three lines set the values of the vector to [1.0, -5.0 , 3.5] mathVectorSetValue(v1, 0, 1.0); mathVectorSetValue(v1, 1, -5.0); mathVectorSetValue(v1, 2, 3.5); //The following line multiply our vector with a scalar value mathVectorScalarMult(v1, v1, 3.0); //param 1 = output, param 2 = vector to multiply, param 3 = scalar value //Now our vector have the values [3.0, -15.0, 10.5] Log(s: mathVectorInfo(v1)); //This shows the vector information in the console. mathDeleteVector(v1); //THIS LINE IS IMPORTANT!, if you're not working with the vector anymore, DELETE IT!
Example #2: (The shorthand way)Example #3: (Using vectors in a loop: THE BAD WAY)Code: Select all
int v1 = mathCreateVec3(MATH_FLOAT, 1.0, -5.0, 3.5); //This create a vector with 3 elements and with the values [1.0, -5.0, 3.5] int v2 = mathCreateVec3(MATH_FLOAT, -1.0, 2.0, 5.0); //This create a vector with 3 elements and with the values [-1.0, 2.0, 5.0] //The following line performs a cross multiplication between v1 and v2, and save the result in v1 mathVec3CrossMult(v1, v1, v2); //param 1 = output, param 2 = vector a, param 3 = vector b Log(s: mathVectorInfo(v1)); mathDeleteVector(v1); mathDeleteVector(v2);
The code above is catastrophic, it creates a vector in a loop but IT NEVER DELETE IT. So you're going to have an overflow of vectors.Code: Select all
script 12312 OPEN { do { int v = mathCreateVec3(MATH_FLOAT, 0.0, 0.0, 0.0); GetActorVelocityVec3(1, v); //Get the actor with tid 1 and save his velocity int the vector "v" mathVectorScalarMult(v, v, 2.0); //multiply the vector with 2.0 SetActorVelocityVec3(1, v, false, false); //Modify the actor (with tid equal to 1) velocity, putting the values that are in "v" delay(35); }while(1); }
Example #4: (Using vectors in a loop: THE GOOD WAY)
All the functions:Code: Select all
script 12312 OPEN { do { int v = mathCreateVec3(MATH_FLOAT, 0.0, 0.0, 0.0); GetActorVelocityVec3(1, v); //Get the actor with tid 1 and save his velocity int the vector "v" mathVectorScalarMult(v, v, 2.0); //multiply the vector with 2.0 SetActorVelocityVec3(1, v, false, false); //Modify the actor (with tid equal to 1) velocity, putting the values that are in "v" mathDeleteVector(v); //THIS IS IMPORTANT TO AVOID OVERFLOW delay(35); }while(1); }
- int mathCreateVector(int size, int type) : Creates a vector, ["size" is the number of elements in the vector, "type" can be MATH_INTEGER (0) or MATH_FLOAT (1)], the return value is the index of the vector created
- int mathCloneVector(int v): Creates a vector with the values of another one (the one with the index "v"). Like mathCreateVector, it returns an integer with the index of the new vector
- bool mathVectorSetValue(int index, int pos, int val) : Modify a value in the vector. return TRUE if the value has been modified properly
- int mathVectorGetValue(int index, int pos) : Get a value from a vector
- int mathVectorGetIntVal(int index, int pos) : Get a value from a vector and force it to be integer (Even if the vector is MATH_FLOAT)
- fixed mathVectorGetFloatVal(int index, int pos) : Get a value from a vector and force it to be fixed (Even if the vector is MATH_INTEGER)
- void mathDeleteVector(int index) : Destroys a vector (You must destroy the vectors that you're not using anymore)
- Basic operations
- bool mathVectorAdd(int out, int a, int b): Peforms a sum between two vectors. (out = vector where the result is going to be saved, a = first vector, b = second vector
- bool mathVectorSub(int out, int a, int b): Peforms a substraction between two vectors. (out = vector where the result is going to be saved, a = first vector, b = second vector
- bool mathVectorScalarMult(int out, int v, int k): Performs a scalar multiplication. (out = vector where the result is going to be saved, v = vector to multiply, k = scalar value)
- fixed mathVectorDotMult(int v, int u): Performs a dot multiplication between two vectors. (v = vector A, u = vector B). Always return a fixed point value.
- fixed mathVectorLength(int v): Obtain the length of a vector. (v = index of the vector). Always return a fixed point value
- str mathVectorInfo(int v) : Return a string with the information of a vector (v = index of the vector)
- Shorthands for working with Vectors with 2 elements
- int mathCreateVec2(int type, int x, int y): Creates a vector with two elements, [ "type" can be MATH_INTEGER (0) or MATH_FLOAT (1)], the return value is the index of the vector created
- int mathVec2GetX(int v): v is the index of the vector
- int mathVec2GetY(int v): v is the index of the vector
- bool mathVec2SetX(int v, int val): v is the index of the vector
- bool mathVec2SetY(int v, int val): v is the index of the vector
- Shorthands for working with Vectors with 3 elements
- int mathCreateVec3(int type, int x, int y, int z): Creates a vector with three elements, [ "type" can be MATH_INTEGER (0) or MATH_FLOAT (1)], the return value is the index of the vector created
- int mathVec3GetX(int v): v is the index of the vector
- int mathVec3GetY(int v): v is the index of the vector
- int mathVec3GetZ(int v): v is the index of the vector
- bool mathVec3SetX(int v, int val): v is the index of the vector
- bool mathVec3SetY(int v, int val): v is the index of the vector
- bool mathVec3SetZ(int v, int val): v is the index of the vector
- bool mathVec3CrossMult(int out, int v, int u): Perform a cross multiplication between two vectors [out = the vector where the result is going to be saved, v = first vector, u = second vector]
- ZDoom functions shorthands with vectors
- bool SetActorVelocityVec3(int tid, int v, bool add, bool setbob): Like the original SetActorVelocity, but v = index of a vector with 3 elements
- bool GetActorVelocityVec3(int tid, int out): This is like using GetActorVelX, GetActorVelY and GetActorVelZ at the same time and saving the values in the vector "out"
- bool SetActorPositionVec3(int tid, int v, bool fog): Like the original SetActorPosition, but v = index of a vector with 3 elements
- bool GetActorPositionVec3(int tid, int out): This is like using GetActorX, GetActorY and GetActorZ at the same time and saving the values in the vector "out"
- int SpawnVec3(str classname, int v, int tid, int angle): Like the original Spawn, but v = index of a vector with 3 elements. (tid & angle are required)
- int SpawnForcedVec3(str classname, int v, int tid, int angle): Like the original SpawnForced, but v = index of a vector with 3 elements. (tid & angle are required)
- Matrices
[spoiler]If for some reason you need to work with matrices, then you have a system to work with them here. Similar to the system to work with vectors (create/delete indices).
Example #1: (The long way)Like in vectors, there're some shorthand functions, in this case for working with 2x2 and 3x3Code: Select all
int m = mathCreateMatrix(3, 3, MATH_FLOAT); //This creates a 3x3 matrix with all the values set to zero mathMatrixIdentity(m); //Converts the matrix "m" into a identity matrix mathMatrixSetValue(m, 0, 1, 5.0); //set to 5.0 the value of the row 0 & col 1 mathMatrixSetValue(m, 1, 0, -2.0); //set to -2.0 the value of the row 1 & col 0 mathMatrixSetValue(m, 2, 1, 12.0); //set to 12.0 the value of the row 2 & col 1 Log(s: mathMatrixInfo(m)); //Print the information of the matrix in the console mathMatrixTranspose(m); //transpose the matrix "m" int determinant = mathMatrixDet(m); //calculate the determinant of m mathMatrixInverse(m, m); //calculate the inverse of "m" and it save it in "m" mathDeleteMatrix(m); //IMPORTANT LINE. If you're not going to use the matrix anymore, delete it!
Example #2: (Shorthand way)All the functions:Code: Select all
int m = mathCreateMat3(MATH_FLOAT, 5.0, -3.0, 1.0, -4.0. 5.0, 2.0, 0.0, 3.0, 4.5); ... ... ... mathDeleteMatrix(m); //IMPORTANT LINE. If you're not going to use the matrix anymore, delete it!
- int mathCreateMatrix(int rows, int cols, int matrixType) : matrixType can be MATH_INTEGER (0) or MATH_FLOAT (1). The return value is the index of the new matrix
- int mathCloneMatrix(int index) : Create a new matrix from another one. The return value is the index of the new matrix
- void mathDeleteMatrix(int index): Delete a matrix with the given index
- bool mathMatrixIdentity(int index): Converts a matrix into a identity one (only NxN matrices)
- bool mathMatrixTranspose(int index): Transpose the matrix with the given index
- bool mathMatrixSetValue(int index, int row, int col, int value): Set the value of a element in the matrix
- int mathMatrixGetValue(int index, int row, int col): Get the value of a element in the matrix
- int mathMatrixGetIntValue(int index, int row, int col): Get a value forced to be integer
- int mathMatrixGetFloatValue(int index, int row, int col): Get a value forced to be fixed point
- fixed mathMatrixGetFloatValue(int index, int row, int col): Get a value forced to be fixed point
- bool mathMatrixAdd(int out, int a, int b): Performs sum/add between two matrices (out = the index of the matrix where the values are going to be saved, a = first matrix, b = second matrix)
- bool mathMatrixSub(int out, int a, int b): Performs substraction between two matrices
- bool mathMatrixMultiply(int out, int a, int b): Performs matrix multiplication
- int mathMatrixDet(int index): Calculate the determinant from a matrix. Returns a fixed point value
- bool mathMatrixInverse(int out, int index): Calculate the inverse of a matrix (out = the index of the matrix where the values are going to be saved, index = the matrix to calculate his determinant)
- Shorthands
- int mathCreateMat2(int matrixType, int v0, int v1, int v2, int v3) : Creates a 2x2 matrix
- int mathCreateMat3(int matrixType, int v0, int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8): Creates a 3x3 matrix
- To use the library just copy the files from the .pk3 in your mod .pk3 (or folder) and in your main acs file add the following line: #include "acsMath.acs"
GitHub: https://github.com/Stradex/acsMath