2D vector objects for positions, velocities, etc.

This commit is contained in:
Antonio Carzaniga 2022-06-01 11:09:35 +02:00
parent 2240ba1b69
commit 7064eb5174
9 changed files with 185 additions and 138 deletions

View File

@ -13,13 +13,13 @@ LIBS=$(GTK_LIBS) -lm
PROGS=balls PROGS=balls
OBJS=balls.o c_index.o game.o gravity.o spaceship.o main.o OBJS=balls.o c_index.o game.o gravity.o spaceship.o main.o
# dependencies (gcc -MM *.c) # dependencies (gcc -MM *.cc)
balls.o: balls.cc game.h balls.h gravity.h balls.o: balls.cc game.h balls.h vec2d.h gravity.h
c_index.o: c_index.cc balls.h game.h c_index.h c_index.o: c_index.cc balls.h vec2d.h game.h c_index.h
game.o: game.cc game.h game.o: game.cc game.h
gravity.o: gravity.cc gravity.h game.h gravity.o: gravity.cc gravity.h balls.h vec2d.h game.h
main.o: main.cc game.h balls.h c_index.h gravity.h spaceship.h main.o: main.cc game.h balls.h vec2d.h c_index.h gravity.h spaceship.h
spaceship.o: spaceship.cc balls.h game.h spaceship.o: spaceship.cc balls.h vec2d.h game.h
stats.o: stats.cc stats.o: stats.cc
.PHONY: run .PHONY: run

101
balls.cc
View File

@ -18,13 +18,19 @@ unsigned int v_angle_max = 100;
ball * balls = nullptr; ball * balls = nullptr;
unsigned int n_balls = 50; unsigned int n_balls = 50;
static void random_velocity(ball * p) { static vec2d random_velocity() {
double r2; double r2;
vec2d v;
do { do {
p->v_x = v_min + rand() % (v_max + 1 - v_min); v.x = v_min + rand() % (v_max + 1 - v_min);
p->v_y = v_min + rand() % (v_max + 1 - v_min); v.y = v_min + rand() % (v_max + 1 - v_min);
r2 = p->v_x*p->v_x + p->v_y*p->v_y; r2 = vec2d::dot(v,v);
} while (r2 > v_max*v_max || r2 < v_min*v_min); } while (r2 > v_max*v_max || r2 < v_min*v_min);
if (rand() % 2)
v.x = -v.x;
if (rand() % 2)
v.y = -v.y;
return v;
} }
void balls_init_state () { void balls_init_state () {
@ -33,13 +39,9 @@ void balls_init_state () {
int h = height < 2*border ? 1 : height - 2*border; int h = height < 2*border ? 1 : height - 2*border;
for (unsigned int i = 0; i < n_balls; ++i) { for (unsigned int i = 0; i < n_balls; ++i) {
balls[i].x = border + rand() % w; balls[i].position.x = border + rand() % w;
balls[i].y = border + rand() % h; balls[i].position.y = border + rand() % h;
random_velocity(balls + i); balls[i].velocity = random_velocity();
if (rand() % 2)
balls[i].v_x = -balls[i].v_x;
if (rand() % 2)
balls[i].v_y = -balls[i].v_y;
balls[i].radius = radius_min + rand() % (radius_max + 1 - radius_min); balls[i].radius = radius_min + rand() % (radius_max + 1 - radius_min);
unsigned int v_angle_360 = (v_angle_min + rand() % (v_angle_max + 1 - v_angle_min)) % 360; 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].v_angle = 2*M_PI*v_angle_360/360;
@ -48,36 +50,32 @@ void balls_init_state () {
} }
void ball_update_state (ball * p) { void ball_update_state (ball * p) {
struct gravity_vector g; vec2d g = gravity_vector (p);
gravity_get_vector (&g, p);
p->x += delta*p->v_x + delta*delta*g.x/2.0; p->position += delta*p->velocity + delta*delta*g/2.0;
p->v_x += delta*g.x; p->velocity += delta*g;
p->y += delta*p->v_y + delta*delta*g.y/2.0; if (p->position.x + p->radius > width) { /* right wall */
p->v_y += delta*g.y; if (p->velocity.x > 0) {
p->position.x -= p->position.x + p->radius - width;
if (p->x + p->radius > width) { /* right wall */ p->velocity.x = -p->velocity.x;
if (p->v_x > 0) {
p->x -= p->x + p->radius - width;
p->v_x = -p->v_x;
} }
} else if (p->x < p->radius) { /* left wall */ } else if (p->position.x < p->radius) { /* left wall */
if (p->v_x < 0) { if (p->velocity.x < 0) {
p->x += p->radius - p->x; p->position.x += p->radius - p->position.x;
p->v_x = -p->v_x; p->velocity.x = -p->velocity.x;
} }
} }
if (p->y + p->radius > height) { /* bottom wall */ if (p->position.y + p->radius > height) { /* bottom wall */
if (p->v_y > 0) { if (p->velocity.y > 0) {
p->y -= p->y + p->radius - height; p->position.y -= p->position.y + p->radius - height;
p->v_y = -p->v_y; p->velocity.y = -p->velocity.y;
} }
} else if (p->y < p->radius) { /* top wall */ } else if (p->position.y < p->radius) { /* top wall */
if (p->v_y < 0) { if (p->velocity.y < 0) {
p->y += p->radius - p->y; p->position.y += p->radius - p->position.y;
p->v_y = -p->v_y; p->velocity.y = -p->velocity.y;
} }
} }
p->angle += delta*p->v_angle; p->angle += delta*p->v_angle;
@ -88,39 +86,34 @@ void ball_update_state (ball * p) {
} }
void ball_elastic_collision (ball * p, ball * q) { void ball_elastic_collision (ball * p, ball * q) {
double dx = q->x - p->x; vec2d dp = q->position - p->position;
double dy = q->y - p->y; double d2 = vec2d::dot(dp,dp);
double d2 = dx*dx + dy*dy;
double r = p->radius + q->radius; double r = p->radius + q->radius;
if (d2 <= r*r) { if (d2 <= r*r) {
double dv_x = q->v_x - p->v_x; vec2d dv = q->velocity - p->velocity;
double dv_y = q->v_y - p->v_y;
double mp = p->radius * p->radius; double mp = p->radius * p->radius;
double mq = q->radius * q->radius; double mq = q->radius * q->radius;
double f = dv_x*dx + dv_y*dy; double f = vec2d::dot(dv,dp);
if (f < 0) { if (f < 0) {
f /= d2*(mp + mq); f /= d2*(mp + mq);
p->v_x += 2*mq*f*dx; p->velocity += 2*mq*f*dp;
p->v_y += 2*mq*f*dy; q->velocity -= 2*mp*f*dp;
q->v_x -= 2*mp*f*dx;
q->v_y -= 2*mp*f*dy;
} }
} }
} }
void ball_reposition (ball * b) { void ball_reposition (ball * b) {
if (b->x < b->radius) if (b->position.x < b->radius)
b->x = b->radius; b->position.x = b->radius;
else if (b->x + b->radius > width) else if (b->position.x + b->radius > width)
b->x = width - b->radius; b->position.x = width - b->radius;
if (b->y < b->radius) if (b->position.y < b->radius)
b->y = b->radius; b->position.y = b->radius;
else if (b->y + b->radius > height) else if (b->position.y + b->radius > height)
b->y = height - b->radius; b->position.y = height - b->radius;
} }
const char * face_filename = 0; const char * face_filename = 0;
@ -234,7 +227,7 @@ static void balls_init_faces () {
void ball::draw (cairo_t * cr) const { void ball::draw (cairo_t * cr) const {
cairo_save (cr); cairo_save (cr);
cairo_translate (cr, x - radius, y - radius); cairo_translate (cr, position.x - radius, position.y - radius);
cairo_set_source_surface(cr, face->get_surface (angle), 0, 0); cairo_set_source_surface(cr, face->get_surface (angle), 0, 0);
cairo_paint(cr); cairo_paint(cr);
cairo_restore(cr); cairo_restore(cr);

View File

@ -3,16 +3,15 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include "vec2d.h"
class ball_face; class ball_face;
class ball { class ball {
public: public:
double x;
double y;
unsigned int radius; unsigned int radius;
vec2d position;
double v_x; vec2d velocity;
double v_y;
double angle; double angle;
double v_angle; double v_angle;

View File

@ -24,24 +24,24 @@ struct bt_node * c_index = 0;
static struct bt_node * c_index_init_node(struct bt_node * n, struct ball * b) { static struct bt_node * c_index_init_node(struct bt_node * n, struct ball * b) {
n->ball = b; n->ball = b;
n->r.min_x = b->x - b->radius; n->r.min_x = b->velocity.x - b->radius;
n->r.min_y = b->y - b->radius; n->r.min_y = b->velocity.y - b->radius;
n->r.max_x = b->x + b->radius; n->r.max_x = b->velocity.x + b->radius;
n->r.max_y = b->y + b->radius; n->r.max_y = b->velocity.y + b->radius;
n->left = 0; n->left = 0;
n->right = 0; n->right = 0;
return n; return n;
} }
static void c_index_add_ball(struct bt_node * n, const struct ball * b) { static void c_index_add_ball(struct bt_node * n, const struct ball * b) {
if (n->r.min_x > b->x - b->radius) if (n->r.min_x > b->velocity.x - b->radius)
n->r.min_x = b->x - b->radius; n->r.min_x = b->velocity.x - b->radius;
if (n->r.min_y > b->y - b->radius) if (n->r.min_y > b->velocity.y - b->radius)
n->r.min_y = b->y - b->radius; n->r.min_y = b->velocity.y - b->radius;
if (n->r.max_x < b->x + b->radius) if (n->r.max_x < b->velocity.x + b->radius)
n->r.max_x = b->x + b->radius; n->r.max_x = b->velocity.x + b->radius;
if (n->r.max_y < b->y + b->radius) if (n->r.max_y < b->velocity.y + b->radius)
n->r.max_y = b->y + b->radius; n->r.max_y = b->velocity.y + b->radius;
} }
static void c_index_insert(struct bt_node * t, struct bt_node * n, struct ball * b) { static void c_index_insert(struct bt_node * t, struct bt_node * n, struct ball * b) {
@ -53,9 +53,9 @@ static void c_index_insert(struct bt_node * t, struct bt_node * n, struct ball *
for (;;) { for (;;) {
c_index_add_ball(t, b); c_index_add_ball(t, b);
if (w > h) { /* horizontal split */ if (w > h) { /* horizontal split */
if (b->x <= t->ball->x) { if (b->velocity.x <= t->ball->velocity.x) {
if (t->left) { if (t->left) {
w = t->ball->x - ref_x; w = t->ball->velocity.x - ref_x;
t = t->left; t = t->left;
} else { } else {
t->left = n; t->left = n;
@ -63,8 +63,8 @@ static void c_index_insert(struct bt_node * t, struct bt_node * n, struct ball *
} }
} else { } else {
if (t->right) { if (t->right) {
w -= t->ball->x - ref_x; w -= t->ball->velocity.x - ref_x;
ref_x = t->ball->x; ref_x = t->ball->velocity.x;
t = t->right; t = t->right;
} else { } else {
t->right = n; t->right = n;
@ -72,9 +72,9 @@ static void c_index_insert(struct bt_node * t, struct bt_node * n, struct ball *
} }
} }
} else { /* vertical split */ } else { /* vertical split */
if (b->y <= t->ball->y) { if (b->velocity.y <= t->ball->velocity.y) {
if (t->left) { if (t->left) {
h = t->ball->y - ref_y; h = t->ball->velocity.y - ref_y;
t = t->left; t = t->left;
} else { } else {
t->left = n; t->left = n;
@ -82,8 +82,8 @@ static void c_index_insert(struct bt_node * t, struct bt_node * n, struct ball *
} }
} else { } else {
if (t->right) { if (t->right) {
h -= t->ball->y - ref_y; h -= t->ball->velocity.y - ref_y;
ref_y = t->ball->y; ref_y = t->ball->velocity.y;
t = t->right; t = t->right;
} else { } else {
t->right = n; t->right = n;
@ -119,10 +119,10 @@ static struct bt_node * c_index_stack_pop() {
} }
static int c_index_ball_in_rectangle(const struct bt_node * n, const struct ball * b) { static int c_index_ball_in_rectangle(const struct bt_node * n, const struct ball * b) {
return n->r.min_x <= b->x + b->radius return n->r.min_x <= b->velocity.x + b->radius
&& n->r.max_x >= b->x - b->radius && n->r.max_x >= b->velocity.x - b->radius
&& n->r.min_y <= b->y + b->radius && n->r.min_y <= b->velocity.y + b->radius
&& n->r.max_y >= b->y - b->radius; && n->r.max_y >= b->velocity.y - b->radius;
} }
static int c_index_must_check(const struct bt_node * n, const struct ball * b) { static int c_index_must_check(const struct bt_node * n, const struct ball * b) {

View File

@ -4,8 +4,7 @@
#include "gravity.h" #include "gravity.h"
#include "game.h" #include "game.h"
static double g_y = 20; static vec2d g{.x = 0, .y = 20};
static double g_x = 0;
static double g_r = 50; static double g_r = 50;
static double g_g = 10000000; static double g_g = 10000000;
@ -17,8 +16,8 @@ static int gravity_vector_init = 300;
void gravity_constant_field (double x, double y) { void gravity_constant_field (double x, double y) {
constant_field = 1; constant_field = 1;
g_x = x; g.x = x;
g_y = y; g.y = y;
} }
void gravity_newton_field (double r, double g) { void gravity_newton_field (double r, double g) {
@ -33,11 +32,11 @@ void gravity_draw (cairo_t * cr) {
cairo_save(cr); cairo_save(cr);
cairo_new_path(cr); cairo_new_path(cr);
cairo_move_to(cr, width/2, height/2); cairo_move_to(cr, width/2, height/2);
cairo_line_to(cr, width/2 + g_x, height/2 + g_y); cairo_line_to(cr, width/2 + g.x, height/2 + g.y);
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
cairo_set_line_width(cr, 1.0); cairo_set_line_width(cr, 1.0);
cairo_stroke(cr); cairo_stroke(cr);
cairo_arc(cr, width/2 + g_x, height/2 + g_y, 3, 0, 2*M_PI); cairo_arc(cr, width/2 + g.x, height/2 + g.y, 3, 0, 2*M_PI);
cairo_fill(cr); cairo_fill(cr);
if (gravity_vector_countdown > 0) if (gravity_vector_countdown > 0)
--gravity_vector_countdown; --gravity_vector_countdown;
@ -61,8 +60,8 @@ void gravity_show () {
void gravity_change (double dx, double dy) { void gravity_change (double dx, double dy) {
if (constant_field) { if (constant_field) {
g_x += dx; g.x += dx;
g_y += dy; g.y += dy;
gravity_show (); gravity_show ();
} else { } else {
g_r += dx; g_r += dx;
@ -70,43 +69,32 @@ void gravity_change (double dx, double dy) {
} }
} }
void gravity_get_vector (struct gravity_vector * v, const struct ball * b) { vec2d gravity_vector (const ball * b) {
if (constant_field) { if (constant_field) {
v->x = g_x; return g;
v->y = g_y;
} else { } else {
double dx = width/2 - b->x; vec2d b_c = vec2d{width/2.0,height/2.0} - b->position;
double dy = height/2 - b->y; double r2 = vec2d::dot(b_c,b_c);
double r2 = dx*dx+dy*dy;
if (r2 < g_r*g_r) { if (r2 < g_r*g_r) {
v->x = 0; return vec2d{0,0};
v->y = 0;
} else { } else {
double r = sqrt(r2); return g_g/r2/sqrt(r2)*b_c;
v->x = g_g/r2/r*dx;
v->y = g_g/r2/r*dy;
} }
} }
} }
void gravity_collisions (struct ball * begin, struct ball * end) { void gravity_collisions (ball * begin, ball * end) {
if (constant_field) if (constant_field)
return; return;
for (struct ball * b = begin; b != end; ++b) { for (ball * b = begin; b != end; ++b) {
double dx = b->x - width/2; vec2d b_c = b->position - vec2d{width/2.0,height/2.0};
double dy = b->y - height/2; double d2 = vec2d::dot(b_c, b_c);
double d2 = dx*dx + dy*dy;
double r = b->radius + g_r; double r = b->radius + g_r;
if (d2 <= r*r) { if (d2 <= r*r) {
double dv_x = b->v_x; double f = vec2d::dot(b->velocity, b_c);
double dv_y = b->v_y;
double f = dv_x*dx + dv_y*dy;
if (f < 0) { if (f < 0) {
f /= d2; f /= d2;
b->v_x -= 2*f*dx; b->velocity -= 2*f*b_c;
b->v_y -= 2*f*dy;
} }
} }
} }

View File

@ -3,15 +3,10 @@
#include "balls.h" #include "balls.h"
struct gravity_vector {
double x;
double y;
};
extern void gravity_constant_field (double x, double y); extern void gravity_constant_field (double x, double y);
extern void gravity_newton_field (double r, double g); extern void gravity_newton_field (double r, double g);
extern void gravity_get_vector (gravity_vector * v, const ball * b); extern vec2d gravity_vector (const ball * b);
extern void gravity_draw (cairo_t * cr); extern void gravity_draw (cairo_t * cr);
extern void gravity_change (double dx, double dy); extern void gravity_change (double dx, double dy);

View File

@ -78,7 +78,7 @@ gint configure_event (GtkWidget *widget, GdkEventConfigure * event) {
width = gtk_widget_get_allocated_width (widget); width = gtk_widget_get_allocated_width (widget);
height = gtk_widget_get_allocated_height (widget); height = gtk_widget_get_allocated_height (widget);
for (struct ball * b = balls; b != balls + n_balls; ++b) for (ball * b = balls; b != balls + n_balls; ++b)
ball_reposition (b); ball_reposition (b);
ball_reposition (&spaceship); ball_reposition (&spaceship);
return TRUE; return TRUE;

View File

@ -10,24 +10,22 @@ int spaceship_thrust_countdown = 0;
int spaceship_thrust_init = 50; int spaceship_thrust_init = 50;
void spaceship_init_state () { void spaceship_init_state () {
spaceship.x = width/2; spaceship.position.x = width/2;
spaceship.y = height/2; spaceship.position.y = height/2;
spaceship.radius = 30; spaceship.radius = 30;
spaceship.v_x = 0; spaceship.velocity.x = 0;
spaceship.v_y = 0; spaceship.velocity.y = 0;
spaceship.angle = 0; spaceship.angle = 0;
spaceship.v_angle = 0; spaceship.v_angle = 0;
} }
void spaceship_update_state () { void spaceship_update_state () {
if (spaceship_thrust > 0) { if (spaceship_thrust > 0) {
double fx = cos(spaceship.angle)*spaceship_thrust*4.0; vec2d f { cos(spaceship.angle)*spaceship_thrust*4.0,
double fy = sin(spaceship.angle)*spaceship_thrust*4.0; sin(spaceship.angle)*spaceship_thrust*4.0 };
spaceship.x += delta*delta*fx/2.0; spaceship.position += delta*delta*f/2.0;
spaceship.v_x += delta*fx; spaceship.velocity += delta*f;
spaceship.y += delta*delta*fy/2.0;
spaceship.v_y += delta*fy;
if (spaceship_thrust_countdown > 0) if (spaceship_thrust_countdown > 0)
--spaceship_thrust_countdown; --spaceship_thrust_countdown;
else else
@ -40,7 +38,7 @@ void spaceship_draw (cairo_t * cr) {
static const double one_over_sqrt_2 = 0.70710678118654752440; static const double one_over_sqrt_2 = 0.70710678118654752440;
cairo_save(cr); cairo_save(cr);
cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0); cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
cairo_translate(cr, spaceship.x, spaceship.y); cairo_translate(cr, spaceship.position.x, spaceship.position.y);
cairo_rotate(cr, spaceship.angle); cairo_rotate(cr, spaceship.angle);
cairo_arc(cr, 0, 0, spaceship.radius, 0, 2*M_PI); cairo_arc(cr, 0, 0, spaceship.radius, 0, 2*M_PI);
cairo_stroke(cr); cairo_stroke(cr);

74
vec2d.h Normal file
View File

@ -0,0 +1,74 @@
#ifndef VEC2D_H_INCLUDED
#define VEC2D_H_INCLUDED
#include <cmath>
class vec2d {
public:
double x;
double y;
vec2d & operator = (const vec2d & other) {
x = other.x;
y = other.y;
return *this;
}
vec2d & operator += (const vec2d & other) {
x += other.x;
y += other.y;
return *this;
}
vec2d & operator -= (const vec2d & other) {
x -= other.x;
y -= other.y;
return *this;
}
vec2d & operator *= (double l) {
x *= l;
y *= l;
return *this;
}
vec2d operator + (const vec2d & other) const {
return vec2d{x + other.x, y + other.y};
}
vec2d operator - (const vec2d & other) const {
return vec2d{x - other.x, y - other.y};
}
vec2d operator * (double l) const {
return vec2d{x*l, y*l};
}
vec2d operator / (double a) const {
return vec2d{x/a, y/a};
}
vec2d & rotate (double angle) {
double sin_a = sin(angle);
double cos_a = cos(angle);
double x1 = x;
double y1 = y;
x = cos_a*x1 - sin_a*y1;
y = sin_a*x1 + cos_a*y1;
return *this;
}
static double dot (const vec2d & a, const vec2d & b) {
return a.x*b.x + a.y*b.y;
}
static double cross (const vec2d & a, const vec2d & b) {
return a.x*b.x + a.y*b.y;
}
};
static inline vec2d operator * (double l, const vec2d & v) {
return vec2d{v.x*l, v.y*l};
}
#endif