2021-12-11 21:32:48 +01:00
|
|
|
#include <math.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include "gravity.h"
|
|
|
|
#include "game.h"
|
|
|
|
|
2022-06-01 11:09:35 +02:00
|
|
|
static vec2d g{.x = 0, .y = 20};
|
2021-12-12 00:36:07 +01:00
|
|
|
|
|
|
|
static double g_r = 50;
|
|
|
|
static double g_g = 10000000;
|
|
|
|
|
|
|
|
static int constant_field = 1;
|
2021-12-11 21:32:48 +01:00
|
|
|
|
|
|
|
static int gravity_vector_countdown = 0;
|
|
|
|
static int gravity_vector_init = 300;
|
|
|
|
|
2021-12-12 00:36:07 +01:00
|
|
|
void gravity_constant_field (double x, double y) {
|
|
|
|
constant_field = 1;
|
2022-06-01 11:09:35 +02:00
|
|
|
g.x = x;
|
|
|
|
g.y = y;
|
2021-12-12 00:36:07 +01:00
|
|
|
}
|
2021-12-12 11:54:43 +01:00
|
|
|
|
2021-12-12 00:36:07 +01:00
|
|
|
void gravity_newton_field (double r, double g) {
|
|
|
|
constant_field = 0;
|
|
|
|
g_r = r;
|
|
|
|
g_g = g;
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
void gravity_draw (cairo_t * cr) {
|
2021-12-12 00:36:07 +01:00
|
|
|
if (constant_field) {
|
|
|
|
if (gravity_vector_countdown != 0) {
|
|
|
|
cairo_save(cr);
|
|
|
|
cairo_new_path(cr);
|
|
|
|
cairo_move_to(cr, width/2, height/2);
|
2022-06-01 11:09:35 +02:00
|
|
|
cairo_line_to(cr, width/2 + g.x, height/2 + g.y);
|
2021-12-12 00:36:07 +01:00
|
|
|
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
|
|
|
|
cairo_set_line_width(cr, 1.0);
|
|
|
|
cairo_stroke(cr);
|
2022-06-01 11:09:35 +02:00
|
|
|
cairo_arc(cr, width/2 + g.x, height/2 + g.y, 3, 0, 2*M_PI);
|
2021-12-12 00:36:07 +01:00
|
|
|
cairo_fill(cr);
|
|
|
|
if (gravity_vector_countdown > 0)
|
|
|
|
--gravity_vector_countdown;
|
|
|
|
cairo_restore(cr);
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-11 21:32:48 +01:00
|
|
|
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
|
2021-12-12 00:36:07 +01:00
|
|
|
cairo_save (cr);
|
|
|
|
cairo_new_path (cr);
|
|
|
|
cairo_arc (cr, width/2, height/2, g_r, 0, 2*M_PI);
|
|
|
|
cairo_set_line_width(cr, 3.0);
|
2021-12-11 21:32:48 +01:00
|
|
|
cairo_stroke(cr);
|
|
|
|
cairo_restore(cr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gravity_show () {
|
2021-12-12 00:36:07 +01:00
|
|
|
if (constant_field)
|
|
|
|
gravity_vector_countdown = gravity_vector_init;
|
2021-12-11 21:32:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void gravity_change (double dx, double dy) {
|
2021-12-12 00:36:07 +01:00
|
|
|
if (constant_field) {
|
2022-06-01 11:09:35 +02:00
|
|
|
g.x += dx;
|
|
|
|
g.y += dy;
|
2021-12-12 00:36:07 +01:00
|
|
|
gravity_show ();
|
|
|
|
} else {
|
|
|
|
g_r += dx;
|
|
|
|
g_g += dy;
|
|
|
|
}
|
|
|
|
}
|
2021-12-11 21:32:48 +01:00
|
|
|
|
2022-06-01 11:09:35 +02:00
|
|
|
vec2d gravity_vector (const ball * b) {
|
2021-12-12 00:36:07 +01:00
|
|
|
if (constant_field) {
|
2022-06-01 11:09:35 +02:00
|
|
|
return g;
|
2021-12-12 00:36:07 +01:00
|
|
|
} else {
|
2022-06-01 11:09:35 +02:00
|
|
|
vec2d b_c = vec2d{width/2.0,height/2.0} - b->position;
|
|
|
|
double r2 = vec2d::dot(b_c,b_c);
|
2021-12-12 00:36:07 +01:00
|
|
|
if (r2 < g_r*g_r) {
|
2022-06-01 11:09:35 +02:00
|
|
|
return vec2d{0,0};
|
2021-12-12 00:36:07 +01:00
|
|
|
} else {
|
2022-06-01 11:09:35 +02:00
|
|
|
return g_g/r2/sqrt(r2)*b_c;
|
2021-12-12 00:36:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 11:09:35 +02:00
|
|
|
void gravity_collisions (ball * begin, ball * end) {
|
2021-12-12 00:36:07 +01:00
|
|
|
if (constant_field)
|
|
|
|
return;
|
2022-06-01 11:09:35 +02:00
|
|
|
for (ball * b = begin; b != end; ++b) {
|
|
|
|
vec2d b_c = b->position - vec2d{width/2.0,height/2.0};
|
|
|
|
double d2 = vec2d::dot(b_c, b_c);
|
2021-12-12 00:36:07 +01:00
|
|
|
double r = b->radius + g_r;
|
|
|
|
if (d2 <= r*r) {
|
2022-12-27 18:26:03 +01:00
|
|
|
double d = sqrt(d2);
|
|
|
|
if (d <= b->radius)
|
|
|
|
b->position = vec2d{width/2.0,height/2.0+r};
|
|
|
|
else
|
|
|
|
b->position += (r - d)/d*b_c;
|
2022-06-01 11:09:35 +02:00
|
|
|
double f = vec2d::dot(b->velocity, b_c);
|
2021-12-12 00:36:07 +01:00
|
|
|
if (f < 0) {
|
|
|
|
f /= d2;
|
2022-06-01 11:09:35 +02:00
|
|
|
b->velocity -= 2*f*b_c;
|
2021-12-12 00:36:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|