Camera Bob: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
mNo edit summary
Line 5: Line 5:


  // Camera Bob
  // Camera Bob
ConVar hf_bob_enabled ( "hf_bob_enabled", "1", 0, "head bobbing state", true, 0, true, 1 );
ConVar cl_viewbob_enabled ( "cl_viewbob_enabled", "1", 0, "Oscillation Toggle", true, 0, true, 1 );
ConVar hf_bob_timer ( "hf_bob_timer", "0.4", 0, "time between bobs" );
ConVar cl_viewbob_timer ( "cl_viewbob_timer", "10", 0, "Speed of Oscillation", OnChangeBobTime );
ConVar hf_bob_scale ( "hf_bob_scale", "0.001", 0, "head bobbing scale");
ConVar cl_viewbob_scale ( "cl_viewbob_scale", "0.05", 0, "Magnitude of Oscillation", OnChangeBobScale);


bool bBobStep;
float fBobTimer;


All we're doing here is declaring our convars, bools and floats for our camera bobbing code below.
All we're doing here is declaring our convars, bools and floats for our camera bobbing code below.


In the same file, find the function void ''CGameMovement::WalkMove( void )'' and add the following to the very top of the function:
In the same file, find the function void ''CGameMovement::WalkMove( void )'' and add the following to the very top of the function:
if ( hf_bob_enabled.GetInt() == 1 )
{
  if ( player->GetAbsVelocity().Length() > 0 ){
  if ( gpGlobals->curtime > fBobTimer )
  {
    fBobTimer = gpGlobals->curtime + ( hf_bob_timer.GetFloat() / ( player->GetAbsVelocity().Length() / 200 ) );
    if ( bBobStep == true )
    {
    player->ViewPunch( QAngle(  1 * player->GetAbsVelocity().Length() * hf_bob_scale.GetFloat(),
                                -1 * player->GetAbsVelocity().Length() * hf_bob_scale.GetFloat(),
                                -1 * player->GetAbsVelocity().Length() * hf_bob_scale.GetFloat()) );
    bBobStep = false;
    }
    else
    {
    player->ViewPunch( QAngle(  1 * player->GetAbsVelocity().Length() * hf_bob_scale.GetFloat(),
                                1 * player->GetAbsVelocity().Length() * hf_bob_scale.GetFloat(),
                                -1 * player->GetAbsVelocity().Length() * hf_bob_scale.GetFloat()) );
    bBobStep = true;
    }
  }
  }
}


Basically, what's happening here is firing a bunch of view punches at certain intervals to simulate the bobbing. It's nothing too complicated, but it works quite well, and is very easy to manipulate according to what one has in mind.
if ( cl_viewbob_enabled.GetInt() == 1 )
{
float xoffset = sin( gpGlobals->curtime * cl_viewbob_timer.GetFloat() ) * player->GetAbsVelocity().Length() * cl_viewbob_scale.GetFloat() / 100;
float yoffset = sin( 2 * gpGlobals->curtime * cl_viewbob_timer.GetFloat() ) * player->GetAbsVelocity().Length() * cl_viewbob_scale.GetFloat() / 400;
player->ViewPunch( QAngle( xoffset, yoffset, 0));
}
 
 


[[Category:Programming]]
[[Category:Programming]]

Revision as of 04:53, 26 May 2009

This tutorial will go through the basics of creating a camera bobbing for when a player walks. The result is a pretty realistic simulation of a head bob that nearly matches HL2's default walking speed and walking sounds. This tutorial has only been tested with the HL2MP OB and the beta OB SDK template, but should work for most every other version of Valve's source code with a few adjustments. A special thanks goes out to sharkkk from the Steam Forums for figuring all of this stuff out.

The Code

In gamemovement.cpp, before the list of defines around lines 65 or so, add the following:

// Camera Bob

ConVar cl_viewbob_enabled ( "cl_viewbob_enabled", "1", 0, "Oscillation Toggle", true, 0, true, 1 ); ConVar cl_viewbob_timer ( "cl_viewbob_timer", "10", 0, "Speed of Oscillation", OnChangeBobTime ); ConVar cl_viewbob_scale ( "cl_viewbob_scale", "0.05", 0, "Magnitude of Oscillation", OnChangeBobScale);


All we're doing here is declaring our convars, bools and floats for our camera bobbing code below.

In the same file, find the function void CGameMovement::WalkMove( void ) and add the following to the very top of the function:

if ( cl_viewbob_enabled.GetInt() == 1 )

{ float xoffset = sin( gpGlobals->curtime * cl_viewbob_timer.GetFloat() ) * player->GetAbsVelocity().Length() * cl_viewbob_scale.GetFloat() / 100; float yoffset = sin( 2 * gpGlobals->curtime * cl_viewbob_timer.GetFloat() ) * player->GetAbsVelocity().Length() * cl_viewbob_scale.GetFloat() / 400; player->ViewPunch( QAngle( xoffset, yoffset, 0));

}