From 9f05130a0449120131d36492b65c62601029c45e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 16 Mar 2025 11:38:17 +1100 Subject: [PATCH] Save a little bit of memory and computation when it comes to bobbing/tilt calculations --- src/Camera.c | 28 ++++++++++++++++++++-------- src/Entity.c | 1 - src/EntityComponents.c | 22 ++++++---------------- src/EntityComponents.h | 6 ++---- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/Camera.c b/src/Camera.c index 7e39263b4..7e6d661c4 100644 --- a/src/Camera.c +++ b/src/Camera.c @@ -136,25 +136,37 @@ 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) { + if (!Game_ViewBobbing) { Camera.TiltM = Matrix_Identity; Camera.TiltPitch = 0.0f; - return; + return; } + + 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); - Matrix_RotateZ(&Camera.TiltM, -p->Tilt.TiltX * e->Anim.BobStrength); - Matrix_RotateX(&tiltY, Math_AbsF(p->Tilt.TiltY) * 3.0f * e->Anim.BobStrength); + 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); diff --git a/src/Entity.c b/src/Entity.c index a581d2855..2d414b592 100644 --- a/src/Entity.c +++ b/src/Entity.c @@ -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); diff --git a/src/EntityComponents.c b/src/EntityComponents.c index 962713385..be2a3f9f5 100644 --- a/src/EntityComponents.c +++ b/src/EntityComponents.c @@ -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) { @@ -90,9 +90,8 @@ void AnimatedComp_GetCurrent(struct Entity* e, float t) { float idleXRot = Math_SinF(idleTime * ANIM_IDLE_XPERIOD) * ANIM_IDLE_MAX; float idleZRot = Math_CosF(idleTime * ANIM_IDLE_ZPERIOD) * ANIM_IDLE_MAX + ANIM_IDLE_MAX; - 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->Swing = Math_Lerp(anim->SwingO, anim->SwingN, t); + anim->WalkTime = Math_Lerp(anim->WalkTimeO, anim->WalkTimeN, 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------------------------------------------------------* diff --git a/src/EntityComponents.h b/src/EntityComponents.h index dc095d6a2..3e7718c30 100644 --- a/src/EntityComponents.h +++ b/src/EntityComponents.h @@ -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 {