Skip to content

Commit

Permalink
Update holding offsets and small walk direction tweaking
Browse files Browse the repository at this point in the history
  • Loading branch information
DaXcess committed Sep 6, 2024
1 parent c827f4e commit c1b9578
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 63 deletions.
26 changes: 26 additions & 0 deletions Source/Patches/PlayerControllerPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,32 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
[HarmonyPatch]
internal static class PlayerControllerPatches
{
/// <summary>
/// Apply walking force based on camera rotation instead of player rotation
/// </summary>
[HarmonyPatch(typeof(PlayerControllerB), nameof(PlayerControllerB.Update))]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> WalkTowardsCameraDirection(IEnumerable<CodeInstruction> instructions)
{
return new CodeMatcher(instructions)
.MatchForward(false,
new CodeMatch(OpCodes.Stfld, Field(typeof(PlayerControllerB), nameof(PlayerControllerB.walkForce))))
.Advance(-18)
.RemoveInstructions(14)
.InsertAndAdvance(
new CodeInstruction(OpCodes.Call, ((Func<PlayerControllerB, Vector3>)GetWalkForce).Method))
.InstructionEnumeration();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static Vector3 GetWalkForce(PlayerControllerB player)
{
var rotation = Quaternion.Euler(0, player.gameplayCamera.transform.eulerAngles.y, 0);

return rotation * Vector3.right * player.moveInputVector.x +
rotation * Vector3.forward * player.moveInputVector.y;
}
}

/// <summary>
/// Prevent the local player visor from being moved when the player dies
/// </summary>
Expand Down
20 changes: 10 additions & 10 deletions Source/Player/Items.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ internal static class Items

public static readonly Dictionary<string, (Vector3, Vector3)> itemOffsets = new()
{
{ "Chemical jug", (new Vector3(0.2f, -0.18f, 0.24f), new Vector3(270, 180, 0)) },
{ "Toilet paper", (new Vector3(0.15f, -0.16f, 0.17f), new Vector3(270, 180, 0)) },
{ "Chemical jug", (new Vector3(-0.05f, 0.14f, -0.29f), new Vector3(0, 90, 120)) },
{ "Toilet paper", (new Vector3(0, 0.13f, -0.4f), new Vector3(0, 90, 90)) },
{ "Boombox", (new Vector3(-0.02f, 0.1f, -0.29f), new Vector3(90, 285, 0)) },
{ "Apparatus", (new Vector3(0.21f, -0.2f, 0.33f), new Vector3(90, 180, 180)) },
{ "Large axle", (new Vector3(0.07f, -0.2f, 0.3f), new Vector3(90, 180, 180)) },
{ "Cash register", (new Vector3(0.05f, -0.29f, 0.11f), new Vector3(270, 132, 288)) },
{ "V-type engine", (new Vector3(-0.10f, -0.06f, 0.26f), new Vector3(66, 190, 0)) },
{ "Apparatus", (new Vector3(-0.04f, 0.12f, -0.21f), new Vector3(0, 270, 0)) },
{ "Large axle", (new Vector3(-0.04f, 0.24f, -0.33f), new Vector3(0, 270, 100)) },
{ "Cash register", (new Vector3(-0.09f, 0.13f, -0.46f), new Vector3(0, 75, 255)) },
{ "V-type engine", (new Vector3(-0.04f, 0.33f, -0.3f), new Vector3(0, 270, 90)) },
{ "Extension ladder", (new Vector3(-0.20f, 0.28f, -0.47f), new Vector3(90, 90, 0)) },
{ "Painting", (new Vector3(0.05f, 0.75f, -0.06f), new Vector3(6, 270, 184)) },
{ "Soccer ball", (new Vector3(-0.07f, 0.17f, -0.19f), Vector3.zero) },
{ "Control pad", (new Vector3(0.25f, -0.22f, 0.27f), new Vector3(340, 248, 167)) },
{ "Garbage lid", (new Vector3(0.23f, -0.1f, 0.44f), new Vector3(14, 15, 17)) },
{ "Hive", (new Vector3(0.13f, -0.15f, 0.16f), Vector3.zero) },
{ "Control pad", (new Vector3(0.06f, 0.09f, -0.23f), new Vector3(90, 90, 0)) },
{ "Garbage lid", (new Vector3(-0.02f, 0.11f, -0.08f), new Vector3(0, 0, 90)) },
{ "Hive", (new Vector3(0.04f, 0.32f, -0.38f), Vector3.zero) },
{ "Plastic fish", (new Vector3(0, 0.12f, -0.06f), new Vector3(0, 80, 165)) },
{ "Maneater", (new Vector3(0.24f, -0.15f, 0.35f), new Vector3(-6, 167, 14)) }
{ "Maneater", (new Vector3(-0.07f, 0.02f, -0.11f), new Vector3(6, 218, 85)) }
};
}
116 changes: 63 additions & 53 deletions Source/Player/VRPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

namespace LCVR.Player;

[DefaultExecutionOrder(-100)]
public class VRPlayer : MonoBehaviour
{
private const float SCALE_FACTOR = 1.5f;
private const int CAMERA_CLIP_MASK = 1 << 8 | 1 << 26;

private const float SQR_MOVE_THRESHOLD = 0.001f;
private const float TURN_ANGLE_THRESHOLD = 120.0f;
private const float TURN_WEIGHT_SHARP = 15.0f;

private Coroutine stopSprintingCoroutine;
private Coroutine resetHeightCoroutine;
Expand Down Expand Up @@ -63,6 +65,11 @@ public class VRPlayer : MonoBehaviour

private Transform mysteriousCube;

private TwoBoneIKConstraint localLeftArmVRRig;
private TwoBoneIKConstraint localRightArmVRRig;
private TwoBoneIKConstraint leftArmVRRig;
private TwoBoneIKConstraint rightArmVRRig;

private Channel prefsChannel;
private Channel rigChannel;
private Channel spectatorRigChannel;
Expand All @@ -81,12 +88,6 @@ public class VRPlayer : MonoBehaviour
public VRFingerCurler LeftFingerCurler { get; private set; }
public VRFingerCurler RightFingerCurler { get; private set; }

// IK Constraints
private TwoBoneIKConstraint LocalLeftArmVRRig { get; set; }
private TwoBoneIKConstraint LocalRightArmVRRig { get; set; }
private TwoBoneIKConstraint LeftArmVRRig { get; set; }
private TwoBoneIKConstraint RightArmVRRig { get; set; }

public Transform LeftHandVRTarget { get; private set; }
public Transform RightHandVRTarget { get; private set; }

Expand Down Expand Up @@ -276,17 +277,17 @@ private void BuildVRRig()
}
}.transform;

LocalLeftArmVRRig = Bones.LocalLeftArmRig.gameObject.AddComponent<TwoBoneIKConstraint>();
localLeftArmVRRig = Bones.LocalLeftArmRig.gameObject.AddComponent<TwoBoneIKConstraint>();

LocalLeftArmVRRig.data.root = Bones.LocalLeftUpperArm;
LocalLeftArmVRRig.data.mid = Bones.LocalLeftLowerArm;
LocalLeftArmVRRig.data.tip = Bones.LocalLeftHand;
LocalLeftArmVRRig.data.target = localLeftArmRigTarget;
LocalLeftArmVRRig.data.hint = localLeftArmRigHint;
LocalLeftArmVRRig.data.hintWeight = 1;
LocalLeftArmVRRig.data.targetRotationWeight = 1;
LocalLeftArmVRRig.data.targetPositionWeight = 1;
LocalLeftArmVRRig.weight = 1;
localLeftArmVRRig.data.root = Bones.LocalLeftUpperArm;
localLeftArmVRRig.data.mid = Bones.LocalLeftLowerArm;
localLeftArmVRRig.data.tip = Bones.LocalLeftHand;
localLeftArmVRRig.data.target = localLeftArmRigTarget;
localLeftArmVRRig.data.hint = localLeftArmRigHint;
localLeftArmVRRig.data.hintWeight = 1;
localLeftArmVRRig.data.targetRotationWeight = 1;
localLeftArmVRRig.data.targetPositionWeight = 1;
localLeftArmVRRig.weight = 1;

RigTrackerLocal.leftHand = new RigTracker.Tracker
{
Expand Down Expand Up @@ -324,17 +325,17 @@ private void BuildVRRig()
}
}.transform;

LocalRightArmVRRig = Bones.LocalRightArmRig.gameObject.AddComponent<TwoBoneIKConstraint>();
localRightArmVRRig = Bones.LocalRightArmRig.gameObject.AddComponent<TwoBoneIKConstraint>();

LocalRightArmVRRig.data.root = Bones.LocalRightUpperArm;
LocalRightArmVRRig.data.mid = Bones.LocalRightLowerArm;
LocalRightArmVRRig.data.tip = Bones.LocalRightHand;
LocalRightArmVRRig.data.target = localRightArmRigTarget;
LocalRightArmVRRig.data.hint = localRightArmRigHint;
LocalRightArmVRRig.data.hintWeight = 1;
LocalRightArmVRRig.data.targetRotationWeight = 1;
LocalRightArmVRRig.data.targetPositionWeight = 1;
LocalRightArmVRRig.weight = 1;
localRightArmVRRig.data.root = Bones.LocalRightUpperArm;
localRightArmVRRig.data.mid = Bones.LocalRightLowerArm;
localRightArmVRRig.data.tip = Bones.LocalRightHand;
localRightArmVRRig.data.target = localRightArmRigTarget;
localRightArmVRRig.data.hint = localRightArmRigHint;
localRightArmVRRig.data.hintWeight = 1;
localRightArmVRRig.data.targetRotationWeight = 1;
localRightArmVRRig.data.targetPositionWeight = 1;
localRightArmVRRig.weight = 1;

RigTrackerLocal.rightHand = new RigTracker.Tracker
{
Expand Down Expand Up @@ -389,16 +390,16 @@ private void BuildVRRig()
}
}.transform;

LeftArmVRRig = Bones.LeftArmRig.gameObject.AddComponent<TwoBoneIKConstraint>();
leftArmVRRig = Bones.LeftArmRig.gameObject.AddComponent<TwoBoneIKConstraint>();

LeftArmVRRig.data.root = Bones.LeftUpperArm;
LeftArmVRRig.data.mid = Bones.LeftLowerArm;
LeftArmVRRig.data.tip = Bones.LeftHand;
LeftArmVRRig.data.target = leftArmRigTarget;
LeftArmVRRig.data.hint = leftArmRigHint;
LeftArmVRRig.data.hintWeight = 1;
LeftArmVRRig.data.targetRotationWeight = 1;
LeftArmVRRig.data.targetPositionWeight = 1;
leftArmVRRig.data.root = Bones.LeftUpperArm;
leftArmVRRig.data.mid = Bones.LeftLowerArm;
leftArmVRRig.data.tip = Bones.LeftHand;
leftArmVRRig.data.target = leftArmRigTarget;
leftArmVRRig.data.hint = leftArmRigHint;
leftArmVRRig.data.hintWeight = 1;
leftArmVRRig.data.targetRotationWeight = 1;
leftArmVRRig.data.targetPositionWeight = 1;

RigTracker.leftHand = new RigTracker.Tracker
{
Expand Down Expand Up @@ -436,16 +437,16 @@ private void BuildVRRig()
}
}.transform;

RightArmVRRig = Bones.RightArmRig.gameObject.AddComponent<TwoBoneIKConstraint>();
rightArmVRRig = Bones.RightArmRig.gameObject.AddComponent<TwoBoneIKConstraint>();

RightArmVRRig.data.root = Bones.RightUpperArm;
RightArmVRRig.data.mid = Bones.RightLowerArm;
RightArmVRRig.data.tip = Bones.RightHand;
RightArmVRRig.data.target = rightArmRigTarget;
RightArmVRRig.data.hint = rightArmRigHint;
RightArmVRRig.data.hintWeight = 1;
RightArmVRRig.data.targetRotationWeight = 1;
RightArmVRRig.data.targetPositionWeight = 1;
rightArmVRRig.data.root = Bones.RightUpperArm;
rightArmVRRig.data.mid = Bones.RightLowerArm;
rightArmVRRig.data.tip = Bones.RightHand;
rightArmVRRig.data.target = rightArmRigTarget;
rightArmVRRig.data.hint = rightArmRigHint;
rightArmVRRig.data.hintWeight = 1;
rightArmVRRig.data.targetRotationWeight = 1;
rightArmVRRig.data.targetPositionWeight = 1;

RigTracker.rightHand = new RigTracker.Tracker
{
Expand All @@ -464,6 +465,8 @@ private void UpdateRigOffsets()
Bones.LocalRightArmRigHint.localPosition = new Vector3(12.5f, -2f, -1f);
Bones.LeftArmRigHint.localPosition = new Vector3(-10f, -2f, -1f);
Bones.RightArmRigHint.localPosition = new Vector3(12.5f, -2f, -1f);
Bones.LocalItemHolder.localPosition = new Vector3(-0.002f, 0.036f, -0.042f);
Bones.LocalItemHolder.localEulerAngles = new Vector3(356.3837f, 357.6979f, 0.1453f);
}

private void Sprint_performed(InputAction.CallbackContext obj)
Expand Down Expand Up @@ -604,11 +607,11 @@ PlayerController.currentTriggerInAnimationWith is not null &&
xrOrigin.localRotation = rotationOffset;

// If head rotated too much, or we moved, force new player rotation
if (PlayerController.moveInputVector.sqrMagnitude > SQR_MOVE_THRESHOLD ||
(!PlayerController.inSpecialInteractAnimation &&
GetBodyToCameraAngle() > TURN_ANGLE_THRESHOLD))
transform.eulerAngles = new Vector3(transform.eulerAngles.x, mainCamera.transform.eulerAngles.y,
transform.eulerAngles.z);
if (PlayerController.moveInputVector.sqrMagnitude > SQR_MOVE_THRESHOLD)
TurnBodyToCamera(TURN_WEIGHT_SHARP * 0.25f);
else if (!PlayerController.inSpecialInteractAnimation &&
GetBodyToCameraAngle() is var angle and > TURN_ANGLE_THRESHOLD)
TurnBodyToCamera(TURN_WEIGHT_SHARP * Mathf.InverseLerp(TURN_ANGLE_THRESHOLD, 170f, angle));

if (!PlayerController.inSpecialInteractAnimation)
lastFrameHmdPosition = mainCamera.transform.localPosition;
Expand Down Expand Up @@ -736,10 +739,10 @@ public void UpdateIKWeights()
PlayerController.rightArmRig.weight = 0;

// VR Rigs
LocalLeftArmVRRig.weight = 1;
LocalRightArmVRRig.weight = 1;
LeftArmVRRig.weight = 1;
RightArmVRRig.weight = 1;
localLeftArmVRRig.weight = 1;
localRightArmVRRig.weight = 1;
leftArmVRRig.weight = 1;
rightArmVRRig.weight = 1;
}

public void EnableInteractorVisuals(bool visible = true)
Expand Down Expand Up @@ -801,6 +804,13 @@ private IEnumerator UpdatePlayerPrefs()
}
}

private void TurnBodyToCamera(float turnWeight)
{
var newRotation = Quaternion.Euler(transform.eulerAngles.x, mainCamera.transform.eulerAngles.y,
transform.eulerAngles.z);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * turnWeight);
}

private float GetBodyToCameraAngle()
{
return Quaternion.Angle(Quaternion.Euler(0, transform.eulerAngles.y, 0),
Expand Down

0 comments on commit c1b9578

Please sign in to comment.