How to calculate user's speed in front of camera?

 now i'm working on a  project,there's one feature is to calclate user's speed,when user are walking in front of the camera. 
if user stop,the speed should be 0 m/s .

anyone has good ideas? thanks a lot
gary

It is easy to find out the user’s speed.
You need to divide the distance traveled by any torso joints (for example, waist) between frames by the time delta of the frame.

Below is an example of a simple script that determines the speed of any joint.

An example of a simple script that determines the speed of any joint.
using UnityEngine;

public class UserVelocity : MonoBehaviour
{
    [SerializeField] nuitrack.JointType jointType = nuitrack.JointType.Waist;
    [SerializeField, Range(0, 5)] int skeletonNumber = 0;

    Vector3? lastPosition = null;

    float lastTime = 0;

    void OnEnable()
    {
        lastPosition = null;
        NuitrackManager.onSkeletonTrackerUpdate += NuitrackManager_onSkeletonTrackerUpdate;
    }

    void OnDisable()
    {
        NuitrackManager.onSkeletonTrackerUpdate -= NuitrackManager_onSkeletonTrackerUpdate;
    }

    private void NuitrackManager_onSkeletonTrackerUpdate(nuitrack.SkeletonData skeletonData)
    {
        if(skeletonNumber < skeletonData.NumUsers)
        {
            nuitrack.Skeleton skeleton = skeletonData.Skeletons[skeletonNumber];
            nuitrack.Joint joint = skeleton.GetJoint(jointType);

            if (joint.Confidence > 0.1)
            {
                Vector3 jointPosition = joint.ToVector3() * 0.001f;

                if (lastPosition != null)
                {
                    float deltaTime = Time.time - lastTime;
                    Vector3 deltaMove = jointPosition - (Vector3)lastPosition;
                    float velocity = deltaMove.sqrMagnitude / deltaTime;

                    Debug.Log(string.Format("Velocity: {0} m/s", velocity));
                }

                lastPosition = jointPosition;
                lastTime = Time.time;
            }
        }
    }
}
1 Like

thanks for your help

another small question need your help,when user are walking along with the oval path (like school play ground) in front of the camera .when user get to the other end want to turn back,how do i know user’s direction or he is make a turning?

thanks Eugene.

You can find out the user’s direction (vector) from the cross product of two vectors:

  1. vector from the joint of the left shoulder to the joint of the right shoulder
  2. vector from the head joint to the torso joint

See Vector3.Cross.

thanks for your respond,but if i don’t use unity in my project,just use C# and wpf ,i can’t find any functions you mentioned .what should i do for this situation?

This is a simple vector algebra.
Let’s say in the C# code we have a Vector3 structure with fields x, y, z, then you can describe this method as follows:

public static Vector3 Cross(Vector3 lhs, Vector3 rhs)
{
    return new Vector3(
        lhs.y * rhs.z - lhs.z * rhs.y,
        lhs.z * rhs.x - lhs.x * rhs.z,
        lhs.x * rhs.y - lhs.y * rhs.x);
}

You can also use Vector3D.CrossProduct from
System.Windows.Media.Media3D

cool,i got it ,thank you eugene~