10void reset(std::vector<Particle>& chain) {
11 for (
auto& p : chain) {
12 p.position = p.target;
13 p.previous = p.target;
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);
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));
29 const glm::vec3 axis = glm::cross(f, t);
31 return glm::normalize(glm::quat(1.0f + d, axis.x, axis.y, axis.z));
37 glm::vec3 nearest = c.
a;
38 const glm::vec3 ab = c.
b - c.
a;
39 const f32 len2 = glm::dot(ab, ab);
41 const f32 t = std::clamp(glm::dot(p - c.
a, ab) / len2, 0.0f, 1.0f);
42 nearest = c.
a + ab * t;
44 const glm::vec3 d = p - nearest;
45 const f32 d2 = glm::dot(d, d);
46 if (d2 >= r * r)
return false;
49 p = nearest + glm::vec3(0.0f, r, 0.0f);
52 const f32 dist = std::sqrt(d2);
53 p = nearest + d * (r / dist);
58 if (gust <= 0.0f)
return 1.0f;
59 constexpr f32 kTwoPi = 6.28318530718f;
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);
68void substep(std::vector<Particle>& chain,
const StepParams& params,
const std::vector<Collider>& colliders,
f32 h,
f32 time) {
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);
75 const size_t n = chain.size();
76 for (
size_t i = 0; i < n; ++i) {
77 Particle& p = chain[i];
79 p.position = p.target;
80 p.previous = p.target;
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;
90 for (
size_t i = 0; i < n; ++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;
98 const f32 dl = glm::length(d);
99 if (dl < 1e-8f)
continue;
100 p.position = parent.position + d / dl * p.restLength;
103 for (
int pass = 0; pass < 3 && !colliders.empty(); ++pass) {
105 for (
const Collider& c : colliders) moved =
pushOut(p.position, c, params.radius) || moved;
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;
116void step(std::vector<Particle>& chain,
const StepParams& params,
const std::vector<Collider>& colliders,
f32 dt) {
117 if (chain.empty() || dt <= 0.0f)
return;
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));
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 ¶ms, 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.
float f32
32-bit floating point
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,...