Skip to content
Download

Math and random

Angles in degrees: 0 = right, 90 = up, 180 = left, 270 = down.

Function What it does
angleTo(x, y), angleTo(other) Angle from this instance to a point or to another instance.
distanceTo(x, y), distanceTo(other) Distance in pixels.
Function What it does
lerp(a, b, t) Blend from a to b: t = 0 gives a, t = 1 gives b.
approach(current, target, step) Moves from current toward target by at most step, without overshooting.
sign(value) -1, 0 or 1.
wrap(value, min, max) Wraps around within the range: wrap(370, 0, 360) = 10.
clamp(value, min, max) Limits to the range. In C++, std::clamp.
Function What it does
random(a, b) Decimal number from a up to almost b.
randomInt(a, b) Integer from a to b, both included.
chance(p) true with probability p, from 0 to 1.
pick(a, b, ...) One of the values, at random.
randomSeed(seed) Sets the seed: the same seed repeats the same draws.
self:setDirection(self:angleTo(mouseX(), mouseY()))
self.hspeed = approach(self.hspeed, 0, 0.2)
self.x = wrap(self.x, 0, roomWidth())
if chance(0.25) then
spawn(pick("Moeda", "Vida"), random(0, roomWidth()), 0, "Instancias")
end

2D vector with x and y, operators + - * /, length(), normalized() and Vec2::fromAngle(length, degrees).

const Vec2 v = Vec2::fromAngle(4.0f, 45);
hspeed = v.x;
vspeed = v.y;