载入中...
搜索中...
未找到
Cloth.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
5
6#include <cstdint>
7#include <string>
8#include <unordered_map>
9#include <unordered_set>
10#include <vector>
11
12namespace eve::graphics {
13class Graphics;
14}
15
16namespace eve::physics {
17
18class World;
19
29public:
38 Cloth(int cols, int rows, float spacing, float originX, float originY);
40 ~Cloth();
41
42 Cloth(const Cloth &) = delete;
43 Cloth &operator=(const Cloth &) = delete;
44
46 void update(float dt);
47
49 [[nodiscard("check the cloth step outcome")]]
52 [[nodiscard]] SimulationObservation observation() const noexcept override { return observation_; }
54 [[nodiscard]] SimulationBackendKind kind() const noexcept override { return SimulationBackendKind::Cpu; }
56 [[nodiscard]] SimulationDeterminism determinism() const noexcept override {
57 return SimulationDeterminism::ToleranceBounded;
58 }
60 [[nodiscard("check cloth observation restore")]]
61 eve::Result<void> restoreObservation(const SimulationObservation &observation) override;
62
64 void setGravity(float gx, float gy);
66 float getGravityX() const { return gravityX_; }
68 float getGravityY() const { return gravityY_; }
69
71 void setStiffness(float stiffness);
73 float getStiffness() const { return stiffness_; }
74
76 void setIterations(int iterations);
78 int getIterations() const { return iterations_; }
79
81 void setDamping(float damping);
83 float getDamping() const { return damping_; }
84
90 void setParticleSize(float size);
92 float getParticleSize() const { return particleSize_; }
93
98 void setParticleMass(float mass);
100 float getParticleMass() const { return particleMass_; }
101
106 void setSelfCollision(bool on);
108 bool getSelfCollision() const { return selfCollision_; }
109
111 void setFoldStiffness(float k);
113 float getFoldStiffness() const { return foldStiffness_; }
114
120 void setMaxFoldAngle(float degrees);
122 float getMaxFoldAngle() const { return maxFoldAngle_ * 180.f / 3.14159265f; }
123
125 void setBounds(float x, float y, float w, float h);
127 void clearBounds();
128
130 void pin(int index);
132 void unpin(int index);
134 void pinTopRow();
136 bool isPinned(int index) const;
137
142 int grabAt(float x, float y, float radius = 24.f);
144 void moveGrab(float x, float y);
146 void releaseGrab();
148 bool isGrabbing() const { return grabIndex_ >= 0; }
150 int getGrabIndex() const { return grabIndex_; }
151
154 void applyForce(float fx, float fy);
155
161 void interactAt(float x, float y, float radius, float strength);
162
175 void setCollideWorld(World *world);
184 World *getCollideWorld() const { return world_; }
185
187 void reset();
188
190 void setColor(float r, float g, float b, float a = 1.f);
192 float getColorR() const { return colorR_; }
194 float getColorG() const { return colorG_; }
196 float getColorB() const { return colorB_; }
198 float getColorA() const { return colorA_; }
199
201 void draw(graphics::Graphics *gfx);
202
204 int getCols() const { return cols_; }
206 int getRows() const { return rows_; }
208 int getParticleCount() const { return static_cast<int>(particles_.size()); }
210 float getParticleX(int index) const;
212 float getParticleY(int index) const;
214 void setParticlePosition(int index, float x, float y);
215
217 float getSpacing() const { return spacing_; }
219 float getOriginX() const { return originX_; }
221 float getOriginY() const { return originY_; }
222
224 void destroy();
225
226private:
227 struct Particle {
228 float x = 0.f, y = 0.f;
229 float px = 0.f, py = 0.f;
230 bool pinned = false;
231 };
232 struct Link {
233 int a = 0, b = 0;
234 float rest = 0.f;
235 };
236
237 void rebuildLinks();
238 void updateSubsteps(float dt, int substeps);
239 void integrate(float dt);
240 void solveConstraints();
241 void solveFoldConstraint();
242 void solveSelfCollision();
243 void collideWorld(float dt);
244 void collideBounds();
245 void rebuildHash();
246 void buildLinkKeys();
247 bool areLinked(int a, int b) const;
248 bool validIndex(int index) const;
249 int64_t cellKey(int cx, int cy) const;
250
251 int cols_ = 0;
252 int rows_ = 0;
253 float spacing_ = 10.f;
254 float originX_ = 0.f;
255 float originY_ = 0.f;
256
257 float gravityX_ = 0.f;
258 float gravityY_ = 980.f;
259 float stiffness_ = 0.85f;
260 float damping_ = 0.01f;
261 int iterations_ = 4;
262 float particleSize_ = 3.f;
263 float particleMass_ = 0.1f;
264 bool selfCollision_ = true;
265 float foldStiffness_ = 0.5f;
266 float maxFoldAngle_ = 90.f * 3.14159265f / 180.f;
267
268 bool hasBounds_ = false;
269 float boundX_ = 0.f, boundY_ = 0.f, boundW_ = 0.f, boundH_ = 0.f;
270
271 int grabIndex_ = -1;
272 float grabX_ = 0.f, grabY_ = 0.f;
273 float forceX_ = 0.f, forceY_ = 0.f;
274 float interactX_ = 0.f, interactY_ = 0.f;
275 float interactRadius_ = 0.f;
276 float interactStrength_ = 0.f;
277
278 World *world_ = nullptr;
279
280 float colorR_ = 0.75f, colorG_ = 0.82f, colorB_ = 0.95f, colorA_ = 1.f;
281
282 bool destroyed_ = false;
283 SimulationObservation observation_;
284
285 std::vector<Particle> particles_;
286 std::vector<Link> links_;
287 std::unordered_set<int64_t> linkKeys_;
288 std::unordered_map<int64_t, std::vector<int>> hash_;
289};
290
291} // namespace eve::physics
float w
Definition AnimClip.cpp:738
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float cx
Definition CardTypes.cpp:33
float cy
Definition CardTypes.cpp:34
float degrees
Definition CardTypes.cpp:35
float py
#define EVENGINE_API_DOMAINS
Definition Export.h:110
int rows
int cols
tensor::Graph g
Definition GpuGraph.cpp:7
double r
eve::ResourcePin pinned
int h
std::vector< Colorf > px
void(* destroy)(void *)
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
MeshVfxBatchedDraw draw
World3D * world
float radius
Backend-neutral, observable fixed-step contract for physics domains.
TerrainThermalSettings settings
int spacing
int iterations
Definition TreeMesh.cpp:311
float step
Definition TreeMesh.cpp:314
float size
Definition TreeMesh.cpp:156
uint32_t index
Move-only operation result carrying either a value or Status.
Definition Result.h:155
Interactive 2D cloth — Verlet particles + distance constraints. Pixel space (same convention as Box2D...
Definition Cloth.h:28
int getGrabIndex() const
Returns the grab index.
Definition Cloth.h:150
int getCols() const
Returns the cols.
Definition Cloth.h:204
float getOriginX() const
Returns the origin x.
Definition Cloth.h:219
float getColorA() const
Returns the color a.
Definition Cloth.h:198
float getDamping() const
Returns the damping.
Definition Cloth.h:83
SimulationDeterminism determinism() const noexcept override
CPU cloth uses bounded floating-point determinism.
Definition Cloth.h:56
float getOriginY() const
Returns the origin y.
Definition Cloth.h:221
float getParticleSize() const
Returns the particle size.
Definition Cloth.h:92
int getRows() const
Returns the rows.
Definition Cloth.h:206
SimulationBackendKind kind() const noexcept override
Identifies this production CPU cloth backend.
Definition Cloth.h:54
float getColorG() const
Returns the color g.
Definition Cloth.h:194
int getParticleCount() const
Returns the particle count.
Definition Cloth.h:208
float getGravityX() const
Returns the gravity x.
Definition Cloth.h:66
Cloth(const Cloth &)=delete
float getStiffness() const
Returns the stiffness.
Definition Cloth.h:73
bool isGrabbing() const
True when grabbing.
Definition Cloth.h:148
float getFoldStiffness() const
Returns the fold stiffness.
Definition Cloth.h:113
float getMaxFoldAngle() const
Returns the max fold angle.
Definition Cloth.h:122
float getGravityY() const
Returns the gravity y.
Definition Cloth.h:68
SimulationObservation observation() const noexcept override
Returns completed tick/time observables.
Definition Cloth.h:52
int getIterations() const
Returns the iterations.
Definition Cloth.h:78
float getColorB() const
Returns the color b.
Definition Cloth.h:196
float getParticleMass() const
Returns the particle mass.
Definition Cloth.h:100
float getColorR() const
Returns the color r.
Definition Cloth.h:192
Cloth & operator=(const Cloth &)=delete
bool getSelfCollision() const
Returns the self collision.
Definition Cloth.h:108
World * getCollideWorld() const
Returns the attached collision world, or null when detached.
Definition Cloth.h:184
float getSpacing() const
Returns the spacing.
Definition Cloth.h:217
Minimal observable contract for physics simulation providers.
Box2D world wrapper (2D physics) with pixel-space coordinates. Handles stepping, gravity,...
Definition World.h:41
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
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.
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.