7 struct quadtree_ops planet_ops;
9 static void putpixel(struct SDL_Surface *screen, const int x, const int y,
10 const unsigned char r, const unsigned char g,
11 const unsigned char b)
13 int offset = y * screen->pitch + x * 4;
14 unsigned char *buf = screen->pixels;
21 static void reshape_planet(struct planet *p)
23 p->radius = pow(p->mass / 100, 1 / 3.0);
26 void init_planet(struct planet *p)
28 memset(p, 0, sizeof(*p));
30 p->r = get_random() % 256;
31 p->g = get_random() % 256;
32 p->b = get_random() % 256;
34 INIT_LIST_HEAD(&p->list);
35 init_quadtree(&p->tree);
39 * setup_planet - set the planet on a "solarsystem"
40 * @p: pointer to struct planet to set up
41 * @mass: mass of the planet to set up
42 * @total_mass: total mass of the system
43 * @radius: maximum radius of the system
46 static void setup_planet(struct planet *p, double mass, double total_mass,
49 double angle = M_PI * 2 * get_random_double();
53 distance = radius * pow(get_random_double(), 2);
54 velocity = sqrt(total_mass / radius);
56 velocity *= pow(distance / radius, 0.2);
58 p->pos.x = cos(angle) * distance;
59 p->pos.y = sin(angle) * distance;
61 p->speed.x = -sin(angle) * velocity;
62 p->speed.y = cos(angle) * velocity;
69 void create_planets(struct planet *p, int num, double total_mass, double radius)
73 struct planet *new_planet;
76 total_mass / num * 2 * get_random_double(),
80 for (i = 0; i < num; i++) {
81 new_planet = malloc(sizeof(*new_planet));
82 init_planet(new_planet);
84 list_add(&new_planet->list, &p->list);
86 setup_planet(new_planet,
87 total_mass / num * 2 * get_random_double(),
91 quadtree_add(&p->tree, &new_planet->tree, &planet_ops);
93 sum += new_planet->mass;
97 void draw_circle(SDL_Surface *screen, const struct vector *pos,
99 char r, char g, char b, int transparent)
101 int x, x_start, y, x_end;
102 float r2 = radius * radius;
104 y = MAX(pos->y - radius, 0);
106 if (radius * 2 <= 1) {
107 if (pos->x >= 0 && pos->x < screen->w &&
108 pos->y >= 0 && pos->y < screen->h)
109 putpixel(screen, (int)pos->x, (int)pos->y,
114 for (; y < MIN(pos->y + radius, screen->h); y++) {
116 unsigned char *buf = screen->pixels;
117 float y2 = (y - pos->y);
119 y2 = sqrt(r2 - y2 * y2);
120 x_start = pos->x - y2;
122 x_start = MAX(0, x_start);
123 x_end = MIN(x_end, screen->w);
125 offset = y * screen->pitch + x_start * 4;
127 for (x = x_start; x < x_end; x++) {
134 for (x = x_start; x < x_end; x++) {
144 void draw_planet(SDL_Surface *screen, struct planet *p,
145 const struct camera *cam)
148 float radius = p->radius * cam->zoom;
151 vector_sub(&p->pos, &cam->pos, &pos);
152 vector_scale(&pos, cam->zoom, &pos);
153 pos.x += screen->w / 2;
154 pos.y += screen->h / 2;
156 draw_circle(screen, &pos, radius, p->r, p->g, p->b, 0);
159 int gravitize_planets(struct planet *a, struct planet *b, const double time)
161 struct vector distance, sum;
164 vector_sub(&a->pos, &b->pos, &distance);
166 dist = vector_abs(&distance);
168 /* Return true in case of a collision */
169 if (dist < (a->radius + b->radius))
172 vector_div(&distance, dist, &distance);
174 f = a->mass * b->mass / (dist * dist) * time;
177 vector_scale(&distance, acc, &sum);
178 vector_add(&b->speed, &sum, &b->speed);
181 vector_scale(&distance, acc, &sum);
182 vector_sub(&a->speed, &sum, &a->speed);
188 * Merge planets a and b into planet a
190 * It is left for the caller to deal with the scrap planet b
192 static void _merge_planets(struct planet *a, struct planet *b)
194 struct vector pa, pb, p;
196 vector_scale(&a->speed, a->mass, &pa);
197 vector_scale(&b->speed, b->mass, &pb);
198 vector_add(&pa, &pb, &p);
200 if (a->mass < b->mass)
205 vector_div(&p, a->mass, &a->speed);
209 * Merge planets a and b into a the new planet a, which pointer is
210 * returned to the caller. Planet b is removed from the linked list
211 * and it's memory is freed. The merged planet will retain in the
214 struct planet *merge_planets(struct planet *a, struct planet *b)
216 _merge_planets(a, b);
219 quadtree_del(&b->tree, &planet_ops);
225 static int planet_search_when_moving(struct quadtree *node,
226 struct quadtree_iterator *itr)
228 struct planet *p = tree_to_planet(node);
229 struct planet_search_iterator *it = qt_itr_to_planet_itr(itr);
230 int direction = 0, i;
231 int up = 0, left = 0, right = 0, down = 0;
233 for (i = 0; i < 2; i++) {
234 if (it->limit[i].x < p->pos.x)
238 if (it->limit[i].y < p->pos.y)
245 direction |= QUADTREE_UPLEFT;
247 direction |= QUADTREE_UPRIGHT;
249 direction |= QUADTREE_DOWNLEFT;
251 direction |= QUADTREE_DOWNRIGHT;
252 if ((left && right) || (up && down))
253 direction |= QUADTREE_SELF;
258 void planet_move_iterator(struct quadtree *node, struct quadtree_iterator *it)
260 struct quadtree *parent;
262 parent = quadtree_del(node, &planet_ops);
263 quadtree_add(parent, node, &planet_ops);
266 struct planet *move_planet(struct planet *p, const double time)
268 struct vector tmp, new_pos;
269 struct quadtree *parent, *tree_parent;
271 struct planet_search_iterator it;
275 vector_scale(&p->speed, time, &tmp);
276 vector_add(&p->pos, &tmp, &new_pos);
278 /* Check if we have crossed any of the parents */
279 parent = p->tree.parent;
281 pa = tree_to_planet(parent);
282 if (p->pos.x < pa->pos.x && new_pos.x > pa->pos.x)
284 if (p->pos.x > pa->pos.x && new_pos.x < pa->pos.x)
286 if (p->pos.y < pa->pos.y && new_pos.y > pa->pos.y)
288 if (p->pos.y > pa->pos.y && new_pos.y < pa->pos.y)
292 parent = parent->parent;
296 tree_parent = quadtree_del(&p->tree, &planet_ops);
298 quadtree_add(tree_parent, &p->tree, &planet_ops);
299 return tree_to_planet(tree_parent);
303 * Now, search the subtree for any crossed children and move
304 * them into correct place within the tree.
306 it.qt_iterator.head = &p->tree;
307 it.limit[0] = p->pos;
308 it.limit[1] = new_pos;
309 it.qt_iterator.direction = planet_search_when_moving;
310 it.qt_iterator.callback = planet_move_iterator;
311 walk_quadtree(&it.qt_iterator);
315 return tree_to_planet(quadtree_find_parent(&p->tree));
318 void print_planet(const struct planet *p)
320 printf("pos: (%f,%f), speed: (%f,%f), mass: %f, radius %f\n",
321 p->pos.x, p->pos.y, p->speed.x, p->speed.y, p->mass, p->radius);
324 int planet_spatial_compare(struct quadtree *ta, struct quadtree *tb)
326 struct planet *a, *b;
328 a = tree_to_planet(ta);
329 b = tree_to_planet(tb);
331 up = b->pos.y < a->pos.y;
332 left = b->pos.x < a->pos.x;
343 int planet_search_rectangular(struct quadtree *node,
344 struct quadtree_iterator *itr)
346 struct planet_search_iterator *it = qt_itr_to_planet_itr(itr);
347 struct planet *p = tree_to_planet(node);
348 int direction = 0, i;
349 int up = 0, left = 0, right = 0, down = 0;
351 for (i = 0; i < 2; i++) {
352 if (it->limit[i].x < p->pos.x)
356 if (it->limit[i].y < p->pos.y)
363 direction |= QUADTREE_UPLEFT;
365 direction |= QUADTREE_UPRIGHT;
367 direction |= QUADTREE_DOWNLEFT;
369 direction |= QUADTREE_DOWNRIGHT;
370 if (direction == (QUADTREE_UPLEFT | QUADTREE_UPRIGHT |
371 QUADTREE_DOWNLEFT | QUADTREE_DOWNRIGHT))
372 direction |= QUADTREE_SELF;
377 void planet_draw_iterator(struct quadtree *node, struct quadtree_iterator *it)
379 struct planet *p = tree_to_planet(node);
380 struct planet_search_iterator *i = qt_itr_to_planet_itr(it);
382 draw_planet(i->screen, p, i->cam);
385 static void planet_recalculate_stats(struct quadtree *node)
387 struct planet *p = tree_to_planet(node), *c;
393 p->tree_mass = p->mass;
395 for (i = 0; i < 4; i++) {
399 c = tree_to_planet(node->child[i]);
400 p->tree_mass += c->tree_mass;
402 vector_sub(&p->pos, &c->pos, &vect);
403 dist = vector_abs(&vect);
404 dist += c->tree_area;
405 p->tree_area = MAX(p->tree_area, dist);
409 struct quadtree_ops planet_ops = {
410 .compare = planet_spatial_compare,
411 .recalculate_stats = planet_recalculate_stats,