KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > physics > RigidBody2D


1 package JSci.physics;
2
3 import JSci.maths.NumericalConstants;
4
5 /**
6 * The RigidBody2D class provides an object for
7 * encapsulating rigid bodies that live in 2D.
8 * @version 1.0
9 * @author Mark Hale
10 */

11 public class RigidBody2D extends ClassicalParticle2D {
12         /**
13         * Moment of inertia.
14         */

15         protected double angMass;
16         /**
17         * Angle (orientation).
18         */

19         protected double ang;
20         /**
21         * Angular velocity.
22         */

23         protected double angVel;
24         /**
25         * Constructs a rigid body.
26         */

27         public RigidBody2D() {}
28         /**
29         * Sets the moment of inertia.
30         */

31         public void setMomentOfInertia(double MoI) {
32                 angMass=MoI;
33         }
34         /**
35         * Returns the moment of inertia.
36         */

37         public double getMomentOfInertia() {
38                 return angMass;
39         }
40         /**
41         * Sets the angle (orientation) of this body.
42         * @param angle an angle in radians.
43         */

44         public void setAngle(double angle) {
45                 ang=angle;
46         }
47         /**
48         * Returns the angle (orientation) of this body.
49         * @return an angle in radians.
50         */

51         public double getAngle() {
52                 return ang;
53         }
54         /**
55         * Sets the angular velocity.
56         */

57         public void setAngularVelocity(double angleVel) {
58                 angVel=angleVel;
59         }
60         /**
61         * Returns the angular velocity.
62         */

63         public double getAngularVelocity() {
64                 return angVel;
65         }
66         public void setAngularMomentum(double angleMom) {
67                 angVel=angleMom/angMass;
68         }
69         public double getAngularMomentum() {
70                 return angMass*angVel;
71         }
72         /**
73         * Returns the kinetic and rotational energy.
74         */

75         public double energy() {
76                 return (mass*(vx*vx+vy*vy)+angMass*angVel*angVel)/2.0;
77         }
78         /**
79         * Evolves this particle forward according to its kinematics.
80         * This method changes the particle's position and orientation.
81         * @return this.
82         */

83         public ClassicalParticle2D move(double dt) {
84                 return rotate(dt).translate(dt);
85         }
86         /**
87         * Evolves this particle forward according to its rotational kinematics.
88         * This method changes the particle's orientation.
89         * @return this.
90         */

91         public RigidBody2D rotate(double dt) {
92                 ang+=angVel*dt;
93                 if(ang>NumericalConstants.TWO_PI)
94                         ang-=NumericalConstants.TWO_PI;
95                 else if(ang<0.0)
96                         ang+=NumericalConstants.TWO_PI;
97                 return this;
98         }
99         /**
100         * Accelerates this particle.
101         * This method changes the particle's angular velocity.
102         * It is additive, that is <code>angularAccelerate(a1, dt).angularAccelerate(a2, dt)</code>
103         * is equivalent to <code>angularAccelerate(a1+a2, dt)</code>.
104         * @return this.
105         */

106         public RigidBody2D angularAccelerate(double a, double dt) {
107                 angVel += a*dt;
108                 return this;
109         }
110         /**
111         * Applies a torque to this particle.
112         * This method changes the particle's angular velocity.
113         * It is additive, that is <code>applyTorque(T1, dt).applyTorque(T2, dt)</code>
114         * is equivalent to <code>applyTorque(T1+T2, dt)</code>.
115         * @return this.
116         */

117         public RigidBody2D applyTorque(double T, double dt) {
118                 return angularAccelerate(T/angMass, dt);
119         }
120         /**
121         * Applies a force acting at a point away from the centre of mass.
122         * Any resultant torques are also applied.
123         * This method changes the particle's angular velocity.
124         * @param x x-coordinate from centre of mass.
125         * @param y y-coordinate from centre of mass.
126         * @return this.
127         */

128         public RigidBody2D applyForce(double fx, double fy, double x, double y, double dt) {
129                 applyTorque(x*fy-y*fx, dt); // T = r x F
130
final double k=(x*fx+y*fy)/(x*x+y*y); // r.F/|r|^2
131
applyForce(k*x, k*y, dt);
132                 return this;
133         }
134         /**
135         * Collides this particle with another.
136         * This method calculates the resultant velocities.
137         * @param theta centre of mass deflection angle.
138         * @param e coefficient of restitution.
139         * @return this.
140         */

141         public RigidBody2D collide(RigidBody2D p,double theta,double e) {
142                 final double totalMass = mass+p.mass;
143                 final double deltaVx = p.vx-vx;
144                 final double deltaVy = p.vy-vy;
145                 final double cos = Math.cos(theta);
146                 final double sin = Math.sin(theta);
147                 vx += p.mass*(e*(deltaVx*cos+deltaVy*sin)+deltaVx)/totalMass;
148                 vy += p.mass*(e*(deltaVy*cos-deltaVx*sin)+deltaVy)/totalMass;
149                 p.vx -= mass*(e*(deltaVx*cos+deltaVy*sin)+deltaVx)/totalMass;
150                 p.vy -= mass*(e*(deltaVy*cos-deltaVx*sin)+deltaVy)/totalMass;
151                 return this;
152         }
153         /**
154         * Collides this particle with another.
155         * This method calculates the resultant angular velocities.
156         * @param e coefficient of restitution.
157         * @return this.
158         */

159         public RigidBody2D angularCollide(RigidBody2D p,double e) {
160                 final double meanMass = (angMass+p.angMass)/(e+1.0);
161                 final double delta = p.angVel-angVel;
162                 angVel += p.angMass*delta/meanMass;
163                 p.angVel -= angMass*delta/meanMass;
164                 return this;
165         }
166 }
167
168
Popular Tags