Help with mass spectrometer program proton. (PVectors)

I'm trying to make a program that demonstrates a mass spectrometer but I am having trouble getting the calculations to work and getting the proton to move correctly. Any help would be greatly appreciated.

Main Class:

int alphaVal, alphaDelta;
int rectX, rectY;      // Position of square button
int rect2X, rect2Y;  // Position of rect2 button
int rectSize = 90;     // Diameter of rect
int rect2Size = 93;   // Diameter of rect2
color rectColor, rect2Color, baseColor;
color rectHighlight, rect2Highlight;
color currentColor;
boolean rectOver = false;
boolean rect2Over = false;
PFont f;
String velocity = "Enter Velocity: ";
float iVel;



void setup(){
size(1000, 800);
f = createFont("Arial",16,true); // STEP 2 Create Font
noStroke();
 alphaVal = 255; // initial value
 alphaDelta = 1; // rate of change
 frameRate(100); // slow f.r. down to pulse smoothly

rectColor = color(0);
  rectHighlight = color(51);
  rect2Color = color(255);
  rect2Highlight = color(204);
  baseColor = color(102);
  currentColor = baseColor;
  rect2X = width/2+rect2Size/2+10;
  rect2Y = height/2;
  rectX = width/2-rectSize-10;
  rectY = height/2-rectSize/2;
  ellipseMode(CENTER);

  //partice
   mouse = new PVector(width/2.,height/2);
  stroke(255,255,0);
  strokeWeight(1.2);
}

void mousePressed(){
  initialVel = new PVector(5, 0);
  particle = new Particle(new PVector(0, 114), initialVel, new PVector(), new PVector(), new PVector());
}//end mouse pressed

void keyPressed(){
    if (keyCode == BACKSPACE) {
      if (velocity.length() > 0) {
        velocity = velocity.substring(0, velocity.length()-1);
      }else if (keyCode == DELETE) {
        velocity = "";
      }else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
      velocity = velocity + key;
      iVel = Float.valueOf(velocity).floatValue();
      }
    }
}//end key pressed


void draw(){ 
  background(23, 100, 240);

  /*
  Draw the magnet field 
  */
  for(int x = -10; x < width; x++){
    for (int y = -10; y < height; y++){
       //fill(#E0DCDC);
       fill(255, 255, 255, alphaVal);
       //ellipse(x, y, 10, 10);
      // stroke(100);
      // line(x, y, x+5, y+5);
       pushMatrix();
       translate(x, y);
       rotate(radians(65));
       rect(0,0, 13, 2);

       popMatrix();
       //noStroke();
       y = y + 30;
    }
    x = x + 30;
  }

/*
Draw the text on the empty space on the background
*/
fill(23, 100, 250);
rect(0, 230, 450, 620); // cover square
//display board
textFont(f, 16);
fill(0);
text("Radius:", 50, 300);
text("Particle Type: Electron", 50, 350);
text("Magnetic Field: 2T", 50, 400);
String eField = "Electric Field: 220 000 000 N/C";
text(eField, 50, 450);
text(velocity, 50, 500);

/*
Draw the lines containing the magnetic field
*/
stroke(5);
fill(#FA3845);
rect(0, 0, 5, 90); // top top side line 1
rect(0, 135, 5, 90); // top bottom side line 2
rect(430, 225, 5, 574); // detection side line
fill(255, 204, 0);
rect(0, 220, 435, 5); // top bottom line
rect(0, 0, 435, 5); // top top line
noStroke();

if ((alphaVal == 0) || (alphaVal == 255)){
    alphaDelta = -alphaDelta;
}
alphaVal+= alphaDelta; 
 if (particle != null){
  particle.updatePos();
  particle.drawParticle();
  }

}

Particle Class:

int WIDTH = 700;
int HEIGHT = 700;
PVector mouse;
PVector initialVel;
float g = 9.8/49;
float mass = 1;
float q = -1;
PVector electricForce;
PVector magneticForce;
Particle particle;

class Particle {
  PVector pos;
  PVector vel;
  PVector acc;
  int charge;
  int mass;

  Particle() {
    pos = new PVector();
    vel = new PVector();
    acc = new PVector();
    electricForce = new PVector();
    magneticForce = new PVector();
  }

  Particle(PVector _pos,PVector _vel, PVector _acc, PVector _electricForce, PVector _magneticForce){
    pos = _pos;
    vel = _vel;
    acc = _acc;
    electricForce = _electricForce;
    magneticForce = _magneticForce;
  }

  void updatePos(){

    if(pos.x > 0 && pos.x < 435){
      acc.set(electricForce);    
     acc.add(vel.cross(magneticForce));
     acc.mult(charge/mass);
    }else{
      electricForce.set(0, 0);
      magneticForce.set(0,0);
    }
    //make net force pvector add all the force to it then add net force to acceleration.
  //  acc.set(0,g);
 //   acc.add(electricForce);
//    acc.add(magneticForce);
    vel.add(acc);
    pos.add(vel);
  }

  void drawParticle (){
    fill (255, 0, 0);
    ellipse (pos.x, pos.y, 5, 5);
  }
}
Sign In or Register to comment.