Added the gravity vector method for polygons (kinda blindly copied and

pasted it from the ball version, hope it works)
This commit is contained in:
Karma Riuk 2023-05-17 13:27:11 +02:00
parent 7df64d460c
commit 2bba3155da

View File

@ -1,6 +1,7 @@
#include "gravity.h" #include "gravity.h"
#include "game.h" #include "game.h"
#include "polygons.h"
#include <cmath> #include <cmath>
#include <gtk/gtk.h> #include <gtk/gtk.h>
@ -82,6 +83,19 @@ void gravity_change(double dx, double dy) {
} }
} }
vec2d gravity_vector(const polygon* p) {
if (constant_field) {
return g;
} else {
vec2d b_c = vec2d{width / 2.0, height / 2.0} - p->centroid();
double r2 = vec2d::dot(b_c, b_c);
if (r2 < g_r * g_r)
return vec2d{0, 0};
else
return g_g / r2 / sqrt(r2) * b_c;
}
}
vec2d gravity_vector(const ball* b) { vec2d gravity_vector(const ball* b) {
if (constant_field) { if (constant_field) {
return g; return g;