How to use classes/objects "correctly"?

I just started using processing, tried learning programming a long time ago but gave up because it was too hard but processing seems a little bit easier.

I made this project with 3 balls in gravity, and although it works I feel like I'm doing something wrong. I made a class called ball, but not sure how to use it. I want to be able to add an arbitrary amount of balls, maybe a loop in the newton function that makes all balls "shake hands" with each other?

`

    Ball rb = new Ball(#ff0000, 10, 10, 1, 25);
    Ball gb = new Ball(#00ff00, 690, 690, 2, 25);
    Ball bb = new Ball(#0000ff, 350, 350, 7, 25);
    float screenx=700; 
    float screeny=700;
    // zooming and panning
    float wt=0; // counter of scroll wheel ticks, for zoom factor calculation
    float zoom=1.0; // final zoom-factor
    float panx; //panning x
    float pany; //panning y
    float camx; //final x position of camera
    float camy; //final x position of camera
    float mxs; // mouse x-coordinate speed
    float mys; // mouse y-coordinate speed
    void setup() {
      size(700, 700);
    }
    void draw() {
      background(65);
      textSize(18);
      fill(#ffffff);
      stroke(85);
      // Lines drawing grid cells
      for (int i = 1; i < 8/zoom; i++) {
        line((-panx*zoom%100)+100*zoom*i, 0, (-panx*zoom%100)+100*zoom*i, screeny*zoom); 
        line(0, (-pany*zoom%100)+100*zoom*i, screenx*zoom, (-pany*zoom%100)+100*zoom*i);
      }
      // lines from 0,0 point to balls
      line(camx, camy, camx+(panx+350)*zoom, camy+(pany+350)*zoom);
      stroke(rb.c);
      line(camx, camy, camx+rb.x*zoom, camy+rb.y*zoom);
      stroke(gb.c);
      line(camx, camy, camx+gb.x*zoom, camy+gb.y*zoom); 
      stroke(bb.c);
      line(camx, camy, camx+bb.x*zoom, camy+bb.y*zoom);
      mxs=(mouseX-pmouseX); // store mouse speed x
      mys=(mouseY-pmouseY); // store mouse speed y
      // start the ball function for all balls
      rb.go();
      gb.go();
      bb.go();
      newton();
      if (keyPressed) {
        if (key == 'g' || key == 'G') {
          stop();
        } else if (key == 'r' || key == 'R') {
          reset();
        }
      }
    }
    void newton() {
      float dx;//delta X
      float dy;//delta Y
      float delta;//delta X²+Y²
      float d;//distance
      float ra;//red acceleration
      float ga;//green acceleration
      float rs;//red compund speed
      float gs;// green compund speed
      float bs;// blue compund speed
      float ba;//blue acceleration
      float G=0.05; // "gravity constant"
      float rel; // relative speed of balls + radiuses
      //red and green ball
      dx=(rb.x-gb.x)*G;
      dy=(rb.y-gb.y)*G;
      delta=sq(dx)+sq(dy);
      d=sqrt(delta);
      ra=abs((gb.m/(delta))/d);
      ga=abs((rb.m/(delta))/d);
      rs=sqrt(sq(rb.xs)+sq(rb.ys));
      gs=sqrt(sq(gb.xs)+sq(gb.ys));
      rel = rs+ra+rb.r+gs+ga+gb.r;
      if (d/G<rel) {
        stop();
      } else {
        rb.xs=rb.xs-(dx*ra);
        rb.ys=rb.ys-(dy*ra);
        gb.xs=gb.xs+(dx*ga);
        gb.ys=gb.ys+(dy*ga);
      }
      //repeating for red and blue ball
      dx=(rb.x-bb.x)*G;
      dy=(rb.y-bb.y)*G;
      delta=sq(dx)+sq(dy);
      d=sqrt(delta);
      ra=abs((bb.m/(delta))/d);
      ba=abs((rb.m/(delta))/d);
      rs=sqrt(sq(rb.xs)+sq(rb.ys));
      bs=sqrt(sq(bb.xs)+sq(bb.ys));
      rel = rs+ra+rb.r+bs+ba+bb.r;
      if (d/G<rel) {
        stop();
      } else {
        rb.xs=rb.xs-(dx*ra);
        rb.ys=rb.ys-(dy*ra);
        bb.xs=bb.xs+(dx*ba);
        bb.ys=bb.ys+(dy*ba);
      }
      //and yet again for green and blue ball
      dx=(bb.x-gb.x)*G;
      dy=(bb.y-gb.y)*G;
      delta=sq(dx)+sq(dy);
      d=sqrt(delta);
      ba=abs((gb.m/(delta))/d);
      ga=abs((bb.m/(delta))/d);
      gs=sqrt(sq(gb.xs)+sq(gb.ys));
      bs=sqrt(sq(bb.xs)+sq(bb.ys));
      rel = gs+ga+gb.r+bs+ba+bb.r;
      if (d/G<rel) {
        stop();
      } else {
        bb.xs=bb.xs-(dx*ba);
        bb.ys=bb.ys-(dy*ba);
        gb.xs=gb.xs+(dx*ga);
        gb.ys=gb.ys+(dy*ga);
      }
    } // end of newton function
    void stop() {
      rb.xs=0;
      rb.ys=0;
      gb.xs=0;
      gb.ys=0;
      bb.xs=0;
      bb.ys=0;
    }
    void reset() {
      rb.xs=0;
      rb.ys=0;
      gb.xs=0;
      gb.ys=0;
      bb.xs=0;
      bb.ys=0;
      rb.x=10;
      rb.y=10;
      gb.x=690;
      gb.y=690;
      bb.x=350;
      bb.y=350;
      rb.m=1;
      gb.m=2;
      bb.m=7;
      rb.r=20;
      gb.r=20;
      bb.r=60;
      zoom=1.0;
      wt=0;
      camx=0.0;
      camy=0.0;
      panx=0.0;
      panx=0.0;
    }
    void mouseWheel(MouseEvent event) {
      wt-=event.getCount(); // wt = wheel ticks
      if (wt==0) {
        zoom=1.0;
      } else if (wt>=1 && wt<=3) {
        zoom=pow(2, wt);
      } else if (wt<=-1 && wt>=-3) {
        zoom=1/pow(2, abs(wt));
      }
      camx=-zoom*panx-(zoom-1)*mouseX;
      camy=-zoom*pany-(zoom-1)*mouseY;
      screenx=700/zoom;
      screeny=700/zoom;
    }
    void mouseDragged(MouseEvent event) {
      if (rb.fm==false && gb.fm==false && bb.fm==false) {
        panx-=(mouseX-pmouseX)/zoom;
        pany-=(mouseY-pmouseY)/zoom;
        camx+=(mouseX-pmouseX);
        camy+=(mouseY-pmouseY);
      }
    }
    void mousePressed() {
      if (rb.mob) {
        rb.fm=true;
      } // can you move this inside the Ball object?
      else if (gb.mob) {
        gb.fm=true;
      } // I tried if(mousePressed) rather than mousepressed()
      else if (bb.mob) {
        bb.fm=true;
      } // but it was buggy
    }
    void mouseReleased() {
      // can you check or change parameter of all instances of Ball, in one go?
      if (rb.fm) {
        rb.release();
        rb.fm=false;
        ;
      } else if (gb.fm) {
        gb.release();
        gb.fm=false;
      } else if (bb.fm) {
        bb.release();
        bb.fm=false;
      }
    }
    class Ball {
      float r;  // radius
      color c;  // color
      float m;  // mass
      float x;  // x-coordinate
      float y;  // y-coordinate
      float xs; // x-coordinate speed
      float ys; // y-coordinate speed
      boolean mob; // mouse on ball flag
      boolean fm; // follow mouse flag
      Ball(color tempC, int tempX, int tempY, float tempM, float tempR) {
        x=tempX;
        y=tempY;
        c=tempC;
        m=tempM;
        r=tempR;
      }
      void go() {
        x=x+xs;
        y=y+ys;
        fill(c);
        ellipse(x*zoom+camx, y*zoom+camy, r*2*zoom, r*2*zoom);
        textSize((r*5/3)*zoom);
        fill(#ffffff);
        text(str(int(m)), (x-r/2)*zoom+camx, (y+r/2)*zoom+camy);
        if (sqrt(sq(mouseX-(x*zoom+camx))+sq(mouseY-(y*zoom+camy)))<=r*zoom) {
          mob=true;
        } else {
          mob=false;
        }
        if (fm) {
          x=(mouseX-camx)/zoom;
          y=(mouseY-camy)/zoom;
        }
        if (keyPressed) {
          if (0<int(str(key))&&mob)
          {
            m=sq(int(str(key)))*10-9;
            r=pow(3*(m/(4*PI)), 0.333)*40; // radius for volume ass mass conversion
          }
        }
      }
      void release() {
        xs = mxs;
        ys = mys;
      }
    } // end of class ball

`

Answers

Sign In or Register to comment.