how to make magnet1 and magnet2 in a single applet with different rotation?

Hi everyone,

i have been successful in making magnet1 and magnet2 in a single applet if the have the same orientation.
http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=1602msg6056;topicseen#msg6056

i even made codes flexible enough to trouble shoot.

[code]// computes magnetic field components and magnitude from dipoles
// rename this getB to getBcomputeField because need to separate the calculation of Field from Compass
public double[] getBcomputeField(double x, double y) {
double[] b=new double[4];
for(int i=0; i<=nd; i++) { // lookang change i//    double dx=x-dipoles[i][0]-xMagnet;
//    double dy=y-dipoles[i][1]-yMagnet;
double dx=x-dipoles[i][0]-(xMagnet*cs+yMagnet*sc);//FKH
double dy=y-dipoles[i][1]-(-xMagnet*sc+yMagnet*cs);//FKH
//   double dx=x-dipoles[i][0]-dipoles2[i][0]-(xMagnet*cs+yMagnet*sc)-(xMagnet2*cs2+yMagnet2*sc2);//FKH
//    double dy=y-dipoles[i][1]-dipoles2[i][1]-(-xMagnet*sc+yMagnet*cs)-(-xMagnet2*sc2+yMagnet2*cs2);//FKH
double dx2=x-dipoles2[i][0]-(xMagnet2*cs2+yMagnet2*sc2);//FKH
double dy2=y-dipoles2[i][1]-(-xMagnet2*sc2+yMagnet2*cs2);//FKH

double r2=dx*dx+dy*dy;
double r=Math.sqrt(r2);
double r3=r2*r;
double cos=dx/r;
double sin=dy/r;

double r4=dx2*dx2+dy2*dy2;
double r22=Math.sqrt(r4);
double r33=r4*r22;
double cos2=dx2/r22;
double sin2=dy2/r22;

// taken from http://en.wikipedia.org/wiki/Magnetic_moment
if (magnet1==true&&magnet2==false)
{
b[0]+=m*(3*cos*cos-1)/r3;   //x component
b[1]+=m*3*sin*cos/r3;   //y component
}
else if (magnet2==true&&magnet1==false)
{
b[0]+=m*(3*cos2*cos2-1)/r33;   //x component
b[1]+=m*3*sin2*cos2/r33;   //y component
}

else if (magnet1==true&&magnet2==true)
{
b[0]+=m*(3*cos*cos-1)/r3 +m*(3*cos2*cos2-1)/r33;   //x component
b[1]+=m*3*sin*cos/r3 +m*3*sin2*cos2/r33;   //y component

}

else {
b[0]=0;
b[1]=0;
}

//   b[0]+=m*((3*cos*cos-1)*cs+(3*sin*cos)*sc)/r3;   //x component
//   b[1]+=m*((3*cos*cos-1)*(-cs)+(3*sin*cos)*sc)/r3;   //y component

if(r2==0||r4==0) {
b[0]=b[1]=0;
}
}
b[2] = Math.sqrt(b[0]*b[0]+b[1]*b[1]);  // magnitude
b[3] = Math.atan2(b[1],b[0]);  // angle
return b;
}

/*
Dipole field:
B=mu/4/pi/r^3[(m*rhat)rhat-m]         //vector form
K=mu*M/4/pi
alpha=0
Bx=K*[(3*cos(theta)^2-1)*cos(alpha)+3*sin(theta)cos(theta)*sin(alpha)]/r^3
By=K*[(3*sin(theta)^2-1)*sin(alpha)+3*sin(theta)cos(theta)*cos(alpha)]/r^3
*/
[/code]

and

[code]public void computeField () {
for (int i=0; i    double x=_view.vectorField2D.indexToX(i);
for (int j=0; j      double y=_view.vectorField2D.indexToY(j);
//double[] b=getB(x,y);//  Gets the components and magnitude

if (magnet1==false&&magnet2==false)
{
double[] b=getBcomputeField(x*cs+y*sc+ x*cs2+y*sc2,-x*sc+y*cs -x*sc2+y*cs2);//
xField[i][j] = 0;
yField[i][j] = 0;
magField[i][j]=b[2];
}
else if (magnet1==true)
{
double[] b=getBcomputeField(x*cs+y*sc,-x*sc+y*cs);//FKH
//xField[i][j] = b[0];
//yField[i][j] = b[1];

// old codes
xField[i][j] = b[0]*cs-b[1]*sc;//FKH
yField[i][j] = b[0]*sc+b[1]*cs;//FKH
magField[i][j]=b[2];
}

else if (magnet2==true)
{
double[] b=getBcomputeField(x*cs2+y*sc2,-x*sc2+y*cs2);//FKH
//xField[i][j] = b[0];
//yField[i][j] = b[1];

// old codes
xField[i][j] = b[0]*cs2-b[1]*sc2;//FKH
yField[i][j] = b[0]*sc2+b[1]*cs2;//FKH
magField[i][j]=b[2];
}

else {
double[] b=getBcomputeField(x*cs+y*sc+ x*cs2+y*sc2,-x*sc+y*cs -x*sc2+y*cs2); // likely the codes here wrong
xField[i][j] = b[0]*cs-b[1]*sc + b[0]*cs2-b[1]*sc2;//FKH
yField[i][j] = b[0]*sc+b[1]*cs + b[0]*sc2+b[1]*cs2;//FKH
magField[i][j]=b[2];
}

}
}
}[/code]

where did the codes i did wrong?,
independently, each magnet works on its own, but when both are turn on, the bug appears to be opposite the actual position of the magnet.