HomeWork 8:质点弹簧系统
这节课比较简单,都是套公式就行;
有个点得提一下,一开始sb了,在src里面间的build文件夹,然后cmake,结果一直报错…
实际上是应该在上一级建的…(也就是 build 和 src 文件夹同一级)
知识点参考的课程:
【Games 101】Lec 21:动画
rope.cpp
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
Rope::Rope(Vector2D start, Vector2D end, int num_nodes, float node_mass, float k, vector<int> pinned_nodes) { for (int i = 0; i < num_nodes; ++i) { Vector2D pos = start + (end - start) * ((double)i / ((double)num_nodes - 1.0)); masses.push_back(new Mass(pos, node_mass, false)); }
for (int i = 0; i < num_nodes - 1; ++i) { springs.push_back(new Spring(masses[i], masses[i + 1], k)); } for (auto &i : pinned_nodes) { masses[i]->pinned = true; } }
void Rope::simulateEuler(float delta_t, Vector2D gravity) { for (auto &s : springs) { double len = (s->m1->position - s->m2->position).norm(); s->m1->forces += -(s->k) * (s->m1->position - s->m2->position) / len * (len - s->rest_length); s->m2->forces += -(s->k) * (s->m2->position - s->m1->position) / len * (len - s->rest_length); }
for (auto &m : masses) { if (!m->pinned) {
m->forces += gravity * m->mass;
float k_d = 0.005; Vector2D f_d = -k_d * m->velocity;
m->forces += f_d;
Vector2D a = m->forces / m->mass;
m->velocity += a * delta_t; m->position += m->velocity * delta_t; }
m->forces = Vector2D(0, 0); } }
void Rope::simulateVerlet(float delta_t, Vector2D gravity) { for (auto &s : springs) { double len = (s->m1->position - s->m2->position).norm(); s->m1->forces += -(s->k) * (s->m1->position - s->m2->position) / len * (len - s->rest_length); s->m2->forces += -(s->k) * (s->m2->position - s->m1->position) / len * (len - s->rest_length); }
for (auto &m : masses) { if (!m->pinned) {
m->forces += gravity * m->mass; Vector2D a = m->forces/m->mass;
Vector2D lastPosition = m->position;
float damping_factor = 0.00005f;
m->position += (1 - damping_factor) * (m->position - m->last_position + a * delta_t * delta_t); m->last_position = lastPosition; } m->forces = Vector2D(0, 0); } }
|
结果: