Repository navigation
Expand file tree
/
Copy pathHandCursor.cpp
More file actions
283 lines (232 loc) · 4.96 KB
/
Copy pathHandCursor.cpp
File metadata and controls
283 lines (232 loc) · 4.96 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <cmath>
#include "HandCursor.h"
#include "Circle.h"
#include "config.h"
HandCursor::HandCursor(Circle* c)
{
circ = c;
circ->On();
yvel = 0;
xvel = 0;
xpos = 0;
ypos = 0;
x0 = .5;
y0 = .5;
xgain = 1;
ygain = 1;
hitMargin = 1;
rotMat[0] = 1;
rotMat[1] = 0;
rotMat[2] = 0;
rotMat[3] = 1;
clamp = 0;
for (int i = 0; i < 5; i++)
{
xhist[i] = 0;
yhist[i] = 0;
xhistScr[i] = 0;
yhistScr[i] = 0;
}
}
void HandCursor::Draw()
{
circ->Draw();
}
GLint HandCursor::drawState()
{
return circ->drawState();
}
void HandCursor::UpdatePos(GLfloat x, GLfloat y)
{
GLfloat xScr, yScr;
xpos = x;
ypos = y;
for (int i = (NCHIST - 1); i > 0; i--)
{
xhist[i] = xhist[i - 1];
yhist[i] = yhist[i - 1];
}
xhist[0] = x;
yhist[0] = y;
//---- Estimate velocity as slope of last 5 observations ----
// get mean
GLfloat xmean = 0;
GLfloat ymean = 0;
for (int i = 0; i < NCHIST; i++)
{
xmean += xhist[i];
ymean += yhist[i];
}
xmean = xmean/NCHIST;
ymean = ymean/NCHIST;
// compute xvel as slope of data over last few 5 samples
xvel = (-2*xhist[4] -1*xhist[3] + 1*xhist[1] + 2*xhist[0])*SAMPRATE/10;
yvel = (-2*yhist[4] -1*yhist[3] + 1*yhist[1] + 2*yhist[0])*SAMPRATE/10;
if(clamp)
{
GLfloat r = sqrtf((x - x0)*(x - x0) + (y - y0)*(y - y0));
if(1)
{
xScr = r*sin(thetaClamp) + x0;
yScr = r*cos(thetaClamp) + y0;
}
else
{
xScr = -r*sin(thetaClamp) + x0;
yScr = -r*cos(thetaClamp) + y0;
}
}
else
{
// apply rotation
xScr = rotMat[0]*(x - x0) + rotMat[1]*(y - y0) + x0;
yScr = rotMat[2]*(x - x0) + rotMat[3]*(y - y0) + y0;
// apply gain
xScr = xgain*(xScr - x0) + x0;
yScr = ygain*(yScr - y0) + y0;
}
for (int i = (NCHIST - 1); i > 0; i--)
{
xhistScr[i] = xhistScr[i - 1];
yhistScr[i] = yhistScr[i - 1];
}
xhistScr[0] = xScr;
yhistScr[0] = yScr;
circ->SetPos(xScr, yScr);
}
GLfloat HandCursor::GetTrueX()
{
return xpos;
}
GLfloat HandCursor::GetTrueY()
{
return ypos;
}
GLfloat HandCursor::GetLastX()
{
return xhist[1]; //xhist[0] is the current sample
}
GLfloat HandCursor::GetLastY()
{
return yhist[1]; //yhist[0] is the current sample
}
void HandCursor::SetRotation(GLfloat theta)
{
rotMat[0] = cos(theta*PI/180);
rotMat[1] = sin(theta*PI/180);
rotMat[2] = -sin(theta*PI/180);
rotMat[3] = cos(theta*PI/180);
}
void HandCursor::SetOrigin(GLfloat x, GLfloat y)
{
x0 = x;
y0 = y;
}
GLfloat HandCursor::GetXVel()
{
return xvel;
}
GLfloat HandCursor::GetYVel()
{
return yvel;
}
GLfloat HandCursor::Distance(GLfloat x, GLfloat y)
{
return sqrtf(powf(circ->GetX() - x, 2.0f) + powf(circ->GetY() - y, 2.0f));
}
GLfloat HandCursor::Distance(Circle* c)
{
return sqrtf(powf(circ->GetX() - c->GetX(), 2.0f) + powf(circ->GetY() - c->GetY(),2.0f));
}
GLfloat HandCursor::Distance(Object2D* obj)
{
return sqrtf(powf(circ->GetX() - obj->GetX(), 2.0f) + powf(circ->GetY() - obj->GetY(),2.0f));
}
void HandCursor::Null()
{
rotMat[0] = 1;
rotMat[1] = 0;
rotMat[2] = 0;
rotMat[3] = 1;
}
void HandCursor::SetClampC(Circle* c1, Circle* c2)
{
GLfloat th = atan2(c2->GetX()-c1->GetX(), c2->GetY() - c1->GetY());
SetClamp(th*PI/180);
}
void HandCursor::SetClamp(GLfloat th)
{
clamp = 1;
thetaClamp = th*180/PI; // clamp angle
}
void HandCursor::ClampOff()
{
clamp = 0;
}
void HandCursor::On()
{
circ->On();
}
void HandCursor::Off()
{
circ->Off();
}
GLfloat HandCursor::GetX()
{
return circ->GetX();
}
GLfloat HandCursor::GetY()
{
return circ->GetY();
}
GLfloat HandCursor::GetVel()
{
return sqrtf(xvel*xvel + yvel*yvel);
}
GLfloat HandCursor::GetHitMargin()
{
return hitMargin;
}
void HandCursor::SetHitMargin(GLfloat m)
{
hitMargin = m;
}
bool HandCursor::HitTarget(Circle* targ)
{
return HitTarget(targ->GetX(), targ->GetY(), hitMargin*targ->GetRadius());
}
// check if the cursor path ever crossed through the target
bool HandCursor::HitTarget(GLfloat x, GLfloat y, GLfloat rad)
{
if(powf(xhistScr[0]-x,2.0f)+powf(yhistScr[0]-y,2.0f) < rad*rad || powf(xhistScr[1]-x,2.0f) + powf(yhistScr[1]-y,2.0f) < rad*rad)
return 1; // if current or previous sample is in circle, then return HIT
else
{
// otherwise, need to check if cursor path crossed the circle (in case samples skipped over target)
// solve simultaneous equations of circle and line. Check if either solution is between consecutive samples
GLfloat A = powf(xhistScr[0] - xhistScr[1],2.0f) + powf(yhistScr[0] - yhistScr[1],2.0f);
GLfloat B = 2*((xhistScr[0] - xhistScr[1])*(xhistScr[1] - x) + (yhistScr[0] - yhistScr[1])*(yhistScr[1] - y));
GLfloat C = powf(xhistScr[1] - x,2.0f) + powf(yhistScr[1] - y,2.0f) - rad*rad;
if(B*B - 4*A*C > 0) // if real solution exists, infinite line intersects target
{
GLfloat t1 = (-B + sqrtf(B*B - 4*A*C))/(2*A);
if(t1 < 1 && t1 > 0) // line intersects target in between the two points
{
return 1;
}
else
return 0;
}
else
return 0;
}
}
void HandCursor::SetGain(GLfloat xg, GLfloat yg)
{
xgain = xg;
ygain = yg;
}
void HandCursor::SetColor(GLfloat clr[])
{
circ->SetColor(clr);
}