Math extensions and operator overloads for LibGDX math API.
Java does not feature operator overloading, which leads to weird constructs like vector.add(a).sub(b)
. Kotlin brings
the possibility to use a much more readable and natural syntax with its operators overloading: vector + a - b
. However,
LibGDX API does not match Kotlin naming conventions (necessary for operators to work), which means extension functions
are necessary to make it work like that.
vec2
is a global factory function that can createVector2
instances with named parameters for extra readability.+
,-
,*
and/
can be used to add, subtract, multiply or divide current values according to the second vector.- Unary
-
operator (a single minus before the vector) allows to negate both vector values. *
and/
can be used with floats and ints to multiply or divide both vector values.++
and--
operators can be used to increment and decrement both x and y values of the vector. Note that sinceVector2
class is mutable, both these operations modify the internal state of the vector. This means that both++vector
andvector++
are effectively the same, as previous state of the vector is not kept (to limit the amount of constructed objects).Vector2
instances can be destructed to two float variables in one step withval (x, y) = vector2
syntax thanks tocomponent1()
andcomponent2()
operator methods.Vector2
instances are now comparable -<
,>
,<=
,>=
operators can be used to determine which vector has greater (or equal) overall length. While this certainly does not fit all use cases, we consider it the most commonly compared value. It can be used to determine which vector is further from a certain common point (for example, which Box2DBody
is further from the center of theWorld
or which touch event is further from theViewport
center). It can be also used to quickly determine which velocity or force is greater. Note that length squared is actually compared, as it is much faster to calculate and yields the same results in most cases.
Note that since Shape2D
has contains(Vector2)
method, in
operator can be used for any Shape2D
implementation
(like Rectangle
, Ellipse
or Circle
). For example, given vec: Vector2
and rect: Rectangle
variables, you can
call vec in rect
(or vec !in rect
) to check if the rectangle contains (or doesn't) the point stored by the vector.
vec3
is a global factory function that can createVector3
instances with named parameters for extra readability.+
,-
,*
and/
can be used to add, subtract, multiply or divide current values according to the second vector.- Unary
-
operator (a single minus before the vector) allows to negate all vector values. *
and/
can be used with floats and ints to multiply or divide all vector values.++
and--
operators can be used to increment and decrement x, y and z values of the vector. Note that sinceVector3
class is mutable, both these operations modify the internal state of the vector. This means that both++vector
andvector++
are effectively the same, as previous state of the vector is not kept (to limit the amount of constructed objects).Vector3
instances can be destructed to tree float variables in one step withval (x, y, z) = vector3
syntax thanks tocomponent1()
,component2()
andcomponent3
operator methods.Vector3
instances are now comparable -<
,>
,<=
,>=
operators can be used to determine which vector has greater (or equal) overall length, similarly to howVector2
now works.
mat3
is a human-readable global factory function that allows to easily createMatrix3
instances.- Unary
-
operator (a single minus before the matrix) can be used to negate all matrix values. !
operator before the matrix inverts it, callinginv()
.+
and-
can be used to add and subtract values from other matrices.*
operator can be used to right-multiply the matrix with another matrix usingmul(Matrix3)
method.*
operator can be used to scale the X and Y components of the matrix with a float of a vector usingscl
methods.Vector3
andVector2
instances can be multiplied with aMatrix3
using*
operator.Matrix3
instances can be destructed into nine float variables (each representing one of its cells) thanks to thecomponent1()
-component9()
operator functions.
mat4
is a human-readable global factory function that allows to easily createMatrix4
instances.- Unary
-
operator (a single minus before the matrix) can be used to negate all matrix values. !
operator before the matrix inverts it, callinginv()
.+
and-
can be used to add and subtract values from other matrices.*
operator can be used to right-multiply the matrix with another matrix usingmul(Matrix4)
method.*
operator can be used to scale the X and Y components of the matrix with a float of a vector usingscl
methods.Vector3
instances can be multiplied with aMatrix4
using*
operator.Matrix4
instances can be destructed into sixteen float variables (each representing one of its cells) thanks to thecomponent1()
-component16()
operator functions.
- Kotlin Statistics contains idiomatic Kotlin wrappers over Apache Commons Math. Its extension functions might prove useful during game development.
Sadly, it does not seem that there are any public math-related Kotlin libraries that would make LibGDX math API usage more natural. You can try browsing through existing general purpose LibGDX extensions - some of them contain static methods for various math operations missing in the official API.