1 package JSci.physics; 2 3 import JSci.maths.NumericalConstants; 4 5 11 public class RigidBody2D extends ClassicalParticle2D { 12 15 protected double angMass; 16 19 protected double ang; 20 23 protected double angVel; 24 27 public RigidBody2D() {} 28 31 public void setMomentOfInertia(double MoI) { 32 angMass=MoI; 33 } 34 37 public double getMomentOfInertia() { 38 return angMass; 39 } 40 44 public void setAngle(double angle) { 45 ang=angle; 46 } 47 51 public double getAngle() { 52 return ang; 53 } 54 57 public void setAngularVelocity(double angleVel) { 58 angVel=angleVel; 59 } 60 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 75 public double energy() { 76 return (mass*(vx*vx+vy*vy)+angMass*angVel*angVel)/2.0; 77 } 78 83 public ClassicalParticle2D move(double dt) { 84 return rotate(dt).translate(dt); 85 } 86 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 106 public RigidBody2D angularAccelerate(double a, double dt) { 107 angVel += a*dt; 108 return this; 109 } 110 117 public RigidBody2D applyTorque(double T, double dt) { 118 return angularAccelerate(T/angMass, dt); 119 } 120 128 public RigidBody2D applyForce(double fx, double fy, double x, double y, double dt) { 129 applyTorque(x*fy-y*fx, dt); final double k=(x*fx+y*fy)/(x*x+y*y); applyForce(k*x, k*y, dt); 132 return this; 133 } 134 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 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 |