Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
SpringBoneSystem.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
17#pragma once
18
19#include "../core/Types.hpp"
20#include "../math/Math.hpp"
21#include "../ecs/Registry.hpp"
23#include "SpringBoneSolver.hpp"
24
25#include <unordered_map>
26#include <vector>
27
28namespace esengine::springbone {
29
31public:
32 SpringBoneSystem() = default;
33
35 void update(ecs::Registry& registry, ecs::TransformSystem* transforms, f32 dt);
36
41 void release(ecs::Registry& registry, Entity root);
42
44 void reset(Entity root);
45
47 u32 simulated() const { return simulated_; }
48
50 const std::vector<Particle>* chain(Entity root) const;
51
56 void snapshot(Entity root, std::vector<f32>& out) const;
57
58private:
60 struct Node {
61 Entity entity = INVALID_ENTITY;
62 i32 firstChild = -1;
63 };
64
66 struct Pose {
67 glm::vec3 animPos{0.0f};
68 glm::quat animRot{1.0f, 0.0f, 0.0f, 0.0f};
69 glm::vec3 animScale{1.0f};
70 glm::vec3 localPosition{0.0f};
71 glm::quat localRotation{1.0f, 0.0f, 0.0f, 0.0f};
72 };
73
74 struct State {
75 std::vector<Entity> entities;
76 std::vector<Particle> particles;
77 std::vector<Node> nodes;
78 glm::vec3 rootPosition{0.0f};
79 bool primed = false;
80 f32 clock = 0.0f;
83 std::vector<glm::quat> animated;
84 std::vector<glm::quat> written;
85 std::vector<bool> hasWritten;
86 };
87
88 std::unordered_map<Entity, State> states_;
89 // Scratch for one chain's frame, kept so a frame allocates nothing once the chains exist.
90 std::vector<Collider> colliders_;
91 std::vector<Entity> found_;
92 std::vector<Entity> stack_;
93 std::vector<Pose> poses_;
94 std::vector<glm::vec3> tipOffsets_;
95 std::vector<glm::vec3> fkPosition_;
96 std::vector<glm::quat> fkRotation_;
97 std::vector<i32> childCount_;
98 std::vector<i32> parentOf_;
99 u32 simulated_ = 0;
100 // RAII: unregisters from the registry's onDestroy when this system is torn down.
101 Connection destroyConn_;
102
103 void restoreBones(ecs::Registry& registry, State& state);
104 void collectBones(ecs::Registry& registry, Entity root, std::vector<Entity>& out);
105 bool gatherColliders(ecs::Registry& registry, const std::vector<Entity>& entities, std::vector<Collider>& out);
106};
107
108} // namespace esengine::springbone
Math utilities and GLM configuration for ESEngine.
Central ECS registry for entity and component management.
The arithmetic of a spring chain, with no registry in it: Verlet integration, length constraints,...
System for computing hierarchical world transforms.
Core type definitions and utilities for ESEngine.
Central container for all entities and components.
Definition Registry.hpp:73
u32 simulated() const
How many chains the last update simulated.
void update(ecs::Registry &registry, ecs::TransformSystem *transforms, f32 dt)
Advance every enabled chain by dt seconds. Composes world transforms first if the animation moved any...
const std::vector< Particle > * chain(Entity root) const
The simulated world positions of a chain, root first, in the order its bones were found (tips last); ...
void snapshot(Entity root, std::vector< f32 > &out) const
A chain's particles as flat floats for the editor to draw: seven a particle, the simulated position,...
void release(ecs::Registry &registry, Entity root)
Give a chain's bones back as the animation had them and forget its state: what stops a preview in the...
void reset(Entity root)
Forget a chain's state: it starts again from the animated pose on its next frame (a teleport,...
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
Definition Types.hpp:174
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
float f32
32-bit floating point
Definition Types.hpp:59
Entity identifier with packed index + generation.
Definition Types.hpp:135