# Gestures

## Gesture from pose

One of the built-in gestures can receive a HandPose as parameter and provide a lerp value that reflects how similar is the current hand gesture to the referenced HandPose. This gesture is *HandPoseMatch*.

![](https://2698598769-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MYpeL-Cll-FXMj9b3XC-887967055%2Fuploads%2FMmyeoFO262KysK3AdrsI%2FHandPoseMatch.png?alt=media\&token=39a01bcd-5109-49d6-9b41-d5cfd66b80b9)

*HandPoseMatch* must be referenced in `GestureDetectionModel.extra` as any other extra gesture. It also needs a reference to a *FingerPoseMatch* for each finger. Each *FingerPoseMatch* must also be referenced in its respective `FingerGestureModel.extra` list so its values are updated by GestureDetectionController.

HandPoseMatch averages the lerp value of each FingerPoseMatch. Each FingerPoseMatch calculates the average angle between the goal bone rotation and the actual bone rotation for each finger bone and translate it to a lerp value. `FingerPoseMatch.lerp` equal to 0 means that the angle is greater than `HandPoseMatch.maxAngle` for every finger bone. `FingerPoseMatch.lerp` equal to 1 means that the angle is 0 for every finger bone so the finger is performing `HandPoseMatch.pose` for that finger perfeclty.  &#x20;

#### Variables

* **Pose:** HandPose that will act as goal pose. Performing this gesture exactly will make `HandPoseMatch.lerp` to be equal to 1.
* **MaxAngle:** Angle from which. Big values (80-200) result in flexible gesture detection. Small values (10-60) result in a strict gesture detection.
* **Related finger gestures:** FingerPoseMatch gestures linked to this HandPoseMatch.

## Scripted gestures

Comparing current hand state against an arbitrary hand pose every frame can be unnecessarily demanding and it can be too strict for some use cases. For a more optimized, more flexible or more complex gesture detection, scripted gestures are the best option.

Scripted gestures inherit from *HandGesture* or *FingerGesture* which, in turn, ultimately inherit from *MonoBehaviour*. From their model variable it's possible to get any other gesture at the same level (same hand or same finger) or even accessing their corresponding avatar submodels through `_model.hand` or `_model.finger`.

#### Hand gestures

This is the code skeleton for a scripted hand gesture:

```csharp
public class YourHandGesture : HandGesture
{
    // GestureDetectionModel HandGesture._model

    public override sealed void InitHandGesture()
    {
        base.InitHandGesture();
        //...
    }

    public override sealed void HandLerpUpdate()
    {
        base.HandLerpUpdate();
        // ...
        _lerp = ...
        if (providesIntention) { //... }
    }

    public override sealed void LateGestureUpdate()
    {
        base.LateGestureUpdate();
        //...
    }
}
```

#### Finger gestures

This is the code skeleton for a scripted finger gesture:

```csharp
public class YourFingerGesture : FingerGesture
{
    // FingerGesturesModel FingerGesture._model

    public override sealed void InitFingerGesture()
    {
        base.InitFingerGesture();      
        //...
    }

    public override sealed void FingerLerpUpdate()
    {
        base.FingerLerpUpdate();      
        //...    
        _lerp = ...    
        if (providesIntention) { //... }
    }

    public override sealed void LateGestureUpdate()
    {
        base.LateGestureUpdate(); 
        //...
    }
}
```

#### Connection

Scripted gestures must be added to the scene and connected to a GestureDetection model which will be the responsible of updating (or not) these gestures. By connecting them to a GestureDetection module, it's possible for other scripts to reach them from AvatarModel (or AvatarView) or any of its sub-modules (or sub-views).

This connection can be performed as follows:

1. Attach the scripted gesture as component to a new empty GameObject.
2. Add its reference to its corresponding list of extra gestures:
   * `GestureDetectionModel.extra` for hand gestures.
   * `FingerGesturesModel.extra` for finger gestures.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jorge-jgnz94.gitbook.io/hptk/master/customization/custom-gestures.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
