载入中...
搜索中...
未找到
Cloth.h
浏览该文件的文档.
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <vector>
6
7namespace eve::graphics {
8class Graphics;
9}
10
11namespace eve::physics {
12
17class Cloth {
18public:
26 Cloth(int cols, int rows, float spacing, float originX, float originY);
27 ~Cloth();
28
29 Cloth(const Cloth &) = delete;
30 Cloth &operator=(const Cloth &) = delete;
31
32 void update(float dt);
33
34 void setGravity(float gx, float gy);
35 float getGravityX() const { return gravityX_; }
36 float getGravityY() const { return gravityY_; }
37
39 void setStiffness(float stiffness);
40 float getStiffness() const { return stiffness_; }
41
43 void setIterations(int iterations);
44 int getIterations() const { return iterations_; }
45
47 void setDamping(float damping);
48 float getDamping() const { return damping_; }
49
51 void setBounds(float x, float y, float w, float h);
52 void clearBounds();
53
54 void pin(int index);
55 void unpin(int index);
56 void pinTopRow();
57 bool isPinned(int index) const;
58
63 int grabAt(float x, float y, float radius = 24.f);
64 void moveGrab(float x, float y);
65 void releaseGrab();
66 bool isGrabbing() const { return grabIndex_ >= 0; }
67 int getGrabIndex() const { return grabIndex_; }
68
70 void applyForce(float fx, float fy);
71
72 void setColor(float r, float g, float b, float a = 1.f);
73 float getColorR() const { return colorR_; }
74 float getColorG() const { return colorG_; }
75 float getColorB() const { return colorB_; }
76 float getColorA() const { return colorA_; }
77
78 void draw(graphics::Graphics *gfx);
79
80 int getCols() const { return cols_; }
81 int getRows() const { return rows_; }
82 int getParticleCount() const { return static_cast<int>(particles_.size()); }
83 float getParticleX(int index) const;
84 float getParticleY(int index) const;
85 void setParticlePosition(int index, float x, float y);
86
87 float getSpacing() const { return spacing_; }
88 float getOriginX() const { return originX_; }
89 float getOriginY() const { return originY_; }
90
91 void destroy();
92
93private:
94 struct Particle {
95 float x = 0.f, y = 0.f;
96 float px = 0.f, py = 0.f;
97 bool pinned = false;
98 };
99 struct Link {
100 int a = 0, b = 0;
101 float rest = 0.f;
102 };
103
104 void rebuildLinks();
105 void integrate(float dt);
106 void solveConstraints();
107 void collideBounds();
108 bool validIndex(int index) const;
109
110 int cols_ = 0;
111 int rows_ = 0;
112 float spacing_ = 10.f;
113 float originX_ = 0.f;
114 float originY_ = 0.f;
115
116 float gravityX_ = 0.f;
117 float gravityY_ = 980.f;
118 float stiffness_ = 0.85f;
119 float damping_ = 0.01f;
120 int iterations_ = 4;
121
122 bool hasBounds_ = false;
123 float boundX_ = 0.f, boundY_ = 0.f, boundW_ = 0.f, boundH_ = 0.f;
124
125 int grabIndex_ = -1;
126 float grabX_ = 0.f, grabY_ = 0.f;
127 float forceX_ = 0.f, forceY_ = 0.f;
128
129 float colorR_ = 0.75f, colorG_ = 0.82f, colorB_ = 0.95f, colorA_ = 1.f;
130
131 bool destroyed_ = false;
132
133 std::vector<Particle> particles_;
134 std::vector<Link> links_;
135};
136
137} // namespace eve::physics
int y
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
int h
int w
std::vector< Colorf > px
uint32_t a
uint32_t b
int spacing
int iterations
Definition TreeMesh.cpp:193
Interactive 2D cloth — Verlet particles + distance constraints. Pixel space (same convention as Box2D...
Definition Cloth.h:17
void setParticlePosition(int index, float x, float y)
Definition Cloth.cpp:178
void setBounds(float x, float y, float w, float h)
Axis-aligned walls; particles bounce inside. Disabled if w/h <= 0.
Definition Cloth.cpp:91
int getGrabIndex() const
Definition Cloth.h:67
void setStiffness(float stiffness)
Constraint relaxation strength in [0,1] (default 0.85).
Definition Cloth.cpp:79
int getCols() const
Definition Cloth.h:80
float getOriginX() const
Definition Cloth.h:88
float getColorA() const
Definition Cloth.h:76
float getDamping() const
Definition Cloth.h:48
void setDamping(float damping)
Damping applied to Verlet velocity [0,1] (default 0.01).
Definition Cloth.cpp:87
float getParticleX(int index) const
Definition Cloth.cpp:168
float getOriginY() const
Definition Cloth.h:89
int getRows() const
Definition Cloth.h:81
bool isPinned(int index) const
Definition Cloth.cpp:120
float getParticleY(int index) const
Definition Cloth.cpp:173
void setGravity(float gx, float gy)
Definition Cloth.cpp:74
float getColorG() const
Definition Cloth.h:74
int getParticleCount() const
Definition Cloth.h:82
void moveGrab(float x, float y)
Definition Cloth.cpp:143
void update(float dt)
Definition Cloth.cpp:273
float getGravityX() const
Definition Cloth.h:35
Cloth(const Cloth &)=delete
float getStiffness() const
Definition Cloth.h:40
void setColor(float r, float g, float b, float a=1.f)
Definition Cloth.cpp:161
bool isGrabbing() const
Definition Cloth.h:66
void pin(int index)
Definition Cloth.cpp:105
float getGravityY() const
Definition Cloth.h:36
void applyForce(float fx, float fy)
Definition Cloth.cpp:156
void draw(graphics::Graphics *gfx)
Definition Cloth.cpp:296
void setIterations(int iterations)
Constraint solver iterations per substep (default 4).
Definition Cloth.cpp:83
int getIterations() const
Definition Cloth.h:44
float getColorB() const
Definition Cloth.h:75
float getColorR() const
Definition Cloth.h:73
int grabAt(float x, float y, float radius=24.f)
Grab nearest free particle within radius (pixels). Returns particle index, or -1 if none.
Definition Cloth.cpp:125
Cloth & operator=(const Cloth &)=delete
void unpin(int index)
Definition Cloth.cpp:110
float getSpacing() const
Definition Cloth.h:87
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition AnimTrail.h:6