float Lapidem_Math::AngleBetweenVectors( tVector2D _v1, tVector2D _v2 ) { float fDot( DotProduct( _v1, _v2 ) ); float fLength( Vector2DLength( _v1 ) * Vector2DLength( _v2 ) ); if( 0.0f == fLength ) return 0.0f; float fAngle( acos( fDot / fLength ) ); if( _isnan( fAngle ) ) return 0.0f; return fAngle; }
void CAttackState::CalculateAI(float fElapsedTime, CBase* pBase) { CEnemy* pEnemy = (CEnemy*)pBase; pEnemy->GetWeapon()->Update(fElapsedTime); tVector2D distanceToTarget; distanceToTarget.fX = pEnemy->GetTarget()->GetPosX() - pEnemy->GetPosX(); distanceToTarget.fY = pEnemy->GetTarget()->GetPosY() - pEnemy->GetPosY(); if( Vector2DLength(distanceToTarget) > pEnemy->GetWeapon()->GetRange() ) { pEnemy->Walk(); distanceToTarget = Vector2DNormalize(distanceToTarget); distanceToTarget = distanceToTarget * pEnemy->GetSpeed(); pEnemy->SetVelX((distanceToTarget.fX)); pEnemy->SetVelY((distanceToTarget.fY)); } else { pEnemy->Shoot(); // Shoot at the player pEnemy->SetVelX(0.0f); pEnemy->SetVelY(0.0f); pEnemy->GetWeapon()->FireWeapon(pEnemy); } }
void CHeavyInfantry::HandleEvent(CEvent* pEvent) { if( pEvent->GetEventID() == "big.bang" && GetTarget() == NULL ) { CPlayer* player = (CPlayer*)pEvent->GetParam(); tVector2D distanceToPlayer; distanceToPlayer.fX = player->GetPosX()-GetPosX(); distanceToPlayer.fY = player->GetPosY()-GetPosY(); player->GetCurrWeapon(); if( Vector2DLength(distanceToPlayer) <= player->GetCurrWeapon()->GetSoundDistance()) { ChangeState(CAttackState::GetInstance()); SetTarget(player); CEventSystem::GetInstance()->SendEvent("player.detected",this); } } if( pEvent->GetEventID() == "player.detected" && pEvent->GetParam() != this ) { CEnemy* pEnemy = (CEnemy*)pEvent->GetParam(); tVector2D toTarget; toTarget.fX = pEnemy->GetPosX() - GetPosX(); toTarget.fY = pEnemy->GetPosY() - GetPosY(); if( Vector2DLength(toTarget) < 200 ) { CEventSystem::GetInstance()->SendEvent("join.squad",this); } } if( pEvent->GetEventID() == "form.up" ) { SetAttack(false); } if( pEvent->GetEventID() == "engage.player") { SetAttack(true); } if( pEvent->GetEventID() == "fire.bullet" ) { GetWeapon()->FireWeapon(this); } }
// Find the angle between 2 vectors float AngleBetweenVectors(tVector2D v1, tVector2D v2) { // Dot product float fDot = DotProduct(v1, v2); // Length of those vectors float fLength = Vector2DLength(v1) * Vector2DLength(v2); // Make sure we don't get a divide by zero error. if (fLength == 0.0f) return 0.0f; // Calculate the angle between the two vectors float fAngle = acos( fDot / fLength ); // Make sure the number is not indefinite. Shows up as -1.#IND00000. Not a number. if (_isnan(fAngle)) return 0.0f; // Return the angle. return fAngle; }
// Normalize a vector tVector2D Vector2DNormalize(tVector2D vec) { tVector2D vNormalized; float fLength = Vector2DLength(vec); vNormalized.fX = vec.fX / fLength; vNormalized.fY = vec.fY / fLength; // Return the normalized vector return vNormalized; }
/* This function checks if the point P is colliding with the circle whose center is "Center" and radius is "Radius" */ int StaticPointToStaticCircle(Vector2D *pP, Vector2D *pCenter, float Radius) { Vector2D distVec; float distance = 0; Vector2DSub(&distVec, pP, pCenter); distance = Vector2DLength(&distVec); // If Distance is less than or equal to radius, its colliding if(distance <= Radius) { return 1; } else return 0; }
void CGate::HandleEvent(CEvent* pEvent) { if( pEvent->GetEventID() == "generator.destroyed" ) { CBase* generator = (CBase*)pEvent->GetParam(); tVector2D toGenerator; toGenerator.fX = generator->GetPosX() - GetPosX(); toGenerator.fY = generator->GetPosY() - GetPosY(); if( Vector2DLength(toGenerator) < 700 ) { CMessageSystem::GetInstance()->SendMsg( new CDestroyBase(this)); } } }
void CHostageState::CalculateAI(float fElapsedTime, CBase* pBase) { CAlly* hostage = (CAlly*)pBase; tVector2D vectorToPlayer; vectorToPlayer.fX = CPlayer::GetInstance()->GetPosX() - hostage->GetPosX(); vectorToPlayer.fY = CPlayer::GetInstance()->GetPosY() - hostage->GetPosY(); if( Vector2DLength(vectorToPlayer) < 100 ) { Vector2DNormalize(vectorToPlayer); vectorToPlayer = vectorToPlayer * (float)hostage->GetSpeed(); hostage->SetVelX(vectorToPlayer.fX); hostage->SetVelY(vectorToPlayer.fY); } else { hostage->SetVelX(0.0f); hostage->SetVelY(0.0f); } }
void CBasePlayer::UpdateStepSound( surfacedata_t *psurface, const Vector &vecOrigin, const Vector &vecVelocity ) { int fWalking; float fvol; Vector knee; Vector feet; float height; float speed; float velrun; float velwalk; float flduck; int fLadder; if ( m_flStepSoundTime > 0 ) { m_flStepSoundTime -= 1000.0f * gpGlobals->frametime; if ( m_flStepSoundTime < 0 ) { m_flStepSoundTime = 0; } } if ( m_flStepSoundTime > 0 ) return; if ( GetFlags() & (FL_FROZEN|FL_ATCONTROLS)) return; if ( GetMoveType() == MOVETYPE_NOCLIP || GetMoveType() == MOVETYPE_OBSERVER ) return; if ( !sv_footsteps.GetFloat() ) return; speed = VectorLength( vecVelocity ); float groundspeed = Vector2DLength( vecVelocity.AsVector2D() ); // determine if we are on a ladder fLadder = ( GetMoveType() == MOVETYPE_LADDER ); // UNDONE: need defined numbers for run, walk, crouch, crouch run velocities!!!! if ( ( GetFlags() & FL_DUCKING) || fLadder ) { velwalk = 60; // These constants should be based on cl_movespeedkey * cl_forwardspeed somehow velrun = 80; flduck = 100; } else { velwalk = 90; velrun = 220; flduck = 0; } bool onground = ( GetFlags() & FL_ONGROUND ); bool movingalongground = ( groundspeed > 0.0f ); bool moving_fast_enough = ( speed >= velwalk ); // To hear step sounds you must be either on a ladder or moving along the ground AND // You must be moving fast enough if ( !moving_fast_enough || !(fLadder || ( onground && movingalongground )) ) return; // MoveHelper()->PlayerSetAnimation( PLAYER_WALK ); fWalking = speed < velrun; VectorCopy( vecOrigin, knee ); VectorCopy( vecOrigin, feet ); height = GetPlayerMaxs()[ 2 ] - GetPlayerMins()[ 2 ]; knee[2] = vecOrigin[2] + 0.2 * height; // find out what we're stepping in or on... if ( fLadder ) { #ifdef CLIENT_DLL psurface = GetFootstepSurface( vecOrigin, "ladder" ); #else psurface = physprops->GetSurfaceData( physprops->GetSurfaceIndex( "ladder" ) ); #endif fvol = 0.5; m_flStepSoundTime = 350; } else if ( enginetrace->GetPointContents( knee ) & MASK_WATER ) { static int iSkipStep = 0; if ( iSkipStep == 0 ) { iSkipStep++; return; } if ( iSkipStep++ == 3 ) { iSkipStep = 0; } psurface = physprops->GetSurfaceData( physprops->GetSurfaceIndex( "wade" ) ); fvol = 0.65; m_flStepSoundTime = 600; } else if ( enginetrace->GetPointContents( feet ) & MASK_WATER ) { psurface = physprops->GetSurfaceData( physprops->GetSurfaceIndex( "water" ) ); fvol = fWalking ? 0.2 : 0.5; m_flStepSoundTime = fWalking ? 400 : 300; } else { if ( !psurface ) return; m_flStepSoundTime = fWalking ? 400 : 300; switch ( psurface->game.material ) { default: case CHAR_TEX_CONCRETE: fvol = fWalking ? 0.2 : 0.5; break; case CHAR_TEX_METAL: fvol = fWalking ? 0.2 : 0.5; break; case CHAR_TEX_DIRT: fvol = fWalking ? 0.25 : 0.55; break; case CHAR_TEX_VENT: fvol = fWalking ? 0.4 : 0.7; break; case CHAR_TEX_GRATE: fvol = fWalking ? 0.2 : 0.5; break; case CHAR_TEX_TILE: fvol = fWalking ? 0.2 : 0.5; break; case CHAR_TEX_SLOSH: fvol = fWalking ? 0.2 : 0.5; break; } } m_flStepSoundTime += flduck; // slower step time if ducking // play the sound // 65% volume if ducking if ( GetFlags() & FL_DUCKING ) { fvol *= 0.65; } PlayStepSound( feet, psurface, fvol, false ); }
int main() { Vector2D v1, v2, v3, result; float scale; Matrix2D id, m0, m1; Vector2D u; float d, x, y; long n; long i, j; float radius; v1.x = v1.y = 7.0f; Vector2DZero(&v1); result.x = result.y = 0.0f; printf("Vector2DZero: %s\n", (CompareVector2D(&result, &v1) < EPSILON) ? "Pass" : "Fail"); Vector2DSet(&v1, 1.0f, 2.0f); result.x = 1.0f; result.y = 2.0f; printf("Vector2DSet: %s\n", (CompareVector2D(&result, &v1) < EPSILON) ? "Pass" : "Fail"); v1.x = 2.0f; v1.y = -4.0f; Vector2DNeg(&v2, &v1); result.x = -2.0f; result.y = 4.0f; printf("Vector2DNeg: %s\n", (CompareVector2D(&result, &v2) < EPSILON) ? "Pass" : "Fail"); v1.x = 2.0f; v1.y = -4.0f; v2.x = 1.0f; v2.y = 7.0f; Vector2DAdd(&v3, &v1, &v2); result.x = result.y = 3.0f; printf("Vector2DAdd: %s\n", (CompareVector2D(&result, &v3) < EPSILON) ? "Pass" : "Fail"); v1.x = 2.0f; v1.y = -4.0f; v2.x = 1.0f; v2.y = 7.0f; Vector2DSub(&v3, &v1, &v2); result.x = 1.0f; result.y = -11.0f; printf("Vector2DSub: %s\n", (CompareVector2D(&result, &v3) < EPSILON) ? "Pass" : "Fail"); v1.x = 3.0f; v1.y = 4.0f; Vector2DNormalize(&v2, &v1); result.x = 0.6f; result.y = 0.8f; printf("Vector2DNormalize: %s\n", (CompareVector2D(&result, &v2) < EPSILON) ? "Pass" : "Fail"); v1.x = 2.0f; v1.y = -5.0f; Vector2DScale(&v2, &v1, 3.0f); result.x = 6.0f; result.y = -15.0f; printf("Vector2DScale: %s\n", (CompareVector2D(&result, &v2) < EPSILON) ? "Pass" : "Fail"); v1.x = 2.0f; v1.y = -5.0f; v2.x = 6.0f; v2.y = 2.0f; scale = 3.0f; Vector2DScaleAdd(&v3, &v1, &v2, scale); result.x = 12.0f; result.y = -13.0f; printf("Vector2DScaleAdd: %s\n", (CompareVector2D(&result, &v3) < EPSILON) ? "Pass" : "Fail"); Vector2DScaleSub(&v3, &v1, &v2, scale); result.x = 0.f; result.y = -17.f; printf("Vector2DScaleSub: %s\n", (CompareVector2D(&result, &v3) < EPSILON) ? "Pass" : "Fail"); v1.x = 3.0f; v1.y = -4.0f; printf("Vector2DLength: %s\n", (fabs(Vector2DLength(&v1) - 5.0f) < EPSILON) ? "Pass" : "Fail"); v1.x = 3.0f; v1.y = -4.0f; printf("Vector2DSquareLength: %s\n", (fabs(Vector2DSquareLength(&v1) - 25.0f) < EPSILON) ? "Pass" : "Fail"); v1.x = 2.0f; v1.y = 3.0f; v2.x = 4.0f; v2.y = -1.0f; printf("Vector2DDistance: %s\n", (fabs(Vector2DDistance(&v1, &v2) - 4.472136) < EPSILON) ? "Pass" : "Fail"); v1.x = 2.0f; v1.y = 3.0f; v2.x = 4.0f; v2.y = -1.0f; printf("Vector2DSquareDistance: %s\n", (fabs(Vector2DSquareDistance(&v1, &v2) - 20.0f) < EPSILON) ? "Pass" : "Fail"); v1.x = 3.0f; v1.y = 2.0f; v2.x = 4.0f; v2.y = -6.0f; printf("Vector2DDotProduct: %s\n", (fabs(Vector2DDotProduct(&v1, &v2)) < EPSILON) ? "Pass" : "Fail"); printf("\n------Testing StaticPointToStaticCircle------\n\n"); v1.x = 10.f; v1.y = 10.f; v2.x = 11.4f; v2.y = 11.4f; radius = 2.f; printf("StaticPointToStaticCircle Collision: %s\n", (StaticPointToStaticCircle(&v2, &v1, radius)) ? "Pass" : "Fail" ); v2.x = 12.f; v2.y = 12.f; printf("StaticPointToStaticCircle Non Collision: %s\n", (!StaticPointToStaticCircle(&v2, &v1, radius)) ? "Pass" : "Fail" ); printf("\n------Running Matrix Tests------\n\n"); // create an id matrix for reference for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) id.m[j][i] = (i == j) ? 1.0f : 0.0f; // ==================== // test Matrix2DIdentity // ==================== Matrix2DIdentity(&m0); d = CompareMatrix2D(&id, &m0); printf("Matrix2DIdentity : %s\n", (CompareMatrix2D(&id, &m0) < EPSILON) ? "Pass" : "Fail"); // ==================== // test Matrix2DTrans // ==================== // generate 2 random numbers x = 2.0f * rand() / (float)(RAND_MAX) - 1.0f; y = 2.0f * rand() / (float)(RAND_MAX) - 1.0f; Matrix2DTranslate(&m0, x, y); m0.m[0][2] -= x; m0.m[1][2] -= y; printf("Matrix2DTranslate: %s\n", (CompareMatrix2D(&id, &m0) < EPSILON) ? "Pass" : "Fail"); // ==================== // test Matrix2DScale // ==================== // generate 2 random numbers x = 2.0f * rand() / (float)(RAND_MAX) - 1.0f; y = 2.0f * rand() / (float)(RAND_MAX) - 1.0f; Matrix2DScale(&m0, x, y); m0.m[0][0] /= x; m0.m[1][1] /= y; printf("Matrix2DScale : %s\n", (CompareMatrix2D(&id, &m0) < EPSILON) ? "Pass" : "Fail"); // ==================== // test Matrix2DConcat // ==================== // generate 2 random numbers x = 2.0f * rand() / (float)(RAND_MAX) - 1.0f; y = 2.0f * rand() / (float)(RAND_MAX) - 1.0f; Matrix2DTranslate (&m0, x, y); Matrix2DScale (&m1, x, y); Matrix2DConcat(&m0, &m0, &m1); m0.m[0][2] -= x; m0.m[1][2] -= y; m0.m[0][0] /= x; m0.m[1][1] /= y; printf("Matrix2DConcat 1 : %s\n", (CompareMatrix2D(&id, &m0) < EPSILON) ? "Pass" : "Fail"); // generate 2 random numbers x = 2.0f * rand() / (float)(RAND_MAX) - 1.0f; y = 2.0f * rand() / (float)(RAND_MAX) - 1.0f; Matrix2DTranslate (&m0, x, y); Matrix2DScale (&m1, x, y); Matrix2DConcat(&m0, &m1, &m0); m0.m[0][2] -= x * x; m0.m[1][2] -= y * y; m0.m[0][0] /= x; m0.m[1][1] /= y; printf("Matrix2DConcat 2 : %s\n", (CompareMatrix2D(&id, &m0) < EPSILON) ? "Pass" : "Fail"); // ==================== // test Matrix2DRotRad // ==================== n = (rand() % 16) + 15; Matrix2DIdentity(&m0); Matrix2DRotRad (&m1, 2.0f * PI / n); for (i = 0; i < n; i++) Matrix2DConcat(&m0, &m1, &m0); printf("Matrix2DRotRad : %s (%d)\n", (CompareMatrix2D(&id, &m0) < EPSILON) ? "Pass" : "Fail", n); // ==================== // test Matrix2DRotDeg // ==================== n = (rand() % 16) + 15; Matrix2DIdentity(&m0); Matrix2DRotDeg (&m1, 360.0f / n); for (i = 0; i < n; i++) Matrix2DConcat(&m0, &m1, &m0); printf("Matrix2DRotDeg : %s (%d)\n", (CompareMatrix2D(&id, &m0) < EPSILON) ? "Pass" : "Fail", n); // ==================== // test Matrix2DTranspose // ==================== Matrix2DRotRad (&m0, rand() / (float)(RAND_MAX) * 2.0f * PI); Matrix2DTranspose(&m1, &m0); Matrix2DConcat (&m0, &m1, &m0); printf("Matrix2DTranspose: %s\n", (CompareMatrix2D(&id, &m0) < EPSILON) ? "Pass" : "Fail"); // ==================== // test Matrix2DMultVec // ==================== // generate 2 random numbers x = 2.0f * rand() / (float)(RAND_MAX) - 1.0f; y = 2.0f * rand() / (float)(RAND_MAX) - 1.0f; n = (rand() % 16) + 15; Vector2DSet (&u, x, y); Matrix2DRotRad (&m0, 2.0f * PI / n); for (i = 0; i < n; i++) Matrix2DMultVec(&u, &m0, &u); printf("Matrix2DMultVec : %s\n", ((fabs(u.x - x) + fabs(u.y - y)) < EPSILON) ? "Pass" : "Fail"); // generate 2 random numbers x = 2.0f * rand() / (float)(RAND_MAX) - 1.0f; y = 2.0f * rand() / (float)(RAND_MAX) - 1.0f; n = (rand() % 16) + 15; Vector2DSet (&u, x, y); Matrix2DTranslate (&m0, x, y); for (i = 1; i < n; i++) Matrix2DMultVec(&u, &m0, &u); printf("Matrix2DMultVec : %s\n", ((fabs(u.x - x * n) + fabs(u.y - y * n)) < EPSILON) ? "Pass" : "Fail"); printf("\n------Testing New Collision Functions------\n\n"); //StaticPointToStaticRect Vector2DSet(&v1, 1.f, 1.f); //point Vector2DSet(&v2, 0.f, 0.f); //rect printf("StaticPointToStaticRect Collision: %s\n", (StaticPointToStaticRect(&v1, &v2, 2.f, 2.f) ? "Pass" : "Fail")); printf("StaticPointToStaticRect Non Collision: %s\n\n", (!StaticPointToStaticRect(&v1, &v2, 1.f, 1.f) ? "Pass" : "Fail")); //StaticCircleToStaticCircle Vector2DSet(&v1, 2.f, 0.f); printf("StaticCircleToStaticCircle Collision Touch: %s\n", (StaticCircleToStaticCircle(&v1, 1.f, &v2, 1.f) ? "Pass" : "Fail")); printf("StaticCircleToStaticCircle Collision: %s\n", (StaticCircleToStaticCircle(&v1, 2.f, &v2, 1.f) ? "Pass" : "Fail")); printf("StaticCircleToStaticCircle Non Collision: %s\n\n", (!StaticCircleToStaticCircle(&v1, 0.5f, &v2, 1.f) ? "Pass" : "Fail")); //StaticRectToStaticRect Vector2DSet(&v1, 2.f, 2.f); printf("StaticRectToStaticRect Non Collision: %s\n", (!StaticRectToStaticRect(&v1, 1.f, 1.f, &v2, 1.f, 1.f) ? "Pass" : "Fail")); printf("StaticRectToStaticRect Collision Touch: %s\n", (StaticRectToStaticRect(&v1, 2.f, 2.f, &v2, 2.f, 2.f) ? "Pass" : "Fail")); printf("StaticRectToStaticRect Collision Intersect: %s\n", (StaticRectToStaticRect(&v1, 3.f, 3.f, &v2, 3.f, 3.f) ? "Pass" : "Fail")); return 1; }
void Vector2DNormalize(Vector2D &Result, const Vector2D &Vec0) { float length = Vector2DLength(Vec0); Result.x = Vec0.x / length; Result.y = Vec0.y / length; }
vec_t Vector2D::Length(void) const { return Vector2DLength(*this); }
void CDODPlayer::UpdateStepSound( surfacedata_t *psurface, const Vector &vecOrigin, const Vector &vecVelocity ) { Vector knee; Vector feet; float height; int fLadder; if ( m_flStepSoundTime > 0 ) { m_flStepSoundTime -= 1000.0f * gpGlobals->frametime; if ( m_flStepSoundTime < 0 ) { m_flStepSoundTime = 0; } } if ( m_flStepSoundTime > 0 ) return; if ( GetFlags() & (FL_FROZEN|FL_ATCONTROLS)) return; if ( GetMoveType() == MOVETYPE_NOCLIP || GetMoveType() == MOVETYPE_OBSERVER ) return; if ( !sv_footsteps.GetFloat() ) return; float speed = VectorLength( vecVelocity ); float groundspeed = Vector2DLength( vecVelocity.AsVector2D() ); // determine if we are on a ladder fLadder = ( GetMoveType() == MOVETYPE_LADDER ); float flDuck; if ( ( GetFlags() & FL_DUCKING) || fLadder ) { flDuck = 100; } else { flDuck = 0; } static float flMinProneSpeed = 10.0f; static float flMinSpeed = 70.0f; static float flRunSpeed = 110.0f; bool onground = ( GetFlags() & FL_ONGROUND ); bool movingalongground = ( groundspeed > 0.0f ); bool moving_fast_enough = ( speed >= flMinSpeed ); // always play a step sound if we are moving faster than // To hear step sounds you must be either on a ladder or moving along the ground AND // You must be moving fast enough CheckProneMoveSound( groundspeed, onground ); if ( !moving_fast_enough || !(fLadder || ( onground && movingalongground )) ) { return; } bool bWalking = ( speed < flRunSpeed ); // or ducking! VectorCopy( vecOrigin, knee ); VectorCopy( vecOrigin, feet ); height = GetPlayerMaxs()[ 2 ] - GetPlayerMins()[ 2 ]; knee[2] = vecOrigin[2] + 0.2 * height; float flVol; // find out what we're stepping in or on... if ( fLadder ) { psurface = physprops->GetSurfaceData( physprops->GetSurfaceIndex( "ladder" ) ); flVol = 1.0; m_flStepSoundTime = 350; } else if ( enginetrace->GetPointContents( knee ) & MASK_WATER ) { static int iSkipStep = 0; if ( iSkipStep == 0 ) { iSkipStep++; return; } if ( iSkipStep++ == 3 ) { iSkipStep = 0; } psurface = physprops->GetSurfaceData( physprops->GetSurfaceIndex( "wade" ) ); flVol = 0.65; m_flStepSoundTime = 600; } else if ( enginetrace->GetPointContents( feet ) & MASK_WATER ) { psurface = physprops->GetSurfaceData( physprops->GetSurfaceIndex( "water" ) ); flVol = bWalking ? 0.2 : 0.5; m_flStepSoundTime = bWalking ? 400 : 300; } else { if ( !psurface ) return; if ( bWalking ) { m_flStepSoundTime = 400; } else { if ( speed > 200 ) { int speeddiff = PLAYER_SPEED_SPRINT - PLAYER_SPEED_RUN; int diff = speed - PLAYER_SPEED_RUN; float percent = (float)diff / (float)speeddiff; m_flStepSoundTime = 300.0f - 30.0f * percent; } else { m_flStepSoundTime = 400; } } switch ( psurface->game.material ) { default: case CHAR_TEX_CONCRETE: flVol = bWalking ? 0.2 : 0.5; break; case CHAR_TEX_METAL: flVol = bWalking ? 0.2 : 0.5; break; case CHAR_TEX_DIRT: flVol = bWalking ? 0.25 : 0.55; break; case CHAR_TEX_VENT: flVol = bWalking ? 0.4 : 0.7; break; case CHAR_TEX_GRATE: flVol = bWalking ? 0.2 : 0.5; break; case CHAR_TEX_TILE: flVol = bWalking ? 0.2 : 0.5; break; case CHAR_TEX_SLOSH: flVol = bWalking ? 0.2 : 0.5; break; } } m_flStepSoundTime += flDuck; // slower step time if ducking if ( GetFlags() & FL_DUCKING ) { flVol *= 0.65; } // protect us from prediction errors a little bit if ( m_flMinNextStepSoundTime > gpGlobals->curtime ) { return; } m_flMinNextStepSoundTime = gpGlobals->curtime + 0.1f; PlayStepSound( feet, psurface, flVol, false ); }
void CBasePlayer::UpdateStepSound( surfacedata_t *psurface, const Vector &vecOrigin, const Vector &vecVelocity ) { bool bWalking; float fvol = 1.0f; Vector knee; Vector feet; float height; float speed; float velrun; float velwalk; int fLadder; if ( m_flStepSoundTime > 0 ) { m_flStepSoundTime -= 1000.0f * gpGlobals->frametime; if ( m_flStepSoundTime < 0 ) { m_flStepSoundTime = 0; } } if ( m_flStepSoundTime > 0 ) return; if ( GetFlags() & (FL_FROZEN|FL_ATCONTROLS)) return; if ( GetMoveType() == MOVETYPE_NOCLIP ) return; if ( !sv_footsteps.GetFloat() ) return; speed = VectorLength( vecVelocity ); float groundspeed = Vector2DLength( vecVelocity.AsVector2D() ); // determine if we are on a ladder fLadder = false; GetStepSoundVelocities( &velwalk, &velrun ); bool onground = ( GetFlags() & FL_ONGROUND ); bool movingalongground = ( groundspeed > 0.0001f ); bool moving_fast_enough = ( speed >= velwalk ); // To hear step sounds you must be either on a ladder or moving along the ground AND // You must be moving fast enough if ( !moving_fast_enough || !( onground && movingalongground ) ) return; // MoveHelper()->PlayerSetAnimation( PLAYER_WALK ); bWalking = speed < velrun; VectorCopy( vecOrigin, knee ); VectorCopy( vecOrigin, feet ); height = GetPlayerMaxs()[ 2 ] - GetPlayerMins()[ 2 ]; knee[2] = vecOrigin[2] + 0.2 * height; if ( !psurface ) return; SetStepSoundTime( STEPSOUNDTIME_NORMAL, bWalking ); #ifdef CLIENT_DLL if (this == GetLocalPlayer()) { fvol = 0.1f; } #endif PlayStepSound( feet, psurface, fvol, false ); }
void C_ASW_Alien::MarineStepSound( surfacedata_t *psurface, const Vector &vecOrigin, const Vector &vecVelocity ) { int fWalking; float fvol; Vector knee; Vector feet; float height; float speed; float velrun; float velwalk; float flduck; int fLadder; if ( GetFlags() & (FL_FROZEN|FL_ATCONTROLS)) return; if ( GetMoveType() == MOVETYPE_NOCLIP || GetMoveType() == MOVETYPE_OBSERVER ) return; if ( gpGlobals->curtime - sm_flLastFootstepTime < asw_alien_footstep_interval.GetFloat() ) return; speed = VectorLength( vecVelocity ); float groundspeed = Vector2DLength( vecVelocity.AsVector2D() ); // determine if we are on a ladder fLadder = ( GetMoveType() == MOVETYPE_LADDER ); // UNDONE: need defined numbers for run, walk, crouch, crouch run velocities!!!! if ( ( GetFlags() & FL_DUCKING) || fLadder ) { velwalk = 60; // These constants should be based on cl_movespeedkey * cl_forwardspeed somehow velrun = 80; flduck = 100; } else { velwalk = 90; velrun = 220; flduck = 0; } bool onground = true; //( GetFlags() & FL_ONGROUND ); bool movingalongground = ( groundspeed > 0.0f ); bool moving_fast_enough = ( speed >= velwalk ); // To hear step sounds you must be either on a ladder or moving along the ground AND // You must be moving fast enough //Msg("og=%d ma=%d mf=%d\n", onground, movingalongground, moving_fast_enough); if ( !moving_fast_enough || !(fLadder || ( onground && movingalongground )) ) return; // MoveHelper()->PlayerSetAnimation( PLAYER_WALK ); fWalking = speed < velrun; VectorCopy( vecOrigin, knee ); VectorCopy( vecOrigin, feet ); height = 72.0f; // bad knee[2] = vecOrigin[2] + 0.2 * height; // find out what we're stepping in or on... if ( fLadder ) { psurface = physprops->GetSurfaceData( physprops->GetSurfaceIndex( "ladder" ) ); fvol = 0.5; } else if ( enginetrace->GetPointContents( knee ) & MASK_WATER ) { static int iSkipStep = 0; if ( iSkipStep == 0 ) { iSkipStep++; return; } if ( iSkipStep++ == 3 ) { iSkipStep = 0; } psurface = physprops->GetSurfaceData( physprops->GetSurfaceIndex( "wade" ) ); fvol = 0.65; } else if ( enginetrace->GetPointContents( feet ) & MASK_WATER ) { psurface = physprops->GetSurfaceData( physprops->GetSurfaceIndex( "water" ) ); fvol = fWalking ? 0.2 : 0.5; } else { if ( !psurface ) return; switch ( psurface->game.material ) { default: case CHAR_TEX_CONCRETE: fvol = fWalking ? 0.2 : 0.5; break; case CHAR_TEX_METAL: fvol = fWalking ? 0.2 : 0.5; break; case CHAR_TEX_DIRT: fvol = fWalking ? 0.25 : 0.55; break; case CHAR_TEX_VENT: fvol = fWalking ? 0.4 : 0.7; break; case CHAR_TEX_GRATE: fvol = fWalking ? 0.2 : 0.5; break; case CHAR_TEX_TILE: fvol = fWalking ? 0.2 : 0.5; break; case CHAR_TEX_SLOSH: fvol = fWalking ? 0.2 : 0.5; break; } } // play the sound // 65% volume if ducking if ( GetFlags() & FL_DUCKING ) { fvol *= 0.65; } if ( asw_override_footstep_volume.GetFloat() > 0 ) { fvol = asw_override_footstep_volume.GetFloat(); } PlayStepSound( feet, psurface, fvol, false ); }
void Vector2DNormalize(Vector2D *pResult, Vector2D *pVec0) { pResult->x = pResult->x / Vector2DLength(pVec0); pResult->y = pResult->y / Vector2DLength(pVec0); }
void CBasePlayer::UpdateStepSound( surfacedata_t *psurface, const Vector &vecOrigin, const Vector &vecVelocity ) { bool bWalking; float fvol; Vector knee; Vector feet; float height; float speed; float velrun; float velwalk; int fLadder; if ( m_flStepSoundTime > 0 ) { m_flStepSoundTime -= 1000.0f * gpGlobals->frametime; if ( m_flStepSoundTime < 0 ) { m_flStepSoundTime = 0; } } if ( m_flStepSoundTime > 0 ) return; if ( GetFlags() & (FL_FROZEN|FL_ATCONTROLS)) return; if ( GetMoveType() == MOVETYPE_NOCLIP || GetMoveType() == MOVETYPE_OBSERVER ) return; if ( !sv_footsteps.GetFloat() ) return; speed = VectorLength( vecVelocity ); float groundspeed = Vector2DLength( vecVelocity.AsVector2D() ); // determine if we are on a ladder fLadder = ( GetMoveType() == MOVETYPE_LADDER ); GetStepSoundVelocities( &velwalk, &velrun ); bool onground = ( GetFlags() & FL_ONGROUND ); bool movingalongground = ( groundspeed > 0.0001f ); bool moving_fast_enough = ( speed >= velwalk ); #ifdef PORTAL // In Portal we MUST play footstep sounds even when the player is moving very slowly // This is used to count the number of footsteps they take in the challenge mode // -Jeep moving_fast_enough = true; #endif // To hear step sounds you must be either on a ladder or moving along the ground AND // You must be moving fast enough if ( !moving_fast_enough || !(fLadder || ( onground && movingalongground )) ) return; // MoveHelper()->PlayerSetAnimation( PLAYER_WALK ); bWalking = speed < velrun; VectorCopy( vecOrigin, knee ); VectorCopy( vecOrigin, feet ); height = GetPlayerMaxs()[ 2 ] - GetPlayerMins()[ 2 ]; knee[2] = vecOrigin[2] + 0.2 * height; // find out what we're stepping in or on... if ( fLadder ) { psurface = GetLadderSurface(vecOrigin); fvol = 0.5; SetStepSoundTime( STEPSOUNDTIME_ON_LADDER, bWalking ); } else if ( GetWaterLevel() == WL_Waist ) { static int iSkipStep = 0; if ( iSkipStep == 0 ) { iSkipStep++; return; } if ( iSkipStep++ == 3 ) { iSkipStep = 0; } psurface = physprops->GetSurfaceData( physprops->GetSurfaceIndex( "wade" ) ); fvol = 0.65; SetStepSoundTime( STEPSOUNDTIME_WATER_KNEE, bWalking ); } else if ( GetWaterLevel() == WL_Feet ) { psurface = physprops->GetSurfaceData( physprops->GetSurfaceIndex( "water" ) ); fvol = bWalking ? 0.2 : 0.5; SetStepSoundTime( STEPSOUNDTIME_WATER_FOOT, bWalking ); } else { if ( !psurface ) return; SetStepSoundTime( STEPSOUNDTIME_NORMAL, bWalking ); switch ( psurface->game.material ) { default: case CHAR_TEX_CONCRETE: fvol = bWalking ? 0.2 : 0.5; break; case CHAR_TEX_METAL: fvol = bWalking ? 0.2 : 0.5; break; case CHAR_TEX_DIRT: fvol = bWalking ? 0.25 : 0.55; break; case CHAR_TEX_VENT: fvol = bWalking ? 0.4 : 0.7; break; case CHAR_TEX_GRATE: fvol = bWalking ? 0.2 : 0.5; break; case CHAR_TEX_TILE: fvol = bWalking ? 0.2 : 0.5; break; case CHAR_TEX_SLOSH: fvol = bWalking ? 0.2 : 0.5; break; } } // play the sound // 65% volume if ducking if ( GetFlags() & FL_DUCKING ) { fvol *= 0.65; } PlayStepSound( feet, psurface, fvol, false ); }
void Vector2DNormalize(Vector2D *pResult, Vector2D *pVec0) { Vector2DScale(pResult, pVec0, (1 / Vector2DLength(pVec0))); }