From e3524e8e485dc24cde39314c539ffc32b9aab599 Mon Sep 17 00:00:00 2001 From: Petr Bartos Date: Sat, 1 Apr 2023 18:48:42 +0200 Subject: [PATCH] Add use haptic feedback; Fix use in 3rd person mode --- .../Android/jni/JKXR/JKXR_SurfaceView.cpp | 3 ++- Projects/Android/jni/JKXR/VrClientInfo.h | 2 ++ .../Android/jni/OpenJK/code/game/g_active.cpp | 12 ++++++--- .../jni/OpenJK/code/game/g_trigger.cpp | 11 ++++++-- .../Android/jni/OpenJK/code/game/g_utils.cpp | 27 ++++++++++++++----- .../jni/OpenJK/codeJK2/game/g_active.cpp | 11 +++++--- .../jni/OpenJK/codeJK2/game/g_trigger.cpp | 11 ++++++-- .../jni/OpenJK/codeJK2/game/g_utils.cpp | 27 ++++++++++++++----- 8 files changed, 81 insertions(+), 23 deletions(-) diff --git a/Projects/Android/jni/JKXR/JKXR_SurfaceView.cpp b/Projects/Android/jni/JKXR/JKXR_SurfaceView.cpp index 87c68a0..2a6515f 100644 --- a/Projects/Android/jni/JKXR/JKXR_SurfaceView.cpp +++ b/Projects/Android/jni/JKXR/JKXR_SurfaceView.cpp @@ -515,7 +515,8 @@ void VR_HapticEvent(const char* event, int position, int flags, int intensity, f { TBXR_Vibrate(400, weaponFireChannel, 1.0); } - else if (strcmp(event, "selector_icon") == 0) + else if (strcmp(event, "selector_icon") == 0 || + strcmp(event, "use_button") == 0 ) { //Quick blip TBXR_Vibrate(50, flags, 1.0); diff --git a/Projects/Android/jni/JKXR/VrClientInfo.h b/Projects/Android/jni/JKXR/VrClientInfo.h index ba0cf47..a56de0f 100644 --- a/Projects/Android/jni/JKXR/VrClientInfo.h +++ b/Projects/Android/jni/JKXR/VrClientInfo.h @@ -10,6 +10,7 @@ #define USE_GESTURE_OFF_HAND 1 #define USE_GESTURE_WEAPON_HAND 2 +#define USE_HAPTIC_FEEDBACK_DELAY 500 typedef struct { bool cin_camera; // cinematic camera taken over @@ -92,6 +93,7 @@ typedef struct { float maxHeight; float curHeight; int useGestureState; + int useHapticFeedbackTime[2]; ////////////////////////////////////// // Test stuff for weapon alignment diff --git a/Projects/Android/jni/OpenJK/code/game/g_active.cpp b/Projects/Android/jni/OpenJK/code/game/g_active.cpp index 1fe0264..a5990c7 100644 --- a/Projects/Android/jni/OpenJK/code/game/g_active.cpp +++ b/Projects/Android/jni/OpenJK/code/game/g_active.cpp @@ -29,6 +29,7 @@ along with this program; if not, see . #include "g_vehicles.h" #include "b_local.h" #include "g_navigator.h" +#include #ifdef _DEBUG #include @@ -1354,7 +1355,7 @@ void ClientImpacts( gentity_t *ent, pmove_t *pm ) { const float TOUCH_DISTANCE = 1.0f; const vec3_t TOUCH_RANGE = { 4, 4, 4 }; -void G_TouchTriggersWithHand( gentity_t *ent, vec3_t src, vec3_t vf ) { +void G_TouchTriggersWithHand( bool offHand, gentity_t *ent, vec3_t src, vec3_t vf ) { vec3_t dest, mins, maxs; gentity_t *touch[MAX_GENTITIES], *hit; qboolean touched[MAX_GENTITIES]; @@ -1396,6 +1397,11 @@ void G_TouchTriggersWithHand( gentity_t *ent, vec3_t src, vec3_t vf ) { touched[i] = qtrue; memset( &trace, 0, sizeof(trace) ); if ( hit->e_TouchFunc != touchF_NULL ) { + int channel = vr->right_handed != offHand ? 1 : 2; + if (level.time > vr->useHapticFeedbackTime[channel - 1]) { + cgi_HapticEvent("use_button", 0, channel, 60, 0, 0); + vr->useHapticFeedbackTime[channel - 1] = level.time + USE_HAPTIC_FEEDBACK_DELAY; + } GEntity_TouchFunc(hit, ent, &trace); } } @@ -1540,13 +1546,13 @@ void G_TouchTriggersLerped( gentity_t *ent ) { vec3_t src, angles, vf; BG_CalculateVRWeaponPosition(src, angles); AngleVectors( angles, vf, NULL, NULL ); - G_TouchTriggersWithHand( ent, src, vf ); + G_TouchTriggersWithHand( false, ent, src, vf ); } if( ent->client->usercmd.buttons & BUTTON_ALT_USE ) { vec3_t src, angles, vf; BG_CalculateVROffHandPosition(src, angles); AngleVectors( angles, vf, NULL, NULL ); - G_TouchTriggersWithHand( ent, src, vf ); + G_TouchTriggersWithHand( true, ent, src, vf ); } } } diff --git a/Projects/Android/jni/OpenJK/code/game/g_trigger.cpp b/Projects/Android/jni/OpenJK/code/game/g_trigger.cpp index 16652ad..4414004 100644 --- a/Projects/Android/jni/OpenJK/code/game/g_trigger.cpp +++ b/Projects/Android/jni/OpenJK/code/game/g_trigger.cpp @@ -264,9 +264,9 @@ void Touch_Multi( gentity_t *self, gentity_t *other, trace_t *trace ) if ( other->client ) { - if ( (other->client->ps.clientNum == 0) && (self->spawnflags & 4) ) + if ( (other->client->ps.clientNum == 0) && (self->spawnflags & 4) && !thirdPersonActive ) { - // In case of USE_BUTTON, check facing by controller and not by head + // In case of USE_BUTTON, check facing by controller and not by head (if not in 3rd person) vec3_t origin, angles; BG_CalculateVRWeaponPosition(origin, angles); AngleVectors( angles, forward, NULL, NULL ); @@ -340,6 +340,13 @@ void Touch_Multi( gentity_t *self, gentity_t *other, trace_t *trace ) if ( self->spawnflags & 4 ) {//USE_BUTTON + if (other->client->ps.clientNum == 0 && !useGestureAllowed) { + int channel = vr->right_handed ? 1 : 2; + if (level.time > vr->useHapticFeedbackTime[channel - 1]) { + cgi_HapticEvent("use_button", 0, channel, 60, 0, 0); + vr->useHapticFeedbackTime[channel - 1] = level.time + + USE_HAPTIC_FEEDBACK_DELAY; + } + } NPC_SetAnim( other, SETANIM_TORSO, BOTH_BUTTON_HOLD, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLD ); /* if ( !VectorLengthSquared( other->client->ps.velocity ) && !PM_CrouchAnim( other->client->ps.legsAnim ) ) diff --git a/Projects/Android/jni/OpenJK/code/game/g_utils.cpp b/Projects/Android/jni/OpenJK/code/game/g_utils.cpp index 7af7d81..908fa5c 100644 --- a/Projects/Android/jni/OpenJK/code/game/g_utils.cpp +++ b/Projects/Android/jni/OpenJK/code/game/g_utils.cpp @@ -1759,7 +1759,7 @@ Try and use an entity in the world, directly ahead of us #define USE_DISTANCE_BUTTON 64.0f #define USE_DISTANCE_GESTURE 16.0f -void TryUse_Internal( gentity_t *ent, vec3_t src, vec3_t vf ) +void TryUse_Internal( bool offHand, gentity_t *ent, vec3_t src, vec3_t vf ) { gentity_t *target; trace_t trace; @@ -1808,6 +1808,13 @@ void TryUse_Internal( gentity_t *ent, vec3_t src, vec3_t vf ) } */ //ent->client->ps.weaponTime = ent->client->ps.torsoAnimTimer; + if (ent->client->ps.clientNum == 0) { + int channel = vr->right_handed != offHand ? 1 : 2; + if (level.time > vr->useHapticFeedbackTime[channel - 1]) { + cgi_HapticEvent("use_button", 0, channel, 60, 0, 0); + vr->useHapticFeedbackTime[channel - 1] = level.time + + USE_HAPTIC_FEEDBACK_DELAY; + } + } GEntity_UseFunc( target, ent, ent ); return; } @@ -1818,6 +1825,13 @@ void TryUse_Internal( gentity_t *ent, vec3_t src, vec3_t vf ) && (target->client->playerTeam == ent->client->playerTeam || target->client->playerTeam == TEAM_NEUTRAL) && !(target->NPC->scriptFlags&SCF_NO_RESPONSE) ) { + if (ent->client->ps.clientNum == 0) { + int channel = vr->right_handed != offHand ? 1 : 2; + if (level.time > vr->useHapticFeedbackTime[channel - 1]) { + cgi_HapticEvent("use_button", 0, channel, 60, 0, 0); + vr->useHapticFeedbackTime[channel - 1] = level.time + + USE_HAPTIC_FEEDBACK_DELAY; + } + } NPC_UseResponse ( target, ent, qfalse ); return; } @@ -1847,15 +1861,16 @@ void TryUse( gentity_t *ent ) { return; }*/ + bool thirdPersonActive = gi.cvar("cg_thirdPerson", "0", CVAR_TEMP)->integer; vec3_t src, angles, vf; - if (ent->client->ps.clientNum == 0) { + if (ent->client->ps.clientNum == 0 && !thirdPersonActive) { BG_CalculateVRWeaponPosition(src, angles); AngleVectors( angles, vf, NULL, NULL ); - TryUse_Internal(ent, src, vf); + TryUse_Internal(false, ent, src, vf); } else { VectorCopy(ent->client->renderInfo.eyePoint, src); AngleVectors(ent->client->ps.viewangles, vf, NULL, NULL); - TryUse_Internal(ent, src, vf); + TryUse_Internal(false, ent, src, vf); } } @@ -1881,11 +1896,11 @@ void TryAltUse( gentity_t *ent ) { if (ent->client->ps.clientNum == 0) { BG_CalculateVROffHandPosition(src, angles); AngleVectors( angles, vf, NULL, NULL ); - TryUse_Internal(ent, src, vf); + TryUse_Internal(true, ent, src, vf); } else { VectorCopy(ent->client->renderInfo.eyePoint, src); AngleVectors(ent->client->ps.viewangles, vf, NULL, NULL); - TryUse_Internal(ent, src, vf); + TryUse_Internal(false, ent, src, vf); } } diff --git a/Projects/Android/jni/OpenJK/codeJK2/game/g_active.cpp b/Projects/Android/jni/OpenJK/codeJK2/game/g_active.cpp index c2aa96d..0d4cc47 100644 --- a/Projects/Android/jni/OpenJK/codeJK2/game/g_active.cpp +++ b/Projects/Android/jni/OpenJK/codeJK2/game/g_active.cpp @@ -825,7 +825,7 @@ void ClientImpacts( gentity_t *ent, pmove_t *pm ) { const float TOUCH_DISTANCE = 1.0f; const vec3_t TOUCH_RANGE = { 4, 4, 4 }; -void G_TouchTriggersWithHand( gentity_t *ent, vec3_t src, vec3_t vf ) { +void G_TouchTriggersWithHand( bool offHand, gentity_t *ent, vec3_t src, vec3_t vf ) { vec3_t dest, mins, maxs; gentity_t *touch[MAX_GENTITIES], *hit; qboolean touched[MAX_GENTITIES]; @@ -867,6 +867,11 @@ void G_TouchTriggersWithHand( gentity_t *ent, vec3_t src, vec3_t vf ) { touched[i] = qtrue; memset( &trace, 0, sizeof(trace) ); if ( hit->e_TouchFunc != touchF_NULL ) { + int channel = vr->right_handed != offHand ? 1 : 2; + if (level.time > vr->useHapticFeedbackTime[channel - 1]) { + cgi_HapticEvent("use_button", 0, channel, 60, 0, 0); + vr->useHapticFeedbackTime[channel - 1] = level.time + USE_HAPTIC_FEEDBACK_DELAY; + } GEntity_TouchFunc(hit, ent, &trace); } } @@ -1003,13 +1008,13 @@ void G_TouchTriggersLerped( gentity_t *ent ) { vec3_t src, angles, vf; BG_CalculateVRWeaponPosition(src, angles); AngleVectors( angles, vf, NULL, NULL ); - G_TouchTriggersWithHand( ent, src, vf ); + G_TouchTriggersWithHand(false, ent, src, vf ); } if( ent->client->usercmd.buttons & BUTTON_ALT_USE ) { vec3_t src, angles, vf; BG_CalculateVROffHandPosition(src, angles); AngleVectors( angles, vf, NULL, NULL ); - G_TouchTriggersWithHand( ent, src, vf ); + G_TouchTriggersWithHand(true, ent, src, vf ); } } } diff --git a/Projects/Android/jni/OpenJK/codeJK2/game/g_trigger.cpp b/Projects/Android/jni/OpenJK/codeJK2/game/g_trigger.cpp index 88aa75a..81c62b9 100644 --- a/Projects/Android/jni/OpenJK/codeJK2/game/g_trigger.cpp +++ b/Projects/Android/jni/OpenJK/codeJK2/game/g_trigger.cpp @@ -234,9 +234,9 @@ void Touch_Multi( gentity_t *self, gentity_t *other, trace_t *trace ) if ( other->client ) { - if ( (other->client->ps.clientNum == 0) && (self->spawnflags & 4) ) + if ( (other->client->ps.clientNum == 0) && (self->spawnflags & 4) && !thirdPersonActive ) { - // In case of USE_BUTTON, check facing by controller and not by head + // In case of USE_BUTTON, check facing by controller and not by head (if not in 3rd person) vec3_t origin, angles; BG_CalculateVRWeaponPosition(origin, angles); AngleVectors( angles, forward, NULL, NULL ); @@ -323,6 +323,13 @@ void Touch_Multi( gentity_t *self, gentity_t *other, trace_t *trace ) if ( self->spawnflags & 4 ) {//USE_BUTTON + if (other->client->ps.clientNum == 0 && !useGestureAllowed) { + int channel = vr->right_handed ? 1 : 2; + if (level.time > vr->useHapticFeedbackTime[channel - 1]) { + cgi_HapticEvent("use_button", 0, channel, 60, 0, 0); + vr->useHapticFeedbackTime[channel - 1] = level.time + + USE_HAPTIC_FEEDBACK_DELAY; + } + } NPC_SetAnim( other, SETANIM_TORSO, BOTH_BUTTON_HOLD, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLD ); /* if ( !VectorLengthSquared( other->client->ps.velocity ) && !PM_CrouchAnim( other->client->ps.legsAnim ) ) diff --git a/Projects/Android/jni/OpenJK/codeJK2/game/g_utils.cpp b/Projects/Android/jni/OpenJK/codeJK2/game/g_utils.cpp index 1a7bb78..20c3669 100644 --- a/Projects/Android/jni/OpenJK/codeJK2/game/g_utils.cpp +++ b/Projects/Android/jni/OpenJK/codeJK2/game/g_utils.cpp @@ -1525,7 +1525,7 @@ Try and use an entity in the world, directly ahead of us #define USE_DISTANCE_BUTTON 64.0f #define USE_DISTANCE_GESTURE 16.0f -void TryUse_Internal( gentity_t *ent, vec3_t src, vec3_t vf ) { +void TryUse_Internal( bool offHand, gentity_t *ent, vec3_t src, vec3_t vf ) { gentity_t *target; trace_t trace; vec3_t dest; @@ -1565,6 +1565,13 @@ void TryUse_Internal( gentity_t *ent, vec3_t src, vec3_t vf ) { } */ //ent->client->ps.weaponTime = ent->client->ps.torsoAnimTimer; + if (ent->client->ps.clientNum == 0) { + int channel = vr->right_handed != offHand ? 1 : 2; + if (level.time > vr->useHapticFeedbackTime[channel - 1]) { + cgi_HapticEvent("use_button", 0, channel, 60, 0, 0); + vr->useHapticFeedbackTime[channel - 1] = level.time + + USE_HAPTIC_FEEDBACK_DELAY; + } + } GEntity_UseFunc( target, ent, ent ); return; } @@ -1575,6 +1582,13 @@ void TryUse_Internal( gentity_t *ent, vec3_t src, vec3_t vf ) { && (target->client->playerTeam == ent->client->playerTeam || target->client->playerTeam == TEAM_NEUTRAL) && !(target->NPC->scriptFlags&SCF_NO_RESPONSE) ) { + if (ent->client->ps.clientNum == 0) { + int channel = vr->right_handed != offHand ? 1 : 2; + if (level.time > vr->useHapticFeedbackTime[channel - 1]) { + cgi_HapticEvent("use_button", 0, channel, 60, 0, 0); + vr->useHapticFeedbackTime[channel - 1] = level.time + + USE_HAPTIC_FEEDBACK_DELAY; + } + } NPC_UseResponse ( target, ent, qfalse ); return; } @@ -1593,15 +1607,16 @@ void TryUse( gentity_t *ent ) { return; } + bool thirdPersonActive = gi.cvar("cg_thirdPerson", "0", CVAR_TEMP)->integer; vec3_t src, angles, vf; - if (ent->client->ps.clientNum == 0) { + if (ent->client->ps.clientNum == 0 && !thirdPersonActive) { BG_CalculateVRWeaponPosition(src, angles); AngleVectors( angles, vf, NULL, NULL ); - TryUse_Internal(ent, src, vf); + TryUse_Internal(false, ent, src, vf); } else { VectorCopy(ent->client->renderInfo.eyePoint, src); AngleVectors(ent->client->ps.viewangles, vf, NULL, NULL); - TryUse_Internal(ent, src, vf); + TryUse_Internal(false, ent, src, vf); } } @@ -1616,11 +1631,11 @@ void TryAltUse( gentity_t *ent ) { if (ent->client->ps.clientNum == 0) { BG_CalculateVROffHandPosition(src, angles); AngleVectors( angles, vf, NULL, NULL ); - TryUse_Internal(ent, src, vf); + TryUse_Internal(true, ent, src, vf); } else { VectorCopy(ent->client->renderInfo.eyePoint, src); AngleVectors(ent->client->ps.viewangles, vf, NULL, NULL); - TryUse_Internal(ent, src, vf); + TryUse_Internal(false, ent, src, vf); } }