Guest User

Classes

a guest
Jan 20th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. public class velocity{
  2.   float delx, dely;
  3.  
  4.   //Constructor 1
  5.   public velocity(){}
  6.  
  7.   //Constructor 2
  8.   public velocity(float delx, float dely){
  9.     this.delx = delx;
  10.     this.dely = dely;
  11.   }
  12.  
  13.   //Mutators for xvelocity and y velocity
  14.   public float getdelx(){
  15.     return this.delx;
  16.   }
  17.  
  18.   public float getdely(){
  19.     return this.dely;
  20.   }
  21.  
  22.   //Accessors for xvelocity and y velocity
  23.   public void setdelx(float delx){
  24.     this.delx = delx;
  25.   }
  26.  
  27.   public void setdely(float dely){
  28.     this.dely = dely;
  29.   }
  30.  
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. public class particle{
  39.  
  40.   private float xpos , ypos, delx , dely,size, decay, mass;
  41.   color colour;
  42.  
  43.   //constructor 1
  44.   public particle(float x, float y){
  45.   this.xpos = x;
  46.   this.ypos = y;
  47.   }
  48.  
  49.   //constructor 2
  50.   public particle(float xpos, float ypos, float size, float delx, float dely, float mass){
  51.     this.xpos = xpos;
  52.     this.ypos = ypos;
  53.     this.size = size;
  54.     this.delx = delx;
  55.     this.dely = dely;
  56.     this.mass = mass;
  57.    
  58.   }
  59.  
  60.   //Mutators for size, x velocity y velocity and velocity vector
  61.   public void setsize(float size){
  62.     this.size = size;
  63.   }
  64.  
  65.   public void setDX(float delx){
  66.     this.delx = delx;
  67.   }
  68.  
  69.   public void setDY(float dely){
  70.     this.dely = dely;
  71.   }
  72.  
  73.  
  74.  
  75.  
  76.   //Accessors for size, x position, y position, x velocity and y velocity
  77.  
  78.   public float getsize(){
  79.     return this.size;
  80.   }
  81.  
  82.   public float getX(){
  83.     return this.xpos;
  84.   }
  85.  
  86.   public float getY(){
  87.     return this.ypos;
  88.   }
  89.    
  90.   public float getDX(){
  91.     return this.delx;
  92.   }
  93.  
  94.   public float getDY(){
  95.     return this.dely;
  96.   }
  97.  
  98.   public float getmass(){
  99.     return this.mass;
  100.   }
  101.  
  102.   public velocity getvel(){
  103.    
  104.     velocity v = new velocity(this.getDX(), this.getDY());
  105.     return v;
  106.   }
  107.    
  108.  
  109.  
  110.   //Functionality. Moving around, Bouncing off of walls, and basic display updates
  111.   public void move(){
  112.     this.xpos += this.delx;
  113.     this.ypos += this.dely;
  114.   }
  115.  
  116.   public void bounce(){
  117.     if((this.xpos - this.size/2.0) < 0 || (this.xpos + this.size/2.0) > width){
  118.       this.setDX(this.getDX()*-1);
  119.     }
  120.     if((this.ypos - this.size/2.0) < 0 || (this.ypos + this.size/2.0) > height){
  121.        this.setDY(this.getDY()*-1);
  122.     }
  123.   }
  124.  
  125.   public void update(particle[] elec){
  126.    
  127.     for(int i =0; i< elec.length; i++){
  128.      
  129.       if(this == elec[i]) continue;
  130.       if(dist(this.getX(),this.getY(),elec[i].getX(),elec[i].getY()) - this.getsize() <0){
  131.         collision(this, elec[i]);
  132.         //println(dist(this.getX(),this.getY(),elec[i].getX(),elec[i].getY()) - this.getsize()/2);
  133.       }
  134.     }
  135.     display();
  136.   }
  137.  
  138.   public void display(){
  139.     stroke(0);
  140.     fill(119,240,153);  
  141.     ellipse(this.xpos, this.ypos, this.size ,this.size);
  142.   }
  143. }
  144.  
  145.  
  146.  
  147. velocity rotate(velocity v, float angle){
  148.   float x = v.getdelx()*cos(angle) - v.getdely()*sin(angle);
  149.   float y = v.getdelx()*sin(angle) + v.getdely()*cos(angle);
  150.  
  151.   velocity vel = new velocity(x,y);
  152.   return vel;
  153. }
  154.  
  155.  
  156.  
  157. void collision(particle p1, particle p2){
  158.  
  159.   float xveldiff = p1.getDX()-p2.getDX();
  160.   float yveldiff = p1.getDY()-p2.getDY();
  161.  
  162.   float xdist = p2.getX() - p1.getX();
  163.   float ydist = p2.getY() - p1.getY();
  164.  
  165.   //Check for accidental overlaps of particles
  166.   if(xveldiff*xdist + yveldiff*ydist > 0){
  167.    
  168.     float angle = -atan2(p2.getY() - p1.getY(), p2.getX() - p1.getY());
  169.    
  170.     float m1 = p1.getmass();
  171.     float m2 = p2.getmass();
  172.    
  173.     velocity u1 = rotate(p1.getvel(),angle);
  174.     velocity u2 = rotate(p2.getvel(),angle);
  175.    
  176.     velocity v1 = new velocity(u1.getdelx() * (m1 - m2) / (m1 + m2) + u2.getdelx() * 2 * m2 / (m1 + m2), u1.getdely());
  177.     velocity v2 = new velocity(u2.getdelx() * (m1 - m2) / (m1 + m2) + u1.getdelx() * 2 * m2 / (m1 + m2), u2.getdely());
  178.    
  179.     velocity vf1 = rotate(v1, -angle);
  180.     velocity vf2 = rotate(v2, -angle);
  181.    
  182.     p1.setDX(vf1.getdelx());
  183.     p1.setDY(vf1.getdely());
  184.    
  185.     p2.setDX(vf2.getdelx());
  186.     p2.setDY(vf2.getdely());
  187.   }
  188.  
  189. }
Add Comment
Please, Sign In to add comment