1

我正在做一个非常简单的事情,我的目标是根据另一个骨架的位置移动一个骨架,为此我基于 HipCenter 位置。(这个算法可能是错误的,这个问题是关于 foreach 循环中发生的异常)

这是我的实际代码:

public static Skeleton MoveTo(this Skeleton skOrigin, Skeleton skDestiny)
{
     Skeleton skReturn = skOrigin; // just making a copy

        // find the factor to move, based on the HipCenter.
        float whatToMultiplyX = skOrigin.Joints[JointType.HipCenter].Position.X / skDestiny.Joints[JointType.HipCenter].Position.X;
        float whatToMultiplyY = skOrigin.Joints[JointType.HipCenter].Position.Y / skDestiny.Joints[JointType.HipCenter].Position.Y;
        float whatToMultiplyZ = skOrigin.Joints[JointType.HipCenter].Position.Z / skDestiny.Joints[JointType.HipCenter].Position.Z;


        SkeletonPoint movedPosition = new SkeletonPoint();
        Joint movedJoint = new Joint();
        foreach (JointType item in Enum.GetValues(typeof(JointType)))
        {
            // Updating the position
            movedPosition.X = skOrigin.Joints[item].Position.X * whatToMultiplyX;
            movedPosition.Y = skOrigin.Joints[item].Position.Y * whatToMultiplyY;
            movedPosition.Z = skOrigin.Joints[item].Position.Z * whatToMultiplyZ;

            // Setting the updated position to the skeleton that will be returned.
            movedJoint.Position = movedPosition;
            skReturn.Joints[item] = movedJoint;
        }

        return skReturn;
    }

使用 F10 调试一切正常,直到 foreach 循环的第二遍。当我在 foreach 中第二次通过时,我在这条线上遇到了异常

skReturn.Joints[item] = movedJoint;

异常说:

JointType index value must match Joint.JointType 

但价值实际上是脊椎。

怎么了?

4

1 回答 1

2

解决了,这里是解决方案

 Joint newJoint = new Joint(); // declare a new Joint

// Iterate in the 20 Joints
foreach (JointType item in Enum.GetValues(typeof(JointType)))
{
    newJoint = skToBeMoved.Joints[item];

            // applying the new values to the joint
            SkeletonPoint pos = new SkeletonPoint()
            {
                X = (float)(newJoint.Position.X + (someNumber)),
                Y = (float)(newJoint.Position.Y + (someNumber)),
                Z = (float)(newJoint.Position.Z + (someNumber))
            };

            newJoint.Position = pos;
            skToBeChanged.Joints[item] = newJoint;
        }

这将起作用。

于 2012-11-27T22:30:17.623 回答