Angles are in degrees: 0 = right, 90 = up, 180 = left, 270 = down.
| Function |
What it does |
Example |
float angleTo(float targetX, float targetY) const |
Angle from this instance to a point. |
setDirection(angleTo(mouseX(), mouseY())); |
float angleTo(const Entity& other) const |
Angle to another instance. |
angleTo(*player) |
float distanceTo(float targetX, float targetY) const |
Distance in pixels to a point. |
if (distanceTo(160, 90) < 8) ... |
float distanceTo(const Entity& other) const |
Distance to another instance. |
if (distanceTo(*player) < 40) ... |
| Function |
What it does |
Example |
float lerp(float a, float b, float t) |
Blend from a to b: t = 0 gives a, t = 1 gives b. |
x = lerp(x, target, 0.1f); |
float approach(float current, float target, float step) |
Moves from current to target by at most step, without overshooting. |
hspeed = approach(hspeed, 0, 0.2f); |
int sign(float v) |
-1, 0 or 1, according to the sign. |
if (hspeed != 0) scaleX = sign(hspeed); |
float wrap(float v, float min, float max) |
Wraps around inside the range. wrap(370, 0, 360) = 10. |
x = wrap(x, 0, roomWidth()); |
A 2D vector with x and y. y grows downwards, as on screen.
| Member |
What it does |
Vec2{x, y} |
Creates a vector. |
+, -, *, / (and +=, -=, *=, /=) |
Arithmetic with vectors and numbers. |
float length() const |
Length of the vector. |
Vec2 normalized() const |
The same vector with length 1. |
static Vec2 fromAngle(float distance, float degrees) |
Vector with the given length and angle. |
const Vec2 v = Vec2::fromAngle(4.0f, 45); // 4 pixels diagonally up and to the right
| Function |
What it does |
Example |
float random(float a, float b) |
Decimal number from a up to almost b. |
vspeed = random(0.8f, 1.6f); |
int randomInt(int a, int b) |
Whole number from a to b, including both. |
int side = randomInt(0, 3); |
bool chance(float p) |
true with probability p (0 = never, 1 = always). |
if (chance(0.25f)) ... |
pick(a, b, ...) |
Picks one of the values at random. Mixed types become the common type. |
pick(1, 5, 9) |
void randomSeed(unsigned seed) |
Fixes the seed: the same seed repeats the same random results. |
randomSeed(42); |
See Random numbers for examples.