Skip to content

Instantly share code, notes, and snippets.

@klaussilveira
Created December 12, 2025 16:07
Show Gist options
  • Select an option

  • Save klaussilveira/c27bf91e25f257ab564d89bee2996dfc to your computer and use it in GitHub Desktop.

Select an option

Save klaussilveira/c27bf91e25f257ab564d89bee2996dfc to your computer and use it in GitHub Desktop.
D3 AdjustBodyAngles from view instead of Up/Down anim blend
void idPlayer::AdjustBodyAngles( void ) {
idMat3 lookAxis;
idMat3 legsAxis;
bool blend;
float diff;
float frac;
float upBlend;
float forwardBlend;
float downBlend;
if ( health < 0 ) {
return;
}
blend = true;
if ( !physicsObj.HasGroundContacts() ) {
idealLegsYaw = 0.0f;
legsForward = true;
} else if ( usercmd.forwardmove < 0 ) {
idealLegsYaw = idMath::AngleNormalize180( idVec3( -usercmd.forwardmove, usercmd.rightmove, 0.0f ).ToYaw() );
legsForward = false;
} else if ( usercmd.forwardmove > 0 ) {
idealLegsYaw = idMath::AngleNormalize180( idVec3( usercmd.forwardmove, -usercmd.rightmove, 0.0f ).ToYaw() );
legsForward = true;
} else if ( ( usercmd.rightmove != 0 ) && physicsObj.IsCrouching() ) {
if ( !legsForward ) {
idealLegsYaw = idMath::AngleNormalize180( idVec3( idMath::Abs( usercmd.rightmove ), usercmd.rightmove, 0.0f ).ToYaw() );
} else {
idealLegsYaw = idMath::AngleNormalize180( idVec3( idMath::Abs( usercmd.rightmove ), -usercmd.rightmove, 0.0f ).ToYaw() );
}
} else if ( usercmd.rightmove != 0 ) {
idealLegsYaw = 0.0f;
legsForward = true;
} else {
legsForward = true;
diff = idMath::Fabs( idealLegsYaw - legsYaw );
idealLegsYaw = idealLegsYaw - idMath::AngleNormalize180( viewAngles.yaw - oldViewYaw );
if ( diff < 0.1f ) {
legsYaw = idealLegsYaw;
blend = false;
}
}
if ( !physicsObj.IsCrouching() ) {
legsForward = true;
}
oldViewYaw = viewAngles.yaw;
AI_TURN_LEFT = false;
AI_TURN_RIGHT = false;
if ( idealLegsYaw < -45.0f ) {
idealLegsYaw = 0;
AI_TURN_RIGHT = true;
blend = true;
} else if ( idealLegsYaw > 45.0f ) {
idealLegsYaw = 0;
AI_TURN_LEFT = true;
blend = true;
}
if ( blend ) {
legsYaw = legsYaw * 0.9f + idealLegsYaw * 0.1f;
}
legsAxis = idAngles( 0.0f, -legsYaw, 0.0f ).ToMat3();
animator.SetJointAxis( hipJoint, JOINTMOD_WORLD, legsAxis );
// Pitch the torso up and down based on the view pitch with a clamp
const float maxTorsoPitch = 60.0f;
const float torsoPitch = idMath::ClampFloat( -maxTorsoPitch, maxTorsoPitch, viewAngles.pitch );
// Apply the pitch on the chest joint so it layers on top of the base anim
lookAxis = idAngles( torsoPitch, 0.0f, 0.0f ).ToMat3();
animator.SetJointAxis( chestJoint, JOINTMOD_LOCAL, lookAxis );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment