0

我对这段代码有疑问。

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

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

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

所以,问题是:为什么骷髅会轻弹?

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

1 回答 1

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]
于 2013-03-22T22:31:23.917 回答