7 static void putpixel(struct SDL_Surface *screen, const int x, const int y,
8 const unsigned char r, const unsigned char g,
11 int offset = y * screen->pitch + x * 4;
12 unsigned char *buf = screen->pixels;
19 static void reshape_planet(struct planet *p)
21 p->radius = pow(p->mass / 100, 1 / 3.0);
24 void init_planet(struct planet *p)
32 p->r = get_random() % 256;
33 p->g = get_random() % 256;
34 p->b = get_random() % 256;
36 INIT_LIST_HEAD(&p->list);
37 init_quadtree(&p->tree);
41 * setup_planet - set the planet on a "solarsystem"
42 * @p: pointer to struct planet to set up
43 * @mass: mass of the planet to set up
44 * @total_mass: total mass of the system
45 * @radius: maximum radius of the system
48 static void setup_planet(struct planet *p, double mass, double total_mass,
51 double angle = M_PI * 2 * get_random_double();
55 distance = radius * pow(get_random_double(), 2);
56 velocity = sqrt(total_mass / radius);
58 velocity *= pow(distance / radius, 0.2);
60 p->pos.x = cos(angle) * distance;
61 p->pos.y = sin(angle) * distance;
63 p->speed.x = -sin(angle) * velocity;
64 p->speed.y = cos(angle) * velocity;
71 void create_planets(struct planet *p, int num, double total_mass, double radius)
75 struct planet *new_planet;
78 total_mass / num * 2 * get_random_double(),
82 for (i = 0; i < num; i++) {
83 new_planet = malloc(sizeof(*new_planet));
84 init_planet(new_planet);
86 list_add(&new_planet->list, &p->list);
88 setup_planet(new_planet,
89 total_mass / num * 2 * get_random_double(),
93 quadtree_add(&p->tree, &new_planet->tree,
94 planet_spatial_compare);
96 sum += new_planet->mass;
100 void draw_planet(SDL_Surface *screen, struct planet *p,
101 const struct camera *cam)
104 float radius = p->radius * cam->zoom;
105 float r2 = radius * radius;
106 int x, x_start, y, x_end;
108 vector_sub(&p->pos, &cam->pos, &pos);
109 vector_scale(&pos, cam->zoom, &pos);
110 pos.x += screen->w / 2;
111 pos.y += screen->h / 2;
113 y = MAX(pos.y - radius, 0);
115 if (radius * 2 <= 1) {
116 if (pos.x >= 0 && pos.x < screen->w &&
117 pos.y >= 0 && pos.y < screen->h)
118 putpixel(screen, (int)pos.x, (int)pos.y,
123 for (; y < MIN(pos.y + radius, screen->h); y++) {
125 unsigned char *buf = screen->pixels;
126 float y2 = (y - pos.y);
128 y2 = sqrt(r2 - y2 * y2);
129 x_start = pos.x - y2;
131 x_start = MAX(0, x_start);
132 x_end = MIN(x_end, screen->w);
134 offset = y * screen->pitch + x_start * 4;
135 for (x = x_start; x < x_end; x++) {
136 buf[offset++] = p->b;
137 buf[offset++] = p->g;
138 buf[offset++] = p->r;
144 int gravitize_planets(struct planet *a, struct planet *b, const double time)
146 struct vector distance, sum;
149 vector_sub(&a->pos, &b->pos, &distance);
151 dist = vector_abs(&distance);
153 /* Return true in case of a collision */
154 if (dist < (a->radius + b->radius))
157 vector_div(&distance, dist, &distance);
159 f = a->mass * b->mass / (dist * dist) * time;
162 vector_scale(&distance, acc, &sum);
163 vector_add(&b->speed, &sum, &b->speed);
166 vector_scale(&distance, acc, &sum);
167 vector_sub(&a->speed, &sum, &a->speed);
173 * Merge planets a and b into planet a
175 * It is left for the caller to deal with the scrap planet b
177 static void _merge_planets(struct planet *a, struct planet *b)
179 struct vector pa, pb, p;
181 vector_scale(&a->speed, a->mass, &pa);
182 vector_scale(&b->speed, b->mass, &pb);
183 vector_add(&pa, &pb, &p);
185 if (a->mass < b->mass)
190 vector_div(&p, a->mass, &a->speed);
194 * Merge planets a and b into a the new planet a, which pointer is
195 * returned to the caller. Planet b is removed from the linked list
196 * and it's memory is freed. The merged planet will retain in the
199 struct planet *merge_planets(struct planet *a, struct planet *b)
201 _merge_planets(a, b);
204 quadtree_del(&b->tree, planet_spatial_compare);
210 static int planet_search_when_moving(struct quadtree *node,
211 struct quadtree_iterator *itr)
213 struct planet *p = tree_to_planet(node);
214 struct planet_search_iterator *it = qt_itr_to_planet_itr(itr);
215 int direction = 0, i;
216 int up = 0, left = 0, right = 0, down = 0;
218 for (i = 0; i < 2; i++) {
219 if (it->limit[i].x < p->pos.x)
223 if (it->limit[i].y < p->pos.y)
230 direction |= QUADTREE_UPLEFT;
232 direction |= QUADTREE_UPRIGHT;
234 direction |= QUADTREE_DOWNLEFT;
236 direction |= QUADTREE_DOWNRIGHT;
237 if ((left && right) || (up && down))
238 direction |= QUADTREE_SELF;
243 void planet_move_iterator(struct quadtree *node, struct quadtree_iterator *it)
245 struct quadtree *parent;
247 parent = quadtree_del(node, planet_spatial_compare);
248 quadtree_add(parent, node, planet_spatial_compare);
251 struct planet *move_planet(struct planet *p, const double time)
253 struct vector tmp, new_pos;
254 struct quadtree *parent, *tree_parent;
256 struct planet_search_iterator it;
260 vector_scale(&p->speed, time, &tmp);
261 vector_add(&p->pos, &tmp, &new_pos);
263 /* Check if we have crossed any of the parents */
264 parent = p->tree.parent;
266 pa = tree_to_planet(parent);
267 if (p->pos.x < pa->pos.x && new_pos.x > pa->pos.x)
269 if (p->pos.x > pa->pos.x && new_pos.x < pa->pos.x)
271 if (p->pos.y < pa->pos.y && new_pos.y > pa->pos.y)
273 if (p->pos.y > pa->pos.y && new_pos.y < pa->pos.y)
277 parent = parent->parent;
281 tree_parent = quadtree_del(&p->tree, planet_spatial_compare);
283 quadtree_add(tree_parent, &p->tree, planet_spatial_compare);
284 return tree_to_planet(tree_parent);
288 * Now, search the subtree for any crossed children and move
289 * them into correct place within the tree.
291 it.qt_iterator.head = &p->tree;
292 it.limit[0] = p->pos;
293 it.limit[1] = new_pos;
294 it.qt_iterator.direction = planet_search_when_moving;
295 it.qt_iterator.callback = planet_move_iterator;
296 walk_quadtree(&it.qt_iterator);
300 return tree_to_planet(quadtree_find_parent(&p->tree));
303 void print_planet(const struct planet *p)
305 printf("pos: (%f,%f), speed: (%f,%f), mass: %f, radius %f\n",
306 p->pos.x, p->pos.y, p->speed.x, p->speed.y, p->mass, p->radius);
309 int planet_spatial_compare(struct quadtree *ta, struct quadtree *tb)
311 struct planet *a, *b;
313 a = tree_to_planet(ta);
314 b = tree_to_planet(tb);
316 up = b->pos.y < a->pos.y;
317 left = b->pos.x < a->pos.x;
328 int planet_search_rectangular(struct quadtree *node,
329 struct quadtree_iterator *itr)
331 struct planet_search_iterator *it = qt_itr_to_planet_itr(itr);
332 struct planet *p = tree_to_planet(node);
333 int direction = 0, i;
334 int up = 0, left = 0, right = 0, down = 0;
336 for (i = 0; i < 2; i++) {
337 if (it->limit[i].x < p->pos.x)
341 if (it->limit[i].y < p->pos.y)
348 direction |= QUADTREE_UPLEFT;
350 direction |= QUADTREE_UPRIGHT;
352 direction |= QUADTREE_DOWNLEFT;
354 direction |= QUADTREE_DOWNRIGHT;
355 if (direction == (QUADTREE_UPLEFT | QUADTREE_UPRIGHT |
356 QUADTREE_DOWNLEFT | QUADTREE_DOWNRIGHT))
357 direction |= QUADTREE_SELF;
362 void planet_draw_iterator(struct quadtree *node, struct quadtree_iterator *it)
364 struct planet *p = tree_to_planet(node);
365 struct planet_search_iterator *i = qt_itr_to_planet_itr(it);
367 draw_planet(i->screen, p, i->cam);