Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
SpringBoneSolver.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
12#pragma once
13
14#include "../core/Types.hpp"
15#include "../math/Math.hpp"
16
17#include <vector>
18
19namespace esengine::springbone {
20
22struct Particle {
24 i32 parent = -1;
26 glm::vec3 position{0.0f};
27 glm::vec3 previous{0.0f};
29 glm::vec3 target{0.0f};
32};
33
35struct StepParams {
36 f32 stiffness = 0.1f;
37 f32 drag = 0.5f;
38 glm::vec3 gravity{0.0f, -980.0f, 0.0f};
39 f32 radius = 0.0f;
40 glm::vec3 wind{0.0f};
41 f32 windGust = 0.0f;
43 f32 time = 0.0f;
44};
45
47f32 gustFactor(f32 time, f32 frequency, f32 gust, i32 index);
48
50struct Collider {
51 glm::vec3 a{0.0f};
52 glm::vec3 b{0.0f};
53 f32 radius = 0.0f;
54};
55
57constexpr f32 kReferenceStep = 1.0f / 60.0f;
58
60constexpr f32 kMaxStep = 1.0f / 20.0f;
61
63void reset(std::vector<Particle>& chain);
64
71void step(std::vector<Particle>& chain, const StepParams& params, const std::vector<Collider>& colliders, f32 dt);
72
74glm::quat shortestArc(const glm::vec3& from, const glm::vec3& to);
75
77bool pushOut(glm::vec3& p, const Collider& c, f32 extra);
78
79} // namespace esengine::springbone
Math utilities and GLM configuration for ESEngine.
Core type definitions and utilities for ESEngine.
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.
i32 parent
Index of the parent particle, -1 for the root, which the animation drives.
glm::vec3 position
Simulated world position, and where it was a step ago (Verlet's velocity).
f32 restLength
Its distance from its parent, which the chain keeps.
glm::vec3 target
Where the animation puts it this frame.
f32 time
Seconds on the chain's own clock at the start of the call; gusts are a function of it,...
glm::vec3 wind
Steady wind acceleration, world units a second squared.
f32 radius
The strand's own thickness, kept clear of colliders.
f32 stiffness
Pull toward the animated pose per 60 Hz frame, 0 to 1.
f32 windGust
How far gusts swing the wind's strength, 0 to 1: 0.5 takes it between half and one and a half times w...
f32 drag
Share of speed lost per 60 Hz frame, 0 to 1.