aboutsummaryrefslogtreecommitdiff
path: root/msd3D/main.cpp
diff options
context:
space:
mode:
authorN.N. <nimon@users.sourceforge.net>2005-04-20 15:33:15 +0000
committerN.N. <nimon@users.sourceforge.net>2005-04-20 15:33:15 +0000
commit23da1f7dc2d2c2984e4b391e69d7bbb26cca2430 (patch)
treee9adc97e6baf3c6d73fed20d298690b679a61d4c /msd3D/main.cpp
parent8f279ae3684cc3184a86752cf889b8c0ad7868da (diff)
MSD Version 0.02
svn path=/trunk/externals/nusmuk/; revision=2795
Diffstat (limited to 'msd3D/main.cpp')
-rwxr-xr-xmsd3D/main.cpp120
1 files changed, 61 insertions, 59 deletions
diff --git a/msd3D/main.cpp b/msd3D/main.cpp
index a8cc727..428a86b 100755
--- a/msd3D/main.cpp
+++ b/msd3D/main.cpp
@@ -106,7 +106,7 @@ public:
protected:
// --------------------------------------------------------------- RESET
-
+// ----------------------------------------------------------------------
void m_reset()
{
t_int i;
@@ -126,7 +126,7 @@ protected:
}
// -------------------------------------------------------------- COMPUTE
-
+// -----------------------------------------------------------------------
void m_bang()
{
t_float F=0,Fx=0,Fy=0, Fz=0,distance,vitesse, X_new, Y_new, Z_new;
@@ -135,44 +135,44 @@ protected:
for (i=0; i<nb_link; i++) {
// compute link forces
- distance = sqrt(pow(link[i]->mass1->posX-link[i]->mass2->posX,2) +
- pow(link[i]->mass1->posY-link[i]->mass2->posY,2) + pow(link[i]->mass1->posZ-link[i]->mass2->posZ,2));
- 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 = sqrt(pow(link[i]->mass1->posX-link[i]->mass2->posX,2) + pow(link[i]->mass1->posY
+ -link[i]->mass2->posY,2) + pow(link[i]->mass1->posZ-link[i]->mass2->posZ,2)); // L[n]
+ F = link[i]->K1 * (distance - link[i]->longueur) ; // F = K1(L[n] - L[0])
+ F += link[i]->D1 * (distance - link[i]->distance_old) ; // F = F + D1(L[n] - L[n-1])
if (distance != 0) {
- Fx = F * (link[i]->mass1->posX - link[i]->mass2->posX)/distance;
- Fy = F * (link[i]->mass1->posY - link[i]->mass2->posY)/distance;
- Fz = F * (link[i]->mass1->posZ - link[i]->mass2->posZ)/distance;
+ Fx = F * (link[i]->mass1->posX - link[i]->mass2->posX)/distance; // Fx[n] = F * Lx[n]/L[n]
+ Fy = F * (link[i]->mass1->posY - link[i]->mass2->posY)/distance; // Fy[n] = F * Ly[n]/L[n]
+ Fz = F * (link[i]->mass1->posZ - link[i]->mass2->posZ)/distance; // Fy[n] = F * Lz[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]->mass1->forceY -= Fy;
- link[i]->mass1->forceY -= link[i]->D2*link[i]->mass1->speedY;
- link[i]->mass2->forceY += Fy;
- link[i]->mass2->forceY -= link[i]->D2*link[i]->mass2->speedY;
- link[i]->mass1->forceZ -= Fz;
- link[i]->mass1->forceZ -= link[i]->D2*link[i]->mass1->speedZ;
- link[i]->mass2->forceZ += Fz;
- link[i]->mass2->forceZ -= link[i]->D2*link[i]->mass2->speedZ;
- 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]->mass1->forceY -= Fy; // Fy1[n] = -Fy
+ link[i]->mass1->forceY -= link[i]->D2*link[i]->mass1->speedY; // Fy1[n] = Fy1[n] - D2 * vy1[n-1]
+ link[i]->mass2->forceY += Fy; // Fy2[n] = Fy
+ link[i]->mass2->forceY -= link[i]->D2*link[i]->mass2->speedY; // Fy2[n] = Fy2[n] - D2 * vy2[n-1]
+ link[i]->mass1->forceZ -= Fz; // Fz1[n] = -Fz
+ link[i]->mass1->forceZ -= link[i]->D2*link[i]->mass1->speedZ; // Fz1[n] = Fz1[n] - D2 * vz1[n-1]
+ link[i]->mass2->forceZ += Fz; // Fz2[n] = Fz
+ link[i]->mass2->forceZ -= link[i]->D2*link[i]->mass2->speedZ; // Fz2[n] = Fz2[n] - D2 * vz2[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_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]
- Y_new = mass[i]->forceY * mass[i]->invM + 2*mass[i]->posY - mass[i]->posY2;
- mass[i]->posY2 = mass[i]->posY;
- mass[i]->posY = max(min(Y_new,Ymax),Ymin);
+ Y_new = mass[i]->forceY * mass[i]->invM + 2*mass[i]->posY - mass[i]->posY2; // y[n] =Fy[n]/M+2y[n]-y[n-1]
+ mass[i]->posY2 = mass[i]->posY; // y[n-2] = y[n-1]
+ mass[i]->posY = max(min(Y_new,Ymax),Ymin); // y[n-1] = y[n]
mass[i]->speedY = mass[i]->posY - mass[i]->posY2; // vy[n] = y[n] - y[n-1]
- Z_new = mass[i]->forceZ * mass[i]->invM + 2*mass[i]->posZ - mass[i]->posZ2;
- mass[i]->posZ2 = mass[i]->posZ;
- mass[i]->posZ = max(min(Z_new,Zmax),Zmin);
+ Z_new = mass[i]->forceZ * mass[i]->invM + 2*mass[i]->posZ - mass[i]->posZ2; // x[n] =Fx[n]/M+2x[n]-x[n-1]
+ mass[i]->posZ2 = mass[i]->posZ; // z[n-2] = z[n-1]
+ mass[i]->posZ = max(min(Z_new,Zmax),Zmin); // z[n-1] = z[n]
mass[i]->speedZ = mass[i]->posZ - mass[i]->posZ2; // vz[n] = z[n] - z[n-1]
}
@@ -188,7 +188,7 @@ protected:
}
// -------------------------------------------------------------- MASSES
-
+// ----------------------------------------------------------------------
void m_mass(int argc,t_atom *argv)
// add a mass
// Id, nbr, mobile, invM, speedX, posX, forceX
@@ -196,26 +196,26 @@ protected:
t_atom sortie[7], 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]->invM = 1/(M); // 1/M
mass[nb_mass]->speedX = 0; // vx[n]
- mass[nb_mass]->posX = GetFloat(argv[3]); // x(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]->speedY = 0; // vy[n]
- mass[nb_mass]->posY = GetFloat(argv[4]); // y[n]
+ mass[nb_mass]->posY = GetFloat(argv[4]); // y[n]
mass[nb_mass]->posY2 = GetFloat(argv[4]); // y[n-1]
mass[nb_mass]->forceY = 0; // Fz[n]
mass[nb_mass]->speedZ = 0; // vz[n]
- mass[nb_mass]->posZ = GetFloat(argv[5]); // z[n]
+ mass[nb_mass]->posZ = GetFloat(argv[5]); // z[n]
mass[nb_mass]->posZ2 = GetFloat(argv[5]); // z[n-1]
mass[nb_mass]->forceZ = 0; // Fz[n]
- mass[nb_mass]->nbr = id_mass; // id_nbr
+ mass[nb_mass]->nbr = id_mass; // id number
nb_mass++ ;
id_mass++;
nb_mass = min ( nb_max_mass -1, nb_mass );
@@ -346,6 +346,7 @@ protected:
t_int i,nb_link_delete=0;
t_atom sortie[7], aux[nb_link];
+ // Delete associated links
for (i=0; i<nb_link;i++) {
if (link[i]->mass1->nbr == GetInt(argv[0]) || link[i]->mass2->nbr == GetInt(argv[0])) {
SetFloat((aux[nb_link_delete]),link[i]->nbr);
@@ -356,7 +357,7 @@ 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 == GetInt(argv[0])) {
SetFloat((sortie[0]),mass[i]->nbr);
@@ -367,7 +368,7 @@ protected:
SetFloat((sortie[5]),mass[i]->posY);
SetFloat((sortie[6]),mass[i]->posZ);
delete mass[i];
- mass[i] = mass[nb_mass-1];
+ mass[i] = mass[nb_mass-1]; // copy last mass indeed
nb_mass--;
ToOutAnything(1,S_Mass_deleted,7,sortie);
break;
@@ -410,8 +411,9 @@ protected:
// set minimum Z of all masses
Zmin = GetFloat(argv[0]);
}
-// -------------------------------------------------------------- LINKS
+// -------------------------------------------------------------- LINKS
+// ---------------------------------------------------------------------
void m_link(int argc,t_atom *argv)
// add a link
// Id, nbr, *mass1, *mass2, K1, D1
@@ -419,21 +421,21 @@ 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 link
+ link[nb_link]->Id = GetSymbol(argv[0]); // ID
for (i=0; i<nb_mass;i++)
- if (mass[i]->nbr==GetInt(argv[1]))
+ if (mass[i]->nbr==GetInt(argv[1])) // pointer on mass 1
link[nb_link]->mass1 = mass[i];
- else if(mass[i]->nbr==GetInt(argv[2]))
+ else if(mass[i]->nbr==GetInt(argv[2])) // pointer on mass 2
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]->longy = link[nb_link]->mass1->posY - link[nb_link]->mass2->posY;
- link[nb_link]->longz = link[nb_link]->mass1->posZ - link[nb_link]->mass2->posZ;
+ 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]->longy = link[nb_link]->mass1->posY - link[nb_link]->mass2->posY; // Ly[0]
+ link[nb_link]->longz = link[nb_link]->mass1->posZ - link[nb_link]->mass2->posZ; // Lz[0]
link[nb_link]->longueur = sqrt( pow(link[nb_link]->longx,2) + pow(link[nb_link]->longy,2) + pow(link[nb_link]->longz,2));
- link[nb_link]->nbr = id_link;
+ link[nb_link]->nbr = id_link; // id number
link[nb_link]->distance_old = link[nb_link]->longueur;
nb_link++;
id_link++;
@@ -500,7 +502,7 @@ protected:
void m_setD(int argc,t_atom *argv)
{
- // set damping of link(s) named Id
+ // set viscosity of link(s) named Id
t_int i;
const t_symbol *sym = GetSymbol(argv[0]);
@@ -540,16 +542,15 @@ 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
nb_link--;
ToOutAnything(1,S_Link_deleted,7,sortie);
break;
}
}
-
// -------------------------------------------------------------- GET
-
+// -------------------------------------------------------------------
void m_get(int argc,t_atom *argv)
// get attributes
{
@@ -794,16 +795,17 @@ protected:
}
// -------------------------------------------------------------- GLOBAL VARIABLES
-
+// --------------------------------------------------------------------------------
t_link * link[nb_max_link];
t_mass * mass[nb_max_mass];
t_float Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
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;