Math extensions and operator overloads for LibGDX math API and Kotlin ranges.
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.
Kotlin also provides convenient syntax for ranges, which can be used for clearly describing criteria for selecting random numbers.
vec2is a global factory function that can createVector2instances with named parameters for extra readability.+=,-=,*=and/=can be used to add, subtract, multiply or divide current values according to the second vector or number. Use these operators to mutate existing vectors.+,-,*and/can be used to add, subtract, multiply or divide vectors according to the second vector or number, resulting in a new vector. Use these operators to create new instances of vectors.- Unary
-operator (a single minus before the vector) allows to negate both vector values, creating a new vector. ++and--operators can be used to increment and decrement both x and y values of the vector, resulting in a new vector. To avoid creating new vectors, prefer+= 1and-= 1instead.Vector2instances can be destructed to two float variables in one step withval (x, y) = vector2syntax thanks tocomponent1()andcomponent2()operator methods.Vector2instances 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 Box2DBodyis further from the center of theWorldor which touch event is further from theViewportcenter). 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.dotinfix function allows to calculate the dot product of 2 vectors.xinfix function allows to calculate the cross product of 2 vectors.
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.
ImmutableVector2is an immutable equivalent toVector2. It provides most of the functionality ofVector2, but mutation methods return new vectors instead of mutate the reference.- Note that one may want to create type aliases to makes the usage more concise:
typealias Vect2 = ImmutableVector2 ImmutableVectoris comparable (>,>=,<,<=are available). Comparison is evaluated by length.- Instances can be destructed:
val (x, y) = vector2. Vector2.toImmutable()Returns an immutable vector with samexandyattributes than thisVector2ImmutableVector2.toVector2()Returns an mutable vector with samexandyattributes than thisImmutableVector2- Most of the functions of
Vector2which mutate the vector are provided but deprecated. This allow smooth migration fromVector2. - Notable differences with
Vector2:+,-,*,/are available and replaceadd,subandscl.withLength()andwithLength2()replacesetLength()andsetLength2()and return a new vector of same direction with the specified length.withRandomRotationreplacesetToRandomRotationand return a new vector of same length and a random rotation.withAngleDeg()andwithAngleRadreplacesetAngleandsetAngleRadand return a new vector of same length and the given angle to x-axis.cpyis deprecated and is not necessary. Immutable vectors can be safely shared. However sinceImmutableVectoris adata class, there is acopy(x, y)method available allowing to easily create new vectors based on existing ones.set(x, y)andsetZero()are not provided.- Functions dealing with angles in degree are suffixed with
Degand all returns values between-180and+180. - All angle functions return the angle toward positive y-axis.
dotis an infix function.xandcrsinfix functions replacecrs(cross product).
Obtaining ImmutableVector2 instances:
import ktx.math.*
val v0 = ImmutableVector2.ZERO // pre-defined vector
val v1 = ImmutableVector2(1f, 2f) // arbitrary vector
val v2 = ImmutableVector2.X.withRotationDeg(30f) // unit vector of given angle
val v3 = -ImmutableVector2.X // inverse of a vectorConverting from LibGDX Vector2 to ImmutableVector2 (and vice versa):
import ktx.math.*
import com.badlogic.gdx.math.Vector2
val mutable1: Vector2 = Vector2()
val immutable: ImmutableVector2 = mutable1.toImmutable()
val mutable2: Vector2 = immutable.toMutable()Working with immutable vectors:
import ktx.math.*
var vector1 = ImmutableVector2.X
// Reassignment of variables is only possible with `var`.
// Note that the original vector instance is not modified.
vector1 += ImmutableVector2.Y
vector1 *= 3f
val vector2 = vector1.withClamp(0f, 1f) * 5f // `vector1` is not modified.Creating convenience type alias to ease the use of immutable vectors:
import ktx.math.*
// If you don't want to use the rather verbose ImmutableVector2,
// you can declare a more convenient typealias.
typealias Vec2 = ImmutableVector2
var v1 = (Vec2.X + Vec2.Y).nor
var v2 = Vec2(1f, 2f).withLength(3f)vec3is a global factory function that can createVector3instances with named parameters for extra readability. It is also overloaded with a second variant that allows to convertVector2instances toVector3.+=,-=,*=and/=can be used to add, subtract, multiply or divide current values according to the second vector or number. Use these operators to mutate existing vectors.+,-,*and/can be used to add, subtract, multiply or divide vectors according to the second vector or number, resulting in a new vector. Use these operators to create new instances of vectors.- Unary
-operator (a single minus before the vector) allows to negate both vector values, creating a new vector. ++and--operators can be used to increment and decrement x, y and z values of the vector, resulting in a new vector. To avoid creating new vectors, prefer+= 1and-= 1instead.Vector3instances can be destructed to tree float variables in one step withval (x, y, z) = vector3syntax thanks tocomponent1(),component2()andcomponent3operator methods.Vector3instances are now comparable -<,>,<=,>=operators can be used to determine which vector has greater (or equal) overall length, similarly to howVector2now works.dotinfix function allows to calculate the dot product of 2 vectors.xinfix function allows to calculate the cross product of 2 vectors.
mat3is a human-readable global factory function that allows to easily createMatrix3instances.- 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 second matrix. Use these operators to mutate existing matrix instances.+and-can be used to add and subtract values from other matrices. Use these operators to create new instances of 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 usingsclmethods.Matrix3instances can be multiplied with aVector2using*operator.Matrix3instances can be destructed into nine float variables (each representing one of its cells) thanks to thecomponent1()-component9()operator functions.
mat4is a human-readable global factory function that allows to easily createMatrix4instances.- 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 second matrix. Use these operators to mutate existing matrix instances.+and-can be used to add and subtract values from other matrices. Use these operators to create new instances of 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 usingsclmethods.Matrix4instances can be multiplied with aVector3using*operator.Matrix4instances can be destructed into sixteen float variables (each representing one of its cells) thanks to thecomponent1()-component16()operator functions.
- The
amidinfix function for Int and Float allows easy creation of a range by using a center and a tolerance. Such a definition is a convenient way to think about a range from which random values will be selected. - The four arithmetic operators are available for easily shifting or scaling ranges. This allows intuitive modification of ranges in code, which can be useful for code clarity when defining a range for random number selection, or for rapidly iterating a design.
IntRange.random(random: java.util.Random)allows using a Java Random to select a number from the range, and is provided in case there is a need to use theMathUtils.randominstance or an instance of LibGDX's fast RandomXS128.ClosedRange<Float>.random()allows a evenly distributed random number to be selected from a range (but treating theendInclusiveas exclusive for simplicity).ClosedRange<Float>.randomGaussian()selects a normally distributed value to be selected from the range, scaled so the range is six standard deviations wide.ClosedRange<Float>.randomTriangular()allow easy selection of a triangularly distributed number from the range. A anormalizedModecan be passed for asymmetrical distributions.ClosedRange<Float>.lerp(Float)linearly interpolates between the ends of a range.ClosedRange<Float>.interpolate(Float, Interpolation)interpolates between the ends of a range using anInterpolationinstance.
Suppose there is a class that has a random behavior. Its can be constructed by passing several ranges to its constructor.
class CreatureSpawner(val spawnIntervalRange: ClosedRange<Float>) {
//...
fun update(dt: Float){
untilNext -= dt
while (untilNext <= 0){
untilNext += spawnIntervalRange.random()
spawnSomething()
}
}
}In a parent class, there are many of these instances set up. The ranges can be described intuitively:
val spawners = listOf(
//...
CreatureSpawner(0.5f amid 0.2f),
//...
)And as the design is iterated, the range can be adjusted quickly and intuitively by applying arithmetic operations:
val spawners = listOf(
//...
CreatureSpawner((0.5f amid 0.2f) * 1.2f + 0.1f),
//...
)You can use LibGDX APIs directly or rely on third-party math libraries:
- Kotlin Statistics contains idiomatic Kotlin wrappers over Apache Commons Math. Its extension functions might prove useful during game development.
- Jvm Glm is the kotlin port of the famous glm lib by g-truc.