home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / game / vehicle / VehicleRigid.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  4KB  |  164 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 "VehicleRigid.h"
  12.  
  13. CLASS_DECLARATION( rvVehicle, rvVehicleRigid )
  14. END_CLASS
  15.  
  16. rvVehicleRigid::rvVehicleRigid ( void ) {
  17. }
  18.  
  19. rvVehicleRigid::~rvVehicleRigid ( void ) {
  20.     SetPhysics( NULL );
  21. }
  22.  
  23. /* 
  24. ================
  25. rvVehicleRigid::Spawn
  26. ================
  27. */
  28. void rvVehicleRigid::Spawn( void ) {
  29.     physicsObj.SetSelf( this );    
  30.  
  31.     SetClipModel ( );
  32.  
  33.     physicsObj.SetOrigin( GetPhysics()->GetOrigin ( ) );
  34.     physicsObj.SetAxis ( GetPhysics()->GetAxis ( ) );
  35.     physicsObj.SetContents( CONTENTS_BODY );
  36.     physicsObj.SetClipMask( MASK_PLAYERSOLID|CONTENTS_VEHICLECLIP );
  37.     physicsObj.SetFriction ( spawnArgs.GetFloat ( "friction_linear", "1" ), spawnArgs.GetFloat ( "friction_angular", "1" ), spawnArgs.GetFloat ( "friction_contact", "1" ) );
  38.     physicsObj.SetBouncyness ( spawnArgs.GetFloat ( "bouncyness", "0.6" ) );
  39.     physicsObj.SetGravity( gameLocal.GetGravity() );
  40.     SetPhysics( &physicsObj );
  41.     
  42.     animator.CycleAnim ( ANIMCHANNEL_ALL, animator.GetAnim( spawnArgs.GetString( "anim", "idle" ) ), gameLocal.time, 0 );    
  43.  
  44.     BecomeActive( TH_THINK );        
  45. }
  46.  
  47. /*
  48. ================
  49. rvVehicleRigid::SetClipModel
  50. ================
  51. */
  52. void rvVehicleRigid::SetClipModel ( void ) {    
  53.     idStr            clipModelName;
  54.     idTraceModel    trm;
  55.     float            mass;
  56.  
  57.     // rebuild clipmodel
  58.     spawnArgs.GetString( "clipmodel", "", clipModelName );
  59.  
  60.     // load the trace model
  61.     if ( clipModelName.Length() ) {
  62.         if ( !collisionModelManager->TrmFromModel( gameLocal.GetMapName(), clipModelName, trm ) ) {
  63.             gameLocal.Error( "rvVehicleRigid '%s': cannot load collision model %s", name.c_str(), clipModelName.c_str() );
  64.             return;
  65.         }
  66.  
  67.         physicsObj.SetClipModel( new idClipModel( trm ), spawnArgs.GetFloat ( "density", "1" ) );
  68.     } else {
  69.         physicsObj.SetClipModel( new idClipModel( GetPhysics()->GetClipModel() ), spawnArgs.GetFloat ( "density", "1" ) );
  70.     }
  71.  
  72.     if ( spawnArgs.GetFloat ( "mass", "0", mass ) && mass > 0 )    {
  73.         physicsObj.SetMass ( mass );
  74.     }
  75. }
  76.  
  77.  
  78. /*
  79. ================
  80. rvVehicleRigid::RunPrePhysics
  81. ================
  82. */
  83. void rvVehicleRigid::RunPrePhysics ( void ) {
  84.     storedVelocity = physicsObj.GetLinearVelocity();
  85. }
  86.  
  87. /*
  88. ================
  89. rvVehicleRigid::RunPostPhysics
  90. ================
  91. */
  92. void rvVehicleRigid::RunPostPhysics ( void ) {
  93.  
  94.     if ( autoCorrectionBegin + 1250 > static_cast<unsigned>( gameLocal.time )) {
  95.         autoCorrectionBegin = 0;
  96.  
  97.         float lengthSq = physicsObj.GetLinearVelocity().LengthSqr();
  98.         if ( !autoCorrectionBegin && ( ( storedVelocity * 0.4f ).LengthSqr() >= lengthSq ) || ( lengthSq < 0.01f ) ) {
  99.             autoCorrectionBegin = gameLocal.time;
  100.         }
  101.     }
  102.  
  103.     if ( g_debugVehicle.GetInteger() == 10 ) {
  104.         gameLocal.Printf( "Speed: %f\n", physicsObj.GetLinearVelocity().Length() );
  105.     }
  106. }
  107.  
  108. /*
  109. ================
  110. rvVehicleRigid::WriteToSnapshot
  111. ================
  112. */
  113. void rvVehicleRigid::WriteToSnapshot( idBitMsgDelta &msg ) const {
  114.     rvVehicle::WriteToSnapshot( msg );
  115.     physicsObj.WriteToSnapshot( msg );
  116. }
  117.  
  118. /*
  119. ================
  120. rvVehicleRigid::ReadFromSnapshot
  121. ================
  122. */
  123. void rvVehicleRigid::ReadFromSnapshot( const idBitMsgDelta &msg ) {
  124.     rvVehicle::ReadFromSnapshot( msg );
  125.     physicsObj.ReadFromSnapshot( msg );
  126. }
  127.  
  128. /*
  129. ================
  130. rvVehicleRigid::Save
  131. ================
  132. */
  133. void rvVehicleRigid::Save ( idSaveGame *savefile ) const {
  134.  
  135.     savefile->WriteVec3 ( storedVelocity ); // cnicholson: Added unsaved var
  136.     savefile->WriteStaticObject ( physicsObj );
  137. }
  138.  
  139. /*
  140. ================
  141. rvVehicleRigid::Restore
  142. ================
  143. */
  144. void rvVehicleRigid::Restore ( idRestoreGame *savefile ) {
  145.  
  146.     savefile->ReadVec3 ( storedVelocity ); // cnicholson: Added unrestored var
  147.  
  148.     physicsObj.SetSelf( this );    
  149.     
  150.     SetClipModel ( );
  151.  
  152.     savefile->ReadStaticObject ( physicsObj );
  153.     RestorePhysics( &physicsObj );
  154. }
  155.  
  156. /*
  157. =====================
  158. rvVehicleRigid::SkipImpulse
  159. =====================
  160. */
  161. bool rvVehicleRigid::SkipImpulse( idEntity* ent, int id ) {    
  162.     return false;
  163. }
  164.