SFML-cloth-simulation

SFML cloth simulation

使用 粒子弹簧模型 进行 运动学模拟

理论公式

位置 Verlet 积分

选用 Verlet 位置更新公式

Verlet 积分:

公式推导

泰勒展开法

更新质点状态

  1. 假设粒子运动状态

    • 粒子的位置为
    • 速度为
    • 加速度为
    • 以上三项公式都仅依赖于 位置
  2. 取时间步长为 ,分别在 时刻前后作三阶泰勒展开

  3. 将上述两式相加

    • 将上式进行整理 得到上述公式
    • 可见,该式表示,当前的位置仅依赖于上一时刻的位置,以及当前的加速度
    • 若需要速度,可是使用 中心差分估计

点到线段距离

连线破坏事件

  • 使用 鼠标左键点击事件 确定将要被破坏的连线
  • 需要计算事件发生时光标位置与连线间的最短距离

公式推导

  • 假设计算 点P 到 线段AB 的距离
1
2
3
4
        P.
|
|
A--------+------------B
投影公式
  1. 投影长度计算

      • 余弦公式
    • 将余弦公式带入后可得
  2. 投影参数 t (投影相对于AB的长度)

    • 带入后可得
  3. 计算A到投影点的向量

    选择A是由于在上述公式推导中,是以 AB AP 向量为基础进行推导的
    同时,在计算后可以知道,参数t是 向量 相对于 AB向量 的方向与大小

      • 更靠近A侧
      • return
      • 更靠近B侧
      • return
      • 在线段AB上
      • return 这时 就是P到AB的垂段

类模型

质点 Particle

字段

1
2
3
4
5
6
7
8
9
class Particle
{
private:
sf::Vector2f position; // 当前位置
sf::Vector2f old_position; // 上一位置
sf::Vector2f acceleration; // 加速度
bool pinned; // 是否固定

};

关键方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

// 将力应用到质点
void Particle::applyForce(const sf::Vector2f& force) {
// 由于此处是点并未设计质量字段 将质量记为 1.f
acceleration += force;
}

// 使用 Verlet 积分 更新质点的位置
// Verlet 积分: x(t + dt) = x(t) + (x(t) - x(t - dt)) + a(t)dt^2
void Particle::update(float dt) {
// 更新未固定质点
if (pinned)
return;
sf::Vector2f diff = position - old_position;
old_position = position;
position += diff + acceleration * std::pow(dt, 2.f);
acceleration = sf::Vector2f(0.f, 0.f);
}

// 约束质点在边界限定内
void Particle::constraintToBound(float width, float height) {
if (position.x < 0) {
position.x = 0;
}
if (position.x > width) {
position.x = width;
}
if (position.y < 0) {
position.y = 0;
}
if (position.y > height) {
position.y = height;
}
}

弹簧约束 Spring Constraint

字段

1
2
3
4
5
6
7
8
class SpringConstraint
{
private:
Particle *p1; // 质点1
Particle *p2; // 质点2
float initial_length; // 两质点间的初始距离
bool active; // 是否存在连接
};

关键方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 使模拟满足约束
void SpringConstraint::satisfyConstraint() {
if (!active)
return;

sf::Vector2f delta = p2->getPosition() - p1->getPosition();
// 两质点间的距离
float current_length = std::hypot(delta.x, delta.y);
// 除零错误
if (current_length == 0.f)
return;
// 向质点两方向恢复
float diff = (current_length - initial_length) / current_length;
sf::Vector2f correction = delta * 0.5f * diff;
if (!p1->isPinned())
p1->setPosition(p1->getPosition() + correction);
if (!p2->isPinned())
p2->setPosition(p2->getPosition() - correction);
}

布料 Cloth

字段

1
2
3
4
5
6
7
8
9
10
11
12
class Cloth
{
std::vector<Particle*> particles; // 质点
std::vector<SpringConstraint*> springs; // 弹簧约束
sf::Vector2f position; // 左上角位置
int cols; // 列数
int rows; // 行数
float spacing; // 质点间的间距
// 质点限制
float constraint_width; // 质点位置约束 width
float constraint_height; // 质点位置约束 height
};

关键方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Cloth::Cloth(sf::Vector2f pos,
int cols_, int rows_, float spacing_,
float c_width, float c_heigth)
: position(pos)
, cols(cols_), rows(rows_), spacing(spacing_)
, constraint_width(c_width), constraint_height(c_heigth)
{
particles.reserve(cols * rows);
// 初始化质点
for (int y = 0; y < rows; ++y) {
for (int x = 0; x < cols; ++x) {
float px = position.x + x * spacing;
float py = position.y + y * spacing;
auto particle = Particle(sf::Vector2f(px, py));
// 固定布料顶部
if (y == 0)
particle.setPinned(true);
particles.push_back(particle);
}
}
// 创建弹簧约束
for (int y = 0; y < rows; ++y) {
for (int x = 0; x < cols; ++x) {
// 横向约束连接
if (x < cols - 1) {
springs.emplace_back(
&particles[y * cols + x], &particles[y * cols + x + 1]);
}
// 纵向约束连接
if (y < rows - 1) {
springs.emplace_back(
&particles[y * cols + x], &particles[(y + 1) * cols + x]);
}
}
}
}


// 为所有质点应用附加力
void applyForceToAll(const sf::Vector2f& force,
float dt, int constraint_iterations)
{
for (auto &p : particles) {
// 力
p.applyForce(force);
// 更新
p.update(dt);
// 限制质点位置范围
p.constraintToBound(constraint_width, constraint_height);
}
// 使质点满足约束
for (int itr = 0; itr < constraint_iterations; ++itr) {
for (auto &s : springs) {
s.satisfyConstraint();
}
}
}