7 struct quadtree_ops planet_ops;
9 static int draw_lines = 0;
11 static void putpixel(struct SDL_Surface *screen, const int x, const int y,
12 const unsigned char r, const unsigned char g,
13 const unsigned char b)
15 int offset = y * screen->pitch + x * 4;
16 unsigned char *buf = screen->pixels;
23 static void draw_line(SDL_Surface *screen, const struct vector *p1,
24 const struct vector *p2,
25 const unsigned char r, const unsigned char g,
26 const unsigned char b, const struct camera *cam)
32 vector_sub(p2, p1, &dir);
34 len = vector_abs(&dir);
35 vector_scale(&dir, 1 / len, &dir);
38 vector_sub(p1, &cam->pos, &v);
39 vector_scale(&v, cam->zoom, &v);
44 for (i = 0; i < len ; i++) {
45 if (v.x >= 0 && v.x < screen->w &&
46 v.y >= 0 && v.y < screen->h)
47 putpixel(screen, v.x, v.y, r, g, b);
48 vector_add(&v, &dir, &v);
52 static void reshape_planet(struct planet *p)
54 p->radius = pow(p->mass / 100, 1 / 3.0);
57 void init_planet(struct planet *p)
59 memset(p, 0, sizeof(*p));
61 p->r = get_random() % 256;
62 p->g = get_random() % 256;
63 p->b = get_random() % 256;
65 INIT_LIST_HEAD(&p->list);
66 init_quadtree(&p->tree);
70 * setup_planet - set the planet on a "solarsystem"
71 * @p: pointer to struct planet to set up
72 * @mass: mass of the planet to set up
73 * @total_mass: total mass of the system
74 * @radius: maximum radius of the system
77 static void setup_planet(struct planet *p, double mass, double total_mass,
80 double angle = M_PI * 2 * get_random_double();
84 distance = radius * pow(get_random_double(), 2);
85 velocity = sqrt(total_mass / radius);
87 velocity *= pow(distance / radius, 0.2);
89 p->pos.x = cos(angle) * distance;
90 p->pos.y = sin(angle) * distance;
92 p->speed.x = -sin(angle) * velocity;
93 p->speed.y = cos(angle) * velocity;
100 void create_planets(struct planet *p, int num, double total_mass, double radius)
104 struct planet *new_planet;
107 total_mass / num * 2 * get_random_double(),
111 for (i = 0; i < num; i++) {
112 new_planet = malloc(sizeof(*new_planet));
113 init_planet(new_planet);
115 list_add(&new_planet->list, &p->list);
117 setup_planet(new_planet,
118 total_mass / num * 2 * get_random_double(),
122 quadtree_add(&p->tree, &new_planet->tree, &planet_ops);
124 sum += new_planet->mass;
128 void draw_circle(SDL_Surface *screen, const struct vector *pos,
130 char r, char g, char b, int transparent)
132 int x, x_start, y, x_end;
133 float r2 = radius * radius;
135 y = MAX(pos->y - radius, 0);
137 if (radius * 2 <= 1) {
138 if (pos->x >= 0 && pos->x < screen->w &&
139 pos->y >= 0 && pos->y < screen->h)
140 putpixel(screen, (int)pos->x, (int)pos->y,
145 for (; y < MIN(pos->y + radius, screen->h); y++) {
147 unsigned char *buf = screen->pixels;
148 float y2 = (y - pos->y);
150 y2 = sqrt(r2 - y2 * y2);
151 x_start = pos->x - y2;
153 x_start = MAX(0, x_start);
154 x_end = MIN(x_end, screen->w);
156 offset = y * screen->pitch + x_start * 4;
158 for (x = x_start; x < x_end; x++) {
165 for (x = x_start; x < x_end; x++) {
175 void draw_planet(SDL_Surface *screen, struct planet *p,
176 const struct camera *cam)
179 float radius = p->radius * cam->zoom;
182 vector_sub(&p->pos, &cam->pos, &pos);
183 vector_scale(&pos, cam->zoom, &pos);
184 pos.x += screen->w / 2;
185 pos.y += screen->h / 2;
188 for (i = 0; i < 4; i++) {
189 if (!p->tree.child[i])
192 struct planet *q = tree_to_planet(p->tree.child[i]);
194 draw_line(screen, &p->pos, &q->pos,
195 p->r, p->g, p->b, cam);
199 draw_circle(screen, &pos, radius, p->r, p->g, p->b, 0);
202 int gravitize_planets(struct planet *a, struct planet *b, const double time)
204 struct vector distance, sum;
207 vector_sub(&a->pos, &b->pos, &distance);
209 dist = vector_abs(&distance);
211 /* Return true in case of a collision */
212 if (dist < (a->radius + b->radius))
215 vector_div(&distance, dist, &distance);
217 f = a->mass * b->mass / (dist * dist) * time;
220 vector_scale(&distance, acc, &sum);
221 vector_add(&b->speed, &sum, &b->speed);
224 vector_scale(&distance, acc, &sum);
225 vector_sub(&a->speed, &sum, &a->speed);
231 * Merge planets a and b into planet a
233 * It is left for the caller to deal with the scrap planet b
235 static void _merge_planets(struct planet *a, struct planet *b)
237 struct vector pa, pb, p;
239 vector_scale(&a->speed, a->mass, &pa);
240 vector_scale(&b->speed, b->mass, &pb);
241 vector_add(&pa, &pb, &p);
243 if (a->mass < b->mass)
248 vector_div(&p, a->mass, &a->speed);
252 * Merge planets a and b into a the new planet a, which pointer is
253 * returned to the caller. Planet b is removed from the linked list
254 * and it's memory is freed. The merged planet will retain in the
257 struct planet *merge_planets(struct planet *a, struct planet *b)
259 _merge_planets(a, b);
262 quadtree_del(&b->tree, &planet_ops);
268 static int planet_search_when_moving(struct quadtree *node,
269 struct quadtree_iterator *itr)
271 struct planet *p = tree_to_planet(node);
272 struct planet_search_iterator *it = qt_itr_to_planet_itr(itr);
273 int direction = 0, i;
274 int up = 0, left = 0, right = 0, down = 0;
276 for (i = 0; i < 2; i++) {
277 if (it->limit[i].x < p->pos.x)
281 if (it->limit[i].y < p->pos.y)
288 direction |= QUADTREE_UPLEFT;
290 direction |= QUADTREE_UPRIGHT;
292 direction |= QUADTREE_DOWNLEFT;
294 direction |= QUADTREE_DOWNRIGHT;
295 if ((left && right) || (up && down))
296 direction |= QUADTREE_SELF;
301 void planet_move_iterator(struct quadtree *node, struct quadtree_iterator *it)
303 struct quadtree *parent;
305 if (node == it->head)
308 parent = quadtree_del(node, &planet_ops);
309 quadtree_add(parent, node, &planet_ops);
312 struct planet *move_planet(struct planet *p, const double time)
314 struct vector tmp, new_pos;
315 struct quadtree *parent, *tree_parent;
317 struct planet_search_iterator it;
321 vector_scale(&p->speed, time, &tmp);
322 vector_add(&p->pos, &tmp, &new_pos);
324 /* Check if we have crossed any of the parents */
325 parent = p->tree.parent;
327 pa = tree_to_planet(parent);
328 if (p->pos.x < pa->pos.x && new_pos.x > pa->pos.x)
330 if (p->pos.x > pa->pos.x && new_pos.x < pa->pos.x)
332 if (p->pos.y < pa->pos.y && new_pos.y > pa->pos.y)
334 if (p->pos.y > pa->pos.y && new_pos.y < pa->pos.y)
338 parent = parent->parent;
342 tree_parent = quadtree_del(&p->tree, &planet_ops);
344 quadtree_add(tree_parent, &p->tree, &planet_ops);
345 return tree_to_planet(tree_parent);
348 if(p->tree.children) {
350 * Now, search the subtree for any crossed children and move
351 * them into correct place within the tree.
353 it.qt_iterator.head = &p->tree;
354 it.limit[0] = p->pos;
355 it.limit[1] = new_pos;
356 it.qt_iterator.direction = planet_search_when_moving;
357 it.qt_iterator.callback = planet_move_iterator;
358 walk_quadtree(&it.qt_iterator);
362 return tree_to_planet(quadtree_find_parent(&p->tree));
365 void print_planet(const struct planet *p)
367 printf("pos: (%f,%f), speed: (%f,%f), mass: %f, radius %f\n",
368 p->pos.x, p->pos.y, p->speed.x, p->speed.y, p->mass, p->radius);
371 int planet_spatial_compare(struct quadtree *ta, struct quadtree *tb)
373 struct planet *a, *b;
375 a = tree_to_planet(ta);
376 b = tree_to_planet(tb);
378 up = b->pos.y < a->pos.y;
379 left = b->pos.x < a->pos.x;
390 int planet_search_rectangular(struct quadtree *node,
391 struct quadtree_iterator *itr)
393 struct planet_search_iterator *it = qt_itr_to_planet_itr(itr);
394 struct planet *p = tree_to_planet(node);
395 int direction = 0, i;
396 int up = 0, left = 0, right = 0, down = 0;
398 for (i = 0; i < 2; i++) {
399 if (it->limit[i].x < p->pos.x)
403 if (it->limit[i].y < p->pos.y)
410 direction |= QUADTREE_UPLEFT;
412 direction |= QUADTREE_UPRIGHT;
414 direction |= QUADTREE_DOWNLEFT;
416 direction |= QUADTREE_DOWNRIGHT;
417 if (direction == (QUADTREE_UPLEFT | QUADTREE_UPRIGHT |
418 QUADTREE_DOWNLEFT | QUADTREE_DOWNRIGHT))
419 direction |= QUADTREE_SELF;
424 void planet_draw_iterator(struct quadtree *node, struct quadtree_iterator *it)
426 struct planet *p = tree_to_planet(node);
427 struct planet_search_iterator *i = qt_itr_to_planet_itr(it);
429 draw_planet(i->screen, p, i->cam);
432 static void planet_recalculate_stats(struct quadtree *node)
434 struct planet *p = tree_to_planet(node), *c;
440 p->tree_mass = p->mass;
442 for (i = 0; i < 4; i++) {
446 c = tree_to_planet(node->child[i]);
447 p->tree_mass += c->tree_mass;
449 vector_sub(&p->pos, &c->pos, &vect);
450 dist = vector_abs(&vect);
451 dist += c->tree_area;
452 p->tree_area = MAX(p->tree_area, dist);
456 struct quadtree_ops planet_ops = {
457 .compare = planet_spatial_compare,
458 .recalculate_stats = planet_recalculate_stats,