From 9868d4631cca99954cf4854320475f9f67830314 Mon Sep 17 00:00:00 2001 From: Holger Frydrych Date: Mon, 20 Jan 2025 14:18:06 +0100 Subject: [PATCH] Only trigger melee attacks when they would actually hit something --- Code/Melee.cpp | 8 ++++---- Code/Melee.h | 8 ++++---- Code/VR/OpenXRInput.cpp | 10 +++++++++- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Code/Melee.cpp b/Code/Melee.cpp index 5e782be..176fb43 100644 --- a/Code/Melee.cpp +++ b/Code/Melee.cpp @@ -338,7 +338,7 @@ const char *CMelee::GetDamageType() const //------------------------------------------------------------------------ -bool CMelee::PerformRayTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote) +bool CMelee::PerformRayTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote, bool execute) { IEntity *pOwner = m_pWeapon->GetOwner(); IPhysicalEntity *pIgnore = pOwner?pOwner->GetPhysics():0; @@ -359,7 +359,7 @@ bool CMelee::PerformRayTest(const Vec3 &pos, const Vec3 &dir, float strength, bo } //================================================================================= - if (n>0) + if (n>0 && execute) { Hit(&hit, dir, strength, remote); Impulse(hit.pt, dir, hit.n, hit.pCollider, hit.partid, hit.ipart, hit.surface_idx, strength); @@ -369,7 +369,7 @@ bool CMelee::PerformRayTest(const Vec3 &pos, const Vec3 &dir, float strength, bo } //------------------------------------------------------------------------ -bool CMelee::PerformCylinderTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote) +bool CMelee::PerformCylinderTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote, bool execute) { IEntity *pOwner = m_pWeapon->GetOwner(); IPhysicalEntity *pIgnore = pOwner?pOwner->GetPhysics():0; @@ -436,7 +436,7 @@ bool CMelee::PerformCylinderTest(const Vec3 &pos, const Vec3 &dir, float strengt } - if (closestc) + if (closestc && execute) { IPhysicalEntity *pCollider = gEnv->pPhysicalWorld->GetPhysicalEntityById(closestc->iPrim[0]); diff --git a/Code/Melee.h b/Code/Melee.h index e35544a..a72c540 100644 --- a/Code/Melee.h +++ b/Code/Melee.h @@ -8,7 +8,7 @@ Description: Beam Fire Mode Implementation ------------------------------------------------------------------------- History: -- 23:3:2006 13:02 : Created by Márcio Martins +- 23:3:2006 13:02 : Created by M�rcio Martins *************************************************************************/ #ifndef __MELEE_H__ @@ -187,8 +187,8 @@ class CMelee : virtual void Unlock() {}; //~IFireMode - virtual bool PerformRayTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote); - virtual bool PerformCylinderTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote); + virtual bool PerformRayTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote, bool execute = true); + virtual bool PerformCylinderTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote, bool execute = true); virtual void Hit(ray_hit *hit, const Vec3 &dir, float damageScale, bool remote); virtual void Hit(geom_contact *contact, const Vec3 &dir, float damageScale, bool remote); virtual void Hit(const Vec3 &pt, const Vec3 &dir, const Vec3 &normal, IPhysicalEntity *pCollider, int partId, int ipart, int surfaceIdx, float damageScale, bool remote); @@ -225,4 +225,4 @@ class CMelee : }; -#endif //__MELEE_H__ \ No newline at end of file +#endif //__MELEE_H__ diff --git a/Code/VR/OpenXRInput.cpp b/Code/VR/OpenXRInput.cpp index c091d11..bcd6bf3 100644 --- a/Code/VR/OpenXRInput.cpp +++ b/Code/VR/OpenXRInput.cpp @@ -5,6 +5,7 @@ #include "GameCVars.h" #include "imgui.h" #include "IPlayerInput.h" +#include "Melee.h" #include "OpenXRRuntime.h" #include "Player.h" #include "VRManager.h" @@ -519,7 +520,14 @@ void OpenXRInput::UpdateMeleeAttacks() // also, only consider movements that are somewhat in the direction of where you look at if (GetGripAmount(i) >= .95f && controllerVelocity.GetLength() > g_pGameCVars->vr_melee_trigger_velocity && controllerVelocity.Dot(hmdForward) > 0.7f) { - weapon->MeleeAttack(); + // check if the attack would actually hit something, otherwise don't perform one + SMovementState info; + player->GetMovementController()->GetMovementState(info); + Vec3 pos = info.eyePosition; + Vec3 dir = info.eyeDirection; + CMelee* melee = static_cast(weapon->GetMeleeFireMode()); + if (melee->PerformRayTest(pos, dir, 1.f, false, false) || melee->PerformCylinderTest(pos, dir, 1.f, false, false)) + weapon->MeleeAttack(); break; } }