home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / game / vehicle / VehicleAnimated.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  6KB  |  238 lines

  1.  
  2. // Vehicle.cpp
  3. //
  4. // Copyright 2002-2004 Raven Software
  5. //----------------------------------------------------------------
  6.  
  7. #include "../../idlib/precompiled.h"
  8. #pragma hdrstop
  9.  
  10. #include "../Game_local.h"
  11. #include "VehicleAnimated.h"
  12.  
  13. CLASS_DECLARATION( rvVehicle, rvVehicleAnimated )
  14. END_CLASS
  15.  
  16. rvVehicleAnimated::rvVehicleAnimated ( void ) {
  17. }
  18.  
  19. rvVehicleAnimated::~rvVehicleAnimated ( void ) {
  20.     SetPhysics( NULL );
  21. }
  22.  
  23. /*
  24. ================
  25. rvVehicleAnimated::Spawn
  26. ================
  27. */
  28. void rvVehicleAnimated::Spawn( void ) {
  29.  
  30.     turnRate   = spawnArgs.GetFloat ( "turnRate", "90" );
  31.     viewAngles = GetPhysics()->GetAxis ( ).ToAngles ( );
  32.     viewAxis   = viewAngles.ToMat3();
  33.  
  34.     // Initialize the physics object
  35.     physicsObj.SetSelf( this );
  36.     physicsObj.SetClipModel( new idClipModel( GetPhysics()->GetClipModel() ), 1.0f );
  37.     physicsObj.SetContents( CONTENTS_BODY );
  38.     physicsObj.SetClipMask( MASK_PLAYERSOLID|CONTENTS_VEHICLECLIP );
  39.     physicsObj.SetMaxStepHeight( spawnArgs.GetFloat ( "stepheight", "14" ) );
  40.  
  41.     // Start just a tad above the floor
  42.     physicsObj.SetOrigin( GetPhysics()->GetOrigin() + idVec3( 0, 0, CM_CLIP_EPSILON ) );
  43.     physicsObj.SetMass( spawnArgs.GetFloat( "mass", "100" ) );
  44.     physicsObj.SetDelta( vec3_origin );
  45.  
  46.     // Gravity
  47.     idVec3 gravity = spawnArgs.GetVector( "gravityDir", "0 0 -1" );
  48.     gravity *= g_gravity.GetFloat ( );
  49.     physicsObj.SetGravity( gravity );
  50.     SetPhysics( &physicsObj );
  51.  
  52.     animator.RemoveOriginOffset( true );
  53.         
  54.     additionalDelta.Zero();
  55.  
  56.     BecomeActive( TH_THINK );        
  57. }
  58.  
  59. /*
  60. ================
  61. rvVehicleAnimated::ClientPredictionThink
  62. ================
  63. */
  64. void rvVehicleAnimated::ClientPredictionThink ( void ) {
  65.     Think();
  66. }
  67.  
  68. /*
  69. ================
  70. rvVehicleAnimated::Think
  71. ================
  72. */
  73. void rvVehicleAnimated::Think ( void ) {
  74.  
  75.     rvVehicle::Think();    
  76.  
  77.     float rate = 0.0f;
  78.     usercmd_t& cmd = positions[0].mInputCmd;
  79.     idVec3 delta;
  80.  
  81.     if( positions[0].IsOccupied() && !IsFrozen() && IsMovementEnabled() )    {
  82.         
  83.         if (( g_vehicleMode.GetInteger() == 0 && !( cmd.buttons & BUTTON_STRAFE )) ||     // If we're in the old driving mode and we aren't strafing or...
  84.             ( g_vehicleMode.GetInteger() != 0 && ( cmd.buttons & BUTTON_STRAFE )))        // If we're in the new driving mode and we are strafing
  85.         {
  86.             rate = SignZero(cmd.forwardmove) * turnRate;
  87.             rate *= idMath::MidPointLerp( 0.0f, 0.9f, 1.0f, idMath::Fabs(cmd.rightmove) / 127.0f );
  88.             viewAngles.yaw += Sign(cmd.rightmove) * rate * MS2SEC(gameLocal.GetMSec());    
  89.         }
  90.     }
  91.  
  92.     viewAngles.Normalize360();
  93.     animator.GetDelta( gameLocal.time - gameLocal.GetMSec(), gameLocal.time, delta );
  94.     delta += additionalDelta;
  95.  
  96.     idStr alignmentJoint;
  97.     if ( g_vehicleMode.GetInteger() != 0 && spawnArgs.GetString( "alignment_joint", 0, alignmentJoint ) ) {
  98.         idVec3 offset;
  99.         idMat3 axis;
  100.         GetJointWorldTransform( animator.GetJointHandle( alignmentJoint ), gameLocal.time, offset, axis );
  101.         delta *= axis;
  102.     } else {
  103.         viewAxis = viewAngles.ToMat3() * physicsObj.GetGravityAxis();
  104.         delta *= viewAxis;
  105.     }
  106.             
  107.     physicsObj.SetDelta( delta );
  108.     additionalDelta.Zero();
  109. }
  110.  
  111. /*
  112. ================
  113. rvVehicleAnimated::GetAxis
  114. ================
  115. */
  116. const idMat3& rvVehicleAnimated::GetAxis( int id ) const {
  117.     return viewAxis;
  118. }
  119.  
  120. /*
  121. ================
  122. rvVehicleAnimated::WriteToSnapshot
  123. ================
  124. */
  125. void rvVehicleAnimated::WriteToSnapshot( idBitMsgDelta &msg ) const {
  126.     rvVehicle::WriteToSnapshot ( msg );
  127.     
  128.      physicsObj.WriteToSnapshot( msg );
  129.     msg.WriteFloat( viewAngles[0] );
  130.     msg.WriteFloat( viewAngles[1] );
  131.     msg.WriteFloat( viewAngles[2] );
  132. }
  133.  
  134. /*
  135. ================
  136. rvVehicleAnimated::ReadFromSnapshot
  137. ================
  138. */
  139. void rvVehicleAnimated::ReadFromSnapshot( const idBitMsgDelta &msg ) {
  140.     rvVehicle::ReadFromSnapshot ( msg );
  141.  
  142.     physicsObj.ReadFromSnapshot( msg );
  143.     viewAngles[0] = msg.ReadFloat( );
  144.     viewAngles[1] = msg.ReadFloat( );
  145.     viewAngles[2] = msg.ReadFloat( );
  146. }
  147.  
  148. /*
  149. ================
  150. rvVehicleAnimated::Save
  151. ================
  152. */
  153. void rvVehicleAnimated::Save ( idSaveGame *savefile ) const {
  154.     savefile->WriteVec3( storedPosition );
  155.  
  156.     savefile->WriteStaticObject ( physicsObj );
  157.  
  158.     savefile->WriteAngles ( viewAngles );
  159.     savefile->WriteFloat ( turnRate );
  160.  
  161.     // why are we writing out the viewAxis here???
  162.     savefile->WriteMat3 ( viewAxis );
  163.  
  164.     // TEMP
  165.     animator.Save( savefile );
  166. }
  167.  
  168. /*
  169. ================
  170. rvVehicleAnimated::Restore
  171. ================
  172. */
  173. void rvVehicleAnimated::Restore ( idRestoreGame *savefile ) {
  174.     savefile->ReadVec3( storedPosition );
  175.  
  176.     physicsObj.SetSelf( this );
  177.     physicsObj.SetClipModel( new idClipModel( GetPhysics()->GetClipModel() ), 1.0f );
  178.     savefile->ReadStaticObject ( physicsObj );
  179.     RestorePhysics ( &physicsObj );
  180.     physicsObj.EnableClip();
  181.  
  182.     savefile->ReadAngles ( viewAngles );
  183.     savefile->ReadFloat ( turnRate );
  184.  
  185.     savefile->ReadMat3 ( viewAxis );
  186.  
  187.     // TEMP - restore animator, because it was cleared when loading the AF
  188.     animator.Restore( savefile );
  189.     animator.GetJoints( &renderEntity.numJoints, &renderEntity.joints );
  190.     animator.GetBounds( gameLocal.time, renderEntity.bounds );
  191.     // TEMP
  192.  
  193.     additionalDelta.Zero();
  194. }
  195.  
  196. /*
  197. ================
  198. rvVehicleAnimated::GetPhysicsToVisualTransform
  199. ================
  200. */
  201. bool rvVehicleAnimated::GetPhysicsToVisualTransform ( idVec3 &origin, idMat3 &axis ) {
  202.     origin = modelOffset;
  203.     axis = viewAxis;
  204.     return true;
  205. }
  206.  
  207. /*
  208. ================
  209. rvVehicleAnimated::RunPrePhysics
  210. ================
  211. */
  212. void rvVehicleAnimated::RunPrePhysics ( void ) {
  213.     storedPosition = physicsObj.GetOrigin();
  214. }
  215.  
  216. /*
  217. ================
  218. rvVehicleAnimated::RunPostPhysics
  219. ================
  220. */
  221. void rvVehicleAnimated::RunPostPhysics ( void ) {
  222.  
  223.     if ( autoCorrectionBegin + 3000 > static_cast<unsigned>( gameLocal.time )) {
  224.         autoCorrectionBegin = 0;
  225.  
  226.         idVec3 delta;
  227.         idVec3 actualDelta = physicsObj.GetOrigin() - storedPosition;
  228.  
  229.         animator.GetDelta( gameLocal.time - gameLocal.GetMSec(), gameLocal.time, delta );
  230.  
  231.         if ( !autoCorrectionBegin && delta.LengthSqr() * 0.2f > actualDelta.LengthSqr() ) {
  232.             autoCorrectionBegin = gameLocal.time;
  233.         }
  234.     }
  235. }
  236.  
  237.  
  238.