Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
SpringBoneSolver.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
4
5#include <algorithm>
6#include <cmath>
7
9
10void reset(std::vector<Particle>& chain) {
11 for (auto& p : chain) {
12 p.position = p.target;
13 p.previous = p.target;
14 }
15}
16
17glm::quat shortestArc(const glm::vec3& from, const glm::vec3& to) {
18 const f32 lf = glm::length(from), lt = glm::length(to);
19 if (lf < 1e-8f || lt < 1e-8f) return glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
20 const glm::vec3 f = from / lf, t = to / lt;
21 const f32 d = glm::dot(f, t);
22 if (d > 0.999999f) return glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
23 if (d < -0.999999f) {
24 // Opposite: any axis across `from` turns it round.
25 glm::vec3 axis = glm::cross(glm::vec3(1.0f, 0.0f, 0.0f), f);
26 if (glm::dot(axis, axis) < 1e-6f) axis = glm::cross(glm::vec3(0.0f, 1.0f, 0.0f), f);
27 return glm::angleAxis(3.14159265f, glm::normalize(axis));
28 }
29 const glm::vec3 axis = glm::cross(f, t);
30 // (1 + dot, cross) normalised is the half-angle quaternion of the arc.
31 return glm::normalize(glm::quat(1.0f + d, axis.x, axis.y, axis.z));
32}
33
34bool pushOut(glm::vec3& p, const Collider& c, f32 extra) {
35 const f32 r = c.radius + extra;
36 // The nearest point of the capsule's segment (a sphere's is its centre).
37 glm::vec3 nearest = c.a;
38 const glm::vec3 ab = c.b - c.a;
39 const f32 len2 = glm::dot(ab, ab);
40 if (len2 > 1e-12f) {
41 const f32 t = std::clamp(glm::dot(p - c.a, ab) / len2, 0.0f, 1.0f);
42 nearest = c.a + ab * t;
43 }
44 const glm::vec3 d = p - nearest;
45 const f32 d2 = glm::dot(d, d);
46 if (d2 >= r * r) return false;
47 if (d2 < 1e-12f) {
48 // On the axis: leave along any direction rather than not at all.
49 p = nearest + glm::vec3(0.0f, r, 0.0f);
50 return true;
51 }
52 const f32 dist = std::sqrt(d2);
53 p = nearest + d * (r / dist);
54 return true;
55}
56
57f32 gustFactor(f32 time, f32 frequency, f32 gust, i32 index) {
58 if (gust <= 0.0f) return 1.0f;
59 constexpr f32 kTwoPi = 6.28318530718f;
60 // Two incommensurate sines: no beat repeats soon enough to read as a loop. The index lags the phase so each bone meets a gust a moment after the one above.
61 const f32 phase = time * frequency * kTwoPi - static_cast<f32>(index) * 0.35f;
62 const f32 n = 0.6f * std::sin(phase) + 0.4f * std::sin(phase * 2.37f + 1.3f);
63 return std::max(0.0f, 1.0f + std::clamp(gust, 0.0f, 1.0f) * n);
64}
65
66namespace {
67
68void substep(std::vector<Particle>& chain, const StepParams& params, const std::vector<Collider>& colliders, f32 h, f32 time) {
69 // Stiffness and drag are quoted per 60 Hz frame; at another step they are the same decay over that time.
70 const f32 frames = h / kReferenceStep;
71 const f32 keep = std::pow(std::clamp(1.0f - params.drag, 0.0f, 1.0f), frames);
72 const f32 pull = 1.0f - std::pow(std::clamp(1.0f - params.stiffness, 0.0f, 1.0f), frames);
73 const glm::vec3 fall = params.gravity * (h * h);
74
75 const size_t n = chain.size();
76 for (size_t i = 0; i < n; ++i) {
77 Particle& p = chain[i];
78 if (p.parent < 0) {
79 p.position = p.target;
80 p.previous = p.target;
81 continue;
82 }
83 const glm::vec3 velocity = (p.position - p.previous) * keep;
84 p.previous = p.position;
85 p.position += velocity + fall;
86 if (glm::dot(params.wind, params.wind) > 0.0f) p.position += params.wind * (gustFactor(time, params.windFrequency, params.windGust, static_cast<i32>(i)) * h * h);
87 p.position += (p.target - p.position) * pull;
88 }
89 // Parents first: a node's parent is already where it will be, so each node is put at its distance from it and the chain cannot stretch.
90 for (size_t i = 0; i < n; ++i) {
91 Particle& p = chain[i];
92 if (p.parent < 0) continue;
93 const Particle& parent = chain[static_cast<size_t>(p.parent)];
94 glm::vec3 d = p.position - parent.position;
95 const f32 len = glm::length(d);
96 if (len < 1e-8f) d = p.target - parent.target;
97 else d /= len;
98 const f32 dl = glm::length(d);
99 if (dl < 1e-8f) continue;
100 p.position = parent.position + d / dl * p.restLength;
101 // Out of the colliders and back onto the bone's length, a few times over: each undoes a little of the other, and they meet where the
102 // bone slides round the collider instead of through it.
103 for (int pass = 0; pass < 3 && !colliders.empty(); ++pass) {
104 bool moved = false;
105 for (const Collider& c : colliders) moved = pushOut(p.position, c, params.radius) || moved;
106 if (!moved) break;
107 glm::vec3 e = p.position - parent.position;
108 const f32 el = glm::length(e);
109 if (el > 1e-8f) p.position = parent.position + e / el * p.restLength;
110 }
111 }
112}
113
114} // namespace
115
116void step(std::vector<Particle>& chain, const StepParams& params, const std::vector<Collider>& colliders, f32 dt) {
117 if (chain.empty() || dt <= 0.0f) return;
118 dt = std::min(dt, kMaxStep);
119 const i32 steps = std::max(1, static_cast<i32>(std::ceil(dt / kReferenceStep - 1e-4f)));
120 const f32 h = dt / static_cast<f32>(steps);
121 for (i32 s = 0; s < steps; ++s) substep(chain, params, colliders, h, params.time + h * static_cast<f32>(s));
122}
123
124} // namespace esengine::springbone
The arithmetic of a spring chain, with no registry in it: Verlet integration, length constraints,...
constexpr f32 kReferenceStep
The rate the integration is stepped at: the stiffness and drag are per frame of this.
bool pushOut(glm::vec3 &p, const Collider &c, f32 extra)
Moves p out of c if it is inside (radius widened by extra), returning whether it moved.
f32 gustFactor(f32 time, f32 frequency, f32 gust, i32 index)
The factor gusts put on the wind's strength for a particle at a moment: a travelling wave down the ch...
constexpr f32 kMaxStep
Longest stretch of time one call integrates; a stall must not become a lurch.
glm::quat shortestArc(const glm::vec3 &from, const glm::vec3 &to)
The shortest rotation that turns direction from onto direction to; identity when either has no length...
void step(std::vector< Particle > &chain, const StepParams &params, const std::vector< Collider > &colliders, f32 dt)
Advances the chain by dt seconds.
void reset(std::vector< Particle > &chain)
Sets every particle to rest at its target: the state a chain starts in and the one it is put back in ...
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
float f32
32-bit floating point
Definition Types.hpp:59
A collider in world space: a sphere when a == b, else a capsule from a to b.
One simulated point of a chain.
glm::vec3 position
Simulated world position, and where it was a step ago (Verlet's velocity).
f32 time
Seconds on the chain's own clock at the start of the call; gusts are a function of it,...