KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > physics > ClassicalParticle2D


1 package JSci.physics;
2
3 import JSci.maths.vectors.AbstractDoubleVector;
4 import JSci.maths.vectors.Double2Vector;
5
6 /**
7 * The ClassicalParticle2D class provides an object for
8 * encapsulating classical point particles that live in 2D.
9 * @version 1.0
10 * @author Mark Hale
11 */

12 public class ClassicalParticle2D extends AbstractClassicalParticle {
13         /**
14         * Mass.
15         */

16         protected double mass;
17         /**
18         * Position coordinates.
19         */

20         protected double x, y;
21         /**
22         * Velocity coordinates.
23         */

24         protected double vx, vy;
25         /**
26         * Constructs a classical particle.
27         */

28         public ClassicalParticle2D() {}
29         /**
30         * Sets the mass of this particle.
31         */

32         public void setMass(double m) {
33                 mass=m;
34         }
35         /**
36         * Returns the mass of this particle.
37         */

38         public double getMass() {
39                 return mass;
40         }
41         /**
42         * Sets the position of this particle.
43         */

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         /**
64         * Sets the velocity of this particle.
65         */

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         /**
80         * Returns the speed of this particle.
81         */

82         public double speed() {
83                 return Math.sqrt(vx*vx+vy*vy);
84         }
85         /**
86         * Sets the momentum of this particle.
87         */

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         /**
102         * Returns the kinetic energy.
103         */

104         public double energy() {
105                 return mass*(vx*vx+vy*vy)/2.0;
106         }
107         /**
108         * Evolves this particle forward according to its kinematics.
109         * This method changes the particle's position.
110         * @return this.
111         */

112         public ClassicalParticle2D move(double dt) {
113                 return translate(dt);
114         }
115         /**
116         * Evolves this particle forward according to its linear kinematics.
117         * This method changes the particle's position.
118         * @return this.
119         */

120         public ClassicalParticle2D translate(double dt) {
121                 x+=vx*dt;
122                 y+=vy*dt;
123                 return this;
124         }
125         /**
126         * Accelerates this particle.
127         * This method changes the particle's velocity.
128         * It is additive, that is <code>accelerate(a1, dt).accelerate(a2, dt)</code>
129         * is equivalent to <code>accelerate(a1+a2, dt)</code>.
130         * @return this.
131         */

132         public ClassicalParticle2D accelerate(double ax,double ay,double dt) {
133                 vx+=ax*dt;
134                 vy+=ay*dt;
135                 return this;
136         }
137         /**
138         * Applies a force to this particle.
139         * This method changes the particle's velocity.
140         * It is additive, that is <code>applyForce(F1, dt).applyForce(F2, dt)</code>
141         * is equivalent to <code>applyForce(F1+F2, dt)</code>.
142         * @return this.
143         */

144         public ClassicalParticle2D applyForce(double Fx,double Fy,double dt) {
145                 return accelerate(Fx/mass, Fy/mass, dt);
146         }
147         /**
148         * Evolves two particles under their mutual gravitational attraction.
149         * This method changes the velocity of both particles.
150         * @return this.
151         */

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         /**
166         * Collides this particle with another (elastic collision).
167         * This method calculates the resultant velocities.
168         * @param theta centre of mass deflection angle.
169         * @return this.
170         */

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