#include #include "random.h" #include "planet.h" #include "utils.h" static void putpixel(struct SDL_Surface *screen, const int x, const int y, const unsigned char r, const unsigned char g, const unsigned char b) { int offset = y * screen->pitch + x * 4; unsigned char *buf = screen->pixels; buf[offset++] = b; buf[offset++] = g; buf[offset] = r; } static void reshape_planet(struct planet *p) { p->radius = pow(p->mass / 100, 1 / 3.0); } void init_planet(struct planet *p) { p->speed.x = 0; p->speed.y = 0; p->pos.x = 0; p->pos.y = 0; p->mass = 0; reshape_planet(p); p->r = get_random() % 256; p->g = get_random() % 256; p->b = get_random() % 256; INIT_LIST_HEAD(&p->list); } /** * setup_planet - set the planet on a "solarsystem" * @p: pointer to struct planet to set up * @mass: mass of the planet to set up * @total_mass: total mass of the system * @radius: maximum radius of the system */ static void setup_planet(struct planet *p, double mass, double total_mass, double radius) { double angle = M_PI * 2 * get_random_double(); double velocity; double distance; distance = radius * pow(get_random_double(), 2); velocity = sqrt(total_mass / radius); velocity *= pow(distance / radius, 0.2); p->pos.x = cos(angle) * distance; p->pos.y = sin(angle) * distance; p->speed.x = -sin(angle) * velocity; p->speed.y = cos(angle) * velocity; p->mass = mass; reshape_planet(p); } void create_planets(struct planet *p, int num, double total_mass, double radius) { int i; double sum = 0; struct planet *new_planet; setup_planet(p, total_mass / num * 2 * get_random_double(), total_mass, radius); for (i = 0; i < num; i++) { new_planet = malloc(sizeof(*new_planet)); init_planet(new_planet); list_add(&new_planet->list, &p->list); setup_planet(new_planet, total_mass / num * 2 * get_random_double(), total_mass, radius); sum += new_planet->mass; } } void draw_planet(SDL_Surface *screen, struct planet *p, const struct camera *cam) { struct vector pos; float radius = p->radius * cam->zoom; float r2 = radius * radius; int x, x_start, y, x_end; vector_sub(&p->pos, &cam->pos, &pos); vector_scale(&pos, cam->zoom, &pos); pos.x += screen->w / 2; pos.y += screen->h / 2; y = MAX(pos.y - radius, 0); if (radius * 2 <= 1) { if (pos.x >= 0 && pos.x < screen->w && pos.y >= 0 && pos.y < screen->h) putpixel(screen, (int)pos.x, (int)pos.y, p->r, p->g, p->b); return; } for (; y < MIN(pos.y + radius, screen->h); y++) { int offset; unsigned char *buf = screen->pixels; float y2 = (y - pos.y); y2 = sqrt(r2 - y2 * y2); x_start = pos.x - y2; x_end = pos.x + y2; x_start = MAX(0, x_start); x_end = MIN(x_end, screen->w); offset = y * screen->pitch + x_start * 4; for (x = x_start; x < x_end; x++) { buf[offset++] = p->b; buf[offset++] = p->g; buf[offset++] = p->r; offset++; } } } int gravitize_planets(struct planet *a, struct planet *b, const double time) { struct vector distance, sum; double dist, f, acc; vector_sub(&a->pos, &b->pos, &distance); dist = vector_abs(&distance); /* Return true in case of a collision */ if (dist < (a->radius + b->radius)) return 1; vector_div(&distance, dist, &distance); f = a->mass * b->mass / (dist * dist) * time; acc = f / b->mass; vector_scale(&distance, acc, &sum); vector_add(&b->speed, &sum, &b->speed); acc = f / a->mass; vector_scale(&distance, acc, &sum); vector_sub(&a->speed, &sum, &a->speed); return 0; } /* * Merge planets a and b into planet a * * It is left for the caller to deal with the scrap planet b */ static void _merge_planets(struct planet *a, struct planet *b) { struct vector pa, pb, p; vector_scale(&a->speed, a->mass, &pa); vector_scale(&b->speed, b->mass, &pb); vector_add(&pa, &pb, &p); if (a->mass < b->mass) a->pos = b->pos; a->mass += b->mass; reshape_planet(a); vector_div(&p, a->mass, &a->speed); } /* * Merge planets a and b into a the new planet a, which pointer is * returned to the caller. Planet b is removed from the linked list * and it's memory is freed. The merged planet will retain in the * list. */ struct planet *merge_planets(struct planet *a, struct planet *b) { _merge_planets(a, b); list_del(&b->list); free(b); return a; } void move_planet(struct planet *p, const double time) { struct vector tmp; vector_scale(&p->speed, time, &tmp); vector_add(&p->pos, &tmp, &p->pos); } void print_planet(const struct planet *p) { printf("pos: (%f,%f), speed: (%f,%f), mass: %f, radius %f\n", p->pos.x, p->pos.y, p->speed.x, p->speed.y, p->mass, p->radius); }