aboutsummaryrefslogtreecommitdiff
path: root/msd/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'msd/main.cpp')
-rwxr-xr-xmsd/main.cpp104
1 files changed, 57 insertions, 47 deletions
diff --git a/msd/main.cpp b/msd/main.cpp
index b01bc5e..a9aad8a 100755
--- a/msd/main.cpp
+++ b/msd/main.cpp
@@ -101,7 +101,7 @@ public:
protected:
// --------------------------------------------------------------- RESET
-
+// ----------------------------------------------------------------------
void m_reset()
{
t_int i;
@@ -121,6 +121,7 @@ protected:
}
// -------------------------------------------------------------- COMPUTE
+// -----------------------------------------------------------------------
void m_bang()
{
@@ -130,37 +131,37 @@ protected:
for (i=0; i<nb_link; i++) {
// compute link forces
- distance = link[i]->mass1->posX-link[i]->mass2->posX;
- F = link[i]->K1 * (distance - link[i]->longueur) ; // F = k1(x1-x2)
- F += link[i]->D1 * (distance - link[i]->distance_old) ; // F = F + D1(v1-v2)
+ distance = link[i]->mass1->posX-link[i]->mass2->posX; // L[n] = x1 - x2
+ F = link[i]->K1 * (distance - link[i]->longueur) ; // F[n] = k1 (L[n] - L[0])
+ F += link[i]->D1 * (distance - link[i]->distance_old) ; // F[n] = F[n] + D1 (L[n] - L[n-1])
if (distance != 0) {
- Fx = F * (link[i]->mass1->posX - link[i]->mass2->posX)/distance;
+ Fx = F * (link[i]->mass1->posX - link[i]->mass2->posX)/distance; // Fx = F * Lx[n]/L[n]
}
- link[i]->mass1->forceX -= Fx;
- link[i]->mass1->forceX -= link[i]->D2*link[i]->mass1->speedX;
- link[i]->mass2->forceX += Fx;
- link[i]->mass2->forceX -= link[i]->D2*link[i]->mass2->speedX;
- link[i]->distance_old = distance;
+ link[i]->mass1->forceX -= Fx; // Fx1[n] = -Fx
+ link[i]->mass1->forceX -= link[i]->D2*link[i]->mass1->speedX; // Fx1[n] = Fx1[n] - D2 * vx1[n-1]
+ link[i]->mass2->forceX += Fx; // Fx2[n] = Fx
+ link[i]->mass2->forceX -= link[i]->D2*link[i]->mass2->speedX; // Fx2[n] = Fx2[n] - D2 * vx2[n-1]
+ link[i]->distance_old = distance; // L[n-1] = L[n]
}
for (i=0; i<nb_mass; i++)
// compute new masses position only if mobile = 1
if (mass[i]->mobile == 1) {
- X_new = mass[i]->forceX * mass[i]->invM + 2*mass[i]->posX - mass[i]->posX2;
- mass[i]->posX2 = mass[i]->posX;
- mass[i]->posX = max(min(X_new,Xmax),Xmin); // x = x + v
- mass[i]->speedX = mass[i]->posX - mass[i]->posX2; // v = v + F/M
+ X_new = mass[i]->forceX * mass[i]->invM + 2*mass[i]->posX - mass[i]->posX2; // x[n] =Fx[n]/M+2x[n]-x[n-1]
+ mass[i]->posX2 = mass[i]->posX; // x[n-2] = x[n-1]
+ mass[i]->posX = max(min(X_new,Xmax),Xmin); // x[n-1] = x[n]
+ mass[i]->speedX = mass[i]->posX - mass[i]->posX2; // vx[n] = x[n] - x[n-1]
}
for (i=0; i<nb_mass; i++) {
// clear forces
mass[i]->out_forceX = mass[i]->forceX;
- mass[i]->forceX = 0;
+ mass[i]->forceX = 0; // Fx[n] = 0
}
}
// -------------------------------------------------------------- MASSES
-
+// ----------------------------------------------------------------------
void m_mass(int argc,t_atom *argv)
// add a mass
// Id, nbr, mobile, invM, speedX, posX, forceX
@@ -168,18 +169,18 @@ protected:
t_atom sortie[5], aux[2];
t_float M;
- mass[nb_mass] = new t_mass; // new pointer
- mass[nb_mass]->Id = GetSymbol(argv[0]); // ID
+ mass[nb_mass] = new t_mass; // new mass
+ mass[nb_mass]->Id = GetSymbol(argv[0]); // ID
mass[nb_mass]->mobile = GetInt(argv[1]); // mobile
if (GetFloat(argv[2])==0)
M=1;
else M = GetFloat(argv[2]);
- mass[nb_mass]->invM = 1/(M); // invM
- mass[nb_mass]->speedX = 0; // vx
- mass[nb_mass]->posX = GetFloat(argv[3]); // x
- mass[nb_mass]->posX2 = GetFloat(argv[3]); // x
- mass[nb_mass]->forceX = 0; // Fx
- mass[nb_mass]->nbr = id_mass; // id_nbr
+ mass[nb_mass]->invM = 1/(M); // 1/M
+ mass[nb_mass]->speedX = 0; // vx[n]
+ mass[nb_mass]->posX = GetFloat(argv[3]); // x[n]
+ mass[nb_mass]->posX2 = GetFloat(argv[3]); // x[n-1]
+ mass[nb_mass]->forceX = 0; // Fx[n]
+ mass[nb_mass]->nbr = id_mass; // id number
nb_mass++ ;
id_mass++;
nb_mass = min ( nb_max_mass -1, nb_mass );
@@ -252,6 +253,8 @@ protected:
t_int i,nb_link_delete=0;
t_atom sortie[5], aux[nb_link];
+
+ // Delete all associated links
for (i=0; i<nb_link;i++) {
if (link[i]->mass1->nbr == GetAInt(argv[0]) || link[i]->mass2->nbr == GetAInt(argv[0])) {
SetFloat((aux[nb_link_delete]),link[i]->nbr);
@@ -261,7 +264,8 @@ protected:
for (i=0; i<nb_link_delete;i++)
m_delete_link(1,&aux[i]);
-
+
+ // Delete mass
for (i=0; i<nb_mass;i++)
if (mass[i]->nbr == GetAInt(argv[0])) {
SetFloat((sortie[0]),mass[i]->nbr);
@@ -270,7 +274,7 @@ protected:
SetFloat((sortie[3]),1/mass[i]->invM);
SetFloat((sortie[4]),mass[i]->posX);
delete mass[i];
- mass[i] = mass[nb_mass-1];
+ mass[i] = mass[nb_mass-1]; // copy last mass instead
nb_mass--;
ToOutAnything(1,S_Mass_deleted,5,sortie);
break;
@@ -281,17 +285,18 @@ protected:
void m_Xmax(int argc,t_atom *argv)
{
- // set damping of link(s) named Id
+ // set X max
Xmax = GetFloat(argv[0]);
}
void m_Xmin(int argc,t_atom *argv)
{
- // set damping of link(s) named Id
+ // set X min
Xmin = GetFloat(argv[0]);
}
// -------------------------------------------------------------- LINKS
+// ---------------------------------------------------------------------
void m_link(int argc,t_atom *argv)
// add a link
@@ -300,20 +305,20 @@ protected:
t_atom sortie[7], aux[2];
t_int i;
- link[nb_link] = new t_link;
- link[nb_link]->Id = GetSymbol(argv[0]);
+ link[nb_link] = new t_link; // New pointer
+ link[nb_link]->Id = GetSymbol(argv[0]); // ID
for (i=0; i<nb_mass;i++)
- if (mass[i]->nbr==GetAInt(argv[1]))
+ if (mass[i]->nbr==GetAInt(argv[1])) // pointer to mass1
link[nb_link]->mass1 = mass[i];
- else if(mass[i]->nbr==GetAInt(argv[2]))
+ else if(mass[i]->nbr==GetAInt(argv[2])) // pointer to mass2
link[nb_link]->mass2 = mass[i];
- link[nb_link]->K1 = GetFloat(argv[3]);
- link[nb_link]->D1 = GetFloat(argv[4]);
- link[nb_link]->D2 = GetFloat(argv[5]);
- link[nb_link]->longx = link[nb_link]->mass1->posX - link[nb_link]->mass2->posX;
- link[nb_link]->longueur = link[nb_link]->longx ;
- link[nb_link]->nbr = id_link;
- link[nb_link]->distance_old = link[nb_link]->longueur;
+ link[nb_link]->K1 = GetFloat(argv[3]); // K1
+ link[nb_link]->D1 = GetFloat(argv[4]); // D1
+ link[nb_link]->D2 = GetFloat(argv[5]); // D2
+ link[nb_link]->longx = link[nb_link]->mass1->posX - link[nb_link]->mass2->posX; // Lx[0]
+ link[nb_link]->longueur = link[nb_link]->longx ;// L[0]
+ link[nb_link]->nbr = id_link; // id number
+ link[nb_link]->distance_old = link[nb_link]->longueur; // L[n-1]
nb_link++;
id_link++;
nb_link = min ( nb_max_link -1, nb_link );
@@ -356,7 +361,7 @@ protected:
SetSymbol((arglist[0]),GetSymbol(argv[0]));
SetFloat((arglist[1]),mass[imass1[i]]->nbr);
SetFloat((arglist[2]),mass[imass2[j]]->nbr);
- SetFloat((arglist[3]),GetInt(argv[3]));
+ SetFloat((arglist[3]),GetFloat(argv[3]));
SetFloat((arglist[4]),GetFloat(argv[4]));
SetFloat((arglist[5]),GetFloat(argv[5]));
m_link(6,arglist);
@@ -418,7 +423,7 @@ protected:
SetFloat((sortie[5]),link[i]->D1);
SetFloat((sortie[6]),link[i]->D2);
delete link[i];
- link[i]=link[nb_link-1];
+ link[i]=link[nb_link-1]; // copy last link instead
nb_link--;
ToOutAnything(1,S_Link_deleted,7,sortie);
break;
@@ -427,6 +432,7 @@ protected:
// -------------------------------------------------------------- GET
+// -------------------------------------------------------------------
void m_get(int argc,t_atom *argv)
// get attributes
@@ -595,7 +601,7 @@ protected:
}
void m_force_dumpl()
- // List of masses positions on first outlet
+ // List of masses forces on first outlet
{
t_atom sortie[nb_mass];
t_int i;
@@ -607,7 +613,7 @@ protected:
}
void m_info_dumpl()
- // List of masses positions on first outlet
+ // List of masses and links infos on first outlet
{
t_atom sortie[7];
t_int i;
@@ -636,16 +642,19 @@ protected:
// -------------------------------------------------------------- PROTECTED VARIABLES
+// -----------------------------------------------------------------------------------
- t_link * link[nb_max_link];
- t_mass * mass[nb_max_mass];
- t_float Xmin, Xmax, Ymin, Ymax;
+ t_link * link[nb_max_link]; // Pointer table on links
+ t_mass * mass[nb_max_mass]; // Pointer table on masses
+ t_float Xmin, Xmax; // Limit values
int nb_link, nb_mass, id_mass, id_link;
// -------------------------------------------------------------- SETUP
+// ---------------------------------------------------------------------
private:
+ // Static symbols
const static t_symbol *S_Reset;
const static t_symbol *S_Mass;
const static t_symbol *S_Link;
@@ -721,7 +730,8 @@ private:
FLEXT_CALLBACK_V(m_delete_link)
FLEXT_CALLBACK_V(m_delete_mass)
};
-
+// -------------------------------------------------------------- STATIC VARIABLES
+// -------------------------------------------------------------------------------
const t_symbol *msd::S_Reset = MakeSymbol("Reset");
const t_symbol *msd::S_Mass = MakeSymbol("Mass");