| name | love-math |
| description | Provides system-independent mathematical functions. Use this skill when working with mathematical operations, random number generation, geometric calculations, or any math-related operations in LÖVE games. |
| license | MIT |
| metadata | {"author":"Ron Dekker <rondekker.nl>"} |
When to use this skill
Provides system-independent mathematical functions. Use this skill when working with mathematical operations, random number generation, geometric calculations, or any math-related operations in LÖVE games.
Common use cases
- Performing mathematical calculations and transformations
- Generating random numbers for game mechanics
- Working with vectors and matrices
- Implementing geometric algorithms
- Handling noise generation and procedural content
Functions
love.math.colorFromBytes(rb: number, gb: number, bb: number, ab: number) -> r: number, g: number, b: number, a: number: Converts a color from 0..255 to 0..1 range.
love.math.colorToBytes(r: number, g: number, b: number, a: number) -> rb: number, gb: number, bb: number, ab: number: Converts a color from 0..1 to 0..255 range.
love.math.gammaToLinear - Converts a color from gamma-space (sRGB) to linear-space (RGB). This is useful when doing gamma-correct rendering and you need to do math in linear RGB in the few cases where LÖVE doesn't handle conversions automatically. Read more about gamma-correct rendering here, here, and here. In versions prior to 11.0, color component values were within the range of 0 to 255 instead of 0 to 1.
love.math.gammaToLinear(r: number, g: number, b: number) -> lr: number, lg: number, lb: number: An alpha value can be passed into the function as a fourth argument, but it will be returned unchanged because alpha is always linear.
love.math.gammaToLinear(color: table) -> lr: number, lg: number, lb: number: No description
love.math.gammaToLinear(c: number) -> lc: number: No description
love.math.getRandomSeed() -> low: number, high: number: Gets the seed of the random number generator. The seed is split into two numbers due to Lua's use of doubles for all number values - doubles can't accurately represent integer values above 2^53, but the seed can be an integer value up to 2^64.
love.math.getRandomState() -> state: string: Gets the current state of the random number generator. This returns an opaque implementation-dependent string which is only useful for later use with love.math.setRandomState or RandomGenerator:setState. This is different from love.math.getRandomSeed in that getRandomState gets the random number generator's current state, whereas getRandomSeed gets the previously set seed number.
love.math.isConvex - Checks whether a polygon is convex. PolygonShapes in love.physics, some forms of Meshes, and polygons drawn with love.graphics.polygon must be simple convex polygons.
love.math.isConvex(vertices: table) -> convex: boolean: No description
love.math.isConvex(x1: number, y1: number, x2: number, y2: number, ...: number) -> convex: boolean: No description
love.math.linearToGamma - Converts a color from linear-space (RGB) to gamma-space (sRGB). This is useful when storing linear RGB color values in an image, because the linear RGB color space has less precision than sRGB for dark colors, which can result in noticeable color banding when drawing. In general, colors chosen based on what they look like on-screen are already in gamma-space and should not be double-converted. Colors calculated using math are often in the linear RGB space. Read more about gamma-correct rendering here, here, and here. In versions prior to 11.0, color component values were within the range of 0 to 255 instead of 0 to 1.
love.math.linearToGamma(lr: number, lg: number, lb: number) -> cr: number, cg: number, cb: number: An alpha value can be passed into the function as a fourth argument, but it will be returned unchanged because alpha is always linear.
love.math.linearToGamma(color: table) -> cr: number, cg: number, cb: number: No description
love.math.linearToGamma(lc: number) -> c: number: No description
love.math.newBezierCurve - Creates a new BezierCurve object. The number of vertices in the control polygon determines the degree of the curve, e.g. three vertices define a quadratic (degree 2) Bézier curve, four vertices define a cubic (degree 3) Bézier curve, etc.
love.math.newBezierCurve(vertices: table) -> curve: BezierCurve: No description
love.math.newBezierCurve(x1: number, y1: number, x2: number, y2: number, ...: number) -> curve: BezierCurve: No description
love.math.newRandomGenerator - Creates a new RandomGenerator object which is completely independent of other RandomGenerator objects and random functions.
love.math.newRandomGenerator() -> rng: RandomGenerator: No description
love.math.newRandomGenerator(seed: number) -> rng: RandomGenerator: See RandomGenerator:setSeed.
love.math.newRandomGenerator(low: number, high: number) -> rng: RandomGenerator: See RandomGenerator:setSeed.
love.math.newTransform - Creates a new Transform object.
love.math.newTransform() -> transform: Transform: Creates a Transform with no transformations applied. Call methods on the returned object to apply transformations.
love.math.newTransform(x: number, y: number, angle: number, sx: number, sy: number, ox: number, oy: number, kx: number, ky: number) -> transform: Transform: Creates a Transform with the specified transformation applied on creation.
love.math.noise - Generates a Simplex or Perlin noise value in 1-4 dimensions. The return value will always be the same, given the same arguments. Simplex noise is closely related to Perlin noise. It is widely used for procedural content generation. There are many webpages which discuss Perlin and Simplex noise in detail.
love.math.noise(x: number) -> value: number: Generates Simplex noise from 1 dimension.
love.math.noise(x: number, y: number) -> value: number: Generates Simplex noise from 2 dimensions.
love.math.noise(x: number, y: number, z: number) -> value: number: Generates Perlin noise (Simplex noise in version 0.9.2 and older) from 3 dimensions.
love.math.noise(x: number, y: number, z: number, w: number) -> value: number: Generates Perlin noise (Simplex noise in version 0.9.2 and older) from 4 dimensions.
love.math.random - Generates a pseudo-random number in a platform independent manner. The default love.run seeds this function at startup, so you generally don't need to seed it yourself.
love.math.random() -> number: number: Get uniformly distributed pseudo-random real number within 1.
love.math.random(max: number) -> number: number: Get a uniformly distributed pseudo-random integer within max.
love.math.random(min: number, max: number) -> number: number: Get uniformly distributed pseudo-random integer within max.
love.math.randomNormal(stddev: number, mean: number) -> number: number: Get a normally distributed pseudo random number.
love.math.setRandomSeed - Sets the seed of the random number generator using the specified integer number. This is called internally at startup, so you generally don't need to call it yourself.
love.math.setRandomSeed(seed: number): Due to Lua's use of double-precision floating point numbers, integer values above 2^53 cannot be accurately represented. Use the other variant of the function if you want to use a larger number.
love.math.setRandomSeed(low: number, high: number): Combines two 32-bit integer numbers into a 64-bit integer value and sets the seed of the random number generator using the value.
love.math.setRandomState(state: string): Sets the current state of the random number generator. The value used as an argument for this function is an opaque implementation-dependent string and should only originate from a previous call to love.math.getRandomState. This is different from love.math.setRandomSeed in that setRandomState directly sets the random number generator's current implementation-dependent state, whereas setRandomSeed gives it a new seed value.
love.math.triangulate - Decomposes a simple convex or concave polygon into triangles.
love.math.triangulate(polygon: table) -> triangles: table: No description
love.math.triangulate(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number) -> triangles: table: No description
Types
Enums
MatrixLayout: The layout of matrix elements (row-major or column-major).
row: The matrix is row-major:
column: The matrix is column-major:
Examples
Random number generation
local randomValue = love.math.random()
local randomInt = love.math.random(1, 100)
Vector operations
local vec1 = {x = 10, y = 20}
local vec2 = {x = 5, y = 15}
local result = {
x = vec1.x + vec2.x,
y = vec1.y + vec2.y
}
Best practices
- Use love.math.random() with proper seeding for reproducibility
- Consider performance implications of complex mathematical operations
- Use appropriate data types for mathematical calculations
- Test mathematical algorithms thoroughly
- Be mindful of floating-point precision issues
Platform compatibility
- Desktop (Windows, macOS, Linux): Full math support
- Mobile (iOS, Android): Full support
- Web: Full support