问题描述
我正在研究一种扩展方法,可以将一个骨骼移动到kinect场os视图中的所需位置.
I am working on a extension method to move one skeleton to a desired position in the kinect field os view.
我的代码接收到要移动的骨骼和目标位置,我计算接收到的骨骼髋关节中心和目标位置之间的距离以找到how much to move
,然后应用此因子在关节中进行迭代.我的代码实际上看起来像这样.
My code receives a skeleton to be moved and the destiny position, i calculate the distance between the received skeleton hip center and the destiny position to find how much to move
, then a iterate in the joint applying this factor. My code, actualy looks like this.
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位置.
Actualy, only X position is considered.
现在,问题:
如果我站在负面位置,并且将我的手移到正面位置,则行为异常,请看这张图片
If i stand in a negative position, and move my hand to a positive position, a have a strange behavior, look this image
要重现此行为,您可以使用以下代码
To reproduce this behaviour you could use this code
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);
}
我知道,这是简单的数学运算,但是我无法弄清楚为什么会发生这种情况.任何帮助将不胜感激.
I know, this is simple math, but i cant figure it out why this is happen.Any help will be apreciated.
推荐答案
问题已解决.这是代码
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 = (skToBeMoved.Joints[JointType.HipCenter].Position.X - destiny.X) * -1;
float howMuchMoveToY = (skToBeMoved.Joints[JointType.HipCenter].Position.Y - destiny.Y) * -1;
float howMuchMoveToZ = (skToBeMoved.Joints[JointType.HipCenter].Position.Z - destiny.Z) * -1;
// 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 + (howMuchMoveToX)),
Y = (float)(newJoint.Position.Y + (howMuchMoveToY)),
Z = (float)(newJoint.Position.Z + (howMuchMoveToZ))
};
newJoint.Position = pos;
skToBeMoved.Joints[item] = newJoint;
}
return skToBeMoved;
}
这篇关于如何将kinect骨骼移动到另一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!