最后一次了,断断续续终于炫完了入门课,泪目
你应该修改的函数是:
• rope.cpp 中的Rope::rope(…)
• rope.cpp 中的void Rope::simulateEuler(…)
• rope.cpp 中的void Rope::simulateVerlet(…)
Rope::rope(…):
直接初始化完事
Rope::Rope(Vector2D start, Vector2D end, int num_nodes, float node_mass, float k, vector<int> pinned_nodes)
{
// TODO (Part 1): Create a rope starting at `start`, ending at `end`, and containing `num_nodes` nodes.
// Comment-in this part when you implement the constructor
// for (auto &i : pinned_nodes) {
// masses[i]->pinned = true;
// }
Vector2D step = (end - start) / (num_nodes + 1);
for (int i = 0; i < num_nodes + 2; i++) {
Mass* nodeCur = new Mass(start + i * step, node_mass, false);
this->masses.push_back(nodeCur);
if (i == 0)continue;
Spring* springCur = new Spring(this->masses[i - 1], this->masses[i], k);
this->springs.push_back(springCur);
}
for (auto& i : pinned_nodes) {
masses[i]->pinned = true;
}
}
void Rope::simulateEuler(…):
gravity变量怎么加似乎有争议,看到有直接加到F上的,也有直接加到a上的,这里我是直接加到F上
void Rope::simulateEuler(float delta_t, Vector2D gravity)
{
for (auto &s : springs)
{
// TODO (Part 2): Use Hooke's law to calculate the force on a node
Vector2D Vb2a = s->m2->position - s->m1->position;
double dis = Vb2a.norm();
Vb2a = Vb2a / dis;
Vector2D Fb2a = -s->k * Vb2a * (dis - s->rest_length);
Vector2D Fa2b = -Fb2a;
s->m1->forces += Fa2b;
s->m2->forces += Fb2a;
// 加上Dumping
Vector2D Vba = s->m2->velocity - s->m1->velocity;
Vector2D Fa = -0.05 * (Vb2a.x * Vba.x + Vb2a.y * Vba.y) * (s->m2->position - s->m1->position) / dis;
Vector2D Fb = -Fa;
s->m1->forces += Fb;
s->m2->forces += Fa;
}
for (auto &m : masses)
{
if (!m->pinned)
{
// TODO (Part 2): Add the force due to gravity, then compute the new velocity and position
// TODO (Part 2): Add global damping
m->forces += gravity;
Vector2D a = m->forces / m->mass;
m->velocity += a * delta_t;
// m->position += m->velocity * delta_t; //显式欧拉
Vector2D vplus = m->velocity + a * delta_t;
m->position += vplus * delta_t; //半隐式
}
// Reset all forces on each mass
m->forces = Vector2D(0, 0);
}
}
void Rope::simulateVerlet(…)
没有细讲,但是直接抄公式然后更新last_position就行
void Rope::simulateVerlet(float delta_t, Vector2D gravity)
{
for (auto &s : springs)
{
// TODO (Part 3): Simulate one timestep of the rope using explicit Verlet (solving constraints)
Vector2D Vb2a = s->m2->position - s->m1->position;
double dis = Vb2a.norm();
Vb2a = Vb2a / dis;
Vector2D Fb2a = -s->k * Vb2a * (dis - s->rest_length);
Vector2D Fa2b = -Fb2a;
s->m1->forces += Fa2b;
s->m2->forces += Fb2a;
}
for (auto &m : masses)
{
if (!m->pinned)
{
Vector2D temp_position = m->position;
// TODO (Part 3.1): Set the new position of the rope mass
// TODO (Part 4): Add global Verlet damping
m->forces += gravity;
Vector2D pos = m->position;
m->position += (1 - 0.00005) * (m->position - m->last_position) +
m->forces / m->mass * pow(delta_t, 2);
m->last_position = pos;
}
}
}