Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
Anton111111's avatar
Anton111111
Expert Protege
1 year ago

clap detection

I wonder how to detect clap (with hand tracking)?

1 Reply

Replies have been turned off for this discussion
  • Anton111111's avatar
    Anton111111
    Expert Protege

    I see posts in blogs that meta has ability to detect clapping but i can't find manual for it. I implemente my own solution. But i don't sure that it's right.

     

    private OVRHand _leftHand;
        private OVRHand _rightHand;
        private bool _clapped = false;
        private float _handActionDeltaTime = -1f;
        private float _handMinSecondsBetweenActions = 1f;
        private float _handsDistanceVelocity = 0;
        private float _handsDistance = 0;
        private float _prevHandsDistance = 0;
    
        
        private void Update()
        {
            HandClapDetection()
        }
    
        private void FixedUpdate()
        {
            if (!_leftHand.IsTracked || !_rightHand.IsTracked)
            {
                _prevHandsDistance = 0f;
                _handsDistanceVelocity = 0f;
                _handActionDeltaTime = -1f;
                return;
            }
    
            _handActionDeltaTime += Time.fixedDeltaTime;
            if (_handActionDeltaTime >= _handMinSecondsBetweenActions)
            {
                _handActionDeltaTime = -1;
            }
    
            _handsDistance = Vector3.Distance(
                _leftHand.transform.position,
                _rightHand.transform.position
            );
    
            _handsDistanceVelocity =
                (_prevHandsDistance > 0)
                    ? (_prevHandsDistance - _handsDistance) / Time.fixedDeltaTime
                    : 0;
    
            _prevHandsDistance = _handsDistance;
        }
    
    
        private bool IsHandActionAllowed() =>
            _handActionDeltaTime < 0 || _handActionDeltaTime >= _handMinSecondsBetweenActions;
    
        private bool HandClapDetection()
        {
            var thresholdDistance = 0.09f;
            var minVelocity = 0.6f;
    
            if (
                !_clapped
                && IsHandActionAllowed()
                && _handsDistanceVelocity > minVelocity
                && _handsDistance < thresholdDistance
            )
            {
                _clapped = true;
                _handActionDeltaTime = 0;
                _viewModel.OnClap();
                return true;
            }
    
            if (_clapped && _handsDistance > thresholdDistance * 4f)
            {
                _clapped = false;
            }
            return false;
        }