-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphylib.c
More file actions
587 lines (497 loc) · 19.6 KB
/
phylib.c
File metadata and controls
587 lines (497 loc) · 19.6 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#include "phylib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// CIS 2750 W24 Assignment 1
// Writen by: Samy Rashed
//Due date: Mon Jan 29 2024
// Function to create a new phylib_object representing a still ball.
// Parameters:
// - unsigned char number: The identification number of the still ball.
// - phylib_coord* pos: Pointer to a structure holding the initial position coordinates.
phylib_object* phylib_new_still_ball(unsigned char number, phylib_coord* pos) {
// Allocate memory for a new phylib_object
phylib_object* newObject = (phylib_object*)malloc(sizeof(phylib_object));
// Check if malloc was successful
if (newObject == NULL) {
return NULL; // Allocation failed, return NULL
}
// Set the type of the object to PHYLIB_STILL_BALL
newObject->type = PHYLIB_STILL_BALL;
// Transfer information from function parameters to the structure
newObject->obj.still_ball.number = number;
newObject->obj.still_ball.pos = *pos; // Copy the coordinates
return newObject; // Return a pointer to the new phylib_object
}
// Function to create a new phylib_object representing a rolling ball.
// Parameters:
// - unsigned char number: The identification number of the rolling ball.
// - phylib_coord* pos: Pointer to a structure holding the initial position coordinates.
// - phylib_coord* vel: Pointer to a structure holding the initial velocity components.
// - phylib_coord* acc: Pointer to a structure holding the initial acceleration components.
phylib_object* phylib_new_rolling_ball(unsigned char number, phylib_coord* pos, phylib_coord* vel, phylib_coord* acc) {
phylib_object* newObject = (phylib_object*)malloc(sizeof(phylib_object));
if (newObject == NULL) {
return NULL; //return null if no new object
}
newObject->type = PHYLIB_ROLLING_BALL;
newObject->obj.rolling_ball.number = number;
newObject->obj.rolling_ball.pos = *pos;
newObject->obj.rolling_ball.vel = *vel;
newObject->obj.rolling_ball.acc = *acc;
return newObject;
}
/**
* Function that creates new hole
* Parameters:
* - phylib_coord* pos: An (x,y) coordinate
*/
phylib_object* phylib_new_hole(phylib_coord* pos) {
phylib_object* newObject = (phylib_object*)malloc(sizeof(phylib_object));
if (newObject == NULL) {
return NULL;
}
newObject->type = PHYLIB_HOLE;
newObject->obj.hole.pos = *pos;
return newObject;
}
// Function to create a new phylib_object representing a horizontal cushion.
// Parameters:
// - double y: The y-coordinate of the horizontal cushion.
phylib_object* phylib_new_hcushion(double y) {
phylib_object* newObject = (phylib_object*)malloc(sizeof(phylib_object));
if (newObject == NULL) {
return NULL;
}
newObject->type = PHYLIB_HCUSHION;
newObject->obj.hcushion.y = y;
return newObject;
}
// Function to create a new phylib_object representing a vertical cushion.
// Parameters:
// - double x: The x-coordinate of the vertical cushion.
phylib_object* phylib_new_vcushion(double x) {
phylib_object* newObject = (phylib_object*)malloc(sizeof(phylib_object));
if (newObject == NULL) {
return NULL;
}
newObject->type = PHYLIB_VCUSHION;
newObject->obj.vcushion.x = x;
return newObject;
}
/**
* Function for creating new table
* Parameters: NONE
*/
phylib_table* phylib_new_table(void) {
// Allocate memory for a new phylib_table
phylib_table* newTable = (phylib_table*)malloc(sizeof(phylib_table));
// Check if malloc was successful
if (newTable == NULL) {
return NULL; // Allocation failed, return NULL
}
// Set the initial time
newTable->time = 0.0;
// Initialize all pointers to NULL
for (int i = 0; i < PHYLIB_MAX_OBJECTS; ++i) {
newTable->object[i] = NULL;
if (i < 10) {
//newTable->object[i] = (phylib_object*)malloc(sizeof(phylib_object));
}
// newTable->object[i] = (phylib_object*)malloc(sizeof(phylib_object));
}
// Add elements to the table in the specified order
// 1) Horizontal cushions
newTable->object[0] = phylib_new_hcushion(0.0); // at y=0.0
newTable->object[1] = phylib_new_hcushion(PHYLIB_TABLE_LENGTH); // at y=PHYLIB_TABLE_LENGTH
// 2) Vertical cushions
newTable->object[2] = phylib_new_vcushion(0.0); // at x=0.0
newTable->object[3] = phylib_new_vcushion(PHYLIB_TABLE_WIDTH); // at x=PHYLIB_TABLE_WIDTH
// 3) Holes
// Corners
newTable->object[4] = phylib_new_hole(&(phylib_coord){0.0, 0.0}); // top-left corner
newTable->object[5] = phylib_new_hole(&(phylib_coord){0.0, PHYLIB_TABLE_WIDTH}); // top-right corner
newTable->object[6] = phylib_new_hole(&(phylib_coord){0.0, PHYLIB_TABLE_LENGTH}); // bottom-left corner
newTable->object[7] = phylib_new_hole(&(phylib_coord){PHYLIB_TABLE_WIDTH, 0.0}); // bottom-right corner
// Midway between top and bottom holes
newTable->object[8] = phylib_new_hole(&(phylib_coord){PHYLIB_TABLE_WIDTH, PHYLIB_TABLE_WIDTH}); // top-middle hole
newTable->object[9] = phylib_new_hole(&(phylib_coord){PHYLIB_TABLE_WIDTH, PHYLIB_TABLE_LENGTH}); // bottom-middle hole
return newTable; // Return a pointer to the new phylib_table
}
/**
* Function for copying an object
* Parameters:
* -phylib_object** dest, double pointer of phylib_object signifying the destination
* -phylib_object** src, double pointer of phylib_object signifying the source
*/
void phylib_copy_object(phylib_object** dest, phylib_object** src) {
// Check if src points to NULL
if (*src == NULL) {
*dest = NULL;
return;
}
// Allocate memory for a new phylib_object
*dest = (phylib_object*)malloc(sizeof(phylib_object));
// Check if malloc was successful
if (*dest == NULL) {
// Allocation failed, return
return;
}
// Use memcpy to copy the entire object from src to dest
memcpy(*dest, *src, sizeof(phylib_object));
}
/**
* Function for copying a table
* Parameters:
* - phylib_table* table: pointer of type phylib_table representing the table structure for the game
*/
phylib_table* phylib_copy_table(phylib_table* table) {
// Check if table is NULL
if (table == NULL) {
return NULL;
}
// Allocate memory for a new phylib_table
phylib_table* newTable = (phylib_table*)malloc(sizeof(phylib_table));
// Check if malloc was successful
if (newTable == NULL) {
return NULL; // Allocation failed, return NULL
}
// Use memcpy to copy the entire table structure
memcpy(newTable, table, sizeof(phylib_table));
// Copy each object in the table
for (int i = 0; i < PHYLIB_MAX_OBJECTS; ++i) {
phylib_copy_object(&(newTable->object[i]), &(table->object[i]));
}
return newTable; // Return a pointer to the new phylib_table
}
/**
* Function for adding an object
* Parameters:
* - phylib_table* table: pointer of type phylib_table representing the table structure for the game
* - phylib_object* object: pointer of type phylib_table representing any object (rolling/still ball, hole, etc)
*/
void phylib_add_object(phylib_table* table, phylib_object* object) {
for (int i = 0; i < PHYLIB_MAX_OBJECTS; ++i) {
if (table->object[i] == NULL) {
table->object[i] = (phylib_object*)malloc(sizeof(phylib_object));
table->object[i] = object;
break;
}
}
}
/**
* Function to free up memory taken by the table
* Parameters:
* - phylib_table* table: pointer of type phylib_table representing the table structure for the game
*/
void phylib_free_table(phylib_table* table) {
for (int i = 0; i < PHYLIB_MAX_OBJECTS; ++i) {
if (table->object[i] != NULL) {
free(table->object[i]);
table->object[i] = NULL;
}
}
free(table);
}
/**
* Function to subtract two coordinates from each other
* Parameters:
* phylib_coord c1, c2: Two (x,y) coordinates
*/
phylib_coord phylib_sub(phylib_coord c1, phylib_coord c2) {
phylib_coord result;
result.x = c1.x - c2.x;
result.y = c1.y - c2.y;
return result;
}
/**
* Returns the length of a certain coordinate by pythagorean theorem
* Parameters:
* - phylib_coord c: A pair of (x,y) coordinates.
*/
double phylib_length(phylib_coord c)
{
double c_squared = (c.x*c.x) + (c.y*c.y);
double coord = sqrt(c_squared);
return coord;
}
/**
* Returns dot product of two coordinates
* Parameters:
* phylib_coord a, b: Two (x,y) coordinates
*/
double phylib_dot_product(phylib_coord a, phylib_coord b) {
double x = a.x * b.x;
double y = a.y * b.y;
double sum = x + y;
return sum;
}
/**
* Calculates distance between two objects
* Parameters:
* phylib_object* obj1, obj2: Two objects (rolling/still ball, h/vcushion, etc)
*/
double phylib_distance(phylib_object* obj1, phylib_object* obj2) {
if (obj1->type != PHYLIB_ROLLING_BALL) {
return -1.0;
}
double distance;
double x_hole_dist = obj1->obj.rolling_ball.pos.x - obj2->obj.hole.pos.x;
double y_hole_dist = obj1->obj.rolling_ball.pos.y - obj2->obj.hole.pos.y;
double x_dist = 0.0;
double y_dist = 0.0;
switch (obj2->type) {
case (PHYLIB_STILL_BALL):;
x_dist = obj1->obj.rolling_ball.pos.x - obj2->obj.still_ball.pos.x;
y_dist = obj1->obj.rolling_ball.pos.y - obj2->obj.still_ball.pos.y;
distance = sqrt(x_dist*x_dist + y_dist*y_dist) - PHYLIB_BALL_DIAMETER;
break;
case (PHYLIB_ROLLING_BALL):;
x_dist = obj1->obj.rolling_ball.pos.x - obj2->obj.rolling_ball.pos.x;
y_dist = obj1->obj.rolling_ball.pos.y - obj2->obj.rolling_ball.pos.y;
distance = sqrt(x_dist*x_dist + y_dist*y_dist) - PHYLIB_BALL_DIAMETER;
break;
case PHYLIB_HOLE:
// Compute distance between ball center and hole and subtract hole radius
distance = sqrt(x_hole_dist*x_hole_dist + y_hole_dist*y_hole_dist) - PHYLIB_HOLE_RADIUS;
break;
case PHYLIB_HCUSHION:
// Compute distance between ball center and horizontal cushion and subtract ball radius
distance = fabs(obj1->obj.rolling_ball.pos.y - obj2->obj.hcushion.y) - PHYLIB_BALL_RADIUS;
break;
case PHYLIB_VCUSHION:
// Compute distance between ball center and vertical cushion and subtract ball radius
distance = fabs(obj1->obj.rolling_ball.pos.x - obj2->obj.vcushion.x) - PHYLIB_BALL_RADIUS;
break;
default:
// Invalid obj2 type, return -1.0
return -1.0;
}
return distance;
}
/**
* Apply roll functionality to at least one rolling ball and another object
* Parameters:
* - phylib_object* new, old: At least one rolling ball and another object
* - double time: Elapsed time
*/
void phylib_roll(phylib_object *new, phylib_object *old, double time)
{
if (new->type != PHYLIB_ROLLING_BALL && old->type != PHYLIB_ROLLING_BALL)
{
return;
}
double oldPos_x = old->obj.rolling_ball.pos.x;
double oldPos_y = old->obj.rolling_ball.pos.y;
double oldVel_x = old->obj.rolling_ball.vel.x;
double oldVel_y = old->obj.rolling_ball.vel.y;
double oldAcc_x = old->obj.rolling_ball.acc.x;
double oldAcc_y = old->obj.rolling_ball.acc.y;
double newPos_x = oldPos_x + (oldVel_x * time) + (0.5 * oldAcc_x * (time * time));
double newPos_y = oldPos_y + (oldVel_y * time) + (0.5 * oldAcc_y * (time * time));
double newVel_x = oldVel_x + (oldAcc_x * time);
double newVel_y = oldVel_y + (oldAcc_y * time);
// Check if either velocity changes sign
if ((oldVel_x * newVel_x) < 0)
{
newVel_x = 0.0;
new->obj.rolling_ball.acc.x = 0.0;
}
if ((oldVel_y * newVel_y) < 0)
{
newVel_y = 0.0;
new->obj.rolling_ball.acc.y = 0.0;
}
new->obj.rolling_ball.pos.x = newPos_x;
new->obj.rolling_ball.pos.y = newPos_y;
new->obj.rolling_ball.vel.x = newVel_x;
new->obj.rolling_ball.vel.y = newVel_y;
}
/**
* Function to check if an object has stopped
* Parameters:
* - phylib_object* object: An object (rolling/still ball, hole, etc)
*/
unsigned char phylib_stopped(phylib_object *object ) {
double vel_length = sqrt((object->obj.rolling_ball.vel.x*object->obj.rolling_ball.vel.x) + (object->obj.rolling_ball.vel.y*object->obj.rolling_ball.vel.y));
if (vel_length < PHYLIB_VEL_EPSILON)
{
object->type = PHYLIB_STILL_BALL;
object->obj.rolling_ball.pos.x = object->obj.still_ball.pos.x;
object->obj.rolling_ball.pos.y = object->obj.still_ball.pos.y;
return 1;
}
return 0;
}
/**
* Function to calculate bounce when two objects collide
* Parameters:
* - phylib_object **a, phylib_object **b: Two double pointer objects of type phylib_object
*/
void phylib_bounce(phylib_object **a, phylib_object **b) {
if (*a == NULL || *b == NULL) {
// Invalid pointers, do nothing
return;
}
phylib_object* obj_a = *a;
phylib_object* obj_b = *b;
switch (obj_b->type) {
case PHYLIB_HCUSHION:
obj_a->obj.rolling_ball.vel.y = -obj_a->obj.rolling_ball.vel.y;
obj_a->obj.rolling_ball.acc.y = -obj_a->obj.rolling_ball.acc.y;
break;
case PHYLIB_VCUSHION:
obj_a->obj.rolling_ball.vel.x = -obj_a->obj.rolling_ball.vel.x;
obj_a->obj.rolling_ball.acc.x = -obj_a->obj.rolling_ball.acc.x;
break;
case PHYLIB_HOLE:
free(obj_a);
*a = NULL;
break;
case PHYLIB_STILL_BALL:;
double temp_x = obj_b->obj.still_ball.pos.x;
double temp_y = obj_b->obj.still_ball.pos.y;
unsigned char temp_num = obj_b->obj.still_ball.number;
obj_b->type = PHYLIB_ROLLING_BALL;
obj_b->obj.rolling_ball.pos.x = temp_x;
obj_b->obj.rolling_ball.pos.y = temp_y;
obj_b->obj.rolling_ball.number = temp_num;
obj_b->obj.rolling_ball.vel.x = 0;
obj_b->obj.rolling_ball.vel.y = 0;
obj_b->obj.rolling_ball.acc.x = 0;
obj_b->obj.rolling_ball.acc.y = 0;
case (PHYLIB_ROLLING_BALL):;
// Compute position of a with respect to b. Subtract b from a.
phylib_coord r_ab = phylib_sub(obj_a->obj.rolling_ball.pos, obj_b->obj.rolling_ball.pos);
// compute relative velocity of a with respect to b. subtract vel b from a.
phylib_coord v_rel = phylib_sub(obj_a->obj.rolling_ball.vel, obj_b->obj.rolling_ball.vel);
// divide x and y of r_ab by length r_ab,
phylib_coord n;
n.x = (r_ab.x / phylib_length(r_ab));
n.y = (r_ab.y / phylib_length(r_ab));
// ratio of relative v_rel by calling dot product of v_rel
double v_rel_n = phylib_dot_product(v_rel, n);
// update x & y velocity of ball a. subtract v_rel_n * n.
obj_a->obj.rolling_ball.vel.x -= (v_rel_n * n.x);
obj_a->obj.rolling_ball.vel.y -= (v_rel_n * n.y);
// update x & y velocity of ball b. add v_rel_n * n.
obj_b->obj.rolling_ball.vel.x += (v_rel_n * n.x);
obj_b->obj.rolling_ball.vel.y += (v_rel_n * n.y);
// compute speed of a and b.
double speedA = phylib_length(obj_a->obj.rolling_ball.vel);
double speedB = phylib_length(obj_b->obj.rolling_ball.vel);
// if speed greater than eplison, set acceleration to negative velocity / speed * drag
if (speedA > PHYLIB_VEL_EPSILON)
{
obj_a->obj.rolling_ball.acc.x = (-obj_a->obj.rolling_ball.vel.x) / speedA * PHYLIB_DRAG;
obj_a->obj.rolling_ball.acc.y = (-obj_a->obj.rolling_ball.vel.y) / speedA * PHYLIB_DRAG;
}
if (speedB > PHYLIB_VEL_EPSILON)
{
obj_b->obj.rolling_ball.acc.x = (-obj_b->obj.rolling_ball.vel.x) / speedB * PHYLIB_DRAG;
obj_b->obj.rolling_ball.acc.y = (-obj_b->obj.rolling_ball.vel.y) / speedB * PHYLIB_DRAG;
}
break;
}
}
/**
* Calculates number of rolling balls in table
* phylib_table *t: Table for the game
*/
unsigned char phylib_rolling( phylib_table *t ) {
unsigned char rollingCount = 0;
// Iterate through the object array
for (int i = 0; i < PHYLIB_MAX_OBJECTS; ++i) {
// Check if the object exists and is of type ROLLING_BALL
if (t->object[i] && t->object[i]->type == PHYLIB_ROLLING_BALL) {
rollingCount++;
}
}
return rollingCount;
}
/**
* Function that returns a segment of the pool shot
* Parameters:
* - phylib_table* table: Table structure for the game
*/
phylib_table* phylib_segment(phylib_table* table) {
if (table == NULL || !phylib_rolling(table)) {
return NULL; // Return NULL if no table or no rolling balls
}
double time = PHYLIB_SIM_RATE;
phylib_table* newTable = phylib_copy_table(table);
if (newTable != NULL) {
int shouldExit = 0; // Flag to indicate whether to exit the loop
while (time < PHYLIB_MAX_TIME && !shouldExit) {
for (int i = 0; i < PHYLIB_MAX_OBJECTS; i++) {
if (newTable->object[i] != NULL && newTable->object[i]->type == PHYLIB_ROLLING_BALL) {
phylib_roll(newTable->object[i], newTable->object[i], PHYLIB_SIM_RATE);
if (phylib_stopped(newTable->object[i])) {
shouldExit = 1; // Set the flag to exit the loop
break; // Exit the inner loop
}
for (int j = 0; j < PHYLIB_MAX_OBJECTS; j++) {
if (newTable->object[j] != NULL && j != i) {
if (phylib_distance(newTable->object[i], newTable->object[j]) < 0.0) {
phylib_bounce(&(newTable->object[i]), &(newTable->object[j]));
shouldExit = 1; // Set the flag to exit the loop
break; // Exit the inner loop
}
}
}
}
}
time += PHYLIB_SIM_RATE;
}
}
newTable->time += time;
return newTable;
}
char *phylib_object_string(phylib_object *object)
{
static char string[80];
if (object == NULL)
{
snprintf(string, 80, "NULL;");
return string;
}
switch (object->type)
{
case PHYLIB_STILL_BALL:
snprintf(string, 80,
"STILL_BALL (%d,%6.1lf,%6.1lf)",
object->obj.still_ball.number,
object->obj.still_ball.pos.x,
object->obj.still_ball.pos.y);
break;
case PHYLIB_ROLLING_BALL:
snprintf(string, 80,
"ROLLING_BALL (%d,%6.1lf,%6.1lf,%6.1lf,%6.1lf,%6.1lf,%6.1lf)",
object->obj.rolling_ball.number,
object->obj.rolling_ball.pos.x,
object->obj.rolling_ball.pos.y,
object->obj.rolling_ball.vel.x,
object->obj.rolling_ball.vel.y,
object->obj.rolling_ball.acc.x,
object->obj.rolling_ball.acc.y);
break;
case PHYLIB_HOLE:
snprintf(string, 80,
"HOLE (%6.1lf,%6.1lf)",
object->obj.hole.pos.x,
object->obj.hole.pos.y);
break;
case PHYLIB_HCUSHION:
snprintf(string, 80,
"HCUSHION (%6.1lf)",
object->obj.hcushion.y);
break;
case PHYLIB_VCUSHION:
snprintf(string, 80,
"VCUSHION (%6.1lf)",
object->obj.vcushion.x);
break;
}
return string;
}