-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeapMotion.s3d
131 lines (112 loc) · 3.69 KB
/
LeapMotion.s3d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef _LEAPMOTION
#define _LEAPMOTION
#define LM_SIDE_W 0
#define LM_SIDE_B 1
#define LM_SIDE_M 2
class LeapMotion
{
var channel;
var data;
var port;
var side;
var reduction_factor;
var dataValues;
var newGesture;
var oldGesture;
var gesture;
var gesturePresence;
Connect();
Update(palm_pos, f0_pos, f1_pos);
ChangeSide(newSide);
GetGesture(g);
};
function LeapMotion::LeapMotion(p, s)
{
port = p;
side = s;
reduction_factor = 10;
dataValues = {"0",0,0,0, 0,0,0 ,0,0,0};
// gesture, palmX, palmY, palmZ, f0X, ... f1X...
newGesture = "";
oldGesture = "";
gesturePresence = false;
}
function LeapMotion::Connect()
{
var t = 3;
do
{
channel = NetCreateChannel(port, 0, VR_NO_BLOCKING);
if(channel !=0) //not working, XVR crash
{
OutputLN("Connected to LeapMotion");
return true;
}
else
{
OutputLN("Connection Fail, try: " + t);
t--;
Sleep(1000);
}
} while(t > 0);
return false;
}
function LeapMotion::Update(palm_pos, f0_pos, f1_pos)
{
if(NetDataReady(channel))
{
var data = "";
var recIp = "";
data = NetReceiveFrom(channel, &recIP);
Sscanf(data,"%s (%f, %f, %f)(%f, %f, %f)(%f, %f, %f)", &dataValues[0], &dataValues[1],&dataValues[2],&dataValues[3],&dataValues[4],&dataValues[5],&dataValues[6], &dataValues[7],&dataValues[8],&dataValues[9] );
newGesture = dataValues[0];
if(newGesture != oldGesture)
{
gesture = newGesture[0];
gesturePresence = true;
}
oldGesture = newGesture;
switch(side)
{
case LM_SIDE_W:
palm_pos = [-dataValues[3] / reduction_factor, dataValues[2]/ reduction_factor -20, dataValues[1]/ reduction_factor];
f0_pos = [-dataValues[6] / reduction_factor, dataValues[5]/ reduction_factor -20, dataValues[4]/ reduction_factor];
f1_pos = [-dataValues[9] / reduction_factor, dataValues[8]/ reduction_factor -20, dataValues[7]/ reduction_factor];
break;
case LM_SIDE_B:
palm_pos = [dataValues[3] / reduction_factor, dataValues[2]/ reduction_factor -20, -dataValues[1]/ reduction_factor];
f0_pos = [dataValues[6] / reduction_factor, dataValues[5]/ reduction_factor -20, -dataValues[4]/ reduction_factor];
f1_pos = [dataValues[9] / reduction_factor, dataValues[8]/ reduction_factor -20, -dataValues[7]/ reduction_factor];
break;
case LM_SIDE_M:
palm_pos = [dataValues[1] / reduction_factor, dataValues[2]/ reduction_factor -20, dataValues[3]/ reduction_factor];
f0_pos = [dataValues[4] / reduction_factor, dataValues[5]/ reduction_factor -20, dataValues[6]/ reduction_factor];
f1_pos = [dataValues[7] / reduction_factor, dataValues[8]/ reduction_factor -20, dataValues[9]/ reduction_factor];
break;
}
return true;
/*default, white side:
palm_pos = [-dataValues[3] / reduction_factor, dataValues[2]/ reduction_factor -20, dataValues[1]/ reduction_factor];
f0_pos = [-dataValues[6] / reduction_factor, dataValues[5]/ reduction_factor -20, dataValues[4]/ reduction_factor];
f1_pos = [-dataValues[9] / reduction_factor, dataValues[8]/ reduction_factor -20, dataValues[7]/ reduction_factor];
*/
}
else
return false;
}
function LeapMotion::ChangeSide(newSide)
{
side = newSide;
}
function LeapMotion::GetGesture(g)
{
if(gesturePresence)
{
g = gesture;
gesturePresence = false;
return true;
}
else
return false;
}
#endif