我正在研究一种扩展方法,将一个骨架移动到 kinect 场操作系统视图中的所需位置。
我的代码接收要移动的骨骼和命运位置,我计算接收到的骨骼髋关节中心和要找到的命运位置之间的距离how much to move
,然后在关节中迭代应用这个因素。我的代码,实际上看起来像这样。
public static Skeleton MoveTo(this Skeleton skToBeMoved, Vector4 destiny)
{
Joint newJoint = new Joint();
///Based on the HipCenter (i dont know if it is reliable, seems it is.)
float howMuchMoveToX = Math.Abs(skToBeMoved.Joints[JointType.HipCenter].Position.X - destiny.X);
float howMuchMoveToY = Math.Abs(skToBeMoved.Joints[JointType.HipCenter].Position.Y - destiny.Y);
float howMuchMoveToZ = Math.Abs(skToBeMoved.Joints[JointType.HipCenter].Position.Z - destiny.Z);
float howMuchToMultiply = 1;
// Iterate in the 20 Joints
foreach (JointType item in Enum.GetValues(typeof(JointType)))
{
newJoint = skToBeMoved.Joints[item];
// This adjust, try to keeps the skToBeMoved in the desired position
if (newJoint.Position.X < 0)
howMuchToMultiply = 1; // if the point is in a negative position, carry it to a "more positive" position
else
howMuchToMultiply = -1; // if the point is in a positive position, carry it to a "more negative" position
// applying the new values to the joint
SkeletonPoint pos = new SkeletonPoint()
{
X = newJoint.Position.X + (howMuchMoveToX * howMuchToMultiply),
Y = newJoint.Position.Y, // * (float)whatToMultiplyY,
Z = newJoint.Position.Z, // * (float)whatToMultiplyZ
};
newJoint.Position = pos;
skToBeMoved.Joints[item] = newJoint;
//if (skToBeMoved.Joints[JointType.HipCenter].Position.X < 0)
//{
// if (item == JointType.HandLeft)
// {
// if (skToBeMoved.Joints[item].Position.X > 0)
// {
// }
// }
//}
}
return skToBeMoved;
}
实际上,只考虑 X 位置。
现在,问题:
如果我站在消极的位置,然后把手移到积极的位置,就会有一种奇怪的行为,看看这张图片
要重现此行为,您可以使用此代码
using (SkeletonFrame frame = e.OpenSkeletonFrame())
{
if (frame == null)
return new Skeleton();
if (skeletons == null || skeletons.Length != frame.SkeletonArrayLength)
{
skeletons = new Skeleton[frame.SkeletonArrayLength];
}
frame.CopySkeletonDataTo(skeletons);
Skeleton skeletonToTest = skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();
Vector4 newPosition = new Vector4();
newPosition.X = -0.03412333f;
newPosition.Y = 0.0407479f;
newPosition.Z = 1.927342f;
newPosition.W = 0; // ignored
skeletonToTest.MoveTo(newPosition);
}
我知道,这是简单的数学运算,但我无法弄清楚为什么会发生这种情况。任何帮助将不胜感激。