Skip to main content
Anton111111
Inspiring
April 20, 2024

clap detection

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

This topic has been closed for replies.

1 reply

Anton111111
Inspiring
April 22, 2024

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;
    }