Kinect SDK:骨架闪烁

时间:2013-03-22 14:19:54

标签: c# kinect kinect-sdk

我的代码存在问题。

当我使用MoveTo时,我的骨架会在屏幕上轻弹。我对原始代码(here)做了一些小改动:

  • 将骨架位置锁定到特定的Z位置
  • 增加水平速度(X)

我使用XNA 4.0并将该成员称为Draw“callback”。

所以,问题是:为什么骨架会轻弹?

private Skeleton MoveTo2(Skeleton skToBeMoved) {
    Joint newJoint = new Joint();
    ///Based on the HipCenter
    float howMuchMoveToX = ((skToBeMoved.Joints[JointType.HipCenter].Position.X - settings_skel_offset_x) * -1) * settings_skel_offset_x_mult;
    float howMuchMoveToY = (skToBeMoved.Joints[JointType.HipCenter].Position.Y - settings_skel_offset_y) * -1;
    float howMuchMoveToZ = (skToBeMoved.Joints[JointType.HipCenter].Position.Z - settings_skel_offset_z) * -1;

    foreach (JointType item in Enum.GetValues(typeof(JointType)))
    {
        newJoint = skToBeMoved.Joints[item];
        SkeletonPoint pos = new SkeletonPoint()
        {
            X = (float)(newJoint.Position.X + (howMuchMoveToX)),
            Y = (float)(newJoint.Position.Y + (howMuchMoveToY)),
            Z = (float)(newJoint.Position.Z + (howMuchMoveToZ)),
        };

        if (XnaBasics.settings_skel_lock_z)
        {
            pos.Z = settings_skel_offset_z;
        }

        newJoint.Position = pos;
        Debug.WriteLine("SkelID: "+skToBeMoved.TrackingId+ " howMuchMoveToX: " + howMuchMoveToX + " oldPosX: " + skToBeMoved.Joints[item].Position.X + " newPosX: " + newJoint.Position.X);
        skToBeMoved.Joints[item] = newJoint;
    }
    return skToBeMoved;
}

1 个答案:

答案 0 :(得分:0)

<强>解决

在从“Draw”回调调用之前,我将成员调用放入“Update”回调中。

正确代码(相关部分)

public override void Update(GameTime gameTime)
{
    // Debug.WriteLine("= UPDATE - SkeletonStreamRender");
    if (null == this.Chooser.Sensor || false == this.Chooser.Sensor.IsRunning || KinectStatus.Connected != this.Chooser.Sensor.Status)
    {
        return;
    }

    if (skeletonDrawn || !drawChk)
    {
        using (var skeletonFrame = this.Chooser.Sensor.SkeletonStream.OpenNextFrame(0))
            {
            if (null == skeletonData || skeletonData.Length != skeletonFrame.SkeletonArrayLength)
            {
                skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength];
            }

            skeletonFrame.CopySkeletonDataTo(skeletonData);

            int counter = 0;
            foreach (Skeleton s in skeletonData)
            {
                if (s.TrackingState == SkeletonTrackingState.Tracked)
                {
                    playersID = s.TrackingId;
                    Skeleton skeleton = MoveTo2(s);
                    skeletonData[counter] = skeleton;
                    continue;
                }
                counter++;
            }

            skeletonDrawn = false;
        }
        [CUT]

来自Draw的错误电话

public override void Draw(GameTime gameTime)
    {
        // Debug.WriteLine("== DRAW - SkeletonStreamRender");
        // If the joint texture isn't loaded, load it now
        if (null == this.jointTexture)
        {
            this.LoadContent();
        }

        // If we don't have data, lets leave
        if (null == skeletonData || null == this.mapMethod)
        {
            return;
        }

        if (false == this.initialized)
        {
            this.Initialize();
        }

        this.SharedSpriteBatch.Begin();

        foreach (var skeleton in skeletonData)
        {
            if (playersID != skeleton.TrackingId)
                continue;

            switch (skeleton.TrackingState)
            {
                case SkeletonTrackingState.NotTracked:
                    // non tracciato
                    break;
                case SkeletonTrackingState.Tracked:
                    // blocco la posizione Z

                    Skeleton skeleton = MoveTo2(skeletonTmp);

                    // Draw Bones
                    this.DrawBone(skeleton.Joints, JointType.Head, JointType.ShoulderCenter);
                    this.DrawBone(skeleton.Joints, JointType.ShoulderCenter, JointType.ShoulderLeft);
                    this.DrawBone(skeleton.Joints, JointType.ShoulderCenter, JointType.ShoulderRight);
                    this.DrawBone(skeleton.Joints, JointType.ShoulderCenter, JointType.Spine);
                    [CUT]