Getting Started
Add Maths to your project and start using easing functions and distributions.
1. Add the dependency
In your build.gradle.kts:
dependencies {
implementation("io.peekandpoke.ultra:maths:0.117.0")
} Find the latest version on Maven Central.
Maths is a Kotlin Multiplatform library — it works in commonMain, jvmMain,
jsMain, and nativeMain (linuxX64, linuxArm64, macosArm64, mingwX64).
2. Use an easing function
All easing functions live on the Ease object. Pass a progress value between 0.0 and 1.0:
import io.peekandpoke.ultra.maths.Ease
// Ease a progress value (0.0 to 1.0)
val eased = Ease.Out.cubic(0.5) // 0.875
// Use calc() to ease between two values
val opacity = Ease.calc(
from = 0.0,
to = 1.0,
progress = 0.5,
fn = Ease.InOut.sine,
) // 0.5 3. Bind to a range
Use bindFromTo to create a reusable easing bound to a numeric range:
val fadeIn = Ease.Out.quad.bindFromTo(from = 0, to = 255)
println(fadeIn(0.0)) // 0.0
println(fadeIn(0.5)) // 191.25
println(fadeIn(1.0)) // 255.0 4. Time-based easing
Use timed() to create an easing that progresses based on elapsed wall-clock time:
import kotlin.time.Duration.Companion.milliseconds
val anim = Ease.Out.elastic.timed(
from = 0,
to = 100,
duration = 800.milliseconds,
)
// Poll the current value (uses Kronos clock internally)
val current = anim()
// Check if the animation has completed
if (anim.isDone) {
// final value reached
} Timed easing uses the Kronos clock, so you can inject a
test clock and advance time manually in unit tests.
5. Sample a distribution
import io.peekandpoke.ultra.maths.stochastic.BucketedDistribution
import io.peekandpoke.ultra.maths.stochastic.sample
import kotlin.random.Random
// Normal distribution centered at 0.5
val normal = BucketedDistribution.createTruncatedNormal(mean = 0.5, std = 0.15)
// Raw sample in [0, 1]
val raw = normal.sample(Random)
// Mapped to a custom range
val damage = normal.sample(Random, from = 10.0, to = 50.0)