2022-05-30 18:05:53 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cassert>
|
2019-06-03 10:09:00 +02:00
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
#include "game.h"
|
2021-12-01 20:18:19 +01:00
|
|
|
#include "balls.h"
|
2021-12-11 21:32:48 +01:00
|
|
|
#include "gravity.h"
|
2019-06-03 10:09:00 +02:00
|
|
|
|
2019-08-20 16:15:29 +02:00
|
|
|
unsigned int radius_min = 5;
|
|
|
|
unsigned int radius_max = 10;
|
2019-06-03 10:09:00 +02:00
|
|
|
|
2019-08-20 16:15:29 +02:00
|
|
|
unsigned int v_max = 100;
|
|
|
|
unsigned int v_min = 0;
|
2019-06-03 10:09:00 +02:00
|
|
|
|
2019-12-18 21:18:21 +01:00
|
|
|
unsigned int v_angle_min = 0;
|
|
|
|
unsigned int v_angle_max = 100;
|
|
|
|
|
2022-05-30 18:05:53 +02:00
|
|
|
ball * balls = nullptr;
|
2019-06-04 10:13:02 +02:00
|
|
|
unsigned int n_balls = 50;
|
2019-06-03 10:09:00 +02:00
|
|
|
|
2022-12-27 17:54:40 +01:00
|
|
|
// Coefficient of restitution:
|
|
|
|
// C_r == 1.0 ==> perfectly elastic collisions
|
|
|
|
// C_r == 0.0 ==> perfectly inelastic collisions
|
|
|
|
//
|
|
|
|
static double C_r = 1.0;
|
|
|
|
|
|
|
|
void restitution_coefficient_set (double c) {
|
|
|
|
C_r = c;
|
|
|
|
if (C_r > 1.0)
|
|
|
|
C_r = 1.0;
|
|
|
|
else if (C_r < 0.0)
|
|
|
|
C_r = 0.0;
|
2022-12-28 11:47:05 +01:00
|
|
|
}
|
2022-12-27 17:54:40 +01:00
|
|
|
|
|
|
|
double restitution_coefficient_get () {
|
|
|
|
return C_r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void restitution_coefficient_change (double d) {
|
|
|
|
C_r += d;
|
|
|
|
if (C_r > 1.0)
|
|
|
|
C_r = 1.0;
|
|
|
|
else if (C_r < 0.0)
|
|
|
|
C_r = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void restitution_coefficient_draw (cairo_t * cr) {
|
|
|
|
static const double margin = 20;
|
2022-12-28 18:50:12 +01:00
|
|
|
cairo_save(cr);
|
|
|
|
cairo_new_path(cr);
|
|
|
|
cairo_move_to(cr, margin, margin);
|
|
|
|
cairo_line_to(cr, margin + (width - 2*margin)*C_r, margin);
|
|
|
|
cairo_set_source_rgb(cr, 1.0, 1.0, 0.0);
|
|
|
|
cairo_set_line_width(cr, margin/2);
|
|
|
|
cairo_stroke(cr);
|
|
|
|
cairo_restore(cr);
|
2022-12-27 17:54:40 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 11:09:35 +02:00
|
|
|
static vec2d random_velocity() {
|
2019-07-05 15:58:10 +02:00
|
|
|
double r2;
|
2022-06-01 11:09:35 +02:00
|
|
|
vec2d v;
|
2019-07-05 15:58:10 +02:00
|
|
|
do {
|
2022-06-01 11:09:35 +02:00
|
|
|
v.x = v_min + rand() % (v_max + 1 - v_min);
|
|
|
|
v.y = v_min + rand() % (v_max + 1 - v_min);
|
|
|
|
r2 = vec2d::dot(v,v);
|
2019-07-05 15:58:10 +02:00
|
|
|
} while (r2 > v_max*v_max || r2 < v_min*v_min);
|
2022-06-01 11:09:35 +02:00
|
|
|
if (rand() % 2)
|
|
|
|
v.x = -v.x;
|
|
|
|
if (rand() % 2)
|
|
|
|
v.y = -v.y;
|
|
|
|
return v;
|
2019-07-05 15:58:10 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 22:24:06 +02:00
|
|
|
void balls_init_state () {
|
2022-05-30 15:59:45 +02:00
|
|
|
static const int border = 10;
|
|
|
|
int w = width < 2*border ? 1 : width - 2*border;
|
|
|
|
int h = height < 2*border ? 1 : height - 2*border;
|
2019-06-04 10:13:02 +02:00
|
|
|
|
2019-06-03 10:09:00 +02:00
|
|
|
for (unsigned int i = 0; i < n_balls; ++i) {
|
2022-06-01 11:09:35 +02:00
|
|
|
balls[i].position.x = border + rand() % w;
|
|
|
|
balls[i].position.y = border + rand() % h;
|
|
|
|
balls[i].velocity = random_velocity();
|
2019-06-04 10:13:02 +02:00
|
|
|
balls[i].radius = radius_min + rand() % (radius_max + 1 - radius_min);
|
2019-12-18 21:18:21 +01:00
|
|
|
unsigned int v_angle_360 = (v_angle_min + rand() % (v_angle_max + 1 - v_angle_min)) % 360;
|
|
|
|
balls[i].v_angle = 2*M_PI*v_angle_360/360;
|
|
|
|
balls[i].angle = (rand() % 360)*2*M_PI/360;
|
2019-06-03 22:24:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 17:54:40 +01:00
|
|
|
void ball_walls_collision (ball * p) {
|
2022-06-01 11:09:35 +02:00
|
|
|
if (p->position.x + p->radius > width) { /* right wall */
|
|
|
|
if (p->velocity.x > 0) {
|
|
|
|
p->position.x -= p->position.x + p->radius - width;
|
2022-12-27 17:54:40 +01:00
|
|
|
p->velocity.x = -C_r*p->velocity.x;
|
2019-06-03 22:24:06 +02:00
|
|
|
}
|
2022-06-01 11:09:35 +02:00
|
|
|
} else if (p->position.x < p->radius) { /* left wall */
|
|
|
|
if (p->velocity.x < 0) {
|
|
|
|
p->position.x += p->radius - p->position.x;
|
2022-12-27 17:54:40 +01:00
|
|
|
p->velocity.x = -C_r*p->velocity.x;
|
2019-06-03 22:24:06 +02:00
|
|
|
}
|
2019-06-03 10:09:00 +02:00
|
|
|
}
|
2022-06-01 11:09:35 +02:00
|
|
|
if (p->position.y + p->radius > height) { /* bottom wall */
|
|
|
|
if (p->velocity.y > 0) {
|
|
|
|
p->position.y -= p->position.y + p->radius - height;
|
2022-12-27 17:54:40 +01:00
|
|
|
p->velocity.y = -C_r*p->velocity.y;
|
2019-06-03 22:24:06 +02:00
|
|
|
}
|
2022-06-01 11:09:35 +02:00
|
|
|
} else if (p->position.y < p->radius) { /* top wall */
|
|
|
|
if (p->velocity.y < 0) {
|
|
|
|
p->position.y += p->radius - p->position.y;
|
2022-12-27 17:54:40 +01:00
|
|
|
p->velocity.y = -C_r*p->velocity.y;
|
2019-06-03 22:24:06 +02:00
|
|
|
}
|
2022-12-27 17:54:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ball_update_state (ball * p) {
|
|
|
|
vec2d g = gravity_vector (p);
|
|
|
|
|
|
|
|
p->position += delta*p->velocity + delta*delta*g/2.0;
|
|
|
|
p->velocity += delta*g;
|
2019-12-18 21:18:21 +01:00
|
|
|
p->angle += delta*p->v_angle;
|
2021-01-12 15:55:25 +01:00
|
|
|
while (p->angle >= 2*M_PI)
|
2019-12-18 21:18:21 +01:00
|
|
|
p->angle -= 2*M_PI;
|
2021-01-12 15:55:25 +01:00
|
|
|
while (p->angle < 0)
|
|
|
|
p->angle += 2*M_PI;
|
2022-12-27 17:54:40 +01:00
|
|
|
ball_walls_collision (p);
|
2019-06-03 10:09:00 +02:00
|
|
|
}
|
|
|
|
|
2022-12-27 17:54:40 +01:00
|
|
|
void ball_ball_collision (ball * p, ball * q) {
|
2022-06-01 13:06:55 +02:00
|
|
|
vec2d pq = q->position - p->position;
|
|
|
|
double d2 = vec2d::dot(pq,pq);
|
2021-12-11 21:32:48 +01:00
|
|
|
double r = p->radius + q->radius;
|
|
|
|
if (d2 <= r*r) {
|
2022-06-01 13:06:55 +02:00
|
|
|
vec2d pq_v = q->velocity - p->velocity;
|
2019-08-20 16:15:29 +02:00
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
double mp = p->radius * p->radius;
|
|
|
|
double mq = q->radius * q->radius;
|
2022-12-27 17:54:40 +01:00
|
|
|
double m_total = mp + mp;
|
|
|
|
|
|
|
|
double d = sqrt(d2);
|
|
|
|
vec2d pq_overlap = (r - d)/d*pq;
|
|
|
|
p->position -= pq_overlap*mq/m_total;
|
|
|
|
q->position += pq_overlap*mp/m_total;
|
2019-08-19 10:45:55 +02:00
|
|
|
|
2022-06-01 13:06:55 +02:00
|
|
|
double f = vec2d::dot(pq_v, pq);
|
2019-08-20 16:15:29 +02:00
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
if (f < 0) {
|
|
|
|
f /= d2*(mp + mq);
|
2022-12-27 17:54:40 +01:00
|
|
|
p->velocity += 2*C_r*mq*f*pq;
|
|
|
|
q->velocity -= 2*C_r*mp*f*pq;
|
2021-12-11 21:32:48 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-19 10:45:55 +02:00
|
|
|
}
|
|
|
|
|
2022-05-30 18:05:53 +02:00
|
|
|
void ball_reposition (ball * b) {
|
2022-06-01 11:09:35 +02:00
|
|
|
if (b->position.x < b->radius)
|
|
|
|
b->position.x = b->radius;
|
|
|
|
else if (b->position.x + b->radius > width)
|
|
|
|
b->position.x = width - b->radius;
|
|
|
|
if (b->position.y < b->radius)
|
|
|
|
b->position.y = b->radius;
|
|
|
|
else if (b->position.y + b->radius > height)
|
|
|
|
b->position.y = height - b->radius;
|
2019-07-28 15:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 22:45:32 +02:00
|
|
|
const char * face_filename = 0;
|
2021-01-03 13:05:23 +01:00
|
|
|
int face_rotation = 0;
|
2019-07-29 22:45:32 +02:00
|
|
|
|
2019-12-18 21:18:21 +01:00
|
|
|
static const double linear_rotation_unit = 2.0;
|
2019-07-29 22:45:32 +02:00
|
|
|
|
2022-05-30 18:05:53 +02:00
|
|
|
static std::vector<ball_face *> faces;
|
2021-01-12 15:55:25 +01:00
|
|
|
|
2022-05-30 18:05:53 +02:00
|
|
|
class ball_face {
|
|
|
|
public:
|
|
|
|
ball_face (unsigned int radius, cairo_surface_t * face, int rotation);
|
|
|
|
~ball_face ();
|
|
|
|
cairo_surface_t * get_surface (double angle) const;
|
|
|
|
private:
|
2021-01-12 15:55:25 +01:00
|
|
|
unsigned int rotations;
|
2022-05-30 18:05:53 +02:00
|
|
|
std::vector <cairo_surface_t *> c_faces;
|
2021-01-12 15:55:25 +01:00
|
|
|
};
|
|
|
|
|
2021-06-07 13:38:13 +02:00
|
|
|
static double random_color_component() {
|
|
|
|
return 1.0*(rand() % 200 + 56)/255;
|
|
|
|
};
|
|
|
|
|
2022-05-30 18:05:53 +02:00
|
|
|
ball_face::ball_face (unsigned int radius, cairo_surface_t * face, int rotation) {
|
2021-01-03 13:05:23 +01:00
|
|
|
if (face && rotation) {
|
2022-05-30 18:05:53 +02:00
|
|
|
rotations = 2*M_PI * radius / linear_rotation_unit;
|
2019-12-18 21:18:21 +01:00
|
|
|
} else {
|
2022-05-30 18:05:53 +02:00
|
|
|
rotations = 1;
|
2019-12-18 21:18:21 +01:00
|
|
|
}
|
2022-05-30 18:05:53 +02:00
|
|
|
c_faces.resize(rotations);
|
|
|
|
for (unsigned int i = 0; i < rotations; ++i) {
|
|
|
|
c_faces[i] = gdk_window_create_similar_surface(gtk_widget_get_window(canvas),
|
|
|
|
CAIRO_CONTENT_COLOR_ALPHA,
|
|
|
|
2*radius, 2*radius);
|
|
|
|
assert(c_faces[i]);
|
|
|
|
cairo_t * ball_cr = cairo_create(c_faces[i]);
|
2021-01-12 15:55:25 +01:00
|
|
|
cairo_translate(ball_cr, radius, radius);
|
|
|
|
cairo_arc(ball_cr, 0.0, 0.0, radius, 0, 2 * M_PI);
|
2019-07-29 22:45:32 +02:00
|
|
|
cairo_clip(ball_cr);
|
2019-07-29 23:04:26 +02:00
|
|
|
|
2019-12-18 21:18:21 +01:00
|
|
|
if (face) {
|
|
|
|
int face_x_offset = cairo_image_surface_get_width (face) / 2;
|
|
|
|
int face_y_offset = cairo_image_surface_get_height (face) / 2;
|
2022-05-30 18:05:53 +02:00
|
|
|
cairo_rotate(ball_cr, i*2*M_PI/rotations);
|
2021-01-12 15:55:25 +01:00
|
|
|
cairo_scale (ball_cr, 1.0 * radius / face_x_offset, 1.0 * radius / face_y_offset);
|
2019-12-18 21:18:21 +01:00
|
|
|
cairo_set_source_surface(ball_cr, face, -face_x_offset, -face_y_offset);
|
2019-07-29 22:45:32 +02:00
|
|
|
cairo_paint(ball_cr);
|
2021-01-09 20:33:40 +01:00
|
|
|
} else {
|
2021-06-07 13:38:13 +02:00
|
|
|
cairo_pattern_t *pat;
|
|
|
|
pat = cairo_pattern_create_radial (-0.2*radius, -0.2*radius, 0.2*radius,
|
2021-06-08 21:00:31 +02:00
|
|
|
-0.2*radius, -0.2*radius, 1.3*radius);
|
2021-06-07 13:38:13 +02:00
|
|
|
double col_r = random_color_component();
|
|
|
|
double col_g = random_color_component();
|
|
|
|
double col_b = random_color_component();
|
|
|
|
cairo_pattern_add_color_stop_rgba (pat, 0, col_r, col_g, col_b, 1);
|
|
|
|
cairo_pattern_add_color_stop_rgba (pat, 1, col_r/3, col_g/3, col_b/3, 1);
|
|
|
|
cairo_set_source (ball_cr, pat);
|
|
|
|
cairo_arc (ball_cr, 0.0, 0.0, radius, 0, 2 * M_PI);
|
|
|
|
cairo_fill (ball_cr);
|
2021-12-12 11:54:43 +01:00
|
|
|
cairo_pattern_destroy (pat);
|
2019-07-29 22:45:32 +02:00
|
|
|
}
|
2022-05-30 18:05:53 +02:00
|
|
|
cairo_surface_flush(c_faces[i]);
|
2019-07-29 22:45:32 +02:00
|
|
|
cairo_destroy(ball_cr);
|
|
|
|
}
|
2022-05-30 18:05:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ball_face::~ball_face() {
|
|
|
|
for (auto f : c_faces)
|
|
|
|
cairo_surface_destroy(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_surface_t * ball_face::get_surface (double angle) const {
|
|
|
|
unsigned int face_id;
|
|
|
|
if (rotations == 1)
|
|
|
|
face_id = 0;
|
|
|
|
else {
|
|
|
|
face_id = rotations*angle/(2*M_PI);
|
|
|
|
assert(face_id < rotations);
|
|
|
|
if (face_id >= rotations)
|
|
|
|
face_id %= rotations;
|
|
|
|
}
|
|
|
|
return c_faces[face_id];
|
2019-12-18 21:18:21 +01:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
static void balls_init_faces () {
|
2019-12-18 21:18:21 +01:00
|
|
|
cairo_surface_t * face_surface = 0;
|
|
|
|
|
|
|
|
if (face_filename) {
|
|
|
|
face_surface = cairo_image_surface_create_from_png (face_filename);
|
|
|
|
if (cairo_surface_status(face_surface) != CAIRO_STATUS_SUCCESS) {
|
|
|
|
cairo_surface_destroy (face_surface);
|
|
|
|
face_surface = 0;
|
|
|
|
fprintf(stderr, "could not create surface from PNG file %s\n", face_filename);
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 15:55:25 +01:00
|
|
|
if (face_surface) {
|
2022-05-30 18:05:53 +02:00
|
|
|
faces.assign(radius_max + 1 - radius_min, nullptr);
|
|
|
|
for(ball * b = balls; b != balls + n_balls; ++b) {
|
2021-01-12 15:55:25 +01:00
|
|
|
unsigned int r_idx = b->radius - radius_min;
|
2022-05-30 18:05:53 +02:00
|
|
|
if (faces[r_idx] == nullptr)
|
|
|
|
faces[r_idx] = new ball_face (b->radius, face_surface, face_rotation);
|
2021-01-12 15:55:25 +01:00
|
|
|
b->face = faces[r_idx];
|
|
|
|
}
|
2019-07-29 22:45:32 +02:00
|
|
|
cairo_surface_destroy (face_surface);
|
2021-01-12 15:55:25 +01:00
|
|
|
} else {
|
2022-05-30 18:05:53 +02:00
|
|
|
faces.resize(n_balls);
|
2021-01-12 15:55:25 +01:00
|
|
|
for (unsigned int i = 0; i < n_balls; ++i)
|
2022-05-30 18:05:53 +02:00
|
|
|
balls[i].face = faces[i] = new ball_face (balls[i].radius, 0, face_rotation);
|
2021-01-12 15:55:25 +01:00
|
|
|
}
|
2019-07-29 19:25:45 +02:00
|
|
|
}
|
|
|
|
|
2022-05-30 18:05:53 +02:00
|
|
|
void ball::draw (cairo_t * cr) const {
|
|
|
|
cairo_save (cr);
|
2022-06-01 11:09:35 +02:00
|
|
|
cairo_translate (cr, position.x - radius, position.y - radius);
|
2022-05-30 18:05:53 +02:00
|
|
|
cairo_set_source_surface(cr, face->get_surface (angle), 0, 0);
|
|
|
|
cairo_paint(cr);
|
|
|
|
cairo_restore(cr);
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
void balls_draw (cairo_t * cr) {
|
2022-05-30 18:05:53 +02:00
|
|
|
for (const ball * b = balls; b != balls + n_balls; ++b)
|
|
|
|
b->draw(cr);
|
2021-12-03 16:00:36 +01:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
static void balls_destroy_faces () {
|
2022-05-30 18:05:53 +02:00
|
|
|
for (ball_face * f : faces)
|
|
|
|
if (f)
|
|
|
|
delete (f);
|
|
|
|
faces.clear();
|
2021-06-13 17:38:07 +02:00
|
|
|
}
|
2019-07-29 22:57:21 +02:00
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
void balls_destroy () {
|
|
|
|
balls_destroy_faces ();
|
2022-05-30 18:05:53 +02:00
|
|
|
delete [] (balls);
|
2019-06-03 10:09:00 +02:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
void balls_init () {
|
2022-05-30 18:05:53 +02:00
|
|
|
balls = new ball[n_balls];
|
2019-06-03 10:09:00 +02:00
|
|
|
assert(balls);
|
2021-12-11 21:32:48 +01:00
|
|
|
balls_init_state ();
|
|
|
|
balls_init_faces ();
|
2019-06-03 10:09:00 +02:00
|
|
|
}
|