Skip to content

Commit

Permalink
Fix locomotion when player platform moves
Browse files Browse the repository at this point in the history
(again)
  • Loading branch information
nicoco007 committed Oct 2, 2024
1 parent 0872296 commit e12331f
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions Source/CustomAvatar/Avatar/AvatarIK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected void Start()
_defaultRootPose = new Pose(vrikManager.references_root.localPosition, vrikManager.references_root.localRotation);

_vrik = _ikHelper.InitializeVRIK(vrikManager, transform);
IKSolver solver = _vrik.GetIKSolver();
IKSolverVR solver = _vrik.solver;

foreach (TwistRelaxer twistRelaxer in _twistRelaxers)
{
Expand All @@ -128,6 +128,7 @@ protected void Start()
}
}

solver.OnPreUpdate += OnPreUpdate;
solver.OnPostUpdate += OnPostUpdate;

if (vrikManager.solver_spine_maintainPelvisPosition > 0 && !_input.allowMaintainPelvisPosition)
Expand Down Expand Up @@ -167,28 +168,16 @@ protected void OnDisable()

protected void OnDestroy()
{
IKSolver solver = _vrik.GetIKSolver();
IKSolverVR solver = _vrik.solver;
solver.OnPreUpdate -= OnPreUpdate;
solver.OnPostUpdate -= OnPostUpdate;

_input.inputChanged -= OnInputChanged;
}

#endregion

private void ApplyPlatformMotion()
{
Transform parent = transform.parent;

if (!parent) return;

Vector3 deltaPosition = parent.position - _previousParentPose.position;
Quaternion deltaRotation = parent.rotation * Quaternion.Inverse(_previousParentPose.rotation);

_vrik.solver.AddPlatformMotion(deltaPosition, deltaRotation, parent.position);
_previousParentPose = new Pose(parent.position, parent.rotation);
}

protected void Update()
private void OnPreUpdate()
{
foreach (BeatSaberDynamicBone::DynamicBone dynamicBone in _dynamicBones)
{
Expand All @@ -206,6 +195,25 @@ private void OnPostUpdate()
}
}

private void ApplyPlatformMotion()
{
Transform parent = _vrik.references.root.parent;

if (parent == null)
{
return;
}

parent.GetPositionAndRotation(out Vector3 parentPosition, out Quaternion parentRotation);

Vector3 deltaPosition = parentPosition - _previousParentPose.position;
Quaternion deltaRotation = Quaternion.Inverse(_previousParentPose.rotation) * parentRotation;

_vrik.solver.AddPlatformMotion(deltaPosition, deltaRotation, parentPosition);

_previousParentPose = new Pose(parentPosition, parentRotation);
}

private void UpdateLocomotion()
{
if (_vrik == null || vrikManager == null)
Expand Down

0 comments on commit e12331f

Please sign in to comment.