2023-11-10 22:27:20 +00:00
|
|
|
#include "math.h"
|
|
|
|
#include "object.h"
|
|
|
|
#include <cglm/cglm.h>
|
|
|
|
|
|
|
|
float frand48(void) {
|
|
|
|
float number = (float) rand() / (float) (RAND_MAX + 1.0);
|
|
|
|
float side = rand() % 2;
|
|
|
|
if (side == 0) {
|
|
|
|
number = -number;
|
|
|
|
}
|
|
|
|
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
|
|
|
void calculate_gravity(struct object *src, struct object *target, vec3 force) {
|
2023-11-11 18:53:11 +00:00
|
|
|
vec4 v4distance;
|
|
|
|
glm_vec4_sub(target->position, src->position, v4distance);
|
2023-11-10 22:27:20 +00:00
|
|
|
|
2023-11-11 18:53:11 +00:00
|
|
|
vec3 v3distance;
|
|
|
|
glm_vec3(v4distance, v3distance);
|
2023-11-10 22:27:20 +00:00
|
|
|
|
2023-11-11 18:53:11 +00:00
|
|
|
float distance_xy = sqrt((v3distance[0] * v3distance[0]) + (v3distance[1] * v3distance[1]));
|
|
|
|
float distance_xyz = sqrt((distance_xy * distance_xy) + (v3distance[2] * v3distance[2]));
|
2023-11-10 22:27:20 +00:00
|
|
|
float force_scale = 4.0f;
|
|
|
|
|
|
|
|
float g = 6.67f * 1e-11f;
|
|
|
|
float top = g * src->mass * target->mass;
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2023-11-11 18:53:11 +00:00
|
|
|
v3distance[i] = (v3distance[i] * v3distance[i] * v3distance[i]);
|
2023-11-10 22:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2023-11-11 18:53:11 +00:00
|
|
|
if (v3distance[i] == 0) {
|
2023-11-10 22:27:20 +00:00
|
|
|
force[i] = 0.0f;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
force[i] = (top / (distance_xyz / (target->position[i] - src->position[i]))) * force_scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|