-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.c
More file actions
56 lines (48 loc) · 1.91 KB
/
simulation.c
File metadata and controls
56 lines (48 loc) · 1.91 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
#define STB_DS_IMPLEMENTATION
#include "stb_ds.h"
#include "simulation.h"
#include "geometry.h"
point* run_simulation(const simulation_args args, point *bounces) {
// the parsing probably should be done in main.c and the values shouldn't be copied to variables
point a = args.a;
long double shot_power = args.shot_power;
auto slope = args.vector.y / args.vector.x;
point out;
bool positive[2] = {args.vector.y > 0, args.vector.x > 0};
arrpush(bounces, a);
while (shot_power > 0) {
if (check_x_bounce(slope, args.table_width, args.table_height, positive[0], a, &out)) {
const auto segment_length = calculate_segment_length(a, out);
if (segment_length > shot_power) {
// because if the vector's x is 0, it's not a linear function, normal line calculations don't work
// properly for it
if (args.vector.x != 0.0) {
const auto p = calculate_segment_end(slope, a, shot_power, positive[1], x);
arrpush(bounces, p);
} else {
const point p = { out.x, positive[0] ? a.y + shot_power : a.y - shot_power };
arrpush(bounces, p);
}
break;
}
positive[0] = !positive[0];
}
if (check_y_bounce(slope, args.table_width, args.table_height, positive[1], a, &out)) {
if (calculate_segment_length(a, out) > shot_power) {
const auto p = calculate_segment_end(slope, a, shot_power, positive[1], x);
arrpush(bounces, p);
break;
}
positive[1] = !positive[1];
}
shot_power -= calculate_segment_length(a, out);
a = out;
arrpush(bounces, a);
shot_power *= ENERGY_LOSS;
slope *= -1;
}
return bounces;
}
void cleanup(point *bounces) {
arrfree(bounces);
}