-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics2d.c
More file actions
134 lines (117 loc) · 4.46 KB
/
Copy pathphysics2d.c
File metadata and controls
134 lines (117 loc) · 4.46 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
#include "headers/physics2d.h"
float *collideRect(Coll rect1, Coll rect2) {
int A = fabsf(rect1.leftEdge - (rect2.rightEdge));
int B = fabsf(rect2.leftEdge - rect1.rightEdge);
int C = fabsf(rect1.top - (rect2.bottom));
int D = fabsf(rect2.top - rect1.bottom);
float toReturn[4] = {A, B, C, D};
float min = A;
if ((rect1.leftEdge < rect2.rightEdge) && (rect1.rightEdge > rect2.leftEdge) && (rect1.top < rect2.bottom) && (rect1.bottom > rect2.top)) {
// collision detected!
printf("Collision!\n");
for (int i = 0; i < 4; i++) {
if (toReturn[i] < min) {
min = toReturn[i];
}
}
for (int j = 0; j < 4; j++) {
if (toReturn[j] != min) {
toReturn[j] = 0;
}
else {
toReturn[j] = -toReturn[j];
}
}
// printf("min: %f\n", min);
printf("A: %f\n", toReturn[0]);
printf("B: %f\n", toReturn[1]);
printf("C: %f\n", toReturn[2]);
printf("D: %f\n", toReturn[3]);
}
else {
// printf("Not colliding\n");
for (int i = 0; i < 4; i++) {
toReturn[i] = 0;
}
}
// if rect1 and rect2 are both static, there's no need to check collisions between them. No adjustment for either rects
// if rect1 is dynamic and rect2 is static, rect1 is adjusted (simpleStop).
float *p = toReturn;
return p;
}
int changePlatforms(Agent *agent, Platform platforms[], int current_platform, bool *noMorePlat, int lenPlatforms) {
bool incremented = false;
// printf("lenPlatforms: %d\n", lenPlatforms);
int index = current_platform;
for (int i =0; i <= index; i++) {
if (index >= lenPlatforms+1) {
printf("You fell off!\n");
*noMorePlat = true;
index -= 1;
}
printf("hello\n");
// printf("agent->y: %f, *current_platform: %d\n", agent->y, *current_platform);
// printf("agent->x: %f\n", agent->x);
// bump(agent, &platforms[*current_platform]);
if (onLedge(agent, &platforms[index]) == 0) {
// printf("remained on platform %d\n", *current_platform);
incremented = false;
break;
}
else if (onLedge(agent, &platforms[index]) == 1) {
if (incremented == false && index <= lenPlatforms) {
// printf("*current_platform: %d\n", *current_platform);
index += 1;
incremented = true;
}
}
// printf("agent->y: %f, *current_platform: %d\n", agent->y, *current_platform);
if (index -1 >= 0) {
// printf("agent->y: %f, *current_platform: %d\n", agent->y, *current_platform);
if (agent->coll.bottom <= platforms[index-1].coll.top) {
// jumped at the level of a platform above
index -= 1;
}
}
}
return index;
}
void bump(Agent *agent, Platform *platform) {
if ( (agent->coll.leftEdge <= 300) ) {
// if ((platform->coll.top - agent->coll.top) < agent->sprite_h && (agent->coll.leftEdge <= platform->coll.rightEdge)) {
// if (agent->coll.leftEdge <= platform->coll.rightEdge) {
// agent->x = platform->coll.rightEdge;
agent->x = 300;
// }
}
else if ((agent->coll.bottom - platform->coll.bottom) < agent->sprite_h && (agent->coll.rightEdge >= platform->coll.leftEdge)) {
}
}
int onLedge(Agent *agent, Platform *platform) {
int result;
if ( agent->coll.bottom >= platform->coll.top) {
// above/on the current platform
if (agent->coll.rightEdge >= platform->coll.leftEdge && agent->coll.leftEdge <= platform->coll.rightEdge) {
// remained on the current platform
agent->y = platform->coll.top - agent->sprite_h;
agent->jumpAgain = true;
agent->coll.bottom = platform->coll.top;
agent->coll.top = agent->y;
agent->dy = 0;
agent->falling = false;
result = 0;
}
else {
// fell off the current platform
agent->falling = true;
result = 1;
}
}
return result;
}
void gravity(Agent *agent) {
agent->dy += 0.5;
agent->y += agent->dy;
agent->coll.bottom += agent->dy;
agent->coll.top += agent->dy;
}