Skip to content

Instantly share code, notes, and snippets.

@sigsegv-mvm
Created October 18, 2016 05:53
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sigsegv-mvm/bda5c53af428878af6889635cd787332 to your computer and use it in GitHub Desktop.
Save sigsegv-mvm/bda5c53af428878af6889635cd787332 to your computer and use it in GitHub Desktop.
Backstabs, brought to you by 2D vector dot products
// reverse engineered from TF2 ServerLinux 20151007a by sigsegv
bool CTFKnife::IsBehindAndFacingTarget(CTFPlayer *pVictim)
{
CTFPlayer *pSpy = ToTFPlayer(this->GetPlayerOwner());
if (pSpy == nullptr) return false;
Vector2D wsc_spy_to_victim = (pVictim->WorldSpaceCenter() - pSpy->WorldSpaceCenter()).AsVector2D();
wsc_spy_to_victim.NormalizeInPlace();
Vector temp1; pSpy->EyeVectors(&temp1);
Vector2D eye_spy = temp1.AsVector2D();
eye_spy.NormalizeInPlace();
Vector temp2; pVictim->EyeVectors(&temp2);
Vector2D eye_victim = temp2.AsVector2D();
eye_victim.NormalizeInPlace();
if (DotProduct2D(wsc_spy_to_victim, eye_victim) <= 0.0f) return false;
if (DotProduct2D(wsc_spy_to_victim, eye_spy) <= 0.5f) return false;
if (DotProduct2D(eye_spy, eye_victim) <= -0.3f) return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment