Microsoft Small Basic

Program Listing: JTH778
' KinectBodyList Sample Code
iOp = 4
Init()
KinectBodyList.BodiesChanged = OnBodiesChanged
KinectBodyList.StartTracking()
tracking = "True"
While tracking
If bodiesChanged Then
count = KinectBodyList.Count
For index = 1 To count
isTracked = KinectBodyList.IsTracked(index)
If isTracked Then
id = KinectBodyList.GetTrackingId(index)
Shapes.SetText(txtId, "Tracking ID = " + id)
If op[iOp] = "GetJointOrientation" Then
GetJointOrientation()
ElseIf op[iOp] = "GetJointPosition" Then
GetJointPosition()
ElseIf op[iOp] = "GetJointScreenPosition" Then
GetJointScreenPosition()
ElseIf op[iOp] = "GetJointOrientation" Then
GetJointOrientation()
ElseIf op[iOp] = "GetHandState" Then
GetHandState()
ElseIf op[iOp] = "GetLean" Then
GetLean()
EndIf
EndIf
EndFor
bodiesChanged = "False"
EndIf
If buttonClicked Then
tracking = "False"
buttonClicked = "False"
EndIf
If keyDown Then
key = GraphicsWindow.LastKey
If Text.IsSubText("D1|D2|D3|D4|D5", key) Then
iOp = Text.GetSubTextToEnd(key, 2)
GraphicsWindow.Clear()
Init()
EndIf
keyDown = "False"
EndIf
Program.Delay(100)
EndWhile
KinectBodyList.StopTracking(closeSensor)

Sub DrawJoint
If joint[j] <> "" Then
Shapes.Remove(joint[j])
Shapes.Remove(lead[j])
Shapes.Remove(caption[j])
EndIf
If link[j] <> "" Then
Shapes.Remove(link[j])
EndIf
If Text.StartsWith(jointType[j], "Spine") Or jointType[j] = "Neck" Then
size = 8
ElseIf jointType[j] = "Head" Then
size = 12
Else
size = 4
EndIf
GraphicsWindow.PenWidth = 1
GraphicsWindow.PenColor = "Gray"
lead[j] = Shapes.AddLine(p["x"], p["y"], p["x"] + 10, p["y"] - 10)
If showOrientation Then
caption[j] = Shapes.AddText(Math.Round(Math.GetDegrees(θ)))
Else
caption[j] = Shapes.AddText(jointType[j])
EndIf
Shapes.Move(caption[j], p["x"] + 10, p["y"] - 20)
needLink = "False"
GraphicsWindow.PenWidth = 2
GraphicsWindow.PenColor = "Black"
If 2 <= j And j <= 5 Then
needLink = "True" ' body
GraphicsWindow.PenWidth = 4
ElseIf 7 <= j And j <= 11 Then
needLink = "True" ' right arm
ElseIf 13 <= j And j <= 17 Then
needLink = "True" ' left arm
ElseIf 19 <= j And j <= 21 Then
needLink = "True" ' right leg
ElseIf 23 <= j And j <= 25 Then
needLink = "True" ' left leg
EndIf
If needLink Then
link[j] = Shapes.AddLine(pLast["x"], pLast["y"], p["x"], p["y"])
EndIf
If j <> 10 And j <> 16 Then
pLast = p
EndIf
GraphicsWindow.PenWidth = 0
joint[j] = Shapes.AddEllipse(size, size)
Shapes.Move(joint[j], p["x"] - size / 2, p["y"] - size / 2)
EndSub

Sub GetHandState
GraphicsWindow.Title = title + " - GetHandState"
buf = ""
For h = 1 To 2
state = KinectBodyList.GetHandState(index, hand[h])
TextWindow.WriteLine("h=" + h)
TextWindow.WriteLine("hand[h]=" + hand[h])
buf = buf + hand[h] + " : " + state + CRLF
EndFor
Shapes.SetText(txtMsg, buf)
EndSub

Sub GetJointOrientation
GraphicsWindow.Title = title + " - GetJointOrientation"
For j = 1 To nJoint
pos = KinectBodyList.GetJointOrientation(index, jointType[j])
If pos <> "" Then
θ = 2 * Math.ArcCos(pos["W"])
Else
θ = "?"
EndIf
pos = KinectBodyList.GetJointPosition(index, jointType[j])
If pos <> "" Then
Map2D()
showOrientation = "True"
DrawJoint()
EndIf
EndFor
EndSub

Sub GetJointScreenPosition
GraphicsWindow.Title = title + " - GetJointScreenPosition"
For j = 1 To nJoint
pos = KinectBodyList.GetJointScreenPosition(index, jointType[j])
If pos <> "" Then
p["x"] = pos["x"] / 3
p["y"] = pos["y"] / 3
showOrientation = "False"
DrawJoint()
EndIf
EndFor
EndSub

Sub GetJointPosition
GraphicsWindow.Title = title + " - GetJointPosition"
For j = 1 To nJoint
pos = KinectBodyList.GetJointPosition(index, jointType[j])
If pos <> "" Then
Map2D()
showOrientation = "False"
DrawJoint()
EndIf
EndFor
EndSub

Sub GetLean
GraphicsWindow.Title = title + " - GetLean"
state = KinectBodyList.GetLeanTrackingState(index)
If state <> "NotTracked" Then
pos = KinectBodyList.GetLean(index)
buf = "X = " + pos["X"]
buf = buf + "Y = " + pos["Y"]
EndIf
Shapes.SetText(txtMsg, buf)
EndSub

Sub Init
title = "KinectBodyList Sample Code"
GraphicsWindow.Title = title
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
gw = 600
gh = 600
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontName = "Trebuchet MS"
GraphicsWindow.FontSize = 10
txtId = Shapes.AddText("")
Shapes.Move(txtId, 10, 10)
txtMsg = Shapes.AddText("")
Shapes.Move(txtMsg, 10, 30)
op = "1=GetJointPosition;2=GetJointScreenPosition;"
op = op + "3=GetHandState;4=GetJointOrientation;5=GetLean;"
hand = "1=right;2=left;"
jointType = "1=Head;2=Neck;3=SpineShoulder;4=SpineMid;5=SpineBase;"
jointType = jointType + "6=ShoulderRight;7=ElbowRight;8=WristRight;"
jointType = jointType + "9=HandRight;10=ThumbRight;11=HandTipRight;"
jointType = jointType + "12=ShoulderLeft;13=ElbowLeft;14=WristLeft;"
jointType = jointType + "15=HandLeft;16=ThumbLeft;17=HandTipLeft;"
jointType = jointType + "18=HipRight;19=KneeRight;20=AnkleRight;"
jointType = jointType + "21=FootRight;22=HipLeft;23=KneeLeft;"
jointType = jointType + "24=AnkleLeft;25=FootLeft;"
nJoint = Array.GetItemCount(jointType)
ru = 200
a60 = Math.GetRadians(60)
a120 = Math.GetRadians(120)
closeSensor = "False"
xo = 0
yo = gh * 0.6
btn = Controls.AddButton("Exit", gw - 40, gh - 30)
Controls.ButtonClicked = OnButtonClicked
GraphicsWindow.KeyDown = OnKeyDown
EndSub

Sub Map2D
' param pos["X"], pos["Y"], pos["Z"]
' return p["x"], p["y"]
p["x"] = xo + ru * Math.Sin(a60) * pos["Z"] + ru * Math.Sin(-a60) * pos["X"]
p["y"] = yo - ru * Math.Cos(a60) * pos["Z"] - ru * pos["Y"] - ru * Math.Cos(-a60) * pos["X"]
EndSub

Sub OnBodiesChanged
bodiesChanged = "True"
EndSub

Sub OnButtonClicked
buttonClicked = "True"
EndSub

Sub OnKeyDown
keyDown = "True"
EndSub