1

我正在学习 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);

有没有人有任何资源或给我一些关于如何工作的提示?

4

0 回答 0