-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysics.cpp
256 lines (213 loc) · 7.41 KB
/
physics.cpp
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
#include "physics.h"
phys::phys(int x, int y)
{
area_x = x;
area_y = y;
list_len = 0;
list_head = NULL;
list_curr = NULL;
}
int phys::add_object(phys_obj *obj)
{
obj_list *list = new obj_list;
list->id = list_len;
list->obj = obj;
list->next = NULL;
// First item
if (list_head == NULL) {
list_head = list;
list_curr = list;
} else {
// Append to list
list_curr->next = list;
list_curr = list;
}
return ++list_len;
}
void phys::advance()
{
obj_list *list = NULL;
phys_obj *obj = NULL;
timespec now;
uint64_t timediff;
int iterations;
bool run_loop = true;
clock_gettime(CLOCK_MONOTONIC, &now);
while (run_loop) {
run_loop = false;
list = list_head;
while (list != NULL) {
// Get object
obj = list->obj;
if (obj->active) {
// Check if object is colliding with another
check_collide(obj, list->id);
}
// Get next
list = list->next;
}
list = list_head;
while (list != NULL) {
// Get object
obj = list->obj;
if (obj->active) {
if (obj->move_x_every == 0) {
iterations = 0;
} else if (obj->move_x_last.tv_sec == 0) {
iterations = 1;
} else {
timediff = ((now.tv_sec - obj->move_x_last.tv_sec) * 1000000000) + (now.tv_nsec - obj->move_x_last.tv_nsec);
iterations = timediff / obj->move_x_every;
}
if (iterations > 0) {
obj->pos_x += obj->step_x;
if (iterations > 1) {
run_loop = true;
obj->move_x_last.tv_nsec += obj->move_x_every;
if (obj->move_x_last.tv_nsec > 1000000000) {
obj->move_x_last.tv_sec++;
obj->move_x_last.tv_nsec -= 1000000000;
}
} else {
obj->move_x_last = now;
}
}
if (obj->move_y_every == 0) {
iterations = 0;
} else if (obj->move_y_last.tv_sec == 0) {
iterations = 1;
} else {
timediff = ((now.tv_sec - obj->move_y_last.tv_sec) * 1000000000) + (now.tv_nsec - obj->move_y_last.tv_nsec);
iterations = timediff / obj->move_y_every;
}
if (iterations > 0) {
obj->pos_y += obj->step_y;
if (iterations > 1) {
run_loop = true;
obj->move_y_last.tv_nsec += obj->move_y_every;
if (obj->move_y_last.tv_nsec > 1000000000) {
obj->move_y_last.tv_sec++;
obj->move_y_last.tv_nsec -= 1000000000;
}
} else {
obj->move_y_last = now;
}
}
}
// Get next
list = list->next;
}
}
}
void phys::reset_timings()
{
obj_list *list = list_head;
timespec inittime {0, 0};
while (list != NULL) {
list->obj->move_x_last = inittime;
list->obj->move_y_last = inittime;
// Get next
list = list->next;
}
}
void phys::check_collide(phys_obj *obj, int id)
{
obj_list *list = NULL;
phys_obj *obj2 = NULL;
int x1;
int x2;
int y1;
int y2;
int diff_x;
int diff_y;
list = list_head;
// Check collision with other objects
while (list != NULL) {
if (list->id != id && list->obj->active) {
obj2 = list->obj;
// Left side
x1 = obj2->pos_x-obj->size_x;
// Right side
x2 = obj2->pos_x+obj2->size_x;
// Top side
y1 = obj2->pos_y-obj->size_y;
// Bottom side
y2 = obj2->pos_y+obj2->size_y;
if (obj->pos_x >= x1 && obj->pos_x <= x2 && obj->pos_y >= y1 && obj->pos_y <= y2) {
if (obj->collided != obj2) {
if (obj->pos_x-x1 > x2-obj->pos_x) {
// Collided on right side
diff_x = x2-obj->pos_x;
} else {
// Collided on left side
diff_x = obj->pos_x-x1;
}
if (obj->pos_y-y1 > y2-obj->pos_y) {
// Collided on bottom side
diff_y = y2-obj->pos_y;
} else {
// Collided on top side
diff_y = obj->pos_y-y1;
}
if (diff_y > diff_x) {
if (obj->callback != NULL) { obj->callback(obj, obj2, 1, area_x, area_y); }
if (obj->bounce > 0) {
if ((obj->step_x > 0 && obj->pos_x < obj2->pos_x) || (obj->step_x < 0 && obj->pos_x > obj2->pos_x)) {
obj->step_x = obj->step_x*-1;
}
} else if (obj->bounce == 0) {
obj->step_x = 0;
obj->step_y = 0;
}
} else {
if (obj->callback != NULL) { obj->callback(obj, obj2, 2, area_x, area_y); }
if (obj->bounce > 0) {
if ((obj->step_y > 0 && obj->pos_y < obj2->pos_y) || (obj->step_y < 0 && obj->pos_y > obj2->pos_y)) {
obj->step_y = obj->step_y*-1;
}
} else if (obj->bounce == 0) {
obj->step_x = 0;
obj->step_y = 0;
}
}
obj->collided = obj2;
}
} else if (obj->collided == obj2) {
obj->collided = NULL;
}
}
list = list->next;
}
// Check collision with edges
if ((obj->pos_x >= area_x-obj->size_x && obj->step_x > 0) || (obj->pos_x <= 0 && obj->step_x < 0)) {
if (obj->callback != NULL) { obj->callback(obj, NULL, 1, area_x, area_y); }
if (obj->bounce > 0) {
obj->step_x = obj->step_x*-1;
} else if (obj->bounce == 0 && (obj->pos_x >= area_x-obj->size_x && obj->step_x > 0 || obj->pos_x <= 0 && obj->step_x < 0)) {
obj->step_x = 0;
obj->step_y = 0;
}
}
if ((obj->pos_y >= area_y-obj->size_y && obj->step_y > 0) || (obj->pos_y <= 0 && obj->step_y < 0)) {
if (obj->callback != NULL) { obj->callback(obj, NULL, 2, area_x, area_y); }
if (obj->bounce > 0) {
obj->step_y = obj->step_y*-1;
} else if (obj->bounce == 0 && (obj->pos_y >= area_y-obj->size_y && obj->step_y > 0 || obj->pos_y <= 0 && obj->step_y < 0)) {
obj->step_x = 0;
obj->step_y = 0;
}
}
}
phys::~phys()
{
if (list_head != NULL) {
obj_list *list = NULL;
obj_list *prev = NULL;
list = list_head;
while (list != NULL) {
prev = list;
list = list->next;
delete prev;
}
}
}