Save a little bit of memory and computation when it comes to bobbing/tilt calculations

This commit is contained in:
UnknownShadow200 2025-03-16 11:38:17 +11:00
parent 053ab631ee
commit 9f05130a04
4 changed files with 28 additions and 29 deletions

View File

@ -136,7 +136,10 @@ static void PerspectiveCamera_UpdateMouse(struct LocalPlayer* p, float delta) {
static void PerspectiveCamera_CalcViewBobbing(struct LocalPlayer* p, float t, float velTiltScale) {
struct Entity* e = &p->Base;
struct Matrix tiltY, velX;
float vel, fall;
float vel, fall, xTilt, yTilt;
float bobStrength, bobbingHor, bobbingVer;
float velTiltStrength;
if (!Game_ViewBobbing) {
Camera.TiltM = Matrix_Identity;
@ -144,17 +147,26 @@ static void PerspectiveCamera_CalcViewBobbing(struct LocalPlayer* p, float t, fl
return;
}
Matrix_RotateZ(&Camera.TiltM, -p->Tilt.TiltX * e->Anim.BobStrength);
Matrix_RotateX(&tiltY, Math_AbsF(p->Tilt.TiltY) * 3.0f * e->Anim.BobStrength);
bobStrength = Math_Lerp(e->Anim.BobStrengthO, e->Anim.BobStrengthN, t);
// See BobbingModel in AnimatedComp_GetCurrent
bobbingHor = Math_CosF(e->Anim.WalkTime) * e->Anim.Swing * (2.5f/16.0f);
bobbingVer = Math_AbsF(Math_SinF(e->Anim.WalkTime)) * e->Anim.Swing * (2.5f/16.0f);
xTilt = Math_CosF(e->Anim.WalkTime) * e->Anim.Swing * (0.15f * MATH_DEG2RAD);
yTilt = Math_SinF(e->Anim.WalkTime) * e->Anim.Swing * (0.15f * MATH_DEG2RAD);
Matrix_RotateZ(&Camera.TiltM, -xTilt * bobStrength);
Matrix_RotateX(&tiltY, Math_AbsF(yTilt) * 3.0f * bobStrength);
Matrix_MulBy(&Camera.TiltM, &tiltY);
Camera.BobbingHor = (e->Anim.BobbingHor * 0.3f) * e->Anim.BobStrength;
Camera.BobbingVer = (e->Anim.BobbingVer * 0.6f) * e->Anim.BobStrength;
Camera.BobbingHor = (bobbingHor * 0.3f) * bobStrength;
Camera.BobbingVer = (bobbingVer * 0.6f) * bobStrength;
velTiltStrength = Math_Lerp(p->Tilt.VelTiltStrengthO, p->Tilt.VelTiltStrengthN, t);
/* When standing on the ground, velocity.y is -0.08 (-gravity) */
/* So add 0.08 to counteract that, so that vel is 0 when standing on ground */
vel = 0.08f + Math_Lerp(p->OldVelocity.y, e->Velocity.y, t);
fall = -vel * 0.05f * p->Tilt.VelTiltStrength / velTiltScale;
fall = -vel * 0.05f * velTiltStrength / velTiltScale;
Matrix_RotateX(&velX, fall);
Matrix_MulBy(&Camera.TiltM, &velX);

View File

@ -707,7 +707,6 @@ static void LocalPlayer_Tick(struct Entity* e, float delta) {
static void LocalPlayer_RenderModel(struct Entity* e, float delta, float t) {
struct LocalPlayer* p = (struct LocalPlayer*)e;
AnimatedComp_GetCurrent(e, t);
TiltComp_GetCurrent(p, &p->Tilt, t);
if (!Camera.Active->isThirdPerson && p == Entities.CurPlayer) return;
Model_Render(e->Model, e);

View File

@ -54,7 +54,7 @@ static void AnimatedComp_CalcHumanAnim(struct AnimatedComp* anim, float idleXRot
void AnimatedComp_Init(struct AnimatedComp* anim) {
Mem_Set(anim, 0, sizeof(struct AnimatedComp));
anim->BobStrength = 1.0f; anim->BobStrengthO = 1.0f; anim->BobStrengthN = 1.0f;
anim->BobStrengthO = 1.0f; anim->BobStrengthN = 1.0f;
}
void AnimatedComp_Update(struct Entity* e, Vec3 oldPos, Vec3 newPos, float delta) {
@ -92,7 +92,6 @@ void AnimatedComp_GetCurrent(struct Entity* e, float t) {
anim->Swing = Math_Lerp(anim->SwingO, anim->SwingN, t);
anim->WalkTime = Math_Lerp(anim->WalkTimeO, anim->WalkTimeN, t);
anim->BobStrength = Math_Lerp(anim->BobStrengthO, anim->BobStrengthN, t);
anim->LeftArmX = (Math_CosF(anim->WalkTime) * anim->Swing * ANIM_ARM_MAX) - idleXRot;
anim->LeftArmZ = -idleZRot;
@ -102,8 +101,7 @@ void AnimatedComp_GetCurrent(struct Entity* e, float t) {
anim->RightLegX = -anim->LeftLegX; anim->RightLegZ = -anim->LeftLegZ;
anim->RightArmX = -anim->LeftArmX; anim->RightArmZ = -anim->LeftArmZ;
anim->BobbingHor = Math_CosF(anim->WalkTime) * anim->Swing * (2.5f/16.0f);
anim->BobbingVer = Math_AbsF(Math_SinF(anim->WalkTime)) * anim->Swing * (2.5f/16.0f);
// See BobbingHor/BobbingVer in PerspectiveCamera_CalcViewBobbing
anim->BobbingModel = Math_AbsF(Math_CosF(anim->WalkTime)) * anim->Swing * (4.0f/16.0f);
if (e->Model->calcHumanAnims && !Game_SimpleArmsAnim) {
@ -116,8 +114,8 @@ void AnimatedComp_GetCurrent(struct Entity* e, float t) {
*------------------------------------------------------TiltComponent------------------------------------------------------*
*#########################################################################################################################*/
void TiltComp_Init(struct TiltComp* anim) {
anim->TiltX = 0.0f; anim->TiltY = 0.0f; anim->VelTiltStrength = 1.0f;
anim->VelTiltStrengthO = 1.0f; anim->VelTiltStrengthN = 1.0f;
anim->VelTiltStrengthO = 1.0f;
anim->VelTiltStrengthN = 1.0f;
}
void TiltComp_Update(struct LocalPlayer* p, struct TiltComp* anim, float delta) {
@ -130,14 +128,6 @@ void TiltComp_Update(struct LocalPlayer* p, struct TiltComp* anim, float delta)
}
}
void TiltComp_GetCurrent(struct LocalPlayer* p, struct TiltComp* anim, float t) {
struct AnimatedComp* pAnim = &p->Base.Anim;
anim->VelTiltStrength = Math_Lerp(anim->VelTiltStrengthO, anim->VelTiltStrengthN, t);
anim->TiltX = Math_CosF(pAnim->WalkTime) * pAnim->Swing * (0.15f * MATH_DEG2RAD);
anim->TiltY = Math_SinF(pAnim->WalkTime) * pAnim->Swing * (0.15f * MATH_DEG2RAD);
}
/*########################################################################################################################*
*-----------------------------------------------------HacksComponent------------------------------------------------------*

View File

@ -14,8 +14,8 @@ struct LocalPlayer;
/* Entity component that performs model animation depending on movement speed and time */
struct AnimatedComp {
float BobbingHor, BobbingVer, BobbingModel;
float WalkTime, Swing, BobStrength;
float BobbingModel;
float WalkTime, Swing;
float WalkTimeO, WalkTimeN, SwingO, SwingN, BobStrengthO, BobStrengthN;
float LeftLegX, LeftLegZ, RightLegX, RightLegZ;
@ -28,13 +28,11 @@ void AnimatedComp_GetCurrent(struct Entity* entity, float t);
/* Entity component that performs tilt animation depending on movement speed and time */
struct TiltComp {
float TiltX, TiltY, VelTiltStrength;
float VelTiltStrengthO, VelTiltStrengthN;
};
void TiltComp_Init(struct TiltComp* anim);
void TiltComp_Update(struct LocalPlayer* p, struct TiltComp* anim, float delta);
void TiltComp_GetCurrent(struct LocalPlayer* p, struct TiltComp* anim, float t);
/* Entity component that performs management of hack states */
struct HacksComp {