diff --git a/gui/public/i18n/en/translation.ftl b/gui/public/i18n/en/translation.ftl index b6889e566..05405686e 100644 --- a/gui/public/i18n/en/translation.ftl +++ b/gui/public/i18n/en/translation.ftl @@ -196,6 +196,10 @@ settings-general-fk_settings-skeleton_settings-description = Toggle skeleton set settings-general-fk_settings-skeleton_settings-extended_spine = Extended spine settings-general-fk_settings-skeleton_settings-extended_pelvis = Extended pelvis settings-general-fk_settings-skeleton_settings-extended_knees = Extended knee +settings-general-fk_settings-vive_emulation-title = Vive emulation +settings-general-fk_settings-vive_emulation-description = Emulate the waist tracker problems that Vive trackers have. This is a joke and makes tracking worse. +settings-general-fk_settings-vive_emulation-label = Enable Vive emulation + ## Gesture control settings (tracker tapping) settings-general-gesture_control-title = Gesture control diff --git a/gui/src/components/settings/pages/GeneralSettings.tsx b/gui/src/components/settings/pages/GeneralSettings.tsx index e1470b84c..321e29fd3 100644 --- a/gui/src/components/settings/pages/GeneralSettings.tsx +++ b/gui/src/components/settings/pages/GeneralSettings.tsx @@ -47,6 +47,7 @@ interface SettingsForm { forceArmsFromHmd: boolean; floorClip: boolean; skatingCorrection: boolean; + viveEmulation: boolean; }; tapDetection: { tapMountingResetEnabled: boolean; @@ -84,6 +85,7 @@ const defaultValues = { forceArmsFromHmd: false, floorClip: false, skatingCorrection: false, + viveEmulation: false, }, filtering: { amount: 0.1, type: FilteringType.NONE }, tapDetection: { @@ -135,6 +137,7 @@ export function GeneralSettings() { toggles.extendedPelvis = values.toggles.extendedPelvis; toggles.extendedSpine = values.toggles.extendedSpine; toggles.forceArmsFromHmd = values.toggles.forceArmsFromHmd; + toggles.viveEmulation = values.toggles.viveEmulation; legTweaks.correctionStrength = values.legTweaks.correctionStrength; modelSettings.toggles = toggles; @@ -495,6 +498,23 @@ export function GeneralSettings() { )} /> + + {t('settings-general-fk_settings-vive_emulation-title')} + +
+ + {t('settings-general-fk_settings-vive_emulation-description')} + +
+
+ +
)} diff --git a/server/src/main/java/dev/slimevr/protocol/rpc/settings/RPCSettingsBuilder.java b/server/src/main/java/dev/slimevr/protocol/rpc/settings/RPCSettingsBuilder.java index 4a91730f1..76ca89c5b 100644 --- a/server/src/main/java/dev/slimevr/protocol/rpc/settings/RPCSettingsBuilder.java +++ b/server/src/main/java/dev/slimevr/protocol/rpc/settings/RPCSettingsBuilder.java @@ -142,7 +142,8 @@ public class RPCSettingsBuilder { config.getToggle(SkeletonConfigToggles.EXTENDED_KNEE_MODEL), config.getToggle(SkeletonConfigToggles.FORCE_ARMS_FROM_HMD), config.getToggle(SkeletonConfigToggles.FLOOR_CLIP), - config.getToggle(SkeletonConfigToggles.SKATING_CORRECTION) + config.getToggle(SkeletonConfigToggles.SKATING_CORRECTION), + config.getToggle(SkeletonConfigToggles.VIVE_EMULATION) ); int ratiosOffset = ModelRatios .createModelRatios( diff --git a/server/src/main/java/dev/slimevr/protocol/rpc/settings/RPCSettingsHandler.java b/server/src/main/java/dev/slimevr/protocol/rpc/settings/RPCSettingsHandler.java index aead63367..5002b1620 100644 --- a/server/src/main/java/dev/slimevr/protocol/rpc/settings/RPCSettingsHandler.java +++ b/server/src/main/java/dev/slimevr/protocol/rpc/settings/RPCSettingsHandler.java @@ -255,6 +255,7 @@ public record RPCSettingsHandler(RPCHandler rpcHandler, ProtocolAPI api) { SkeletonConfigToggles.SKATING_CORRECTION, toggles.skatingCorrection() ); + cfg.setToggle(SkeletonConfigToggles.VIVE_EMULATION, toggles.viveEmulation()); } if (ratios != null) { diff --git a/server/src/main/java/dev/slimevr/vr/processor/skeleton/HumanSkeleton.java b/server/src/main/java/dev/slimevr/vr/processor/skeleton/HumanSkeleton.java index 77523e1bb..28dfcb19f 100644 --- a/server/src/main/java/dev/slimevr/vr/processor/skeleton/HumanSkeleton.java +++ b/server/src/main/java/dev/slimevr/vr/processor/skeleton/HumanSkeleton.java @@ -142,6 +142,10 @@ public class HumanSkeleton extends Skeleton implements SkeletonConfigCallback { protected TapDetectionManager tapDetectionManager = new TapDetectionManager(this); // #endregion + // #region Vive emulation + protected ViveEmulation viveEmulation = new ViveEmulation(this); + // #endregion + // #region Constructors protected HumanSkeleton(List computedTrackers) { assembleSkeleton(false); @@ -795,6 +799,7 @@ public class HumanSkeleton extends Skeleton implements SkeletonConfigCallback { updateRootTrackers(); updateComputedTrackers(); tweakLegPos(); + viveEmulation.update(); } // #endregion @@ -1356,6 +1361,7 @@ public class HumanSkeleton extends Skeleton implements SkeletonConfigCallback { } case SKATING_CORRECTION -> legTweaks.setSkatingReductionEnabled(newValue); case FLOOR_CLIP -> legTweaks.setFloorclipEnabled(newValue); + case VIVE_EMULATION -> viveEmulation.setEnabled(newValue); } } diff --git a/server/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigToggles.java b/server/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigToggles.java index 975c53b78..bfb00c23a 100644 --- a/server/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigToggles.java +++ b/server/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigToggles.java @@ -11,7 +11,8 @@ public enum SkeletonConfigToggles { EXTENDED_KNEE_MODEL(3, "Extended knee model", "extendedKnee", true), FORCE_ARMS_FROM_HMD(4, "Force arms from HMD", "forceArmsFromHMD", true), FLOOR_CLIP(5, "Floor clip", "floorClip", true), - SKATING_CORRECTION(6, "Skating correction", "skatingCorrection", true),; + SKATING_CORRECTION(6, "Skating correction", "skatingCorrection", true), + VIVE_EMULATION(7, "Vive emulation", "viveEmulation", false),; public static final SkeletonConfigToggles[] values = values(); private static final Map byStringVal = new HashMap<>(); diff --git a/server/src/main/java/dev/slimevr/vr/processor/skeleton/ViveEmulation.java b/server/src/main/java/dev/slimevr/vr/processor/skeleton/ViveEmulation.java new file mode 100644 index 000000000..fb47358c4 --- /dev/null +++ b/server/src/main/java/dev/slimevr/vr/processor/skeleton/ViveEmulation.java @@ -0,0 +1,187 @@ +package dev.slimevr.vr.processor.skeleton; + +import com.jme3.math.Quaternion; +import com.jme3.math.Vector3f; +import java.util.Random; + + +// a class that emulates the oclusion issues of vive trackers +// this is done by randomly making the waist tracker fly away from the body +public class ViveEmulation { + + // skeleton + private HumanSkeleton skeleton; + + // random number generator + private Random random = new Random(); + + // hyperparameters + private static final int CHANCE = (int) 1e5; + private static final float MAX_FLY_TIME = 3.5f; + private static final float MIN_FLY_TIME = 0.5f; + private static final float FLY_SPEED = 0.5f; + private static final float FLY_SPEED_VARIANCE = 0.4f; + private static final float FLY_SPEED_DRAG = 0.1f; + private static final float FLY_BACK_SPEED = 20.0f; + private static final float FLY_BACK_OVERSHOOT = 8.0f; + private static final float MIN_OVERSHOOT_DISTANCE = 0.1f; + private static final float MAX_OVERSHOOT_DISTANCE = 1.5f; + private static final float SLERP_AMOUNT = 0.2f; + private static final float NS_CONVERTER = 1e9f; + private static final float NEARLY_ZERO = 0.01f; + private static final Vector3f ADJUSTER = new Vector3f(1.0f, 0.1f, 1.0f); + + // state variables + private boolean enabled = false; + private boolean flying = false; + private boolean flyingBack = false; + private boolean overShooting = false; + private float flyTime = 0.0f; + private float flyStartTime = 0.0f; + private float flySpeed = 0.0f; + private int ticksToFly = random.nextInt(CHANCE); + private Vector3f flyDirection = new Vector3f(); + private Vector3f lastPosition = new Vector3f(); + private Vector3f targetPosition = new Vector3f(); + private Quaternion randomRotation = new Quaternion(); + private Quaternion lastRotation = new Quaternion(); + private long time = System.nanoTime(); + private float timeDelta = 0.0f; + + public ViveEmulation(HumanSkeleton skeleton) { + this.skeleton = skeleton; + if (skeleton.computedWaistTracker != null) + this.lastPosition = skeleton.computedWaistTracker.position.clone(); + } + + public boolean getEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + // this method is called every frame + // it will randomly make the waist tracker fly away + public void update() { + // if the skeleton is not enabled or the waist tracker is not present, + // we do nothing + if (!enabled || skeleton.computedWaistTracker == null) + return; + + // update state + ticksToFly--; + long newTime = System.nanoTime(); + timeDelta = (System.nanoTime() - time) / NS_CONVERTER; + time = newTime; + + // in the flying state update the tracker each frame + if (flying) { + if (System.nanoTime() - flyStartTime > flyTime) { + flying = false; + flyingBack = true; + overShooting = true; + flySpeed = FLY_BACK_SPEED; + getOvershootPosition(targetPosition); + return; + } + skeleton.computedWaistTracker.position.set(getFlyingPos()); + skeleton.computedWaistTracker.rotation.set(getFlyingRotation()); + } + // in this state the tracker is returning to its position but has not + // yet overshot its target + else if (flyingBack && overShooting) { + if (targetPosition.distanceSquared(lastPosition) < NEARLY_ZERO * NEARLY_ZERO) { + overShooting = false; + flySpeed = FLY_BACK_OVERSHOOT; + targetPosition.set(skeleton.computedWaistTracker.position); + return; + } + skeleton.computedWaistTracker.position.set(getFlyingBackPos()); + } + // in this state the tracker will return to its original position + else if (flyingBack) { + if (skeleton.computedWaistTracker.position.distance(lastPosition) < NEARLY_ZERO) { + flyingBack = false; + ticksToFly = random.nextInt(CHANCE); + return; + } + targetPosition.set(skeleton.computedWaistTracker.position); + skeleton.computedWaistTracker.position.set(getFlyingBackPos()); + } + + // if the tracker is not flying, we check if it should fly + // if it should, we set the state variables accordingly + if (ticksToFly <= 0 && !flying && !flyingBack) { + flying = true; + flyDirection + .set( + random.nextFloat() - 0.5f, + random.nextFloat() - 0.5f, + random.nextFloat() - 0.5f + ) + .normalizeLocal(); + flyTime = (random.nextFloat() * (MAX_FLY_TIME - MIN_FLY_TIME) + MIN_FLY_TIME) + * NS_CONVERTER; + flySpeed = (random.nextFloat() * FLY_SPEED_VARIANCE + FLY_SPEED); + flyStartTime = System.nanoTime(); + randomRotation + .set(random.nextFloat(), random.nextFloat(), random.nextFloat(), random.nextFloat()) + .normalizeLocal(); + lastRotation.set(skeleton.computedWaistTracker.rotation); + lastPosition.set(skeleton.computedWaistTracker.position); + } + } + + // returns the position of the waist tracker when it is flying + // and update flySpeed and last position + private Vector3f getFlyingPos() { + lastPosition.addLocal(flyDirection.mult(flySpeed * timeDelta).mult(ADJUSTER)); + flySpeed -= FLY_SPEED_DRAG * timeDelta; + + if (flySpeed < 0.0f) + flySpeed = 0.0f; + + return lastPosition; + } + + // normally you would want to avoid any overshooting + // but since vive trackers do it sometimes, we do it too! + private Vector3f getFlyingBackPos() { + lastPosition + .subtractLocal( + lastPosition + .subtract(targetPosition) + .normalize() + .mult(flySpeed * timeDelta) + ); + + return lastPosition; + } + + // slowly rotate the tracker as it flys away + private Quaternion getFlyingRotation() { + lastRotation.slerp(lastRotation, randomRotation, SLERP_AMOUNT * timeDelta); + return lastRotation; + } + + // get the position to fly back to (initially overshoot the actualy waist + // position) + private void getOvershootPosition(Vector3f store) { + store + .set( + skeleton.computedWaistTracker.position + .subtract( + lastPosition + .subtract(skeleton.computedWaistTracker.position) + .normalize() + .mult( + random.nextFloat() + * (MAX_OVERSHOOT_DISTANCE - MIN_OVERSHOOT_DISTANCE) + + MIN_OVERSHOOT_DISTANCE + ) + ) + ); + } +} diff --git a/solarxr-protocol b/solarxr-protocol index da60e0db2..b7af477b2 160000 --- a/solarxr-protocol +++ b/solarxr-protocol @@ -1 +1 @@ -Subproject commit da60e0db2a1da19863902da01d8a8af845ff0912 +Subproject commit b7af477b25b7ad99096096395d271d834c86b131