载入中...
搜索中...
未找到
Math.cpp
浏览该文件的文档.
1#include "math/Math.h"
2#include "math/Vec2.h"
3#include "math/Vec3.h"
4#include "math/Mat4.h"
5
6#include "common/Exception.h"
7
8#include <simplesquirrel/simplesquirrel.hpp>
9
10#include <glm/glm.hpp>
11#include <glm/gtc/matrix_transform.hpp>
12#include <glm/gtc/noise.hpp>
13
14#include <algorithm>
15#include <chrono>
16#include <cmath>
17#include <cstdint>
18#include <string>
19
20#ifndef M_PI
21#define M_PI 3.14159265358979323846
22#endif
23
24namespace eve::math {
25namespace {
26
27float clamp01(float t) { return std::clamp(t, 0.f, 1.f); }
28
29float noiseToUnit(float n) {
30 // glm::simplex / perlin is roughly [-1, 1]; map to [0, 1] like LÖVE.
31 return clamp01(n * 0.5f + 0.5f);
32}
33
34float hash11(float p) {
35 p = p - std::floor(p);
36 p *= 0.1031f;
37 p = p - std::floor(p);
38 p *= p + 33.33f;
39 p *= p + p;
40 return p - std::floor(p);
41}
42
43float hash21(float x, float y) {
44 glm::vec3 p3 = glm::fract(glm::vec3(x, y, x) * 0.1031f);
45 p3 += glm::dot(p3, glm::vec3(p3.y + 33.33f, p3.z + 33.33f, p3.x + 33.33f));
46 return glm::fract((p3.x + p3.y) * p3.z);
47}
48
49float hash31(float x, float y, float z) {
50 glm::vec3 p3 = glm::fract(glm::vec3(x, y, z) * 0.1031f);
51 p3 += glm::dot(p3, glm::vec3(p3.y + 33.33f, p3.z + 33.33f, p3.x + 33.33f));
52 return glm::fract((p3.x + p3.y) * p3.z);
53}
54
55glm::vec2 voronoiPoint(int ix, int iy) {
56 float hx = hash21(float(ix), float(iy));
57 float hy = hash21(float(ix) + 19.19f, float(iy) + 47.47f);
58 return glm::vec2(float(ix) + hx, float(iy) + hy);
59}
60
61void voronoiF1F2(float x, float y, float &f1, float &f2) {
62 int ix = int(std::floor(x));
63 int iy = int(std::floor(y));
64 f1 = 1e9f;
65 f2 = 1e9f;
66 for (int j = -1; j <= 1; ++j) {
67 for (int i = -1; i <= 1; ++i) {
68 glm::vec2 p = voronoiPoint(ix + i, iy + j);
69 float d = glm::length(glm::vec2(x, y) - p);
70 if (d < f1) {
71 f2 = f1;
72 f1 = d;
73 } else if (d < f2) {
74 f2 = d;
75 }
76 }
77 }
78}
79
80} // namespace
81
83
84Math::Math() : seed_(1), rng_(1) {}
85
86Vec2 *Math::newVec2(float x, float y) { return new Vec2(x, y); }
87Vec3 *Math::newVec3(float x, float y, float z) { return new Vec3(x, y, z); }
88
89Mat4 *Math::newMat4() { return new Mat4(); }
90
91Mat4 *Math::newMat4Translation(float x, float y, float z) {
92 auto *m = new Mat4();
93 m->translate(x, y, z);
94 return m;
95}
96
97Mat4 *Math::newMat4Scale(float sx, float sy, float sz) {
98 auto *m = new Mat4();
99 m->scale(sx, sy, sz);
100 return m;
101}
102
104 auto *m = new Mat4();
105 m->rotateZ(radians);
106 return m;
107}
108
109float Math::clamp(float x, float lo, float hi) const {
110 if (lo > hi) std::swap(lo, hi);
111 return std::clamp(x, lo, hi);
112}
113
114float Math::lerp(float a, float b, float t) const { return a + (b - a) * t; }
115
116float Math::smoothstep(float edge0, float edge1, float x) const {
117 if (edge0 == edge1) return x < edge0 ? 0.f : 1.f;
118 float t = clamp01((x - edge0) / (edge1 - edge0));
119 return t * t * (3.f - 2.f * t);
120}
121
122float Math::remap(float x, float inMin, float inMax, float outMin, float outMax) const {
123 if (inMin == inMax) return outMin;
124 float t = (x - inMin) / (inMax - inMin);
125 return outMin + (outMax - outMin) * t;
126}
127
128float Math::degToRad(float deg) const { return deg * float(M_PI) / 180.f; }
129float Math::radToDeg(float rad) const { return rad * 180.f / float(M_PI); }
130
131float Math::sign(float x) const {
132 if (x > 0.f) return 1.f;
133 if (x < 0.f) return -1.f;
134 return 0.f;
135}
136
137float Math::fract(float x) const { return x - std::floor(x); }
138
139float Math::approach(float current, float target, float maxDelta) const {
140 float d = target - current;
141 if (std::fabs(d) <= maxDelta) return target;
142 return current + sign(d) * maxDelta;
143}
144
145float Math::wrap(float x, float lo, float hi) const {
146 if (lo == hi) return lo;
147 if (lo > hi) std::swap(lo, hi);
148 float range = hi - lo;
149 float t = std::fmod(x - lo, range);
150 if (t < 0.f) t += range;
151 return lo + t;
152}
153
154float Math::pingPong(float t, float length) const {
155 if (length <= 0.f) return 0.f;
156 t = wrap(t, 0.f, length * 2.f);
157 return length - std::fabs(t - length);
158}
159
160float Math::inverseLerp(float a, float b, float x) const {
161 if (a == b) return 0.f;
162 return (x - a) / (b - a);
163}
164
165float Math::smootherstep(float edge0, float edge1, float x) const {
166 if (edge0 == edge1) return x < edge0 ? 0.f : 1.f;
167 float t = clamp01((x - edge0) / (edge1 - edge0));
168 return t * t * t * (t * (t * 6.f - 15.f) + 10.f);
169}
170
171float Math::bias(float t, float b) const {
172 t = clamp01(t);
173 if (b <= 0.f) return 0.f;
174 if (b >= 1.f) return 1.f;
175 return t / ((1.f / b - 2.f) * (1.f - t) + 1.f);
176}
177
178float Math::gain(float t, float g) const {
179 t = clamp01(t);
180 if (t < 0.5f) return bias(t * 2.f, g) * 0.5f;
181 return bias(t * 2.f - 1.f, 1.f - g) * 0.5f + 0.5f;
182}
183
184float Math::ease(float t, const std::string &kind) const {
185 t = clamp01(t);
186 if (kind == "linear" || kind.empty()) return t;
187 if (kind == "inQuad") return t * t;
188 if (kind == "outQuad") return 1.f - (1.f - t) * (1.f - t);
189 if (kind == "inOutQuad")
190 return t < 0.5f ? 2.f * t * t : 1.f - std::pow(-2.f * t + 2.f, 2.f) * 0.5f;
191 if (kind == "inCubic") return t * t * t;
192 if (kind == "outCubic") return 1.f - std::pow(1.f - t, 3.f);
193 if (kind == "inOutCubic")
194 return t < 0.5f ? 4.f * t * t * t : 1.f - std::pow(-2.f * t + 2.f, 3.f) * 0.5f;
195 if (kind == "inSine") return 1.f - std::cos(t * float(M_PI) * 0.5f);
196 if (kind == "outSine") return std::sin(t * float(M_PI) * 0.5f);
197 if (kind == "inOutSine") return -(std::cos(float(M_PI) * t) - 1.f) * 0.5f;
198 if (kind == "inExpo") return t <= 0.f ? 0.f : std::pow(2.f, 10.f * t - 10.f);
199 if (kind == "outExpo") return t >= 1.f ? 1.f : 1.f - std::pow(2.f, -10.f * t);
200 if (kind == "inOutExpo") {
201 if (t <= 0.f) return 0.f;
202 if (t >= 1.f) return 1.f;
203 return t < 0.5f ? std::pow(2.f, 20.f * t - 10.f) * 0.5f
204 : (2.f - std::pow(2.f, -20.f * t + 10.f)) * 0.5f;
205 }
206 throw Exception("Math.ease: unknown kind '%s'", kind.c_str());
207}
208
209float Math::step(float edge, float x) const { return x < edge ? 0.f : 1.f; }
210
211float Math::quantize(float x, float stepSize) const {
212 if (stepSize == 0.f) return x;
213 return std::floor(x / stepSize) * stepSize;
214}
215
216float Math::snap(float x, float grid) const {
217 if (grid == 0.f) return x;
218 return std::round(x / grid) * grid;
219}
220
221float Math::length2(float x, float y) const { return std::sqrt(x * x + y * y); }
222float Math::length3(float x, float y, float z) const {
223 return std::sqrt(x * x + y * y + z * z);
224}
225
226float Math::distance2(float x1, float y1, float x2, float y2) const {
227 return length2(x2 - x1, y2 - y1);
228}
229
230float Math::distance3(float x1, float y1, float z1, float x2, float y2, float z2) const {
231 return length3(x2 - x1, y2 - y1, z2 - z1);
232}
233
234float Math::dot2(float x1, float y1, float x2, float y2) const { return x1 * x2 + y1 * y2; }
235float Math::dot3(float x1, float y1, float z1, float x2, float y2, float z2) const {
236 return x1 * x2 + y1 * y2 + z1 * z2;
237}
238
239float Math::cross2(float x1, float y1, float x2, float y2) const { return x1 * y2 - y1 * x2; }
240float Math::angle2(float x, float y) const { return std::atan2(y, x); }
241
242float Math::angleBetween2(float x1, float y1, float x2, float y2) const {
243 return std::atan2(y2, x2) - std::atan2(y1, x1);
244}
245
246float Math::lerpAngle(float a, float b, float t) const {
247 float twoPi = float(M_PI) * 2.f;
248 float diff = std::fmod(b - a + float(M_PI), twoPi);
249 if (diff < 0.f) diff += twoPi;
250 diff -= float(M_PI);
251 return a + diff * t;
252}
253
254float Math::normalize2X(float x, float y) const {
255 float len = length2(x, y);
256 return len > 0.f ? x / len : 0.f;
257}
258float Math::normalize2Y(float x, float y) const {
259 float len = length2(x, y);
260 return len > 0.f ? y / len : 0.f;
261}
262float Math::normalize3X(float x, float y, float z) const {
263 float len = length3(x, y, z);
264 return len > 0.f ? x / len : 0.f;
265}
266float Math::normalize3Y(float x, float y, float z) const {
267 float len = length3(x, y, z);
268 return len > 0.f ? y / len : 0.f;
269}
270float Math::normalize3Z(float x, float y, float z) const {
271 float len = length3(x, y, z);
272 return len > 0.f ? z / len : 0.f;
273}
274
275float Math::rotate2X(float x, float y, float radians) const {
276 float c = std::cos(radians), s = std::sin(radians);
277 return x * c - y * s;
278}
279float Math::rotate2Y(float x, float y, float radians) const {
280 float c = std::cos(radians), s = std::sin(radians);
281 return x * s + y * c;
282}
283
284float Math::polarX(float radius, float radians) const { return radius * std::cos(radians); }
285float Math::polarY(float radius, float radians) const { return radius * std::sin(radians); }
286float Math::cartesianRadius(float x, float y) const { return length2(x, y); }
287float Math::cartesianAngle(float x, float y) const { return angle2(x, y); }
288
289bool Math::pointInCircle(float px, float py, float cx, float cy, float radius) const {
290 if (radius < 0.f) return false;
291 return distance2(px, py, cx, cy) <= radius;
292}
293
294bool Math::pointInRect(float px, float py, float rx, float ry, float rw, float rh) const {
295 return px >= rx && py >= ry && px <= rx + rw && py <= ry + rh;
296}
297
298bool Math::circlesOverlap(float x1, float y1, float r1, float x2, float y2, float r2) const {
299 if (r1 < 0.f || r2 < 0.f) return false;
300 float rr = r1 + r2;
301 float dx = x2 - x1, dy = y2 - y1;
302 return dx * dx + dy * dy <= rr * rr;
303}
304
305bool Math::rectsOverlap(float x1, float y1, float w1, float h1, float x2, float y2, float w2,
306 float h2) const {
307 return x1 <= x2 + w2 && x1 + w1 >= x2 && y1 <= y2 + h2 && y1 + h1 >= y2;
308}
309
310bool Math::circleRectOverlap(float cx, float cy, float radius, float rx, float ry, float rw,
311 float rh) const {
312 if (radius < 0.f) return false;
313 float nearestX = std::clamp(cx, rx, rx + rw);
314 float nearestY = std::clamp(cy, ry, ry + rh);
315 float dx = cx - nearestX, dy = cy - nearestY;
316 return dx * dx + dy * dy <= radius * radius;
317}
318
319bool Math::segmentsIntersect(float ax, float ay, float bx, float by, float cx, float cy, float dx,
320 float dy) const {
321 auto orient = [](float px, float py, float qx, float qy, float rx, float ry) {
322 return (qy - py) * (rx - qx) - (qx - px) * (ry - qy);
323 };
324 auto onSeg = [](float px, float py, float qx, float qy, float rx, float ry) {
325 return std::min(px, rx) <= qx && qx <= std::max(px, rx) && std::min(py, ry) <= qy &&
326 qy <= std::max(py, ry);
327 };
328 float o1 = orient(ax, ay, bx, by, cx, cy);
329 float o2 = orient(ax, ay, bx, by, dx, dy);
330 float o3 = orient(cx, cy, dx, dy, ax, ay);
331 float o4 = orient(cx, cy, dx, dy, bx, by);
332 if (o1 * o2 < 0.f && o3 * o4 < 0.f) return true;
333 constexpr float eps = 1e-6f;
334 if (std::fabs(o1) <= eps && onSeg(ax, ay, cx, cy, bx, by)) return true;
335 if (std::fabs(o2) <= eps && onSeg(ax, ay, dx, dy, bx, by)) return true;
336 if (std::fabs(o3) <= eps && onSeg(cx, cy, ax, ay, dx, dy)) return true;
337 if (std::fabs(o4) <= eps && onSeg(cx, cy, bx, by, dx, dy)) return true;
338 return false;
339}
340
341float Math::raycastCircle2(float ox, float oy, float dx, float dy, float cx, float cy,
342 float radius) const {
343 if (radius < 0.f) return -1.f;
344 float fx = ox - cx, fy = oy - cy;
345 float a = dx * dx + dy * dy;
346 if (a <= 1e-12f) return -1.f;
347 float b = 2.f * (fx * dx + fy * dy);
348 float c = fx * fx + fy * fy - radius * radius;
349 float disc = b * b - 4.f * a * c;
350 if (disc < 0.f) return -1.f;
351 float s = std::sqrt(disc);
352 float t0 = (-b - s) / (2.f * a);
353 float t1 = (-b + s) / (2.f * a);
354 if (t0 >= 0.f) return t0;
355 if (t1 >= 0.f) return t1;
356 return -1.f;
357}
358
359float Math::raycastRect2(float ox, float oy, float dx, float dy, float rx, float ry, float rw,
360 float rh) const {
361 constexpr float inf = 1e30f;
362 float tMin = 0.f;
363 float tMax = inf;
364 auto slab = [&](float o, float d, float minV, float maxV) -> bool {
365 if (std::fabs(d) < 1e-12f) {
366 return o >= minV && o <= maxV;
367 }
368 float inv = 1.f / d;
369 float t1 = (minV - o) * inv;
370 float t2 = (maxV - o) * inv;
371 if (t1 > t2) std::swap(t1, t2);
372 tMin = std::max(tMin, t1);
373 tMax = std::min(tMax, t2);
374 return tMin <= tMax;
375 };
376 if (!slab(ox, dx, rx, rx + rw)) return -1.f;
377 if (!slab(oy, dy, ry, ry + rh)) return -1.f;
378 if (tMax < 0.f) return -1.f;
379 return tMin >= 0.f ? tMin : 0.f;
380}
381
382namespace {
383float closestSegmentT(float px, float py, float ax, float ay, float bx, float by) {
384 float abx = bx - ax, aby = by - ay;
385 float denom = abx * abx + aby * aby;
386 if (denom <= 1e-12f) return 0.f;
387 float t = ((px - ax) * abx + (py - ay) * aby) / denom;
388 return std::clamp(t, 0.f, 1.f);
389}
390float closestSegmentT3(float px, float py, float pz, float ax, float ay, float az, float bx,
391 float by, float bz) {
392 float abx = bx - ax, aby = by - ay, abz = bz - az;
393 float denom = abx * abx + aby * aby + abz * abz;
394 if (denom <= 1e-12f) return 0.f;
395 float t = ((px - ax) * abx + (py - ay) * aby + (pz - az) * abz) / denom;
396 return std::clamp(t, 0.f, 1.f);
397}
398} // namespace
399
400float Math::closestPointOnSegment2X(float px, float py, float ax, float ay, float bx,
401 float by) const {
402 float t = closestSegmentT(px, py, ax, ay, bx, by);
403 return ax + (bx - ax) * t;
404}
405float Math::closestPointOnSegment2Y(float px, float py, float ax, float ay, float bx,
406 float by) const {
407 float t = closestSegmentT(px, py, ax, ay, bx, by);
408 return ay + (by - ay) * t;
409}
410
411bool Math::pointInSphere(float px, float py, float pz, float cx, float cy, float cz,
412 float radius) const {
413 if (radius < 0.f) return false;
414 return distance3(px, py, pz, cx, cy, cz) <= radius;
415}
416
417bool Math::pointInBox(float px, float py, float pz, float minX, float minY, float minZ, float maxX,
418 float maxY, float maxZ) const {
419 return px >= minX && px <= maxX && py >= minY && py <= maxY && pz >= minZ && pz <= maxZ;
420}
421
422bool Math::spheresOverlap(float x1, float y1, float z1, float r1, float x2, float y2, float z2,
423 float r2) const {
424 if (r1 < 0.f || r2 < 0.f) return false;
425 float rr = r1 + r2;
426 float dx = x2 - x1, dy = y2 - y1, dz = z2 - z1;
427 return dx * dx + dy * dy + dz * dz <= rr * rr;
428}
429
430bool Math::boxesOverlap(float minAx, float minAy, float minAz, float maxAx, float maxAy,
431 float maxAz, float minBx, float minBy, float minBz, float maxBx,
432 float maxBy, float maxBz) const {
433 return minAx <= maxBx && maxAx >= minBx && minAy <= maxBy && maxAy >= minBy &&
434 minAz <= maxBz && maxAz >= minBz;
435}
436
437float Math::raycastSphere(float ox, float oy, float oz, float dx, float dy, float dz, float cx,
438 float cy, float cz, float radius) const {
439 if (radius < 0.f) return -1.f;
440 float fx = ox - cx, fy = oy - cy, fz = oz - cz;
441 float a = dx * dx + dy * dy + dz * dz;
442 if (a <= 1e-12f) return -1.f;
443 float b = 2.f * (fx * dx + fy * dy + fz * dz);
444 float c = fx * fx + fy * fy + fz * fz - radius * radius;
445 float disc = b * b - 4.f * a * c;
446 if (disc < 0.f) return -1.f;
447 float s = std::sqrt(disc);
448 float t0 = (-b - s) / (2.f * a);
449 float t1 = (-b + s) / (2.f * a);
450 if (t0 >= 0.f) return t0;
451 if (t1 >= 0.f) return t1;
452 return -1.f;
453}
454
455float Math::raycastBox(float ox, float oy, float oz, float dx, float dy, float dz, float minX,
456 float minY, float minZ, float maxX, float maxY, float maxZ) const {
457 constexpr float inf = 1e30f;
458 float tMin = 0.f;
459 float tMax = inf;
460 auto slab = [&](float o, float d, float minV, float maxV) -> bool {
461 if (std::fabs(d) < 1e-12f) {
462 return o >= minV && o <= maxV;
463 }
464 float inv = 1.f / d;
465 float t1 = (minV - o) * inv;
466 float t2 = (maxV - o) * inv;
467 if (t1 > t2) std::swap(t1, t2);
468 tMin = std::max(tMin, t1);
469 tMax = std::min(tMax, t2);
470 return tMin <= tMax;
471 };
472 if (!slab(ox, dx, minX, maxX)) return -1.f;
473 if (!slab(oy, dy, minY, maxY)) return -1.f;
474 if (!slab(oz, dz, minZ, maxZ)) return -1.f;
475 if (tMax < 0.f) return -1.f;
476 return tMin >= 0.f ? tMin : 0.f;
477}
478
479float Math::raycastPlane(float ox, float oy, float oz, float dx, float dy, float dz, float px,
480 float py, float pz, float nx, float ny, float nz) const {
481 float denom = nx * dx + ny * dy + nz * dz;
482 if (std::fabs(denom) < 1e-12f) return -1.f;
483 float t = (nx * (px - ox) + ny * (py - oy) + nz * (pz - oz)) / denom;
484 return t >= 0.f ? t : -1.f;
485}
486
487float Math::closestPointOnSegment3X(float px, float py, float pz, float ax, float ay, float az,
488 float bx, float by, float bz) const {
489 float t = closestSegmentT3(px, py, pz, ax, ay, az, bx, by, bz);
490 return ax + (bx - ax) * t;
491}
492float Math::closestPointOnSegment3Y(float px, float py, float pz, float ax, float ay, float az,
493 float bx, float by, float bz) const {
494 float t = closestSegmentT3(px, py, pz, ax, ay, az, bx, by, bz);
495 return ay + (by - ay) * t;
496}
497float Math::closestPointOnSegment3Z(float px, float py, float pz, float ax, float ay, float az,
498 float bx, float by, float bz) const {
499 float t = closestSegmentT3(px, py, pz, ax, ay, az, bx, by, bz);
500 return az + (bz - az) * t;
501}
502
503float Math::bilinear(float v00, float v10, float v01, float v11, float u, float v) const {
504 float a = lerp(v00, v10, u);
505 float b = lerp(v01, v11, u);
506 return lerp(a, b, v);
507}
508
509void Math::setRandomSeed(uint32_t seed) {
510 seed_ = seed == 0 ? 1u : seed;
511 rng_.seed(seed_);
512}
513
515 auto us = std::chrono::duration_cast<std::chrono::microseconds>(
516 std::chrono::steady_clock::now().time_since_epoch())
517 .count();
518 setRandomSeed(static_cast<uint32_t>(us) ^ 0xA5A5A5A5u);
519}
520
521uint32_t Math::getRandomSeed() const { return seed_; }
522
524 std::uniform_real_distribution<float> dist(0.f, 1.f);
525 return dist(rng_);
526}
527
528float Math::randomRange(float min, float max) {
529 if (min > max) std::swap(min, max);
530 std::uniform_real_distribution<float> dist(min, max);
531 return dist(rng_);
532}
533
534int Math::randomInt(int min, int maxInclusive) {
535 if (min > maxInclusive) std::swap(min, maxInclusive);
536 std::uniform_int_distribution<int> dist(min, maxInclusive);
537 return dist(rng_);
538}
539
540float Math::randomGaussian(float mean, float stddev) {
541 std::normal_distribution<float> dist(mean, stddev);
542 return dist(rng_);
543}
544
545float Math::hash1(float x) const { return hash11(x); }
546float Math::hash2(float x, float y) const { return hash21(x, y); }
547float Math::hash3(float x, float y, float z) const { return hash31(x, y, z); }
548
549float Math::noise1(float x) const { return noiseToUnit(glm::simplex(glm::vec2(x, 0.f))); }
550float Math::noise2(float x, float y) const { return noiseToUnit(glm::simplex(glm::vec2(x, y))); }
551float Math::noise3(float x, float y, float z) const {
552 return noiseToUnit(glm::simplex(glm::vec3(x, y, z)));
553}
554
555float Math::perlin2(float x, float y) const { return noiseToUnit(glm::perlin(glm::vec2(x, y))); }
556float Math::perlin3(float x, float y, float z) const {
557 return noiseToUnit(glm::perlin(glm::vec3(x, y, z)));
558}
559
560float Math::fbm2(float x, float y, int octaves, float lacunarity, float gain) const {
561 if (octaves < 1) octaves = 1;
562 if (octaves > 16) octaves = 16;
563 float sum = 0.f, amp = 1.f, freq = 1.f, norm = 0.f;
564 for (int i = 0; i < octaves; ++i) {
565 sum += noise2(x * freq, y * freq) * amp;
566 norm += amp;
567 amp *= gain;
568 freq *= lacunarity;
569 }
570 return norm > 0.f ? sum / norm : 0.f;
571}
572
573float Math::fbm3(float x, float y, float z, int octaves, float lacunarity, float gain) const {
574 if (octaves < 1) octaves = 1;
575 if (octaves > 16) octaves = 16;
576 float sum = 0.f, amp = 1.f, freq = 1.f, norm = 0.f;
577 for (int i = 0; i < octaves; ++i) {
578 sum += noise3(x * freq, y * freq, z * freq) * amp;
579 norm += amp;
580 amp *= gain;
581 freq *= lacunarity;
582 }
583 return norm > 0.f ? sum / norm : 0.f;
584}
585
586float Math::ridged2(float x, float y, int octaves, float lacunarity, float gain) const {
587 if (octaves < 1) octaves = 1;
588 if (octaves > 16) octaves = 16;
589 float sum = 0.f, amp = 0.5f, freq = 1.f, prev = 1.f;
590 for (int i = 0; i < octaves; ++i) {
591 float n = noise2(x * freq, y * freq);
592 n = 1.f - std::fabs(n * 2.f - 1.f);
593 n = n * n;
594 sum += n * amp * prev;
595 prev = n;
596 freq *= lacunarity;
597 amp *= gain;
598 }
599 return clamp01(sum);
600}
601
602float Math::ridged3(float x, float y, float z, int octaves, float lacunarity, float gain) const {
603 if (octaves < 1) octaves = 1;
604 if (octaves > 16) octaves = 16;
605 float sum = 0.f, amp = 0.5f, freq = 1.f, prev = 1.f;
606 for (int i = 0; i < octaves; ++i) {
607 float n = noise3(x * freq, y * freq, z * freq);
608 n = 1.f - std::fabs(n * 2.f - 1.f);
609 n = n * n;
610 sum += n * amp * prev;
611 prev = n;
612 freq *= lacunarity;
613 amp *= gain;
614 }
615 return clamp01(sum);
616}
617
618float Math::turbulence2(float x, float y, int octaves, float lacunarity, float gain) const {
619 if (octaves < 1) octaves = 1;
620 if (octaves > 16) octaves = 16;
621 float sum = 0.f, amp = 1.f, freq = 1.f, norm = 0.f;
622 for (int i = 0; i < octaves; ++i) {
623 sum += std::fabs(noise2(x * freq, y * freq) * 2.f - 1.f) * amp;
624 norm += amp;
625 amp *= gain;
626 freq *= lacunarity;
627 }
628 return norm > 0.f ? clamp01(sum / norm) : 0.f;
629}
630
631float Math::voronoi2(float x, float y) const {
632 float f1, f2;
633 voronoiF1F2(x, y, f1, f2);
634 return f1;
635}
636
637float Math::voronoiEdge2(float x, float y) const {
638 float f1, f2;
639 voronoiF1F2(x, y, f1, f2);
640 return f2 - f1;
641}
642
643float Math::warpNoise2(float x, float y, float warpAmp) const {
644 float wx = noise2(x, y) - 0.5f;
645 float wy = noise2(x + 5.2f, y + 1.3f) - 0.5f;
646 return noise2(x + wx * warpAmp, y + wy * warpAmp);
647}
648
649float Math::bezierQuadratic(float t, float p0, float p1, float p2) const {
650 t = clamp01(t);
651 float u = 1.f - t;
652 return u * u * p0 + 2.f * u * t * p1 + t * t * p2;
653}
654
655float Math::bezierCubic(float t, float p0, float p1, float p2, float p3) const {
656 t = clamp01(t);
657 float u = 1.f - t;
658 float uu = u * u;
659 float tt = t * t;
660 return uu * u * p0 + 3.f * uu * t * p1 + 3.f * u * tt * p2 + tt * t * p3;
661}
662
663float Math::bezierQuadratic2X(float t, float x0, float /*y0*/, float x1, float /*y1*/, float x2,
664 float /*y2*/) const {
665 return bezierQuadratic(t, x0, x1, x2);
666}
667
668float Math::bezierQuadratic2Y(float t, float /*x0*/, float y0, float /*x1*/, float y1, float /*x2*/,
669 float y2) const {
670 return bezierQuadratic(t, y0, y1, y2);
671}
672
673float Math::bezierCubic2X(float t, float x0, float /*y0*/, float x1, float /*y1*/, float x2,
674 float /*y2*/, float x3, float /*y3*/) const {
675 return bezierCubic(t, x0, x1, x2, x3);
676}
677
678float Math::bezierCubic2Y(float t, float /*x0*/, float y0, float /*x1*/, float y1, float /*x2*/,
679 float y2, float /*x3*/, float y3) const {
680 return bezierCubic(t, y0, y1, y2, y3);
681}
682
683void Math::expose(ssq::Table &table) {
684 auto cls = table.addClass(name, Math::create, false);
685 expose(cls);
686
687 auto v2 = table.addClass<Vec2>(
688 "Vec2", std::function<Vec2 *()>([]() -> Vec2 * { return nullptr; }), true);
689 v2.addFunc("getX", &Vec2::getX);
690 v2.addFunc("getY", &Vec2::getY);
691 v2.addFunc("setX", &Vec2::setX);
692 v2.addFunc("setY", &Vec2::setY);
693 v2.addFunc("set", &Vec2::set);
694 v2.addFunc("length", &Vec2::length);
695 v2.addFunc("lengthSquared", &Vec2::lengthSquared);
696 v2.addFunc("normalize", &Vec2::normalize);
697 v2.addFunc("normalized", &Vec2::normalized);
698 v2.addFunc("dot", &Vec2::dot);
699 v2.addFunc("cross", &Vec2::cross);
700 v2.addFunc("distanceTo", &Vec2::distanceTo);
701 v2.addFunc("angle", &Vec2::angle);
702 v2.addFunc("add", &Vec2::add);
703 v2.addFunc("sub", &Vec2::sub);
704 v2.addFunc("scale", &Vec2::scale);
705 v2.addFunc("lerpTo", &Vec2::lerpTo);
706 v2.addFunc("clone", &Vec2::clone);
707
708 auto v3 = table.addClass<Vec3>(
709 "Vec3", std::function<Vec3 *()>([]() -> Vec3 * { return nullptr; }), true);
710 v3.addFunc("getX", &Vec3::getX);
711 v3.addFunc("getY", &Vec3::getY);
712 v3.addFunc("getZ", &Vec3::getZ);
713 v3.addFunc("setX", &Vec3::setX);
714 v3.addFunc("setY", &Vec3::setY);
715 v3.addFunc("setZ", &Vec3::setZ);
716 v3.addFunc("set", &Vec3::set);
717 v3.addFunc("length", &Vec3::length);
718 v3.addFunc("lengthSquared", &Vec3::lengthSquared);
719 v3.addFunc("normalize", &Vec3::normalize);
720 v3.addFunc("normalized", &Vec3::normalized);
721 v3.addFunc("dot", &Vec3::dot);
722 v3.addFunc("cross", &Vec3::cross);
723 v3.addFunc("distanceTo", &Vec3::distanceTo);
724 v3.addFunc("add", &Vec3::add);
725 v3.addFunc("sub", &Vec3::sub);
726 v3.addFunc("scale", &Vec3::scale);
727 v3.addFunc("lerpTo", &Vec3::lerpTo);
728 v3.addFunc("clone", &Vec3::clone);
729
730 auto m4 = table.addClass<Mat4>(
731 "Mat4", std::function<Mat4 *()>([]() -> Mat4 * { return nullptr; }), true);
732 m4.addFunc("identity", &Mat4::identity);
733 m4.addFunc("translate", &Mat4::translate);
734 m4.addFunc("rotateX", &Mat4::rotateX);
735 m4.addFunc("rotateY", &Mat4::rotateY);
736 m4.addFunc("rotateZ", &Mat4::rotateZ);
737 m4.addFunc("scale", &Mat4::scale);
738 m4.addFunc("multiply", &Mat4::multiply);
739 m4.addFunc("multiplied", &Mat4::multiplied);
740 m4.addFunc("transformVec3", &Mat4::transformVec3);
741 m4.addFunc("transformPoint2", &Mat4::transformPoint2);
742 m4.addFunc("get", &Mat4::get);
743 m4.addFunc("set", &Mat4::set);
744 m4.addFunc("clone", &Mat4::clone);
745}
746
747void Math::expose(ssq::Class &cls) {
748 cls.addFunc("getName", &Math::getName);
749 cls.addFunc("newVec2", &Math::newVec2);
750 cls.addFunc("newVec3", &Math::newVec3);
751 cls.addFunc("newMat4", &Math::newMat4);
752 cls.addFunc("newMat4Translation", &Math::newMat4Translation);
753 cls.addFunc("newMat4Scale", &Math::newMat4Scale);
754 cls.addFunc("newMat4RotationZ", &Math::newMat4RotationZ);
755
756 cls.addFunc("clamp", &Math::clamp);
757 cls.addFunc("lerp", &Math::lerp);
758 cls.addFunc("smoothstep", &Math::smoothstep);
759 cls.addFunc("remap", &Math::remap);
760 cls.addFunc("degToRad", &Math::degToRad);
761 cls.addFunc("radToDeg", &Math::radToDeg);
762 cls.addFunc("sign", &Math::sign);
763 cls.addFunc("fract", &Math::fract);
764 cls.addFunc("approach", &Math::approach);
765 cls.addFunc("wrap", &Math::wrap);
766 cls.addFunc("pingPong", &Math::pingPong);
767 cls.addFunc("inverseLerp", &Math::inverseLerp);
768 cls.addFunc("smootherstep", &Math::smootherstep);
769 cls.addFunc("bias", &Math::bias);
770 cls.addFunc("gain", &Math::gain);
771 cls.addFunc("ease", &Math::ease);
772 cls.addFunc("step", &Math::step);
773 cls.addFunc("quantize", &Math::quantize);
774 cls.addFunc("snap", &Math::snap);
775
776 cls.addFunc("length2", &Math::length2);
777 cls.addFunc("length3", &Math::length3);
778 cls.addFunc("distance2", &Math::distance2);
779 cls.addFunc("distance3", &Math::distance3);
780 cls.addFunc("dot2", &Math::dot2);
781 cls.addFunc("dot3", &Math::dot3);
782 cls.addFunc("cross2", &Math::cross2);
783 cls.addFunc("angle2", &Math::angle2);
784 cls.addFunc("angleBetween2", &Math::angleBetween2);
785 cls.addFunc("lerpAngle", &Math::lerpAngle);
786 cls.addFunc("normalize2X", &Math::normalize2X);
787 cls.addFunc("normalize2Y", &Math::normalize2Y);
788 cls.addFunc("normalize3X", &Math::normalize3X);
789 cls.addFunc("normalize3Y", &Math::normalize3Y);
790 cls.addFunc("normalize3Z", &Math::normalize3Z);
791 cls.addFunc("rotate2X", &Math::rotate2X);
792 cls.addFunc("rotate2Y", &Math::rotate2Y);
793 cls.addFunc("polarX", &Math::polarX);
794 cls.addFunc("polarY", &Math::polarY);
795 cls.addFunc("cartesianRadius", &Math::cartesianRadius);
796 cls.addFunc("cartesianAngle", &Math::cartesianAngle);
797 cls.addFunc("pointInCircle", &Math::pointInCircle);
798 cls.addFunc("pointInRect", &Math::pointInRect);
799 cls.addFunc("circlesOverlap", &Math::circlesOverlap);
800 cls.addFunc("rectsOverlap", &Math::rectsOverlap);
801 cls.addFunc("circleRectOverlap", &Math::circleRectOverlap);
802 cls.addFunc("segmentsIntersect", &Math::segmentsIntersect);
803 cls.addFunc("raycastCircle2", &Math::raycastCircle2);
804 cls.addFunc("raycastRect2", &Math::raycastRect2);
805 cls.addFunc("closestPointOnSegment2X", &Math::closestPointOnSegment2X);
806 cls.addFunc("closestPointOnSegment2Y", &Math::closestPointOnSegment2Y);
807 cls.addFunc("pointInSphere", &Math::pointInSphere);
808 cls.addFunc("pointInBox", &Math::pointInBox);
809 cls.addFunc("spheresOverlap", &Math::spheresOverlap);
810 cls.addFunc("boxesOverlap", &Math::boxesOverlap);
811 cls.addFunc("raycastSphere", &Math::raycastSphere);
812 cls.addFunc("raycastBox", &Math::raycastBox);
813 cls.addFunc("raycastPlane", &Math::raycastPlane);
814 cls.addFunc("closestPointOnSegment3X", &Math::closestPointOnSegment3X);
815 cls.addFunc("closestPointOnSegment3Y", &Math::closestPointOnSegment3Y);
816 cls.addFunc("closestPointOnSegment3Z", &Math::closestPointOnSegment3Z);
817 cls.addFunc("bilinear", &Math::bilinear);
818
819 cls.addFunc("setRandomSeed", &Math::setRandomSeed);
820 cls.addFunc("setRandomSeedFromTime", &Math::setRandomSeedFromTime);
821 cls.addFunc("getRandomSeed", &Math::getRandomSeed);
822 cls.addFunc("random", &Math::random);
823 cls.addFunc("randomRange", &Math::randomRange);
824 cls.addFunc("randomInt", &Math::randomInt);
825 cls.addFunc("randomGaussian", &Math::randomGaussian);
826
827 cls.addFunc("hash1", &Math::hash1);
828 cls.addFunc("hash2", &Math::hash2);
829 cls.addFunc("hash3", &Math::hash3);
830
831 cls.addFunc("noise1", &Math::noise1);
832 cls.addFunc("noise2", &Math::noise2);
833 cls.addFunc("noise3", &Math::noise3);
834 cls.addFunc("perlin2", &Math::perlin2);
835 cls.addFunc("perlin3", &Math::perlin3);
836 cls.addFunc("fbm2", &Math::fbm2);
837 cls.addFunc("fbm3", &Math::fbm3);
838 cls.addFunc("ridged2", &Math::ridged2);
839 cls.addFunc("ridged3", &Math::ridged3);
840 cls.addFunc("turbulence2", &Math::turbulence2);
841 cls.addFunc("voronoi2", &Math::voronoi2);
842 cls.addFunc("voronoiEdge2", &Math::voronoiEdge2);
843 cls.addFunc("warpNoise2", &Math::warpNoise2);
844
845 cls.addFunc("bezierQuadratic", &Math::bezierQuadratic);
846 cls.addFunc("bezierCubic", &Math::bezierCubic);
847 cls.addFunc("bezierQuadratic2X", &Math::bezierQuadratic2X);
848 cls.addFunc("bezierQuadratic2Y", &Math::bezierQuadratic2Y);
849 cls.addFunc("bezierCubic2X", &Math::bezierCubic2X);
850 cls.addFunc("bezierCubic2Y", &Math::bezierCubic2Y);
851}
852
853} // namespace eve::math
float cx
Definition CardTypes.cpp:31
float cy
Definition CardTypes.cpp:32
uint32_t seed
Tok kind
HSQOBJECT cls
Definition ECS.cpp:21
int y
Definition Grass.cpp:135
int z
Definition Grass.cpp:135
float u
Definition Grass.cpp:234
int x
Definition Grass.cpp:135
glm::vec3 n
Definition Grass.cpp:64
std::vector< Colorf > px
uint32_t a
uint32_t b
uint32_t c
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:24
glm::vec4 p[6]
const char * name
Definition RockMesh.cpp:21
#define M_PI
Definition SpineAnim.cpp:16
int d
int v
float m[16]
uint32_t s
Definition Weather.cpp:28
virtual std::string getName() const =0
Column-major 4x4 matrix wrapping glm::mat4.
Definition Mat4.h:11
Vec3 * transformVec3(const Vec3 *v) const
Definition Mat4.cpp:39
Vec2 * transformPoint2(const Vec2 *v) const
Definition Mat4.cpp:45
void translate(float x, float y, float z)
Definition Mat4.cpp:17
float get(int index) const
Column-major element 0..15.
Definition Mat4.cpp:51
void identity()
Definition Mat4.cpp:15
void scale(float sx, float sy, float sz)
Definition Mat4.cpp:25
void multiply(const Mat4 *other)
Definition Mat4.cpp:29
Mat4 * clone() const
Definition Mat4.cpp:61
void rotateX(float radians)
Definition Mat4.cpp:21
void rotateZ(float radians)
Definition Mat4.cpp:23
Mat4 * multiplied(const Mat4 *other) const
Definition Mat4.cpp:34
void set(int index, float value)
Definition Mat4.cpp:56
void rotateY(float radians)
Definition Mat4.cpp:22
Math module — glm-backed vectors/matrices, noise, bezier, random. Script: math <- eve....
Definition Math.h:21
bool pointInSphere(float px, float py, float pz, float cx, float cy, float cz, float radius) const
Definition Math.cpp:411
float bezierQuadratic(float t, float p0, float p1, float p2) const
Definition Math.cpp:649
bool spheresOverlap(float x1, float y1, float z1, float r1, float x2, float y2, float z2, float r2) const
Definition Math.cpp:422
Mat4 * newMat4Scale(float sx, float sy, float sz)
Definition Math.cpp:97
float warpNoise2(float x, float y, float warpAmp=1.f) const
Domain warp: sample noise at (x,y) + warpAmp * (noise-0.5). Useful for organic terrain / caves.
Definition Math.cpp:643
float normalize3X(float x, float y, float z) const
Definition Math.cpp:262
bool boxesOverlap(float minAx, float minAy, float minAz, float maxAx, float maxAy, float maxAz, float minBx, float minBy, float minBz, float maxBx, float maxBy, float maxBz) const
Definition Math.cpp:430
float normalize3Z(float x, float y, float z) const
Definition Math.cpp:270
float perlin3(float x, float y, float z) const
Definition Math.cpp:556
float voronoi2(float x, float y) const
Worley / Voronoi F1 distance in [0, ~1.5] (cell size 1). voronoiEdge2 = F2 - F1 (cell borders).
Definition Math.cpp:631
float approach(float current, float target, float maxDelta) const
Definition Math.cpp:139
float turbulence2(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Definition Math.cpp:618
float closestPointOnSegment2Y(float px, float py, float ax, float ay, float bx, float by) const
Definition Math.cpp:405
Mat4 * newMat4Translation(float x, float y, float z)
Definition Math.cpp:91
float normalize2Y(float x, float y) const
Definition Math.cpp:258
float length3(float x, float y, float z) const
Definition Math.cpp:222
float ease(float t, const std::string &kind) const
Easing on [0,1]. kind: "linear"|"inQuad"|"outQuad"|"inOutQuad"|"inCubic"|"outCubic"|"inOutCubic"| "in...
Definition Math.cpp:184
float step(float edge, float x) const
Definition Math.cpp:209
float bezierQuadratic2X(float t, float x0, float y0, float x1, float y1, float x2, float y2) const
Definition Math.cpp:663
float angle2(float x, float y) const
Definition Math.cpp:240
float raycastPlane(float ox, float oy, float oz, float dx, float dy, float dz, float px, float py, float pz, float nx, float ny, float nz) const
Ray vs infinite plane through (px,py,pz) with normal (nx,ny,nz). Returns parametric t,...
Definition Math.cpp:479
float raycastBox(float ox, float oy, float oz, float dx, float dy, float dz, float minX, float minY, float minZ, float maxX, float maxY, float maxZ) const
Definition Math.cpp:455
float angleBetween2(float x1, float y1, float x2, float y2) const
Definition Math.cpp:242
float cartesianAngle(float x, float y) const
Definition Math.cpp:287
float normalize3Y(float x, float y, float z) const
Definition Math.cpp:266
float closestPointOnSegment2X(float px, float py, float ax, float ay, float bx, float by) const
Definition Math.cpp:400
bool circleRectOverlap(float cx, float cy, float radius, float rx, float ry, float rw, float rh) const
Definition Math.cpp:310
float smootherstep(float edge0, float edge1, float x) const
Definition Math.cpp:165
float closestPointOnSegment3Y(float px, float py, float pz, float ax, float ay, float az, float bx, float by, float bz) const
Definition Math.cpp:492
float random()
Definition Math.cpp:523
float fract(float x) const
Definition Math.cpp:137
float noise1(float x) const
Definition Math.cpp:549
Mat4 * newMat4()
Definition Math.cpp:89
float smoothstep(float edge0, float edge1, float x) const
Definition Math.cpp:116
float degToRad(float deg) const
Definition Math.cpp:128
float randomRange(float min, float max)
Definition Math.cpp:528
bool rectsOverlap(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) const
Definition Math.cpp:305
float normalize2X(float x, float y) const
Definition Math.cpp:254
float snap(float x, float grid) const
Definition Math.cpp:216
float polarX(float radius, float radians) const
Definition Math.cpp:284
float rotate2X(float x, float y, float radians) const
Rotate (x,y) by radians around origin.
Definition Math.cpp:275
float wrap(float x, float lo, float hi) const
Definition Math.cpp:145
float voronoiEdge2(float x, float y) const
Definition Math.cpp:637
float sign(float x) const
Definition Math.cpp:131
float cross2(float x1, float y1, float x2, float y2) const
Definition Math.cpp:239
float clamp(float x, float lo, float hi) const
Definition Math.cpp:109
float closestPointOnSegment3X(float px, float py, float pz, float ax, float ay, float az, float bx, float by, float bz) const
Definition Math.cpp:487
float rotate2Y(float x, float y, float radians) const
Definition Math.cpp:279
float closestPointOnSegment3Z(float px, float py, float pz, float ax, float ay, float az, float bx, float by, float bz) const
Definition Math.cpp:497
float bezierCubic2Y(float t, float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) const
Definition Math.cpp:678
float noise3(float x, float y, float z) const
Definition Math.cpp:551
float perlin2(float x, float y) const
Definition Math.cpp:555
bool pointInCircle(float px, float py, float cx, float cy, float radius) const
Definition Math.cpp:289
float hash2(float x, float y) const
Definition Math.cpp:546
float bezierQuadratic2Y(float t, float x0, float y0, float x1, float y1, float x2, float y2) const
Definition Math.cpp:668
float dot3(float x1, float y1, float z1, float x2, float y2, float z2) const
Definition Math.cpp:235
float noise2(float x, float y) const
Definition Math.cpp:550
bool pointInBox(float px, float py, float pz, float minX, float minY, float minZ, float maxX, float maxY, float maxZ) const
Inclusive AABB test against [min,max] on each axis.
Definition Math.cpp:417
void setRandomSeedFromTime()
Definition Math.cpp:514
Vec3 * newVec3(float x=0.f, float y=0.f, float z=0.f)
Definition Math.cpp:87
float ridged3(float x, float y, float z, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Definition Math.cpp:602
uint32_t getRandomSeed() const
Definition Math.cpp:521
float bezierCubic(float t, float p0, float p1, float p2, float p3) const
Definition Math.cpp:655
float gain(float t, float g) const
Definition Math.cpp:178
float bilinear(float v00, float v10, float v01, float v11, float u, float v) const
Bilinear sample of 4 corners (v00,v10,v01,v11) with u,v in [0,1].
Definition Math.cpp:503
float ridged2(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Definition Math.cpp:586
float cartesianRadius(float x, float y) const
Definition Math.cpp:286
float pingPong(float t, float length) const
Definition Math.cpp:154
float hash1(float x) const
Definition Math.cpp:545
float distance2(float x1, float y1, float x2, float y2) const
Definition Math.cpp:226
float bias(float t, float b) const
Schlick bias/gain — shape [0,1] distributions (procgen falloff).
Definition Math.cpp:171
float raycastRect2(float ox, float oy, float dx, float dy, float rx, float ry, float rw, float rh) const
Ray vs axis-aligned rect (x,y,w,h). Returns parametric t >= 0, else -1.
Definition Math.cpp:359
float distance3(float x1, float y1, float z1, float x2, float y2, float z2) const
Definition Math.cpp:230
void setRandomSeed(uint32_t seed)
Definition Math.cpp:509
float length2(float x, float y) const
Definition Math.cpp:221
float radToDeg(float rad) const
Definition Math.cpp:129
float inverseLerp(float a, float b, float x) const
Inverse of lerp: t such that lerp(a,b,t) ≈ x.
Definition Math.cpp:160
bool segmentsIntersect(float ax, float ay, float bx, float by, float cx, float cy, float dx, float dy) const
True if segments AB and CD intersect (including endpoints).
Definition Math.cpp:319
float remap(float x, float inMin, float inMax, float outMin, float outMax) const
Definition Math.cpp:122
Mat4 * newMat4RotationZ(float radians)
Definition Math.cpp:103
Vec2 * newVec2(float x=0.f, float y=0.f)
Definition Math.cpp:86
float randomGaussian(float mean, float stddev)
Box-Muller Gaussian (mean, stddev).
Definition Math.cpp:540
float lerpAngle(float a, float b, float t) const
Definition Math.cpp:246
float bezierCubic2X(float t, float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) const
Definition Math.cpp:673
float fbm3(float x, float y, float z, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Definition Math.cpp:573
float dot2(float x1, float y1, float x2, float y2) const
Definition Math.cpp:234
float raycastCircle2(float ox, float oy, float dx, float dy, float cx, float cy, float radius) const
Ray vs circle. Hit point = (ox,oy) + t*(dx,dy). Returns t >= 0 on hit, else -1. Direction need not be...
Definition Math.cpp:341
float polarY(float radius, float radians) const
Definition Math.cpp:285
bool pointInRect(float px, float py, float rx, float ry, float rw, float rh) const
Definition Math.cpp:294
float lerp(float a, float b, float t) const
Definition Math.cpp:114
float quantize(float x, float stepSize) const
Definition Math.cpp:211
bool circlesOverlap(float x1, float y1, float r1, float x2, float y2, float r2) const
Definition Math.cpp:298
float fbm2(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Fractal Brownian Motion / ridged / turbulence. octaves >= 1; lacunarity ~2; gain/persistence ~0....
Definition Math.cpp:560
float raycastSphere(float ox, float oy, float oz, float dx, float dy, float dz, float cx, float cy, float cz, float radius) const
Definition Math.cpp:437
int randomInt(int min, int maxInclusive)
Definition Math.cpp:534
float hash3(float x, float y, float z) const
Definition Math.cpp:547
2D float vector (script-facing math module value).
Definition Vec2.h:8
Vec2 * sub(const Vec2 *other) const
Definition Vec2.cpp:47
Vec2 * normalized() const
Definition Vec2.cpp:17
float cross(const Vec2 *other) const
Definition Vec2.cpp:28
float dot(const Vec2 *other) const
Dot/cross product, distance, angle (radians).
Definition Vec2.cpp:23
Vec2 * scale(float s) const
Definition Vec2.cpp:52
float angle() const
Definition Vec2.cpp:40
float distanceTo(const Vec2 *other) const
Definition Vec2.cpp:33
void setX(float x)
Definition Vec2.h:17
float length() const
Magnitude (and squared magnitude).
Definition Vec2.h:26
void setY(float y)
Definition Vec2.h:18
float lengthSquared() const
Definition Vec2.h:27
float getY() const
Definition Vec2.h:16
float getX() const
Component accessors.
Definition Vec2.h:15
Vec2 * clone() const
Copies this vector.
Definition Vec2.cpp:59
void set(float x, float y)
Sets both components.
Definition Vec2.h:20
Vec2 * add(const Vec2 *other) const
Arithmetic helpers returning new (caller-owned) vectors.
Definition Vec2.cpp:42
void normalize()
Normalizes in place / returns a normalized copy.
Definition Vec2.cpp:9
Vec2 * lerpTo(const Vec2 *other, float t) const
Linear interpolation to other at t in [0,1].
Definition Vec2.cpp:54
3D float vector (script-facing math module value).
Definition Vec3.h:8
Vec3 * normalized() const
Definition Vec3.cpp:18
float getY() const
Definition Vec3.h:16
Vec3 * cross(const Vec3 *other) const
Definition Vec3.cpp:29
Vec3 * clone() const
Copies this vector.
Definition Vec3.cpp:61
Vec3 * sub(const Vec3 *other) const
Definition Vec3.cpp:48
float getZ() const
Definition Vec3.h:17
void setX(float x)
Definition Vec3.h:18
Vec3 * lerpTo(const Vec3 *other, float t) const
Linear interpolation to other at t in [0,1].
Definition Vec3.cpp:55
float length() const
Magnitude (and squared magnitude).
Definition Vec3.h:29
Vec3 * scale(float s) const
Definition Vec3.cpp:53
Vec3 * add(const Vec3 *other) const
Arithmetic helpers returning new (caller-owned) vectors.
Definition Vec3.cpp:43
void setY(float y)
Definition Vec3.h:19
float dot(const Vec3 *other) const
Dot/cross product and distance.
Definition Vec3.cpp:24
float distanceTo(const Vec3 *other) const
Definition Vec3.cpp:35
float lengthSquared() const
Definition Vec3.h:30
void normalize()
Normalizes in place / returns a normalized copy.
Definition Vec3.cpp:9
void setZ(float z)
Definition Vec3.h:20
float getX() const
Component accessors.
Definition Vec3.h:15
void set(float x, float y, float z)
Sets all three components.
Definition Vec3.h:22