diff --git a/gui/src/components/settings/SettingsSidebar.tsx b/gui/src/components/settings/SettingsSidebar.tsx index b1bbd0201..441412536 100644 --- a/gui/src/components/settings/SettingsSidebar.tsx +++ b/gui/src/components/settings/SettingsSidebar.tsx @@ -54,6 +54,9 @@ export function SettingsSidebar() { FK settings + + Gesture control + Interface diff --git a/gui/src/components/settings/pages/GeneralSettings.tsx b/gui/src/components/settings/pages/GeneralSettings.tsx index d1eff0ffe..877c2ca86 100644 --- a/gui/src/components/settings/pages/GeneralSettings.tsx +++ b/gui/src/components/settings/pages/GeneralSettings.tsx @@ -7,6 +7,8 @@ import { FilteringType, ModelSettingsT, ModelTogglesT, + TapDetectionSettingsT, + LegTweaksSettingsT, RpcMessage, SettingsRequestT, SettingsResponseT, @@ -44,6 +46,13 @@ interface SettingsForm { floorClip: boolean; skatingCorrection: boolean; }; + tapDetection: { + tapResetEnabled: boolean; + tapResetDelay: number; + }; + legTweaks: { + correctionStrength: number; + }; interface: { devmode: boolean; watchNewDevices: boolean; @@ -75,6 +84,8 @@ export function GeneralSettings() { skatingCorrection: false, }, filtering: { amount: 0.1, type: FilteringType.NONE }, + tapDetection: { tapResetEnabled: false, tapResetDelay: 0.2 }, + legTweaks: { correctionStrength: 0.3 }, interface: { devmode: false, watchNewDevices: true }, }, }); @@ -95,16 +106,24 @@ export function GeneralSettings() { const modelSettings = new ModelSettingsT(); const toggles = new ModelTogglesT(); + const legTweaks = new LegTweaksSettingsT(); toggles.floorClip = values.toggles.floorClip; toggles.skatingCorrection = values.toggles.skatingCorrection; toggles.extendedKnee = values.toggles.extendedKnee; toggles.extendedPelvis = values.toggles.extendedPelvis; toggles.extendedSpine = values.toggles.extendedSpine; toggles.forceArmsFromHmd = values.toggles.forceArmsFromHmd; + legTweaks.correctionStrength = values.legTweaks.correctionStrength; modelSettings.toggles = toggles; + modelSettings.legTweaks = legTweaks; settings.modelSettings = modelSettings; + const tapDetection = new TapDetectionSettingsT(); + tapDetection.tapResetEnabled = values.tapDetection.tapResetEnabled; + tapDetection.tapResetDelay = values.tapDetection.tapResetDelay; + settings.tapDetectionSettings = tapDetection; + const filtering = new FilteringSettingsT(); filtering.type = values.filtering.type; filtering.amount = values.filtering.amount; @@ -164,6 +183,14 @@ export function GeneralSettings() { ); } + if (settings.tapDetectionSettings) { + formData.tapDetection = settings.tapDetectionSettings; + } + + if (settings.modelSettings?.legTweaks) { + formData.legTweaks = settings.modelSettings.legTweaks; + } + reset(formData); }); @@ -297,7 +324,8 @@ export function GeneralSettings() { Floor-clip can Reduce or even eliminates clipping with the floor but may cause problems when on your knees. Skating-correction corrects for ice skating, but can decrease accuracy in certain - movement patterns. + movement patterns. Increasing the amount of skating correction + will make the correction more aggressive.
@@ -316,6 +344,17 @@ export function GeneralSettings() { label="Skating correction" />
+
+ `${Math.round(value * 100)} %`} + min={0.1} + max={1.0} + step={0.1} + /> +
Arm FK
@@ -367,6 +406,39 @@ export function GeneralSettings() { + } id="gestureControl"> + <> + Gesture Control + Double Tap quick reset +
+ + Enable or disable double tap quick reset. When enabled double + tapping anywhere on the highest torso tracker will activate a + quick reset. Delay is the time between registering a tap and + resetting. + +
+
+ + `${Math.round(value * 10) / 10} s`} + min={0.2} + max={3.0} + step={0.2} + /> +
+ +
+ } id="interface"> <> Interface diff --git a/server/src/main/java/dev/slimevr/config/LegTweaksConfig.java b/server/src/main/java/dev/slimevr/config/LegTweaksConfig.java new file mode 100644 index 000000000..1752c8c29 --- /dev/null +++ b/server/src/main/java/dev/slimevr/config/LegTweaksConfig.java @@ -0,0 +1,14 @@ +package dev.slimevr.config; + +public class LegTweaksConfig { + + private float correctionStrength = 0.3f; + + public float getCorrectionStrength() { + return correctionStrength; + } + + public void setCorrectionStrength(float correctionStrength) { + this.correctionStrength = correctionStrength; + } +} diff --git a/server/src/main/java/dev/slimevr/config/TapDetectionConfig.java b/server/src/main/java/dev/slimevr/config/TapDetectionConfig.java new file mode 100644 index 000000000..edcec1b7a --- /dev/null +++ b/server/src/main/java/dev/slimevr/config/TapDetectionConfig.java @@ -0,0 +1,23 @@ +package dev.slimevr.config; + +public class TapDetectionConfig { + + private float delay = 0.2f; + private boolean enabled = true; + + public float getDelay() { + return delay; + } + + public void setDelay(float delay) { + this.delay = delay; + } + + public boolean getEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } +} diff --git a/server/src/main/java/dev/slimevr/config/VRConfig.java b/server/src/main/java/dev/slimevr/config/VRConfig.java index 92f57918c..e313fcf97 100644 --- a/server/src/main/java/dev/slimevr/config/VRConfig.java +++ b/server/src/main/java/dev/slimevr/config/VRConfig.java @@ -29,6 +29,10 @@ public class VRConfig { private final SkeletonConfig skeleton = new SkeletonConfig(); + private final LegTweaksConfig legTweaks = new LegTweaksConfig(); + + private final TapDetectionConfig tapDetection = new TapDetectionConfig(); + @JsonDeserialize(using = TrackerConfigMapDeserializer.class) @JsonSerialize(keyUsing = StdKeySerializers.StringKeySerializer.class) private final Map trackers = new HashMap<>(); @@ -71,6 +75,14 @@ public class VRConfig { return skeleton; } + public LegTweaksConfig getLegTweaks() { + return legTweaks; + } + + public TapDetectionConfig getTapDetection() { + return tapDetection; + } + public OverlayConfig getOverlay() { return overlay; } 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 280d41eb6..db9e10bb6 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 @@ -2,7 +2,9 @@ package dev.slimevr.protocol.rpc.settings; import com.google.flatbuffers.FlatBufferBuilder; import dev.slimevr.config.FiltersConfig; +import dev.slimevr.config.LegTweaksConfig; import dev.slimevr.config.OSCConfig; +import dev.slimevr.config.TapDetectionConfig; import dev.slimevr.filtering.TrackerFilters; import dev.slimevr.platform.SteamVRBridge; import dev.slimevr.vr.processor.skeleton.SkeletonConfig; @@ -12,10 +14,12 @@ import dev.slimevr.vr.trackers.TrackerRole; import solarxr_protocol.rpc.FilteringSettings; import solarxr_protocol.rpc.OSCTrackersSetting; import solarxr_protocol.rpc.SteamVRTrackersSetting; +import solarxr_protocol.rpc.TapDetectionSettings; import solarxr_protocol.rpc.VRCOSCSettings; import solarxr_protocol.rpc.settings.ModelRatios; import solarxr_protocol.rpc.settings.ModelSettings; import solarxr_protocol.rpc.settings.ModelToggles; +import solarxr_protocol.rpc.settings.LegTweaksSettings; public class RPCSettingsBuilder { @@ -72,6 +76,18 @@ public class RPCSettingsBuilder { ); } + public static int createTapDetectionSettings( + FlatBufferBuilder fbb, + TapDetectionConfig tapDetectionConfig + ) { + return TapDetectionSettings + .createTapDetectionSettings( + fbb, + tapDetectionConfig.getDelay(), + tapDetectionConfig.getEnabled() + ); + } + public static int createSteamVRSettings(FlatBufferBuilder fbb, SteamVRBridge bridge) { int steamvrTrackerSettings = 0; if (bridge != null) { @@ -93,7 +109,11 @@ public class RPCSettingsBuilder { return steamvrTrackerSettings; } - public static int createModelSettings(FlatBufferBuilder fbb, SkeletonConfig config) { + public static int createModelSettings( + FlatBufferBuilder fbb, + SkeletonConfig config, + LegTweaksConfig legTweaksConfig + ) { int togglesOffset = ModelToggles .createModelToggles( fbb, @@ -114,7 +134,11 @@ public class RPCSettingsBuilder { config.getValue(SkeletonConfigValues.HIP_LEGS_AVERAGING), config.getValue(SkeletonConfigValues.KNEE_TRACKER_ANKLE_AVERAGING) ); - // TODO: legtweaks amount in protocol - return ModelSettings.createModelSettings(fbb, togglesOffset, ratiosOffset, 0); + int legTweaksOffset = LegTweaksSettings + .createLegTweaksSettings( + fbb, + legTweaksConfig.getCorrectionStrength() + ); + return ModelSettings.createModelSettings(fbb, togglesOffset, ratiosOffset, legTweaksOffset); } } 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 fe053678b..f1a3a6329 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 @@ -3,6 +3,7 @@ package dev.slimevr.protocol.rpc.settings; import com.google.flatbuffers.FlatBufferBuilder; import dev.slimevr.config.FiltersConfig; import dev.slimevr.config.OSCConfig; +import dev.slimevr.config.TapDetectionConfig; import dev.slimevr.filtering.TrackerFilters; import dev.slimevr.osc.VRCOSCHandler; import dev.slimevr.platform.SteamVRBridge; @@ -55,7 +56,13 @@ public record RPCSettingsHandler(RPCHandler rpcHandler, ProtocolAPI api) { RPCSettingsBuilder .createModelSettings( fbb, - this.api.server.humanPoseProcessor.getSkeletonConfig() + this.api.server.humanPoseProcessor.getSkeletonConfig(), + this.api.server.getConfigManager().getVrConfig().getLegTweaks() + ), + RPCSettingsBuilder + .createTapDetectionSettings( + fbb, + this.api.server.getConfigManager().getVrConfig().getTapDetection() ) ); int outbound = rpcHandler.createRPCMessage(fbb, RpcMessage.SettingsResponse, settings); @@ -64,7 +71,6 @@ public record RPCSettingsHandler(RPCHandler rpcHandler, ProtocolAPI api) { } public void onChangeSettingsRequest(GenericConnection conn, RpcMessageHeader messageHeader) { - ChangeSettingsRequest req = (ChangeSettingsRequest) messageHeader .message(new ChangeSettingsRequest()); if (req == null) @@ -128,11 +134,31 @@ public record RPCSettingsHandler(RPCHandler rpcHandler, ProtocolAPI api) { } } + if (req.tapDetectionSettings() != null) { + TapDetectionConfig tapDetectionConfig = this.api.server + .getConfigManager() + .getVrConfig() + .getTapDetection(); + var tapDetectionSettings = req.tapDetectionSettings(); + + if (tapDetectionSettings != null) { + tapDetectionConfig.setEnabled(tapDetectionSettings.tapResetEnabled()); + + if (tapDetectionSettings.hasTapResetDelay()) { + tapDetectionConfig.setDelay(tapDetectionSettings.tapResetDelay()); + } + + this.api.server.humanPoseProcessor.getSkeleton().updateTapDetectionConfig(); + } + } + var modelSettings = req.modelSettings(); if (modelSettings != null) { var cfg = this.api.server.humanPoseProcessor.getSkeletonConfig(); + var legTweaksConfig = this.api.server.getConfigManager().getVrConfig().getLegTweaks(); var toggles = modelSettings.toggles(); var ratios = modelSettings.ratios(); + var legTweaks = modelSettings.legTweaks(); if (toggles != null) { // Note: toggles.has____ returns the same as toggles._____ this @@ -209,7 +235,16 @@ public record RPCSettingsHandler(RPCHandler rpcHandler, ProtocolAPI api) { ); } } + + if (legTweaks != null) { + if (legTweaks.hasCorrectionStrength()) { + legTweaksConfig.setCorrectionStrength(legTweaks.correctionStrength()); + } + this.api.server.humanPoseProcessor.getSkeleton().updateLegTweaksConfig(); + } + cfg.save(); + } this.api.server.getConfigManager().saveConfig(); 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 737a9fe1e..4f176deb2 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 @@ -138,6 +138,10 @@ public class HumanSkeleton extends Skeleton implements SkeletonConfigCallback { protected LegTweaks legTweaks = new LegTweaks(this); // #endregion + // #region tap detection + protected TapDetection tapDetection = new TapDetection(this); + // #endregion + // #region Constructors protected HumanSkeleton(List computedTrackers) { assembleSkeleton(false); @@ -160,6 +164,14 @@ public class HumanSkeleton extends Skeleton implements SkeletonConfigCallback { this(computedTrackers); setTrackersFromServer(server); skeletonConfig.loadFromConfig(server.getConfigManager()); + + tapDetection = new TapDetection( + this, + server.getVRCOSCHandler(), + server.getConfigManager().getVrConfig().getTapDetection() + ); + legTweaks.setConfig(server.getConfigManager().getVrConfig().getLegTweaks()); + } public HumanSkeleton( @@ -778,6 +790,7 @@ public class HumanSkeleton extends Skeleton implements SkeletonConfigCallback { @VRServerThread @Override public void updatePose() { + tapDetection.update(); updateLocalTransforms(); updateRootTrackers(); updateComputedTrackers(); @@ -1832,6 +1845,14 @@ public class HumanSkeleton extends Skeleton implements SkeletonConfigCallback { this.legTweaks.resetBuffer(); } + public void updateTapDetectionConfig() { + tapDetection.updateConfig(); + } + + public void updateLegTweaksConfig() { + legTweaks.updateConfig(); + } + @Override public boolean[] getLegTweaksState() { boolean[] state = new boolean[2]; diff --git a/server/src/main/java/dev/slimevr/vr/processor/skeleton/LegTweakBuffer.java b/server/src/main/java/dev/slimevr/vr/processor/skeleton/LegTweakBuffer.java index c9ef9aa57..76b4ca276 100644 --- a/server/src/main/java/dev/slimevr/vr/processor/skeleton/LegTweakBuffer.java +++ b/server/src/main/java/dev/slimevr/vr/processor/skeleton/LegTweakBuffer.java @@ -14,7 +14,10 @@ import com.jme3.math.FastMath; * from its correct position 2. a velocity higher than a threashold is achived * 3. a large acceleration is applied to the foot 4. angular velocity of the * foot goes higher than a threshold. The conditions for a lock are the opposite - * of the above but require a lower value for all of the above conditions + * of the above but require a lower value for all of the above conditions. The + * afformentioned thresholds are computed by applying scalers to a base + * threshold value. This allows one set of initial values to be applicable to a + * large range of actions and body types. */ public class LegTweakBuffer { @@ -77,8 +80,8 @@ public class LegTweakBuffer { // hyperparameters public static final float SKATING_DISTANCE_CUTOFF = 0.5f; - private static final float SKATING_VELOCITY_THRESHOLD = 3.25f; - private static final float SKATING_ACCELERATION_THRESHOLD = 1.00f; + static float SKATING_VELOCITY_THRESHOLD = 2.6f; + static float SKATING_ACCELERATION_THRESHOLD = 0.8f; private static final float SKATING_ROTVELOCITY_THRESHOLD = 4.5f; private static final float SKATING_LOCK_ENGAGE_PERCENT = 0.85f; private static final float SKATING_ACCELERATION_Y_USE_PERCENT = 0.25f; @@ -88,8 +91,8 @@ public class LegTweakBuffer { private static final float FORCE_ERROR_TOLLERANCE = 4.0f; private static final float[] FORCE_VECTOR_FALLBACK = new float[] { 0.1f, 0.1f }; - private static final float PARAM_SCALAR_MAX = 3.2f; - private static final float PARAM_SCALAR_MIN = 0.25f; + static float PARAM_SCALAR_MAX = 3.2f; + static float PARAM_SCALAR_MIN = 0.25f; private static final float PARAM_SCALAR_MID = 1.0f; // the point at which the scalar is at the max or min depending on accel diff --git a/server/src/main/java/dev/slimevr/vr/processor/skeleton/LegTweaks.java b/server/src/main/java/dev/slimevr/vr/processor/skeleton/LegTweaks.java index 06d85106c..595e7ec1a 100644 --- a/server/src/main/java/dev/slimevr/vr/processor/skeleton/LegTweaks.java +++ b/server/src/main/java/dev/slimevr/vr/processor/skeleton/LegTweaks.java @@ -3,6 +3,7 @@ package dev.slimevr.vr.processor.skeleton; import com.jme3.math.FastMath; import com.jme3.math.Quaternion; import com.jme3.math.Vector3f; +import dev.slimevr.config.LegTweaksConfig; import dev.slimevr.vr.processor.TransformNode; @@ -12,6 +13,7 @@ public class LegTweaks { private float floorLevel; private float waistToFloorDist; private float currentDisengagementOffset = 0.0f; + private static float currentCorrectionStrength = 0.3f; // default value private boolean initialized = true; private boolean enabled = true; // master switch private boolean floorclipEnabled = false; @@ -20,8 +22,9 @@ public class LegTweaks { private boolean rightLegActive = false; private boolean leftLegActive = false; - // skeleton + // skeleton and config private HumanSkeleton skeleton; + private LegTweaksConfig config; // leg data private Vector3f leftFootPosition = new Vector3f(); @@ -60,8 +63,8 @@ public class LegTweaks { */ // hyperparameters (clip correction) - private static final float DYNAMIC_DISPLACEMENT_CUTOFF = 1.0f; - private static final float MAX_DYNAMIC_DISPLACEMENT = 0.06f; + static float DYNAMIC_DISPLACEMENT_CUTOFF = 1.0f; + static float MAX_DYNAMIC_DISPLACEMENT = 0.06f; private static final float FLOOR_CALIBRATION_OFFSET = 0.015f; // hyperparameters (skating correction) @@ -92,6 +95,7 @@ public class LegTweaks { private static final float STANDING_CUTOFF_VERTICAL = 0.65f; private static final float MAX_DISENGAGMENT_OFFSET = 0.30f; private static final float DEFAULT_ARM_DISTANCE = 0.15f; + private static final float MAX_CORRECTION_STRENGTH_DELTA = 1.0f; // counters private int leftFramesLocked = 0; @@ -107,6 +111,13 @@ public class LegTweaks { this.skeleton = skeleton; } + public LegTweaks(HumanSkeleton skeleton, LegTweaksConfig config) { + this.skeleton = skeleton; + // set all the hyperparameters from the config + this.config = config; + updateConfig(); + } + public Vector3f getLeftFootPosition() { return leftFootPosition; } @@ -207,6 +218,33 @@ public class LegTweaks { bufferInvalid = true; } + public void setConfig(LegTweaksConfig config) { + this.config = config; + updateConfig(); + } + + public void updateConfig() { + LegTweaks.updateHyperParameters(config.getCorrectionStrength()); + } + + // update the hyper parameters with the config + public static void updateHyperParameters(float newStrength) { + LegTweakBuffer.SKATING_VELOCITY_THRESHOLD = getScaledHyperParameter( + newStrength, + LegTweakBuffer.SKATING_VELOCITY_THRESHOLD + ); + LegTweakBuffer.SKATING_ACCELERATION_THRESHOLD = getScaledHyperParameter( + newStrength, + LegTweakBuffer.SKATING_ACCELERATION_THRESHOLD + ); + currentCorrectionStrength = newStrength; + } + + public static float getScaledHyperParameter(float newStrength, float currentValue) { + return (currentValue - (currentCorrectionStrength * MAX_CORRECTION_STRENGTH_DELTA)) + + (newStrength * MAX_CORRECTION_STRENGTH_DELTA); + } + // set the vectors in this object to the vectors in the skeleton private void setVectors() { // set the positions of the feet and knees to the skeletons current @@ -366,10 +404,6 @@ public class LegTweaks { if (skatingCorrectionEnabled) correctSkating(); - // currently scuffed - // if (skatingCorrectionEnabled && floorclipEnabled) - // correctFloat(); - // determine if either leg is in a position to activate or deactivate // (use the buffer to get the positions before corrections) float leftFootDif = bufferHead diff --git a/server/src/main/java/dev/slimevr/vr/processor/skeleton/Skeleton.java b/server/src/main/java/dev/slimevr/vr/processor/skeleton/Skeleton.java index 5ef5656c2..75c5d7562 100644 --- a/server/src/main/java/dev/slimevr/vr/processor/skeleton/Skeleton.java +++ b/server/src/main/java/dev/slimevr/vr/processor/skeleton/Skeleton.java @@ -43,6 +43,12 @@ public abstract class Skeleton { @VRServerThread public abstract void resetTrackersYaw(); + @VRServerThread + public abstract void updateLegTweaksConfig(); + + @VRServerThread + public abstract void updateTapDetectionConfig(); + @VRServerThread public abstract boolean[] getLegTweaksState(); diff --git a/server/src/main/java/dev/slimevr/vr/processor/skeleton/TapDetection.java b/server/src/main/java/dev/slimevr/vr/processor/skeleton/TapDetection.java new file mode 100644 index 000000000..e471ae5c1 --- /dev/null +++ b/server/src/main/java/dev/slimevr/vr/processor/skeleton/TapDetection.java @@ -0,0 +1,226 @@ +package dev.slimevr.vr.processor.skeleton; + +import java.util.LinkedList; +import com.jme3.math.Vector3f; + +import dev.slimevr.config.TapDetectionConfig; +import dev.slimevr.osc.VRCOSCHandler; +import dev.slimevr.vr.trackers.Tracker; + + +// class that monitors the acceleration of the waist, hip, or chest trackers to detect taps +// and use this to trigger a quick reset (if your wondering why no single tap class exists, it's because +// to many false positives) +public class TapDetection { + + // server and related classes + private HumanSkeleton skeleton; + private VRCOSCHandler oscHandler; + private TapDetectionConfig config; + + + // tap detection + private boolean enabled = false; + private LinkedList accelList = new LinkedList<>(); + private LinkedList tapTimes = new LinkedList<>(); + private float resetDelayNs = 0.20f * 1000000000.0f; + + // hyperparameters + private static final float NEEDED_ACCEL_DELTA = 6.0f; + private static final float ALLOWED_BODY_ACCEL = 1.5f; + private static final float ALLOWED_BODY_ACCEL_SQUARED = ALLOWED_BODY_ACCEL * ALLOWED_BODY_ACCEL; + private static final float CLUMP_TIME_NS = 0.03f * 1000000000.0f; + private static final float TIME_WINDOW_NS = 0.5f * 1000000000.0f; + + // state + private float resetRequestTime = -1.0f; + + public TapDetection(HumanSkeleton skeleton) { + this.skeleton = skeleton; + } + + public TapDetection( + HumanSkeleton skeleton, + VRCOSCHandler oscHandler, + TapDetectionConfig config + ) { + this.skeleton = skeleton; + this.oscHandler = oscHandler; + this.config = config; + updateConfig(); + } + + public void updateConfig() { + this.enabled = config.getEnabled(); + this.resetDelayNs = config.getDelay() * 1000000000.0f; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public boolean getEnabled() { + return enabled; + } + + // main function for tap detection + public void update() { + if (skeleton == null || !enabled) + return; + + Tracker tracker = getTrackerToWatch(); + if (tracker == null) + return; + + // check if we should reset + if (resetRequestTime != -1.0f) { + if (System.nanoTime() - resetRequestTime > resetDelayNs) { + if (oscHandler != null) + oscHandler.yawAlign(); + skeleton.resetTrackersYaw(); + resetRequestTime = -1.0f; + } + return; + } + + // get the acceleration of the tracker and add it to the list + Vector3f accel = new Vector3f(); + tracker.getAcceleration(accel); + float time = System.nanoTime(); + float[] listval = { accel.length(), time }; + accelList.add(listval); + + // remove old values from the list (if they are too old) + while (time - accelList.getFirst()[1] > CLUMP_TIME_NS) { + accelList.removeFirst(); + } + + // check for a tap + if (getAccelDelta() > NEEDED_ACCEL_DELTA) { + tapTimes.add(time); + } + + // remove old taps from the list (if they are too old) + if (!tapTimes.isEmpty()) { + while (time - tapTimes.getFirst() > TIME_WINDOW_NS) { + tapTimes.removeFirst(); + if (tapTimes.isEmpty()) + return; + } + } + + // if the user is moving their body too much, reset the tap list + if (!isUserStatic(tracker)) { + tapTimes.clear(); + accelList.clear(); + } + + // get the amount of taps in the list + int tapEvents = getTapEvents(); + + // if there are two tap events and the user is moving relatively slowly, + // reset the skeleton + if (tapEvents == 2) { + resetRequestTime = System.nanoTime(); + // reset the list + tapTimes.clear(); + accelList.clear(); + } + } + + // returns either the chest tracker, hip tracker, or waist tracker depending + // on which one is available + // if none are available, returns null + public Tracker getTrackerToWatch() { + if (skeleton.chestTracker != null) + return skeleton.chestTracker; + else if (skeleton.hipTracker != null) + return skeleton.hipTracker; + else if (skeleton.waistTracker != null) + return skeleton.waistTracker; + else + return null; + } + + private float getAccelDelta() { + float max = -999.9f; + float min = 999.9f; + for (int i = 0; i < accelList.size(); i++) { + float[] val = accelList.get(i); + if (val[0] > max) + max = val[0]; + if (val[0] < min) + min = val[0]; + } + return max - min; + } + + // return the number of distinct tap events in tapTimes + private int getTapEvents() { + if (tapTimes.isEmpty()) + return 0; + + int tapEvents = 0; + float lastTapTime = tapTimes.getFirst(); + for (int i = 0; i < tapTimes.size(); i++) { + if (tapTimes.get(i) - lastTapTime > CLUMP_TIME_NS) { + tapEvents++; + lastTapTime = tapTimes.get(i); + } + } + return tapEvents; + } + + // returns true if the user is not imparting more than allowedBodyAccel of + // force on any of the torso or upper leg trackers (this sadly implies that + // you need two or more trackers for this feature to be reliable) + private boolean isUserStatic(Tracker trackerToExclude) { + Vector3f accel = new Vector3f(); + if (skeleton.chestTracker != null && !skeleton.chestTracker.equals(trackerToExclude)) { + skeleton.chestTracker.getAcceleration(accel); + if (accel.lengthSquared() > ALLOWED_BODY_ACCEL_SQUARED) + return false; + } + if (skeleton.hipTracker != null && !skeleton.hipTracker.equals(trackerToExclude)) { + skeleton.hipTracker.getAcceleration(accel); + if (accel.lengthSquared() > ALLOWED_BODY_ACCEL_SQUARED) + return false; + } + if (skeleton.waistTracker != null && !skeleton.waistTracker.equals(trackerToExclude)) { + skeleton.waistTracker.getAcceleration(accel); + if (accel.lengthSquared() > ALLOWED_BODY_ACCEL_SQUARED) + return false; + } + if ( + skeleton.leftUpperLegTracker != null + && !skeleton.leftUpperLegTracker.equals(trackerToExclude) + ) { + skeleton.leftUpperLegTracker.getAcceleration(accel); + if (accel.lengthSquared() > ALLOWED_BODY_ACCEL_SQUARED) + return false; + } + if ( + skeleton.rightUpperLegTracker != null + && !skeleton.rightUpperLegTracker.equals(trackerToExclude) + ) { + skeleton.rightUpperLegTracker.getAcceleration(accel); + if (accel.lengthSquared() > ALLOWED_BODY_ACCEL_SQUARED) + return false; + } + if ( + skeleton.leftFootTracker != null && !skeleton.leftFootTracker.equals(trackerToExclude) + ) { + skeleton.leftFootTracker.getAcceleration(accel); + if (accel.lengthSquared() > ALLOWED_BODY_ACCEL_SQUARED) + return false; + } + if ( + skeleton.rightFootTracker != null && !skeleton.rightFootTracker.equals(trackerToExclude) + ) { + skeleton.rightFootTracker.getAcceleration(accel); + if (accel.lengthSquared() > ALLOWED_BODY_ACCEL_SQUARED) + return false; + } + return true; + } +} diff --git a/solarxr-protocol b/solarxr-protocol index 724e50346..9287a2d56 160000 --- a/solarxr-protocol +++ b/solarxr-protocol @@ -1 +1 @@ -Subproject commit 724e50346839b7dcd7c858f185e9ddcb9711e343 +Subproject commit 9287a2d562968a1b467e1f55f2a157539d4c80af