-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmini_project_v1.cpp
More file actions
449 lines (390 loc) · 15 KB
/
Copy pathmini_project_v1.cpp
File metadata and controls
449 lines (390 loc) · 15 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
/* what this program should accomplish:
* 1. Ask the user for input
* 2. use if, else, and while statments
* 3. use functions
* 4. use varbiles that carry values between functions
*/
/* prompt: you are the behind the service line ready
to start the rally. You need to pick a zone to serve at
and how you are going to serve. You can do a float serve
or a top spin serve and each serve moves differently based
on vertical jump and power. You goal is to land the ball in
the court and the code will give you a score telling you
if you missed, sent a free ball, forced out of system, or aced the serve. */
float get_stats(float &vertJump, float &standingReach, int &zone, float &height, float &dist, float &speed, float trig_scaler)
{
cout << "before we start lets get some messurements" << endl;
cout << "Please enter your vertical jump in inches: " << endl;
cin >> vertJump;
vertJump = vertJump * 0.0254; // convert inches to meters
cout << "enter your standing reach in inches: " << endl;
cin >> standingReach;
standingReach = standingReach * 0.0254; // convert inches to meters
//we get the initial jump and reach so they sum to the starting
//height of the ball when served
// the net height is the minimum height the ball needs 30m after the service line
int net;
while (true)
{
cout << " what net are you playing on: 1 for mens, 2 for womens, 3 for coed" << endl;
cin >> net;
if (net == 1)
{
std::cout << "you are playing on a mens net" << std::endl;
height = 2.43; //meters
break;
}
else if (net == 2)
{
std::cout << "you are playing on a womens net" << std::endl;
height = 2.24; //meters
break;
}
else if (net == 3)
{
std::cout << "you are playing on a coed net" << std::endl;
height = 2.34; //meters
break;
}
else
{
std::cout << "not a valid net type, try again" << std::endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// this clears the input buffer and ignores any invalid input until a newline is encountered
}
}
// to know if the ball will land in we need to know how far the ball can go
// because pythag it depends on the starting zone (1) and the target zone (1-6)
while (true)
{
std::cout << "Please enter the zone you want to serve to (1,6,5): " << std::endl;
std::cin >> zone;
if (zone == 1)
{
std::cout << "you are serving to zone 1" << std::endl;
dist = 67.0f; // distance in feet from service line to zone 1
trig_scaler = 0.894f; // cos(26.565 degrees) for trig calculations
break;
}
/* else if (zone == 2)
{
std::cout << "you are serving to zone 2" << std::endl;
dist = 45.5f;
break;
}
else if (zone == 3)
{
std::cout << "you are serving to zone 3" << std::endl;
dist = 42.25f;
break;
}
else if (zone == 4)
{
std::cout << "you are serving to zone 4" << std::endl;
dist = 41.0f;
break;
} */
// tbh I don't want worry about short serves because they will land in the court even if they miss the serve
// thats a whole lot of extra workj
else if (zone == 5)
{
std::cout << "you are serving to zone 5" << std::endl;
dist = 60.0f;
trig_scaler = 0.97f; // cos(14.03 degrees) for trig calculations
break;
}
else if (zone == 6)
{
std::cout << "you are serving to zone 6" << std::endl;
dist = 61.8f; // distance in feet from service line to zone 6
trig_scaler = 1.0f; // cos(0 degrees) for trig calculations
break;
}
else
{
std::cout << "not a valid zone, try again" << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
return 0;
}
float floater(float &vertJump, float &standingReach, int &zone, float &height, float &dist, float &speed, float &trig_scaler)
{
dist = (dist * trig_scaler) * 0.3048f; // in feet to meters
std::cout << "float serve selected with vertJump= " << vertJump << "m"<< ", standingReach= " << standingReach << "m" << std::endl;
std::cout << "your max touch is " << vertJump + standingReach <<"m"<< std::endl;
std::cout << ", zone=" << zone << std::endl;
std::cout << "Please enter your serve angle (0-90): " << std::endl;
int angle;
std::cin >> angle;
float O = angle * 3.14159f / 180.0f; // convert angle to radians for trig functions
std::cout << "Please enter your speed in Kph: " << std::endl;
std::cin >> speed;
float v = speed * 0.27778f; // convert speed to meters per second
float g = 9.8f; // gravity
float h = vertJump + standingReach; // starting height the ball is contacted at inches to meters
// ball + air constants, Molten V5500 volleyball
float r = 0.105f; // ball radius, meters
float m = 0.27f; // ball mass, kg
float rho = 1.225f; // air density, kg/m^3
float Cd = 0.3f; // drag coefficient for a volleyball at serve speed
float area = 3.14159f * r * r; // cross sectional area
float D = 0.5f * rho * Cd * area / m; // drag constant, combines rho/Cd/area/mass into one number
float dt = 0.0005f; // size of one Euler's method time step, seconds
float vx = v * cos(O); // x component of initial velocity
float vy = v * sin(O); // y component of initial velocity
float x = 0.0f;
float y = h;
float yAtNet = -1.0f; // height of the ball at x=30 feet or 9.14 meters, stays -1 if it never gets there
bool checkedNet = false; // have we recorded yAtNet yet?
while (y > 0.0f) // eular's method loop, stop when the ball hits the ground
{
float speedNow = sqrt(vx * vx + vy * vy); // total speed this instant
float ax = -D * speedNow * vx; // drag deceleration, x direction
float ay = -g - D * speedNow * vy; // gravity + drag deceleration, y direction
float oldVx = vx; // save this step's velocity before changing it,
float oldVy = vy; // position has to move using THIS step's speed
x = x + oldVx * dt;
y = y + oldVy * dt;
vx = oldVx + ax * dt;
vy = oldVy + ay * dt;
if (!checkedNet && x >= 9.14f) // 30 feet in meters
{
yAtNet = y;
checkedNet = true;
if (yAtNet < height) // mens net height is 2.43 meters, womens is 2.24 meters, coed is 2.34 meters
{
break; // hit the net, no point simulating further
}
}
}
std::cout << "The ball lands at x = " << x/0.3048f * trig_scaler << " feet" << std::endl;
// this is the height of the ball at 30m,
//if its above the net height then it may land in,
//if its below then it lands out
std::cout << "the height of the ball at 30 feet is " << yAtNet - height << " Meters over the net" << std::endl;
if (yAtNet >= height)
{
std::cout << "the ball is over the net" << std::endl;
if (x < dist)
{
std::cout << "900 SQUARE FEET AND YOU" << std::endl;
std::cout << "CAN'T" << std::endl;
std::cout << "HIT" << std::endl;
std::cout << "ONE" << std::endl;
std::cout << "BALL" << std::endl;
std::cout << " you missed by " << x - dist << " meters" << std::endl;
return 1;
}
else
{
std::cout << "the ball is going to the passers!" << std::endl;
return 0;
}
}
else
{
std::cout << "THER'S A NET THERE" << std::endl;
return 1;
}
}
int topper(float &vertJump, float &standingReach, int &zone, float &height, float &dist, float &speed, float &trig_scaler)
{
dist = dist * trig_scaler * 0.3048f; // in feet to meters
std::cout << "top spin serve selected with vertJump=" << vertJump <<"m"
<< ", standingReach=" << standingReach <<"m"<<std::endl;
std::cout << "your max touch is " << vertJump + standingReach <<"m"<< std::endl;
std::cout << ", zone=" << zone << std::endl;
std::cout << "Please enter your serve angle (0-90): " << std::endl;
int angle;
std::cin >> angle;
float O = angle * 3.14159f / 180.0f; // convert angle to radians for trig functions
std::cout << "Please enter your speed in Kph: " << std::endl;
std::cin >> speed;
float v = speed * 0.27778f; // convert speed to meters per second
float g = 9.8f; // gravity
// ball + air constants, Molten V5500 volleyball (same ball as the float serve)
float r = 0.105f; // ball radius, meters
float m = 0.27f; // ball mass, kg
float rho = 1.225f; // air density, kg/m^3
float Cd = 0.3f; // drag coefficient
float area = 3.14159f * r * r; // cross sectional area
float D = 0.5f * rho * Cd * area / m; // drag constant
float w = 94.25f; // spin rate, rad/s
float h = vertJump + standingReach;
float vx = v * cos(O); // x component of initial velocity
float vy = v * sin(O); // y component of initial velocity
float x = 0.0f;
float y = h;
float dt = 0.0005f; // size of one Euler's method time step, seconds
float yAtNet = -1.0f; // height of the ball at the net, stays -1 if it never gets there
bool checkedNet = false; // have we recorded y_at_30f yet?
while (y > 0.0f) // euler's method loop, stop when the ball hits the ground
{
float speedNow = sqrt(vx * vx + vy * vy); // total speed this instant
// Magnus lift coefficient from the spin parameter S = r*w/v,
// capped at 0.25 since that's where real volleyball spin data plateaus
float S = (r * w) / speedNow;
float Cl = 0.5f * S;
if (Cl > 0.25f)
{
Cl = 0.25f;
}
float M = 0.5f * rho * Cl * area / m; // magnus constant, same shape as D
float ax = -D * speedNow * vx + M * speedNow * vy; // drag + magnus, x direction
float ay = -g - D * speedNow * vy - M * speedNow * vx; // gravity + drag + magnus, y direction
float oldVx = vx; // save this step's velocity before changing it,
float oldVy = vy; // position has to move using THIS step's speed
x = x + oldVx * dt;
y = y + oldVy * dt;
vx = oldVx + ax * dt;
vy = oldVy + ay * dt;
if (!checkedNet && x >= 9.14f) // 30 feet in meters
{
yAtNet = y;
checkedNet = true;
if (yAtNet < height)
{
break; // hit the net, no point simulating further
}
}
}
if (yAtNet >= height )
{
std::cout << "the ball is over the net" << std::endl;
if (x < 60)
{
return 0; // return 0 to indicate the ball landed in
}
else
{
std::cout << "the ball goes looooooooong" << std::endl;
return 1; // return 1 to indicate the ball went long
}
}
else
{
std::cout << "THERE'S A NET THERE" << std::endl;
return 1; // return 1 to indicate the ball hit the net
}
}
void floater_Score()
{
// use rand num 1-7 for the serve and compair it to the passer to get the score
int serve = rand() % 7 + 1; // generates a random number between 1 and 7
int passer = rand() % 7 + 1; // generates a random number between 1 and 7
if (serve - passer == 0 || serve - passer == 1)
{
std::cout << "you got dimed" << std::endl;
}
else if (serve - passer == -2 || serve - passer == -1 || serve - passer == 2 || serve - passer == 3)
{
std::cout << "the serve was in system" << std::endl;
}
else if (serve - passer == -4 || serve - passer == -3 || serve - passer == 4 || serve - passer == 5)
{
std::cout << "the serve was out of system" << std::endl;
}
else if (serve - passer == -6 || serve - passer == -5 || serve - passer == 6)
{
std::cout << "ACE ACE ACE ACEEEE" << std::endl;
}
}
void topper_Score(float speed)
{
// use the speed and calculate efficentcy
int passer = rand() % 5 + 1; // generates a random number between 1 and 5
int recive = rand() % 100 + 1; // generates a random number between 1 and 100
if (passer == 1)
{
std::cout << "ACE ACE ACE ACEEEE" << std::endl;
}
else if (passer == 2)
{
if (recive < speed * 0.6f)
{
std::cout << "ACE ACE ACE ACEEEE" << std::endl;
}
else
{
std::cout << "the serve was out of system, good serve!" << std::endl;
}
}
else if (passer == 3)
{
if (recive < speed * 0.4f)
{
std::cout << "ACE ACE ACE ACEEEE" << std::endl;
}
else
{
std::cout << "the serve was out of system, good serve!" << std::endl;
}
}
else if (passer == 4)
{
if (recive < speed * 0.3f)
{
std::cout << "the serve was out of system" << std::endl;
}
else
{
std::cout << "the serve was in system" << std::endl;
}
}
else if (passer == 5)
{
std::cout << "the serve was in system" << std::endl;
}
}
int main()
{
std::cout << "Welcome to the VB serve simulator! You are in zone 1 preparing to serve." << std::endl;
float vertJump, standingReach, height;
int zone;
float dist, speed;
float trig_scaler;
get_stats(vertJump, standingReach, zone, height, dist, speed, trig_scaler);
std::cout << "Please enter the type of serve you want to do (1 for float, 2 for top spin): " << std::endl;
int serveType;
std::cin >> serveType;
if (serveType == 1)
{
std::cout << "you chose a float serve" << std::endl;
int result = floater(vertJump, standingReach, zone, height, dist, speed, trig_scaler);
if (result == 0)
{
floater_Score();
}
else
{
std::cout << "Game over" << std::endl;
return 0;
}
}
else if (serveType == 2)
{
std::cout << "you chose a top spin serve" << std::endl;
int result = topper(vertJump, standingReach, zone, height, dist, speed, trig_scaler);
if (result == 0)
{
topper_Score(speed);
}
else
{
std::cout << "Game over" << std::endl;
return 0;
}
}
else
{
std::cout << "not a valid serve type, try again" << std::endl;
return 0;
}
}