2012-09-27 75 views
7

這是我在uni的第一個圖形主題,實現的某些部分不適合我。我可以正確地繪製關節,但我正在嘗試編寫一個將關節之間的「骨骼」放入其中的函數。在這一點上,骨骼只是立方體,被轉換成直角棱鏡,稍後我會嘗試從攪拌機或其他東西介紹適當的模型。使用Kinect SDK創建一個完整的3D骨架

我的煩惱是旋轉。大約5個小時左右後,我的伴侶和我有一些工作,但只要你的手臂或腿移動,立方體就會變形,看起來很奇怪。任何幫助,將不勝感激。以下是試圖繪製骨骼的功能。

private void DrawBone(Skeleton skeleton, JointType jointType0, JointType jointType1) 
{ 
    Joint joint0 = skeleton.Joints[jointType0]; 
    Joint joint1 = skeleton.Joints[jointType1]; 

    // If we can't find either of these joints, exit 
    if (joint0.TrackingState == JointTrackingState.NotTracked || 
     joint1.TrackingState == JointTrackingState.NotTracked) 
    { 
     return; 
    } 

    // Don't draw if both points are inferred 
    if (joint0.TrackingState == JointTrackingState.Inferred && 
     joint1.TrackingState == JointTrackingState.Inferred) 
    { 
     return; 
    } 

    // We assume all drawn bones are inferred unless BOTH joints are tracked 
    if (joint0.TrackingState == JointTrackingState.Tracked && joint1.TrackingState == JointTrackingState.Tracked) 

     //jointvector(joint) takes a joint and returns a Vector2 with its position data, with the z invtred to work properly with directx11 
     Vector3 bonevector = Vector3.Subtract(jointVector(joint0), jointVector(joint1)); 
     //cubevector is supposed to describe the initial vector of a cube centred at (0,0,0) with a side length of 2. A fair amount of guesswork has gone into this bit. 
     Vector3 cubevector = new Vector3(0, 2, 0); 
     //midpoint of two joints 
     Vector3 transTest = Vector3.Divide(Vector3.Add(jointVector(joint0),jointVector(joint1)), 2); 
     //divide by two because our original cube has side length 2 
     float scaleFactor = Math.Abs(bonevector.Length()/cubevector.Length()); 

     //we haven't been taught quaternions in class or anything, but after a fair while searching for similar problems they seemed like the go-to option for rotations. 
     world = Matrix.Transformation(new Vector3(0, 0, 0), new Quaternion(0), new Vector3(0.05f, scaleFactor, 0.05f), new Vector3(0, 0, 0), new Quaternion(Vector3.Cross(cubevector, bonevector), (float)Math.Acos(Vector3.Dot(bonevector, cubevector))), transTest);  

     //view and proj are defined elsewhere and are working fine, it's just the world that is being lame   
     worldViewProj = world * view * proj; 
     worldViewProj.Transpose(); 
     sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer); 
     //36 vertices of the cube (two triangles per face) defined elsewhere 
     sDXdata.context.Draw(36, 0); 
     return; 
+0

嗨文斯,你有過這樣的事嗎?將有興趣瞭解如何在3D中顯示模型。 –

回答

3

骨方向,檢查:

skeleton.BoneOrientations[joint.JointType] 

它會給你一個BoneOrientation類

http://msdn.microsoft.com/en-us/library/microsoft.kinect.boneorientation.aspx

從那裏,你可以旋轉的任一四元/矩陣,無論是在世界空間或父骨骼空間(用於剝皮)

同時使用:

new Quaternion(0) 

這是一個0矢量,而不是旋轉有效的四元數,使用:

Quaternion.Identity; 

這將是(0,0,0,1)

1

雖然這是前一段時間提出的,但我認爲在計算角度時也存在錯誤。您有:

(float)Math.Acos(Vector3.Dot(bonevector, cubevector) 

我想你需要:

(float)Math.Acos(Vector3.Dot(bonevector, cubevector)/(bonevector.Length() * 
cubevector.Length())) 

Here是數學的一個可愛的小例子。你也可以讓他們的單位矢量,所以他們有一個的長度,並且不與理會師:

(float)Math.Acos(Vector3.Dot(bonevector.Normalize(), cubevector.Normalize())) 

退房的MSDN頁面瞭解更多信息。