我指的是 A(x1,y1), B(x2,y2)。
4个声明的结构:
struct Complex
{
float x;
float y;
};
struct Particle
{
struct Complex P; //Position
struct Complex V; //Motion
struct Complex F; //Gravitational Force
struct Complex Fel; //Elastic force
float m; //which I will use later
};
struct Spring
{
float k; // k is a constant factor characteristic of the spring
float lo; //initial length
int a; // Spring has 2 points added to it(the actual spring), which stores the indice position for struct Particle T[MAX]
int b;
};
struct World
{
struct Particle T[MAX];
struct Spring S[MAX];
int nb; //number of particles
int rs; //number of springs
};
计算力程序:
void computeParticleForceSpring(struct World &w)
{
for(int g=0; g<w.rs; g++)
{
float D = Distance(w.T[w.S[g].a].P, w.T[w.S[g].b].P); //calculate distance between 2 points
struct Complex er1 = (w.T[w.S[g].b].P-w.T[w.S[g].a].P)*(1/D); // calculate unit vector(direction of elastic force for first particle
w.T[w.S[g].a].Fel = er1*w.S[g].k*(D-w.S[g].lo); // calculate elastic force of first particle
w.T[w.S[g].b].Fel = w.T[w.S[g].a].Fel*(-1); // calculate elastic force of second particle
}
}
改变粒子运动的程序:
void update(struct World &w, float dt) //where dt is like 0.0001
{
for(int g=0; g<w.nb; g++)
{
w.T[g].V = w.T[g].V + w.T[g].F + w.T[g].Fel;
w.T[g].P = w.T[g].P + w.T[g].V*dt;
}
}
我的问题是2点的线,由于所获得的运动而不断地压缩和延伸,我无法找到一种方法来找到无限制地停止压缩/延伸弹簧直到它之间的距离缓慢停止他们很好。
有没有办法绕过它,增加摩擦力,如果弹簧是惰性的,并且点移动了,那么这个过程又重新开始了吗?
SOLUTION:
我添加了
struct Complex er
到结构粒子以记住指向与另一个点相反方向的点的单位向量。
在computeParticleForceSpring过程中,我添加了:
w.T[w.S[g].a].er = er1*(-1);// to remember the unit vector of A
w.T[w.S[g].b].er = er1;// to remember the unit vector of B
在更新程序中,我添加了:
w.T[g].er = w.T[g].V*FRICTION;
w.T[g].V = w.T[g].V - w.T[g].er;