载入中...
搜索中...
未找到
SoftBody3D.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
8
9#include <cstdint>
10#include <memory>
11#include <vector>
12
13namespace eve::physics {
14
15class World3D;
16
28public:
34 [[nodiscard("check soft-body creation")]]
36
50 [[nodiscard("check soft-body creation")]]
51 static eve::Result<std::unique_ptr<SoftBody3D>> create(int cols, int rows, int layers, float spacing, float originX,
52 float originY, float originZ);
53
55 ~SoftBody3D() override;
56 SoftBody3D(const SoftBody3D&) = delete;
57 SoftBody3D& operator=(const SoftBody3D&) = delete;
58
60 void update(float dt);
62 [[nodiscard("check the soft-body step outcome")]]
65 [[nodiscard]] SimulationObservation observation() const noexcept override { return observation_; }
67 [[nodiscard]] SimulationBackendKind kind() const noexcept override { return SimulationBackendKind::Cpu; }
69 [[nodiscard]] SimulationDeterminism determinism() const noexcept override {
70 return SimulationDeterminism::ToleranceBounded;
71 }
73 [[nodiscard("check soft-body observation restore")]]
74 eve::Result<void> restoreObservation(const SimulationObservation& observation) override;
75
77 void setGravity(float x, float y, float z);
79 [[nodiscard]] float getGravityX() const { return gravityX_; }
81 [[nodiscard]] float getGravityY() const { return gravityY_; }
83 [[nodiscard]] float getGravityZ() const { return gravityZ_; }
85 void setDeformationResistance(float value);
87 [[nodiscard]] float getDeformationResistance() const;
89 void setIterations(int value);
91 [[nodiscard]] int getIterations() const { return iterations_; }
93 void setDamping(float value);
95 [[nodiscard]] float getDamping() const { return damping_; }
97 void setParticleRadius(float value);
99 [[nodiscard]] float getParticleRadius() const;
101 void setParticleMass(float value);
103 [[nodiscard]] float getParticleMass() const { return particleMass_; }
105 void setPlasticity(float yield, float creep, float recovery, float maxDeformation);
107 [[nodiscard]] float getPlasticYield() const;
109 [[nodiscard]] float getPlasticCreep() const;
111 [[nodiscard]] float getPlasticRecovery() const;
113 [[nodiscard]] float getMaxDeformation() const;
115 void setSelfCollision(bool enabled) { selfCollision_ = enabled; }
117 [[nodiscard]] bool getSelfCollision() const { return selfCollision_; }
119 void setBounds(float x, float y, float z, float width, float height, float depth);
121 void clearBounds() { hasBounds_ = false; }
122
124 void pin(int index);
126 void unpin(int index);
128 [[nodiscard]] bool isPinned(int index) const;
130 [[nodiscard]] int grabAt(float x, float y, float z, float radius);
132 void moveGrab(float x, float y, float z);
134 void releaseGrab() { grabIndex_ = -1; }
136 [[nodiscard]] bool isGrabbing() const { return grabIndex_ >= 0; }
138 [[nodiscard]] int getGrabIndex() const { return grabIndex_; }
140 void applyForce(float x, float y, float z);
142 void interactAt(float x, float y, float z, float radius, float strength);
143
150 void setCollideWorld(World3D* world);
152 [[nodiscard]] World3D* getCollideWorld() const;
153
159 ownedCollisionWorld_.reset();
160 world_ = nullptr;
161 collisionWorld_ = world;
162 }
163
165 void reset();
167 void destroy();
169 [[nodiscard]] bool isDestroyed() const noexcept { return destroyed_; }
170
171 [[nodiscard]] int getCols() const { return cols_; }
172 [[nodiscard]] int getRows() const { return rows_; }
173 [[nodiscard]] int getLayers() const;
174 [[nodiscard]] int getParticleCount() const { return static_cast<int>(particles_.size()); }
175 [[nodiscard]] float getParticleX(int index) const;
176 [[nodiscard]] float getParticleY(int index) const;
177 [[nodiscard]] float getParticleZ(int index) const;
178 void setParticlePosition(int index, float x, float y, float z);
180 [[nodiscard]] float getVolumeRatio() const;
181
182private:
183 struct Particle {
184 float x, y, z;
185 float px, py, pz;
186 float rx, ry, rz;
187 bool pinned = false;
188 };
189 struct Cluster {
190 int indices[8]{};
191 float plastic[8][3]{};
192 };
193
194 SoftBody3D(int cols, int rows, int layers, float spacing, float originX, float originY, float originZ);
195 [[nodiscard]] bool validIndex(int index) const noexcept;
196 [[nodiscard]] int indexOf(int x, int y, int z) const noexcept;
197 void updateSubsteps(float dt, int substeps);
198 void integrate(float dt);
199 void solveShapeMatching(float dt);
200 void solveSelfCollision();
201 void collideWorld(float dt);
202 void collideBounds();
203
204 int cols_ = 0, rows_ = 0, layers_ = 0;
205 float spacing_ = 1.f;
206 std::vector<Particle> particles_;
207 std::vector<Cluster> clusters_;
208 float gravityX_ = 0.f, gravityY_ = -9.8f, gravityZ_ = 0.f;
209 float forceX_ = 0.f, forceY_ = 0.f, forceZ_ = 0.f;
210 float damping_ = 0.04f;
211 float deformationResistance_ = 0.8f;
212 float particleRadius_ = 0.2f;
213 float particleMass_ = 0.1f;
214 int iterations_ = 5;
215 bool selfCollision_ = false;
216 float plasticYield_ = 0.f, plasticCreep_ = 0.f, plasticRecovery_ = 0.f, maxDeformation_ = 0.f;
217 bool hasBounds_ = false;
218 float boundX_ = 0.f, boundY_ = 0.f, boundZ_ = 0.f;
219 float boundW_ = 0.f, boundH_ = 0.f, boundD_ = 0.f;
220 int grabIndex_ = -1;
221 float grabX_ = 0.f, grabY_ = 0.f, grabZ_ = 0.f;
222 bool hasInteraction_ = false;
223 float interactX_ = 0.f, interactY_ = 0.f, interactZ_ = 0.f;
224 float interactRadius_ = 0.f, interactStrength_ = 0.f;
225 World3D* world_ = nullptr;
226 softbody::ISoftBodyCollisionWorld* collisionWorld_ = nullptr;
227 std::unique_ptr<softbody::ISoftBodyCollisionWorld> ownedCollisionWorld_;
228 bool destroyed_ = false;
229 SimulationObservation observation_{};
230};
231
232} // namespace eve::physics
double value
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
float py
float pz
#define EVENGINE_API_BACKENDS
Definition Export.h:108
int rows
int cols
std::vector< std::uint32_t > indices
eve::ResourcePin pinned
std::vector< Colorf > px
std::uint32_t height
std::uint32_t width
void(* destroy)(void *)
World3D * world
float radius
Backend-neutral, observable fixed-step contract for physics domains.
LogicalId definition
TerrainThermalSettings settings
int spacing
float step
Definition TreeMesh.cpp:314
uint32_t index
std::uint32_t depth
Move-only operation result carrying either a value or Status.
Definition Result.h:155
Minimal observable contract for physics simulation providers.
Volumetric particle soft body using overlapping shape-matching clusters.
Definition SoftBody3D.h:27
SimulationBackendKind kind() const noexcept override
Identifies the built-in CPU implementation.
Definition SoftBody3D.h:67
bool isDestroyed() const noexcept
Whether destroy() has disabled this runtime instance.
Definition SoftBody3D.h:169
SimulationDeterminism determinism() const noexcept override
Results are repeatable within documented floating-point tolerance.
Definition SoftBody3D.h:69
int getParticleCount() const
Definition SoftBody3D.h:174
float getGravityY() const
Returns the gravity y.
Definition SoftBody3D.h:81
float getParticleMass() const
Returns the particle mass.
Definition SoftBody3D.h:103
void releaseGrab()
Release grab.
Definition SoftBody3D.h:134
float getGravityZ() const
Returns the gravity z.
Definition SoftBody3D.h:83
void setCollisionWorld(softbody::ISoftBodyCollisionWorld *world)
Set a backend-neutral borrowed collision provider. @lifetime The provider must outlive this object or...
Definition SoftBody3D.h:158
SimulationObservation observation() const noexcept override
Returns completed fixed-step metadata by value.
Definition SoftBody3D.h:65
bool getSelfCollision() const
Returns the self collision.
Definition SoftBody3D.h:117
int getIterations() const
Returns the iterations.
Definition SoftBody3D.h:91
SoftBody3D & operator=(const SoftBody3D &)=delete
bool isGrabbing() const
True when grabbing.
Definition SoftBody3D.h:136
int getGrabIndex() const
Returns the grab index.
Definition SoftBody3D.h:138
SoftBody3D(const SoftBody3D &)=delete
float getDamping() const
Returns the damping.
Definition SoftBody3D.h:95
void clearBounds()
Clears bounds.
Definition SoftBody3D.h:121
float getGravityX() const
Returns the gravity x.
Definition SoftBody3D.h:79
void setSelfCollision(bool enabled)
Sets the self collision.
Definition SoftBody3D.h:115
Box3D rigid-body world. Script coordinates are meters (Box3D native), unlike 2D World which uses pixe...
Definition World3D.h:63
Narrow borrowed collision service consumed by the soft-body core.
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
SimulationBackendKind
Kind of implementation that owns a simulation step.
SimulationDeterminism
Determinism guarantee made by a simulation backend.
bool enabled
One deterministic fixed-step emitted by SimulationClock.
Definition Time.h:158
Observable backend progress shared by CPU and accelerator providers.
Validated solver policy for one simulation step.
Versioned, owning creation definition for one volumetric soft body.