使用Kinect SDK for Windows,用裸手或(对象)击中天空中的物体

时间:2014-01-17 00:33:21

标签: c# wpf kinect nui

我正在阅读Rob Miles的一本名为Learn the Kinect API的书中的教程。 基本上,它是一个增强现实游戏,蜘蛛从屏幕顶部落下,你用木槌击中它们。按照教程并查看代码后,我了解蜘蛛是如何堕落以及它们的位置如何随机化等。

我对槌的理解有问题,我希望用篮子的图像代替槌。

这就是槌的代码看起来像

Brush malletHandleBrush = new SolidColorBrush(Colors.Black);
Brush malletHeadBrush = new SolidColorBrush(Colors.Red);
float malletHandleLength = 100;
float malletHeadLength = 50;
System.Windows.Vector malletPosition;
float malletHitRadius = 40;
bool malletValid = false;

void updateMallet(Joint j1, Joint j2)
{
    // If Joint 1 (Right Wrist) or Joint 2 (Right Hand) is not tracked, we stop here
    if (j1.TrackingState != JointTrackingState.Tracked || j2.TrackingState != JointTrackingState.Tracked)
        return;

    // Get the start and end positions of the mallet vector
    ColorImagePoint j1P = myKinect.CoordinateMapper.MapSkeletonPointToColorPoint(j1.Position, ColorImageFormat.RgbResolution640x480Fps30);
    ColorImagePoint j2P = myKinect.CoordinateMapper.MapSkeletonPointToColorPoint(j2.Position, ColorImageFormat.RgbResolution640x480Fps30);

    int dX = j2P.X - j1P.X;
    int dY = j2P.Y - j1P.Y;

    System.Windows.Vector malletDirection = new System.Windows.Vector(dX, dY);

    if (malletDirection.Length < 1) return;

    // Convert into a vector of length 1 unit
    malletDirection.Normalize();

    // now set the length of the mallet 
    System.Windows.Vector handleVector = malletDirection * malletHandleLength;

    Line handleLine = new Line();

    handleLine.Stroke = malletHandleBrush;
    handleLine.StrokeThickness = 10;

    handleLine.X1 = j1P.X;
    handleLine.Y1 = j1P.Y;

    handleLine.X2 = j1P.X + handleVector.X;
    handleLine.Y2 = j1P.Y + handleVector.Y;

    //malletCanvas.Children.Add(handleLine);

    Line headLine = new Line();

    headLine.Stroke = malletHeadBrush;
    headLine.StrokeThickness = 50;

    System.Windows.Vector headVector = malletDirection * malletHeadLength;

    headLine.X1 = handleLine.X2;
    headLine.Y1 = handleLine.Y2;

    headLine.X2 = handleLine.X2 + headVector.X;
    headLine.Y2 = handleLine.Y2 + headVector.Y;
    //malletCanvas.Children.Add(headLine);

    malletPosition = new System.Windows.Vector(j1P.X, j1P.Y);

    malletPosition = malletPosition + (malletDirection * (malletHandleLength + (malletHeadLength / 2)));

    malletValid = true;
}

这是用于检测槌是否击中对象的代码的方式

// Declare Hit Vector for each Dollar Note
System.Windows.Vector _spiderHitVector = new System.Windows.Vector(malletPosition.X - _spiderCenterX, malletPosition.Y - _spiderCenterY);

有没有人有任何资源或给我一些关于如何处理这个问题的提示?

0 个答案:

没有答案