Skip to content

Commit

Permalink
Update Kinematics to slightly fix jittering + fixing code up a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBigz committed Nov 6, 2024
1 parent b9f3047 commit daee3a1
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions TABGVR/Patches/KinematicsPatch.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,54 @@
using Epic.OnlineServices;
using Epic.OnlineServices.Presence;
using HarmonyLib;
using TABGVR.Player;
using UltimateIK;
using UnityEngine;

namespace TABGVR.Patches;

[HarmonyPatch(typeof(Holding), nameof(Holding.LateUpdate))]
[HarmonyPatch(typeof(Holding))]
class KinematicsPatch
{
static void Postfix(Holding __instance)
static void SetupConnection(Rigidbody joint)
{
joint.GetComponentInChildren<Collider>().enabled = false;

// disable gravity in arms
joint.useGravity = false;
joint.transform.parent.Find(joint.gameObject.name).GetComponent<Rigidbody>().useGravity = false;
}

static void UpdateConnection(Rigidbody joint, GameObject controller)
{
var controllerRelativeToGameCamera = controller.transform.position - Controllers.Head.transform.position +
Camera.current.transform.position;

if (Vector3.Distance(joint.position, controllerRelativeToGameCamera) < 0.01f) return;

joint.MovePosition(joint.position + controllerRelativeToGameCamera -
joint.transform.GetChild(0).position);
}

[HarmonyPatch(nameof(Holding.Start))]
[HarmonyPostfix]
static void StartPostfix(Holding __instance)
{
SetupConnection(__instance.rightHand);
SetupConnection(__instance.leftHand);
}

[HarmonyPatch(nameof(Holding.Update))]
[HarmonyPostfix]
static void UpdatePostfix(Holding __instance)
{
if (!Controllers.LeftHand || !Controllers.RightHand || !Controllers.Head) return;
if (__instance.player != global::Player.localPlayer) return;

// Plugin.Logger.LogInfo(
// $"KP {__instance.player} / Head: {Controllers.Head.transform.position} / Left: {Controllers.LeftHand.transform.position} / Right: {Controllers.RightHand.transform.position}");

__instance.rightHand.GetComponentInChildren<Collider>().enabled = false;
__instance.leftHand.GetComponentInChildren<Collider>().enabled = false;

__instance.rightHand.MovePosition(__instance.rightHand.position + Controllers.RightHand.transform.position -
Controllers.Head.transform.position +
Camera.current.transform.position -
__instance.rightHand.transform.GetChild(0).position);

__instance.leftHand.MovePosition(__instance.leftHand.position + Controllers.LeftHand.transform.position -
Controllers.Head.transform.position +
Camera.current.transform.position -
__instance.leftHand.transform.GetChild(0).position);
UpdateConnection(__instance.rightHand, Controllers.RightHand);
UpdateConnection(__instance.leftHand, Controllers.LeftHand);
}
}

0 comments on commit daee3a1

Please sign in to comment.