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,25 +136,37 @@ static void PerspectiveCamera_UpdateMouse(struct LocalPlayer* p, float delta) {
static void PerspectiveCamera_CalcViewBobbing(struct LocalPlayer* p, float t, float velTiltScale) { static void PerspectiveCamera_CalcViewBobbing(struct LocalPlayer* p, float t, float velTiltScale) {
struct Entity* e = &p->Base; struct Entity* e = &p->Base;
struct Matrix tiltY, velX; 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.TiltM = Matrix_Identity;
Camera.TiltPitch = 0.0f; 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); xTilt = Math_CosF(e->Anim.WalkTime) * e->Anim.Swing * (0.15f * MATH_DEG2RAD);
Matrix_RotateX(&tiltY, Math_AbsF(p->Tilt.TiltY) * 3.0f * e->Anim.BobStrength); 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); Matrix_MulBy(&Camera.TiltM, &tiltY);
Camera.BobbingHor = (e->Anim.BobbingHor * 0.3f) * e->Anim.BobStrength; Camera.BobbingHor = (bobbingHor * 0.3f) * bobStrength;
Camera.BobbingVer = (e->Anim.BobbingVer * 0.6f) * e->Anim.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) */ /* 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 */ /* 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); 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_RotateX(&velX, fall);
Matrix_MulBy(&Camera.TiltM, &velX); 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) { static void LocalPlayer_RenderModel(struct Entity* e, float delta, float t) {
struct LocalPlayer* p = (struct LocalPlayer*)e; struct LocalPlayer* p = (struct LocalPlayer*)e;
AnimatedComp_GetCurrent(e, t); AnimatedComp_GetCurrent(e, t);
TiltComp_GetCurrent(p, &p->Tilt, t);
if (!Camera.Active->isThirdPerson && p == Entities.CurPlayer) return; if (!Camera.Active->isThirdPerson && p == Entities.CurPlayer) return;
Model_Render(e->Model, e); 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) { void AnimatedComp_Init(struct AnimatedComp* anim) {
Mem_Set(anim, 0, sizeof(struct AnimatedComp)); 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) { 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 idleXRot = Math_SinF(idleTime * ANIM_IDLE_XPERIOD) * ANIM_IDLE_MAX;
float idleZRot = Math_CosF(idleTime * ANIM_IDLE_ZPERIOD) * ANIM_IDLE_MAX + 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->Swing = Math_Lerp(anim->SwingO, anim->SwingN, t);
anim->WalkTime = Math_Lerp(anim->WalkTimeO, anim->WalkTimeN, 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->LeftArmX = (Math_CosF(anim->WalkTime) * anim->Swing * ANIM_ARM_MAX) - idleXRot;
anim->LeftArmZ = -idleZRot; anim->LeftArmZ = -idleZRot;
@ -102,8 +101,7 @@ void AnimatedComp_GetCurrent(struct Entity* e, float t) {
anim->RightLegX = -anim->LeftLegX; anim->RightLegZ = -anim->LeftLegZ; anim->RightLegX = -anim->LeftLegX; anim->RightLegZ = -anim->LeftLegZ;
anim->RightArmX = -anim->LeftArmX; anim->RightArmZ = -anim->LeftArmZ; anim->RightArmX = -anim->LeftArmX; anim->RightArmZ = -anim->LeftArmZ;
anim->BobbingHor = Math_CosF(anim->WalkTime) * anim->Swing * (2.5f/16.0f); // See BobbingHor/BobbingVer in PerspectiveCamera_CalcViewBobbing
anim->BobbingVer = Math_AbsF(Math_SinF(anim->WalkTime)) * anim->Swing * (2.5f/16.0f);
anim->BobbingModel = Math_AbsF(Math_CosF(anim->WalkTime)) * anim->Swing * (4.0f/16.0f); anim->BobbingModel = Math_AbsF(Math_CosF(anim->WalkTime)) * anim->Swing * (4.0f/16.0f);
if (e->Model->calcHumanAnims && !Game_SimpleArmsAnim) { if (e->Model->calcHumanAnims && !Game_SimpleArmsAnim) {
@ -116,8 +114,8 @@ void AnimatedComp_GetCurrent(struct Entity* e, float t) {
*------------------------------------------------------TiltComponent------------------------------------------------------* *------------------------------------------------------TiltComponent------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
void TiltComp_Init(struct TiltComp* anim) { void TiltComp_Init(struct TiltComp* anim) {
anim->TiltX = 0.0f; anim->TiltY = 0.0f; anim->VelTiltStrength = 1.0f; anim->VelTiltStrengthO = 1.0f;
anim->VelTiltStrengthO = 1.0f; anim->VelTiltStrengthN = 1.0f; anim->VelTiltStrengthN = 1.0f;
} }
void TiltComp_Update(struct LocalPlayer* p, struct TiltComp* anim, float delta) { 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------------------------------------------------------* *-----------------------------------------------------HacksComponent------------------------------------------------------*

View File

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