6fa184444687cf11f07f1ab6d403657e538d26bf.svn-base (1421B)
1 #include "Physics.h" 2 3 //Spring 4 Spring::Spring(int* pos, float* vel, float k, int n, float damp, int Vmax, int Amax, int ExMax) 5 { 6 mPos = pos; 7 mVelocity = vel; 8 mSpringK = k; 9 mDamper = damp; 10 mNeutral = n; 11 12 mMaxExten = ExMax; 13 mMaxVel = Vmax; 14 mMaxAccel = Amax; 15 } 16 17 int Spring::ZeroNeutralAxis() 18 { 19 mNeutral = *mPos; 20 return mNeutral; 21 } 22 23 float Spring::CalcAccel() 24 { 25 float disp = (float)((mNeutral - *mPos)); 26 27 if (mMaxExten != 0) 28 { 29 if (fabs(disp) > mMaxExten) 30 { 31 disp = (float)mMaxExten*GetSign(disp); 32 } 33 } 34 35 float Accel = (disp)*mSpringK; 36 if (mMaxAccel != 0) 37 { 38 if (fabs(Accel) > mMaxAccel) 39 { 40 //gCons->ConsPrintf("Bouyant Accel = %f\n", mMaxAccel*GetSign(Accel)); 41 return mMaxAccel*GetSign(Accel); 42 } 43 } 44 //gCons->ConsPrintf("Bouyant Accel = %f\n", Accel); 45 return Accel; 46 } 47 48 bool Spring::CalcVelocity() 49 { 50 if (mMaxVel != 0) 51 { 52 if ( abs( (int)(*mVelocity + CalcAccel()) ) > mMaxVel) 53 { 54 *mVelocity = mMaxVel*GetSign(*mVelocity); 55 return true; 56 } 57 } 58 //*mVelocity += CalcAccel(); 59 *mVelocity = (*mVelocity + CalcAccel())*mDamper; 60 //gCons->ConsPrintf("Spring Vel = %f\n", *mVelocity); 61 return true; 62 } 63 64 //Physics 65 PhysicsManager::PhysicsManager(float GravAccel, float BouyancyK, int* pos, float* Xvel, float* Yvel, bool Grav ) 66 { 67 mGravAccel = GravAccel; 68 mGravity = Grav; 69 mFloating = false; 70 mXvel = Xvel; 71 mYvel = Yvel; 72 mBouyancy = Spring(pos, Yvel, BouyancyK, 0, (float)0.85, 9, 0, 10); 73 }