1 package JSci.physics; 2 3 import JSci.maths.vectors.AbstractDoubleVector; 4 import JSci.maths.vectors.Double2Vector; 5 6 12 public class ClassicalParticle2D extends AbstractClassicalParticle { 13 16 protected double mass; 17 20 protected double x, y; 21 24 protected double vx, vy; 25 28 public ClassicalParticle2D() {} 29 32 public void setMass(double m) { 33 mass=m; 34 } 35 38 public double getMass() { 39 return mass; 40 } 41 44 public void setPosition(double xPos,double yPos) { 45 x=xPos; 46 y=yPos; 47 } 48 public AbstractDoubleVector getPosition() { 49 return new Double2Vector(x, y); 50 } 51 public void setXPosition(double xPos) { 52 x=xPos; 53 } 54 public double getXPosition() { 55 return x; 56 } 57 public void setYPosition(double yPos) { 58 y=yPos; 59 } 60 public double getYPosition() { 61 return y; 62 } 63 66 public void setVelocity(double xVel,double yVel) { 67 vx=xVel; 68 vy=yVel; 69 } 70 public AbstractDoubleVector getVelocity() { 71 return new Double2Vector(vx, vy); 72 } 73 public double getXVelocity() { 74 return vx; 75 } 76 public double getYVelocity() { 77 return vy; 78 } 79 82 public double speed() { 83 return Math.sqrt(vx*vx+vy*vy); 84 } 85 88 public void setMomentum(double xMom,double yMom) { 89 vx=xMom/mass; 90 vy=yMom/mass; 91 } 92 public AbstractDoubleVector getMomentum() { 93 return new Double2Vector(mass*vx, mass*vy); 94 } 95 public double getXMomentum() { 96 return mass*vx; 97 } 98 public double getYMomentum() { 99 return mass*vy; 100 } 101 104 public double energy() { 105 return mass*(vx*vx+vy*vy)/2.0; 106 } 107 112 public ClassicalParticle2D move(double dt) { 113 return translate(dt); 114 } 115 120 public ClassicalParticle2D translate(double dt) { 121 x+=vx*dt; 122 y+=vy*dt; 123 return this; 124 } 125 132 public ClassicalParticle2D accelerate(double ax,double ay,double dt) { 133 vx+=ax*dt; 134 vy+=ay*dt; 135 return this; 136 } 137 144 public ClassicalParticle2D applyForce(double Fx,double Fy,double dt) { 145 return accelerate(Fx/mass, Fy/mass, dt); 146 } 147 152 public ClassicalParticle2D gravitate(ClassicalParticle2D p,double dt) { 153 final double dx=p.x-x; 154 final double dy=p.y-y; 155 final double rr=dx*dx+dy*dy; 156 final double r=Math.sqrt(rr); 157 final double g=p.mass/rr; 158 final double pg=mass/rr; 159 vx-=g*dx*dt/r; 160 vy-=g*dy*dt/r; 161 p.vx+=pg*dx*dt/r; 162 p.vy+=pg*dy*dt/r; 163 return this; 164 } 165 171 public ClassicalParticle2D collide(ClassicalParticle2D p, double theta) { 172 final double totalMass = mass+p.mass; 173 final double deltaVx = p.vx-vx; 174 final double deltaVy = p.vy-vy; 175 final double cos = Math.cos(theta); 176 final double sin = Math.sin(theta); 177 vx += p.mass*(deltaVx*cos+deltaVy*sin+deltaVx)/totalMass; 178 vy += p.mass*(deltaVy*cos-deltaVx*sin+deltaVy)/totalMass; 179 p.vx -= mass*(deltaVx*cos+deltaVy*sin+deltaVx)/totalMass; 180 p.vy -= mass*(deltaVy*cos-deltaVx*sin+deltaVy)/totalMass; 181 return this; 182 } 183 } 184 | Popular Tags |