Table of Contents

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

float

DEG2RAD_DOUBLE

public const double DEG2RAD_DOUBLE = 0.017453292519943295

Field Value

double

PI

180°

public const float PI = 3.1415927

Field Value

float

PIHALF

90°

public const float PIHALF = 1.5707964

Field Value

float

RAD2DEG

public const float RAD2DEG = 57.295776

Field Value

float

TWOPI

360°

public const float TWOPI = 6.2831855

Field Value

float

Methods

Acos(double)

public static double Acos(double value)

Parameters

value double

Returns

double

Acos(float)

public static float Acos(float value)

Parameters

value float

Returns

float

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

start float
end float

Returns

float

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

start float
end float

Returns

float

Asin(double)

public static double Asin(double value)

Parameters

value double

Returns

double

Asin(float)

public static float Asin(float value)

Parameters

value float

Returns

float

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

topleft double
topright double
botleft double
botright double
x double
z double

Returns

double

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

topleft float
topright float
botleft float
botright float
x float
z float

Returns

float

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 float
ly float
byteIndex int

0, 1, 2 or 3

leftTop int
rightTop int
leftBottom int
rightBottom int

Returns

byte

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 IntDataMap2D
zoom 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

lx float
ly float
leftTop int
rightTop int
leftBottom int
rightBottom int

Returns

int

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

lx float
ly float
leftTop int
rightTop int
leftBottom int
rightBottom int

Returns

int

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 float
ly float
byteIndex int

0, 1, 2 or 3

leftTop int
rightTop int
leftBottom int
rightBottom int

Returns

byte

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

lx float
ly float
leftTop int
rightTop int
leftBottom int
rightBottom int

Returns

int

BoolsFromInt(bool[], int)

public static void BoolsFromInt(bool[] bools, int v)

Parameters

bools bool[]
v int

BresenHamPlotLine2d(int, int, int, int, PlotDelegate2D)

public static void BresenHamPlotLine2d(int x0, int y0, int x1, int y1, PlotDelegate2D onPlot)

Parameters

x0 int
y0 int
x1 int
y1 int
onPlot PlotDelegate2D

BresenHamPlotLine3d(int, int, int, int, int, int, PlotDelegate3D)

public static void BresenHamPlotLine3d(int x0, int y0, int z0, int x1, int y1, int z1, PlotDelegate3D onPlot)

Parameters

x0 int
y0 int
z0 int
x1 int
y1 int
z1 int
onPlot PlotDelegate3D

BresenHamPlotLine3d(int, int, int, int, int, int, PlotDelegate3DBlockPos)

public static void BresenHamPlotLine3d(int x0, int y0, int z0, int x1, int y1, int z1, PlotDelegate3DBlockPos onPlot)

Parameters

x0 int
y0 int
z0 int
x1 int
y1 int
z1 int
onPlot PlotDelegate3DBlockPos

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 double

the 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

double

Clamp(byte, byte, byte)

Force val to be inside a certain range

public static byte Clamp(byte val, byte min, byte max)

Parameters

val byte
min byte
max byte

Returns

byte

Clamp(double, double, double)

Force val to be inside a certain range

public static double Clamp(double val, double min, double max)

Parameters

val double
min double
max double

Returns

double

Clamp(int, int, int)

Force val to be inside a certain range

public static int Clamp(int val, int min, int max)

Parameters

val int
min int
max int

Returns

int

Clamp(float, float, float)

Force val to be inside a certain range

public static float Clamp(float val, float min, float max)

Parameters

val float
min float
max float

Returns

float

Cos(double)

public static double Cos(double value)

Parameters

value double

Returns

double

Cos(float)

public static float Cos(float value)

Parameters

value float

Returns

float

Crc32(byte[])

public static uint Crc32(byte[] input)

Parameters

input byte[]

Returns

uint

Crc32(string)

public static uint Crc32(string input)

Parameters

input string

Returns

uint

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

start double
end double
period double

Returns

double

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

start float
end float
period float

Returns

float

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

int

FastCos(float)

Faster Cos at the cost of lower accuracy

public static float FastCos(float rad)

Parameters

rad float

Returns

float

FastCosDeg(float)

Faster Cos at the cost of lower accuracy

public static float FastCosDeg(float deg)

Parameters

deg float

Returns

float

FastSin(float)

Faster Sin at the cost of lower accuracy

public static float FastSin(float rad)

Parameters

rad float

Returns

float

FastSinDeg(float)

Faster Sin at the cost of lower accuracy

public static float FastSinDeg(float deg)

Parameters

deg float

Returns

float

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

x1 double
x2 double

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

x1 int
x2 int

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

sigma double
size int

Returns

double[,]

IntFromBools(bool[])

public static int IntFromBools(bool[] bools)

Parameters

bools bool[]

Returns

int

IntFromBools(int[])

public static int IntFromBools(int[] intBools)

Parameters

intBools int[]

Returns

int

InverseClamp(int, int, int)

Force val to be outside a certain range

public static int InverseClamp(int val, int atLeastNeg, int atLeastPos)

Parameters

val int
atLeastNeg int
atLeastPos int

Returns

int

Largest(double, double)

Returns the smallest number, ignoring the sign of either value

public static double Largest(double a, double b)

Parameters

a double
b double

Returns

double

Lerp(double, double, double)

Basic Lerp

public static double Lerp(double v0, double v1, double t)

Parameters

v0 double
v1 double
t double

Returns

double

Lerp(float, float, float)

Basic Lerp

public static float Lerp(float v0, float v1, float t)

Parameters

v0 float
v1 float
t float

Returns

float

LerpByte(float, byte, byte)

Linear Interpolates a single byte

public static byte LerpByte(float lx, byte left, byte right)

Parameters

lx float
left byte
right byte

Returns

byte

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

lx float
left int
right int

Returns

int

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

lx float
left int
right int

Returns

int

Max(params double[])

public static double Max(params double[] values)

Parameters

values double[]

Returns

double

Max(params int[])

public static int Max(params int[] values)

Parameters

values int[]

Returns

int

Max(params float[])

public static float Max(params float[] values)

Parameters

values float[]

Returns

float

Md5Hash(string)

public static string Md5Hash(string input)

Parameters

input string

Returns

string

Min(params int[])

public static int Min(params int[] values)

Parameters

values int[]

Returns

int

Min(params float[])

public static float Min(params float[] values)

Parameters

values float[]

Returns

float

Mix(int, int, float)

public static int Mix(int v0, int v1, float t)

Parameters

v0 int
v1 int
t float

Returns

int

Mix(float, float, float)

public static float Mix(float v0, float v1, float t)

Parameters

v0 float
v1 float
t float

Returns

float

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

k double
n double

Returns

double

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

k int
n int

Returns

int

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

k float
n float

Returns

float

Mod(uint, uint)

public static uint Mod(uint k, uint n)

Parameters

k uint
n uint

Returns

uint

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

x int
y int
z int

Returns

int

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

x int
y int
z int
mod int

Returns

int

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

float

PrettyBadHash(int, int)

A really bad, but very fast hashing method.

public static float PrettyBadHash(int x, int y)

Parameters

x int
y int

Returns

float

R2Sequence1D(int)

public static double R2Sequence1D(int n)

Parameters

n int

Returns

double

R2Sequence2D(int)

public static Vec2d R2Sequence2D(int n)

Parameters

n int

Returns

Vec2d

R2Sequence3D(int)

public static Vec3d R2Sequence3D(int n)

Parameters

n int

Returns

Vec3d

RootSumOfSquares(float, float, float)

public static float RootSumOfSquares(float a, float b, float c)

Parameters

a float
b float
c float

Returns

float

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

rand Random
value float

Returns

int

RoundRandom(LCGRandom, 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(LCGRandom rand, float value)

Parameters

rand LCGRandom
value float

Returns

int

Serp(float, float, float)

Smooth Interpolation using inlined Smoothstep

public static float Serp(float v0, float v1, float t)

Parameters

v0 float
v1 float
t float

Returns

float

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

rand Random
array List<T>

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 Random
array 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

rand LCGRandom
array List<T>

Type Parameters

T

Sin(double)

public static double Sin(double value)

Parameters

value double

Returns

double

Sin(float)

public static float Sin(float value)

Parameters

value float

Returns

float

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

a double
b double

Returns

double

SmoothMax(double, double, double)

public static double SmoothMax(double a, double b, double smoothingFactor)

Parameters

a double
b double
smoothingFactor double

Returns

double

SmoothMax(float, float, float)

public static float SmoothMax(float a, float b, float smoothingFactor)

Parameters

a float
b float
smoothingFactor float

Returns

float

SmoothMin(double, double, double)

public static double SmoothMin(double a, double b, double smoothingFactor)

Parameters

a double
b double
smoothingFactor double

Returns

double

SmoothMin(float, float, float)

public static float SmoothMin(float a, float b, float smoothingFactor)

Parameters

a float
b float
smoothingFactor float

Returns

float

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

double

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

float

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

double

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

edge0 double
edge1 double
x double

Returns

double

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

edge0 float
edge1 float
x float

Returns

float

Sqrt(double)

public static float Sqrt(double value)

Parameters

value double

Returns

float

Sqrt(float)

public static float Sqrt(float value)

Parameters

value float

Returns

float

Square(double)

public static double Square(double a)

Parameters

a double

Returns

double

SumOfSquares(double, double, double)

public static double SumOfSquares(double a, double b, double c)

Parameters

a double
b double
c double

Returns

double

Tan(double)

public static double Tan(double value)

Parameters

value double

Returns

double

Tan(float)

public static float Tan(float value)

Parameters

value float

Returns

float

ToEulerAngles(Vec4d)

public static Vec3d ToEulerAngles(Vec4d q)

Parameters

q Vec4d

Returns

Vec3d

ToEulerAngles(Vec4f)

public static Vec3f ToEulerAngles(Vec4f q)

Parameters

q Vec4f

Returns

Vec3f

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

val int
left int
right int

Returns

float

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

val float
left float
right float

Returns

float

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

val int
mid int
range int

Returns

float

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

x int
y int

Returns

int

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

x int
y int
z int

Returns

int

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

int

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

int

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

int

oaatHashMany(int, int)

A single iteration of Bob Jenkins' One-At-A-Time hashing algorithm.

public static int oaatHashMany(int x, int count)

Parameters

x int
count int

Returns

int

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

x int
y int
z int

Returns

uint

oaatHashUMany(uint)

Bob Jenkins' One-At-A-Time hashing algorithm

public static uint oaatHashUMany(uint x)

Parameters

x uint

Returns

uint