Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > physics > RigidBody3D

 `1 package JSci.physics;2 3 import JSci.maths.NumericalConstants;4 5 /**6 * The RigidBody3D class provides an object for encapsulating rigid bodies that live in 3D.7 * @version 1.08 * @author Mark Hale9 */10 public class RigidBody3D extends ClassicalParticle3D {11         /**12         * Moment of inertia.13         */14         protected double angMass;15         /**16         * Angles (orientation).17         */18         protected double angx, angy, angz;19         /**20         * Angular velocity.21         */22         protected double angxVel, angyVel, angzVel;23         /**24         * Constructs a rigid body.25         */26         public RigidBody3D() {}27         /**28         * Sets the moment of inertia.29         */30         public void setMomentOfInertia(double MoI) {31                 angMass = MoI;32         }33         /**34         * Returns the moment of inertia.35         */36         public double getMomentOfInertia() {37                 return angMass;38         }39         /**40         * Sets the angles (orientation) of this body.41         * @param angleX an angle in radians.42         * @param angleY an angle in radians.43         * @param angleZ an angle in radians.44         */45         public void setAngles(double angleX, double angleY, double angleZ) {46                 angx = angleX;47                 angy = angleY;48                 angz = angleZ;49         }50         /**51         * Returns the x-axis angle of this body.52         * @return an angle in radians.53         */54         public double getXAngle() {55                 return angx;56         }57         /**58         * Returns the y-axis angle of this body.59         * @return an angle in radians.60         */61         public double getYAngle() {62                 return angy;63         }64         /**65         * Returns the z-axis angle of this body.66         * @return an angle in radians.67         */68         public double getZAngle() {69                 return angz;70         }71         public void setAngularVelocity(double angleXVel, double angleYVel, double angleZVel) {72                 angxVel = angleXVel;73                 angyVel = angleYVel;74                 angzVel = angleZVel;75         }76         public double getXAngularVelocity() {77                 return angxVel;78         }79         public double getYAngularVelocity() {80                 return angyVel;81         }82         public double getZAngularVelocity() {83                 return angzVel;84         }85         public void setAngularMomentum(double angleXMom, double angleYMom, double angleZMom) {86                 angxVel = angleXMom/angMass;87                 angyVel = angleYMom/angMass;88                 angzVel = angleZMom/angMass;89         }90         public double getXAngularMomentum() {91                 return angMass*angxVel;92         }93         public double getYAngularMomentum() {94                 return angMass*angyVel;95         }96         public double getZAngularMomentum() {97                 return angMass*angzVel;98         }99         /**100         * Returns the kinetic and rotational energy.101         */102         public double energy() {103                 return (mass*(vx*vx+vy*vy+vz*vz)+angMass*(angxVel*angxVel+angyVel*angyVel+angzVel*angzVel))/2.0;104         }105         /**106         * Evolves this particle forward according to its kinematics.107         * This method changes the particle's position and orientation.108         * @return this.109         */110         public ClassicalParticle3D move(double dt) {111                 return rotate(dt).translate(dt);112         }113         /**114         * Evolves this particle forward according to its rotational kinematics.115         * This method changes the particle's orientation.116         * @return this.117         */118         public RigidBody3D rotate(double dt) {119                 angx += angxVel*dt;120                 if(angx > NumericalConstants.TWO_PI)121                         angx -= NumericalConstants.TWO_PI;122                 else if(angx < 0.0)123                         angx += NumericalConstants.TWO_PI;124                 angy += angyVel*dt;125                 if(angy > NumericalConstants.TWO_PI)126                         angy -= NumericalConstants.TWO_PI;127                 else if(angy < 0.0)128                         angy += NumericalConstants.TWO_PI;129                 angz += angzVel*dt;130                 if(angz > NumericalConstants.TWO_PI)131                         angz -= NumericalConstants.TWO_PI;132                 else if(angz < 0.0)133                         angz += NumericalConstants.TWO_PI;134                 return this;135         }136         /**137         * Accelerates this particle.138         * This method changes the particle's angular velocity.139         * It is additive, that is angularAccelerate(a1, dt).angularAccelerate(a2, dt)140         * is equivalent to angularAccelerate(a1+a2, dt).141         * @return this.142         */143         public RigidBody3D angularAccelerate(double ax, double ay, double az, double dt) {144                 angxVel += ax*dt;145                 angyVel += ay*dt;146                 angzVel += az*dt;147                 return this;148         }149         /**150         * Applies a torque to this particle.151         * This method changes the particle's angular velocity.152         * It is additive, that is applyTorque(T1, dt).applyTorque(T2, dt)153         * is equivalent to applyTorque(T1+T2, dt).154         * @return this.155         */156         public RigidBody3D applyTorque(double tx, double ty, double tz, double dt) {157                 return angularAccelerate(tx/angMass, ty/angMass, tz/angMass, dt);158         }159         /**160         * Applies a force acting at a point away from the centre of mass.161         * Any resultant torques are also applied.162         * This method changes the particle's angular velocity.163         * @param x x-coordinate from centre of mass.164         * @param y y-coordinate from centre of mass.165         * @param z z-coordinate from centre of mass.166         * @return this.167         */168         public RigidBody3D applyForce(double fx, double fy, double fz, double x, double y, double z, double dt) {169                 applyTorque(y*fz-z*fy, z*fx-x*fz, x*fy-y*fx, dt); // T = r x F170 final double k=(x*fx+y*fy+z*fz)/(x*x+y*y+z*z); // r.F/|r|^2171 applyForce(k*x, k*y, k*z, dt);172                 return this;173         }174 }175 176 ` Popular Tags