Class GameMath
- Namespace
- Vintagestory.API.MathTools
- Assembly
- VintagestoryAPI.dll
A large set of useful game mathematics functions
public static class GameMath
- Inheritance
-
GameMath
- Inherited Members
Fields
DEG2RAD
public const float DEG2RAD = 0.017453292
Field Value
DEG2RAD_DOUBLE
public const double DEG2RAD_DOUBLE = 0.017453292519943295
Field Value
PI
180°
public const float PI = 3.1415927
Field Value
PIHALF
90°
public const float PIHALF = 1.5707964
Field Value
RAD2DEG
public const float RAD2DEG = 57.295776
Field Value
TWOPI
360°
public const float TWOPI = 6.2831855
Field Value
Methods
Acos(double)
public static double Acos(double value)
Parameters
value
double
Returns
Acos(float)
public static float Acos(float value)
Parameters
value
float
Returns
AngleDegDistance(float, float)
Returns the shortest distance between 2 angles See also https://stackoverflow.com/a/14498790/1873041
public static float AngleDegDistance(float start, float end)
Parameters
Returns
AngleRadDistance(float, float)
Returns the shortest distance between 2 angles See also https://stackoverflow.com/a/14498790/1873041
public static float AngleRadDistance(float start, float end)
Parameters
Returns
Asin(double)
public static double Asin(double value)
Parameters
value
double
Returns
Asin(float)
public static float Asin(float value)
Parameters
value
float
Returns
BiLerp(double, double, double, double, double, double)
Basic Bilinear Lerp
public static double BiLerp(double topleft, double topright, double botleft, double botright, double x, double z)
Parameters
Returns
BiLerp(float, float, float, float, float, float)
Basic Bilinear Lerp
public static float BiLerp(float topleft, float topright, float botleft, float botright, float x, float z)
Parameters
Returns
BiLerpByte(float, float, int, int, int, int, int)
Linear Interpolates one selected bytes of the 4 ints
public static byte BiLerpByte(float lx, float ly, int byteIndex, int leftTop, int rightTop, int leftBottom, int rightBottom)
Parameters
lx
floatly
floatbyteIndex
int0, 1, 2 or 3
leftTop
intrightTop
intleftBottom
intrightBottom
int
Returns
BiLerpColorMap(IntDataMap2D, int)
Does linear interpolation on a 2d map for each of the 4 bytes individually (e.g. RGBA color). It's basically a bilinear zoom of an image like you know it from common image editing software. Only intended for square images. The resulting map will be without any paddding (also requires at least 1 padding at bottom and left side)
public static int[] BiLerpColorMap(IntDataMap2D map, int zoom)
Parameters
map
IntDataMap2Dzoom
int
Returns
- int[]
BiLerpRgbColor(float, float, int, int, int, int)
Linear Interpolates the lower 3 bytes of the int individually (i.e. interpolates RGB values individually)
public static int BiLerpRgbColor(float lx, float ly, int leftTop, int rightTop, int leftBottom, int rightBottom)
Parameters
Returns
BiLerpRgbaColor(float, float, int, int, int, int)
Linear Interpolates the bytes of the int individually (i.e. interpolates RGB values individually)
public static int BiLerpRgbaColor(float lx, float ly, int leftTop, int rightTop, int leftBottom, int rightBottom)
Parameters
Returns
BiSerpByte(float, float, int, int, int, int, int)
Linear Interpolates one selected bytes of the 4 ints
public static byte BiSerpByte(float lx, float ly, int byteIndex, int leftTop, int rightTop, int leftBottom, int rightBottom)
Parameters
lx
floatly
floatbyteIndex
int0, 1, 2 or 3
leftTop
intrightTop
intleftBottom
intrightBottom
int
Returns
BiSerpRgbColor(float, float, int, int, int, int)
Smoothstep Interpolates the lower 3 bytes of the int individually (i.e. interpolates RGB values individually)
public static int BiSerpRgbColor(float lx, float ly, int leftTop, int rightTop, int leftBottom, int rightBottom)
Parameters
Returns
BoolsFromInt(bool[], int)
public static void BoolsFromInt(bool[] bools, int v)
Parameters
BresenHamPlotLine2d(int, int, int, int, PlotDelegate2D)
Plot a 2d line, see also http://members.chello.at/~easyfilter/bresenham.html
public static void BresenHamPlotLine2d(int x0, int y0, int x1, int y1, PlotDelegate2D onPlot)
Parameters
x0
inty0
intx1
inty1
intonPlot
PlotDelegate2D
BresenHamPlotLine3d(int, int, int, int, int, int, PlotDelegate3D)
Plot a 3d line, see also http://members.chello.at/~easyfilter/bresenham.html
public static void BresenHamPlotLine3d(int x0, int y0, int z0, int x1, int y1, int z1, PlotDelegate3D onPlot)
Parameters
BresenHamPlotLine3d(int, int, int, int, int, int, PlotDelegate3DBlockPos)
Plot a 3d line, see also http://members.chello.at/~easyfilter/bresenham.html
public static void BresenHamPlotLine3d(int x0, int y0, int z0, int x1, int y1, int z1, PlotDelegate3DBlockPos onPlot)
Parameters
CPCatmullRomSplineLerp(double, double[], double[])
Unlike the other implementation here, which uses the default "uniform" treatment of t, this computation is used to calculate the same values but introduces the ability to "parameterize" the t values used in the calculation. This is based on Figure 3 from http://www.cemyuksel.com/research/catmullrom_param/catmullrom.pdf
public static double CPCatmullRomSplineLerp(double t, double[] p, double[] time)
Parameters
t
doublethe actual interpolation ratio from 0 to 1 representing the position between p1 and p2 to interpolate the value.
p
double[]An array of double values of length 4, where interpolation occurs from p1 to p2.
time
double[]An array of time measures of length 4, corresponding to each p value.
Returns
Clamp(byte, byte, byte)
Force val to be inside a certain range
public static byte Clamp(byte val, byte min, byte max)
Parameters
Returns
Clamp(double, double, double)
Force val to be inside a certain range
public static double Clamp(double val, double min, double max)
Parameters
Returns
Clamp(int, int, int)
Force val to be inside a certain range
public static int Clamp(int val, int min, int max)
Parameters
Returns
Clamp(float, float, float)
Force val to be inside a certain range
public static float Clamp(float val, float min, float max)
Parameters
Returns
Cos(double)
public static double Cos(double value)
Parameters
value
double
Returns
Cos(float)
public static float Cos(float value)
Parameters
value
float
Returns
Crc32(byte[])
public static uint Crc32(byte[] input)
Parameters
input
byte[]
Returns
Crc32(string)
public static uint Crc32(string input)
Parameters
input
string
Returns
CyclicValueDistance(double, double, double)
Returns the shortest distance between 2 values that are cyclical (e.g. angles, daytime hours, etc.) See also https://stackoverflow.com/a/14498790/1873041
public static double CyclicValueDistance(double start, double end, double period)
Parameters
Returns
CyclicValueDistance(float, float, float)
Returns the shortest distance between 2 values that are cyclical (e.g. angles, daytime hours, etc.) See also https://stackoverflow.com/a/14498790/1873041
public static float CyclicValueDistance(float start, float end, float period)
Parameters
Returns
DotNetStringHash(string)
Pretty much taken directly from the string.GetHashCode() implementation, but on these methods the documentation states: "You should never persist or use a hash code outside the application domain in which it was created, [...]." Hence, this is one basic 32bit bit implementation that can be used in a platform independent, persistent way.
public static int DotNetStringHash(string text)
Parameters
text
string
Returns
FastCos(float)
Faster Cos at the cost of lower accuracy
public static float FastCos(float rad)
Parameters
rad
float
Returns
FastCosDeg(float)
Faster Cos at the cost of lower accuracy
public static float FastCosDeg(float deg)
Parameters
deg
float
Returns
FastSin(float)
Faster Sin at the cost of lower accuracy
public static float FastSin(float rad)
Parameters
rad
float
Returns
FastSinDeg(float)
Faster Sin at the cost of lower accuracy
public static float FastSinDeg(float deg)
Parameters
deg
float
Returns
FlipVal(ref double, ref double)
Assigns the value of x1 to x2 and vice versa
public static void FlipVal(ref double x1, ref double x2)
Parameters
FlipVal(ref int, ref int)
Assigns the value of x1 to x2 and vice versa
public static void FlipVal(ref int x1, ref int x2)
Parameters
GenGaussKernel(double, int)
Generates a gaussian blur kernel to be used when blurring something
public static double[,] GenGaussKernel(double sigma = 1, int size = 5)
Parameters
Returns
- double[,]
IntFromBools(bool[])
public static int IntFromBools(bool[] bools)
Parameters
bools
bool[]
Returns
IntFromBools(int[])
public static int IntFromBools(int[] intBools)
Parameters
intBools
int[]
Returns
InverseClamp(int, int, int)
Force val to be outside a certain range
public static int InverseClamp(int val, int atLeastNeg, int atLeastPos)
Parameters
Returns
Largest(double, double)
Returns the smallest number, ignoring the sign of either value
public static double Largest(double a, double b)
Parameters
Returns
Lerp(double, double, double)
Basic Lerp
public static double Lerp(double v0, double v1, double t)
Parameters
Returns
Lerp(float, float, float)
Basic Lerp
public static float Lerp(float v0, float v1, float t)
Parameters
Returns
LerpByte(float, byte, byte)
Linear Interpolates a single byte
public static byte LerpByte(float lx, byte left, byte right)
Parameters
Returns
LerpRgbColor(float, int, int)
Linear Interpolates the lower 3 bytes of the int individually (i.e. interpolates RGB values individually)
public static int LerpRgbColor(float lx, int left, int right)
Parameters
Returns
LerpRgbaColor(float, int, int)
Linear Interpolates the 4 bytes of the int individually (i.e. interpolates RGB values individually)
public static int LerpRgbaColor(float lx, int left, int right)
Parameters
Returns
Map<T>(T, T, T, T, T)
Map a value from one range to another
public static T Map<T>(T value, T fromMin, T fromMax, T toMin, T toMax) where T : INumber<T>
Parameters
value
TfromMin
TfromMax
TtoMin
TtoMax
T
Returns
- T
Type Parameters
T
Max(params double[])
public static double Max(params double[] values)
Parameters
values
double[]
Returns
Max(params int[])
public static int Max(params int[] values)
Parameters
values
int[]
Returns
Max(params float[])
public static float Max(params float[] values)
Parameters
values
float[]
Returns
Md5Hash(string)
See also https://msdn.microsoft.com/en-us/library/system.security.cryptography.md5%28v=vs.110%29.aspx
public static string Md5Hash(string input)
Parameters
input
string
Returns
Min(params int[])
public static int Min(params int[] values)
Parameters
values
int[]
Returns
Min(params float[])
public static float Min(params float[] values)
Parameters
values
float[]
Returns
Mix(int, int, float)
Same as Lerp(float, float, float)
public static int Mix(int v0, int v1, float t)
Parameters
Returns
Mix(float, float, float)
Same as Lerp(float, float, float)
public static float Mix(float v0, float v1, float t)
Parameters
Returns
Mod(double, double)
C#'s %-Operation is actually not modulo but remainder, so this is the actual modulo function that ensures positive numbers as return value
public static double Mod(double k, double n)
Parameters
Returns
Mod(int, int)
C#'s %-Operation is actually not modulo but remainder, so this is the actual modulo function that ensures positive numbers as return value
public static int Mod(int k, int n)
Parameters
Returns
Mod(float, float)
C#'s %-Operation is actually not modulo but remainder, so this is the actual modulo function that ensures positive numbers as return value
public static float Mod(float k, float n)
Parameters
Returns
Mod(uint, uint)
public static uint Mod(uint k, uint n)
Parameters
Returns
MurmurHash3(int, int, int)
A not so fast, but higher quality than oaatHash(). See also https://en.wikipedia.org/wiki/MurmurHash
public static int MurmurHash3(int x, int y, int z)
Parameters
Returns
MurmurHash3Mod(int, int, int, int)
A not so fast, but higher quality than oaatHash(). See also https://en.wikipedia.org/wiki/MurmurHash. Includes a modulo operation.
public static int MurmurHash3Mod(int x, int y, int z, int mod)
Parameters
Returns
NormaliseAngleRad(float)
For angles in radians, normalise to the range 0 to 2 * PI and also, if barely close to a right angle, set it to a right angle (fixes loss of precision after multiple rotation operations etc.)
public static float NormaliseAngleRad(float angleRad)
Parameters
angleRad
float
Returns
PrettyBadHash(int, int)
A really bad, but very fast hashing method.
public static float PrettyBadHash(int x, int y)
Parameters
Returns
R2Sequence1D(int)
Quasirandom sequence by Martin Roberts (http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/)
public static double R2Sequence1D(int n)
Parameters
n
int
Returns
R2Sequence2D(int)
Quasirandom sequence by Martin Roberts (http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/)
public static Vec2d R2Sequence2D(int n)
Parameters
n
int
Returns
R2Sequence3D(int)
Quasirandom sequence by Martin Roberts (http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/)
public static Vec3d R2Sequence3D(int n)
Parameters
n
int
Returns
RootSumOfSquares(float, float, float)
public static float RootSumOfSquares(float a, float b, float c)
Parameters
Returns
RoundRandom(Random, float)
Treats given value as a statistical average. Example: 2.1 will turn into 2 90% of the times and into 3 10% of times.
public static int RoundRandom(Random rand, float value)
Parameters
Returns
RoundRandom(IRandom, float)
Treats given value as a statistical average. Example: 2.1 will turn into 2 90% of the times and into 3 10% of times.
public static int RoundRandom(IRandom rand, float value)
Parameters
Returns
Serp(float, float, float)
Smooth Interpolation using inlined Smoothstep
public static float Serp(float v0, float v1, float t)
Parameters
Returns
Shuffle<T>(Random, List<T>)
Performs a Fisher-Yates shuffle in linear time or O(n)
public static void Shuffle<T>(Random rand, List<T> array)
Parameters
Type Parameters
T
Shuffle<T>(Random, T[])
Performs a Fisher-Yates shuffle in linear time or O(n)
public static void Shuffle<T>(Random rand, T[] array)
Parameters
rand
Randomarray
T[]
Type Parameters
T
Shuffle<T>(LCGRandom, List<T>)
Performs a Fisher-Yates shuffle in linear time or O(n)
public static void Shuffle<T>(LCGRandom rand, List<T> array)
Parameters
Type Parameters
T
Sin(double)
public static double Sin(double value)
Parameters
value
double
Returns
Sin(float)
public static float Sin(float value)
Parameters
value
float
Returns
Smallest(double, double)
Returns the smallest number, ignoring the sign of either value. Examples:
Smallest(1, 3) returns 1
Smallest(-20, 3) returns 3
public static double Smallest(double a, double b)
Parameters
Returns
SmoothMax(double, double, double)
public static double SmoothMax(double a, double b, double smoothingFactor)
Parameters
Returns
SmoothMax(float, float, float)
public static float SmoothMax(float a, float b, float smoothingFactor)
Parameters
Returns
SmoothMin(double, double, double)
public static double SmoothMin(double a, double b, double smoothingFactor)
Parameters
Returns
SmoothMin(float, float, float)
public static float SmoothMin(float a, float b, float smoothingFactor)
Parameters
Returns
SmoothStep(double)
Better Lerp but more CPU intensive, see also https://en.wikipedia.org/wiki/Smoothstep
public static double SmoothStep(double x)
Parameters
x
double
Returns
SmoothStep(float)
Better Lerp but more CPU intensive, see also https://en.wikipedia.org/wiki/Smoothstep
public static float SmoothStep(float x)
Parameters
x
float
Returns
Smootherstep(double)
Better Lerp but more CPU intensive, see also https://en.wikipedia.org/wiki/Smoothstep. x must be in range of 0..1
public static double Smootherstep(double x)
Parameters
x
double
Returns
Smootherstep(double, double, double)
Better Lerp but more CPU intensive, see also https://en.wikipedia.org/wiki/Smoothstep
public static double Smootherstep(double edge0, double edge1, double x)
Parameters
Returns
Smootherstep(float, float, float)
Better Lerp but more CPU intensive, see also https://en.wikipedia.org/wiki/Smoothstep
public static float Smootherstep(float edge0, float edge1, float x)
Parameters
Returns
Sqrt(double)
public static float Sqrt(double value)
Parameters
value
double
Returns
Sqrt(float)
public static float Sqrt(float value)
Parameters
value
float
Returns
Square(double)
public static double Square(double a)
Parameters
a
double
Returns
SumOfSquares(double, double, double)
public static double SumOfSquares(double a, double b, double c)
Parameters
Returns
Tan(double)
public static double Tan(double value)
Parameters
value
double
Returns
Tan(float)
public static float Tan(float value)
Parameters
value
float
Returns
ToEulerAngles(Vec4d)
public static Vec3d ToEulerAngles(Vec4d q)
Parameters
q
Vec4d
Returns
ToEulerAngles(Vec4f)
public static Vec3f ToEulerAngles(Vec4f q)
Parameters
q
Vec4f
Returns
TriangleStep(int, int, int)
Returns a value between 0..1. Returns 0 if val is smaller than left or greater than right. For val == (left+right)/2 the return value is 1. Every other value is a linear interpolation based on the distance to the middle value. Ascii art representation:
1 | /
| /
0.5| /
| /
| /
0 /______________
left right
public static float TriangleStep(int val, int left, int right)
Parameters
Returns
TriangleStep(float, float, float)
Returns a value between 0..1. Returns 0 if val is smaller than left or greater than right. For val == (left+right)/2 the return value is 1. Every other value is a linear interpolation based on the distance to the middle value. Ascii art representation:
1 | /
| /
0.5| /
| /
| /
0 /______________
left right
public static float TriangleStep(float val, float left, float right)
Parameters
Returns
TriangleStepFast(int, int, int)
Same as TriangleStep but skipping the step to calc mid and range.
public static float TriangleStepFast(int val, int mid, int range)
Parameters
Returns
oaatHash(int, int)
Bob Jenkins' One-At-A-Time hashing algorithm. Fast, but not very random.
public static int oaatHash(int x, int y)
Parameters
Returns
oaatHash(int, int, int)
Bob Jenkins' One-At-A-Time hashing algorithm. Fast, but not very random.
public static int oaatHash(int x, int y, int z)
Parameters
Returns
oaatHash(Vec2i)
Bob Jenkins' One-At-A-Time hashing algorithm. Fast, but not very random.
public static int oaatHash(Vec2i v)
Parameters
v
Vec2i
Returns
oaatHash(Vec3i)
Bob Jenkins' One-At-A-Time hashing algorithm. Fast, but not very random.
public static int oaatHash(Vec3i v)
Parameters
v
Vec3i
Returns
oaatHash(Vec4i)
Bob Jenkins' One-At-A-Time hashing algorithm. Fast, but not very random.
public static int oaatHash(Vec4i v)
Parameters
v
Vec4i
Returns
oaatHashMany(int, int)
A single iteration of Bob Jenkins' One-At-A-Time hashing algorithm.
public static int oaatHashMany(int x, int count)
Parameters
Returns
oaatHashU(int, int, int)
Bob Jenkins' One-At-A-Time hashing algorithm. Fast, but not very random.
public static uint oaatHashU(int x, int y, int z)
Parameters
Returns
oaatHashUMany(uint)
Bob Jenkins' One-At-A-Time hashing algorithm
public static uint oaatHashUMany(uint x)
Parameters
x
uint