Skip to content

Commit

Permalink
fix(player): sync player position with relative hmd position (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed Dec 17, 2023
1 parent b7ef8db commit 8c58733
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 20 deletions.
6 changes: 4 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ Steps to reproduce the behavior:
- OS: [e.g. Linux <Distro and Version>, Windows 10]
- Headset and Controllers: [e.g. Valve Index with Knuckles]
- Game Version; [e.g. 0.2.0f]
- Mod Version: [e.g. 0.1.0]
- Mod Version: [e.g. 0.1.0]

**Additional context**
Add any other context about the problem here.

<!-- Add any other context about the problem here. -->
<!-- Please attach the BepInEx log file (Techtonica/BepInEx/LogOutput.log) -->
10 changes: 9 additions & 1 deletion libs/StreamingAssets/SteamVR/bindings_knuckles.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
"sources" : []
},
"/actions/default" : {
"chords" : [],
"chords" : [
{
"inputs" : [
[ "/user/hand/right/input/b", "held" ],
[ "/user/hand/left/input/b", "held" ]
],
"output" : "/actions/default/in/pausemenu"
}
],
"haptics" : [
{
"output" : "/actions/default/out/haptic",
Expand Down
55 changes: 42 additions & 13 deletions plugin/src/camera/TechMainCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,46 @@ namespace TechtonicaVR.VRCamera;

public class TechMainCamera : MonoBehaviour
{
private PostProcessLayer postProcessLayer;

private void Start()
{
// Get the PostProcessLayer component attached to the camera
postProcessLayer = GetComponent<PostProcessLayer>();

// Disable the PostProcessLayer
if (postProcessLayer != null)
{
postProcessLayer.enabled = false;
}
}
public Transform camRoot;
private PostProcessLayer postProcessLayer;
private Vector3 offset = Vector3.zero;
private void Start()
{
// Get the PostProcessLayer component attached to the camera
postProcessLayer = GetComponent<PostProcessLayer>();

// Disable the PostProcessLayer
if (postProcessLayer != null)
{
postProcessLayer.enabled = false;
}
}

private void Update()
{
if (PlayerFirstPersonController.instance == null || camRoot == null)
{
return;
}

var camPosition = transform.localPosition;

var diff = camPosition + offset;
diff.y = 0;
diff = PlayerFirstPersonController.instance.transform.rotation * diff;

offset = -camPosition;

if (float.IsNaN(diff.x) || float.IsNaN(diff.z))
{
Plugin.Logger.LogError("Diff is NaN");
return;
}

var newCamRootPosition = offset;
newCamRootPosition.y = 0;

PlayerFirstPersonController.instance.transform.position += diff;
camRoot.localPosition = newCamRootPosition;
}
}
10 changes: 7 additions & 3 deletions plugin/src/camera/camera_manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace TechtonicaVR.VRCamera;
public class VRCameraManager : MonoBehaviour
{
public Transform vrRoot;

public SteamVR_CameraHelper cameraHelperPrefab;

private SteamVR_CameraHelper cameraHelper;

Check warning on line 17 in plugin/src/camera/camera_manager.cs

View workflow job for this annotation

GitHub Actions / build

The field 'VRCameraManager.cameraHelper' is never used

Check warning on line 17 in plugin/src/camera/camera_manager.cs

View workflow job for this annotation

GitHub Actions / build

The field 'VRCameraManager.cameraHelper' is never used
Expand Down Expand Up @@ -64,23 +65,26 @@ private void SetupCamera()

mainCamera.gameObject.AddComponent<SteamVR_Camera>();
mainCamera.gameObject.AddComponent<SteamVR_TrackedObject>();
mainCamera.gameObject.AddComponent<TechMainCamera>();
var techCam = mainCamera.gameObject.AddComponent<TechMainCamera>();
HmdMatrix44_t leftEyeMatrix = OpenVR.System.GetProjectionMatrix(EVREye.Eye_Left, mainCamera.nearClipPlane, mainCamera.farClipPlane);
HmdMatrix44_t rightEyeMatrix = OpenVR.System.GetProjectionMatrix(EVREye.Eye_Right, mainCamera.nearClipPlane, mainCamera.farClipPlane);

if (PlayerFirstPersonController.instance != null)
{
vrRoot = PlayerFirstPersonController.instance.transform;
techCam.camRoot = new GameObject("CamRoot").transform;

mainCamera.transform.parent = vrRoot;
techCam.camRoot.parent = vrRoot;
techCam.camRoot.localPosition = Vector3.zero;
mainCamera.transform.parent = techCam.camRoot;
foreach (var a in ReInput.mapping.Actions)
{
Plugin.Logger.LogInfo("Action: " + a.name);
Plugin.Logger.LogInfo(" id: " + a.id);
}

StartCoroutine(PatchCoroutine());
SpawnHands(vrRoot);
SpawnHands(techCam.camRoot);
}

FindObjectsOfType<Headlamp>().ForEach(h => h.transform.parent = mainCamera.transform);
Expand Down
14 changes: 14 additions & 0 deletions plugin/src/camera/patch/player_first_person_controller_patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using HarmonyLib;

namespace TechtonicaVR.VRCamera.Patch;

[HarmonyPatch]
class PlayerFirstPersonControllerPatch
{
[HarmonyPatch(typeof(PlayerFirstPersonController), nameof(PlayerFirstPersonController.AutoCrouch))]
[HarmonyPrefix]
static bool StartPatch()
{
return true;
}
}
10 changes: 9 additions & 1 deletion unity/Assets/StreamingAssets/SteamVR/bindings_knuckles.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
"sources" : []
},
"/actions/default" : {
"chords" : [],
"chords" : [
{
"inputs" : [
[ "/user/hand/right/input/b", "held" ],
[ "/user/hand/left/input/b", "held" ]
],
"output" : "/actions/default/in/pausemenu"
}
],
"haptics" : [
{
"output" : "/actions/default/out/haptic",
Expand Down

0 comments on commit 8c58733

Please sign in to comment.