]> git.itanic.dy.fi Git - sdl-planets/blob - planet.c
planet.c: Use the quadtree_move function from quadtree lib
[sdl-planets] / planet.c
1 #include <math.h>
2
3 #include "random.h"
4 #include "planet.h"
5 #include "utils.h"
6
7 struct quadtree_ops planet_ops;
8
9 static int draw_lines = 0;
10
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)
14 {
15         int offset = y * screen->pitch + x * 4;
16         unsigned char *buf = screen->pixels;
17
18         buf[offset++] = b;
19         buf[offset++] = g;
20         buf[offset]   = r;
21 }
22
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)
27 {
28         struct vector dir, v;
29         float len;
30         int i;
31
32         vector_sub(p2, p1, &dir);
33
34         len = vector_abs(&dir);
35         vector_scale(&dir, 1 / len, &dir);
36         len *= cam->zoom;
37
38         vector_sub(p1, &cam->pos, &v);
39         vector_scale(&v, cam->zoom, &v);
40
41         v.x += screen->w / 2;
42         v.y += screen->h / 2;
43
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);
49         }
50 }
51
52 static void reshape_planet(struct planet *p)
53 {
54         p->radius = pow(p->mass / 100, 1 / 3.0);
55 }
56
57 void init_planet(struct planet *p)
58 {
59         memset(p, 0, sizeof(*p));
60         reshape_planet(p);
61         p->r = get_random() % 256;
62         p->g = get_random() % 256;
63         p->b = get_random() % 256;
64
65         INIT_LIST_HEAD(&p->list);
66         init_quadtree(&p->tree);
67 }
68
69 /**
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
75  */
76
77 static void setup_planet(struct planet *p, double mass, double total_mass,
78                          double radius)
79 {
80         double angle = M_PI * 2 * get_random_double();
81         double velocity;
82         double distance;
83
84         distance = radius * pow(get_random_double(), 2);
85         velocity = sqrt(total_mass / radius);
86
87         velocity *= pow(distance / radius, 0.2);
88
89         p->tree.pos.x = cos(angle) * distance;
90         p->tree.pos.y = sin(angle) * distance;
91
92         p->speed.x = -sin(angle) * velocity;
93         p->speed.y = cos(angle) * velocity;
94
95         p->mass = mass;
96
97         reshape_planet(p);
98 }
99
100 void create_planets(struct planet *p, int num, double total_mass, double radius)
101 {
102         int i;
103         double sum = 0;
104         struct planet *new_planet;
105
106         setup_planet(p,
107                      total_mass / num * 2 * get_random_double(),
108                      total_mass,
109                      radius);
110
111         for (i = 0; i < num; i++) {
112                 new_planet = malloc(sizeof(*new_planet));
113                 init_planet(new_planet);
114
115                 list_add(&new_planet->list, &p->list);
116
117                 setup_planet(new_planet,
118                              total_mass / num * 2 * get_random_double(),
119                              total_mass,
120                              radius);
121
122                 quadtree_add(&p->tree, &new_planet->tree, &planet_ops);
123
124                 sum += new_planet->mass;
125         }
126 }
127
128 void draw_circle(SDL_Surface *screen, const struct vector *pos,
129                  const double radius,
130                  char r, char g, char b, int transparent)
131 {
132         int x, x_start, y, x_end;
133         float r2 = radius * radius;
134
135         y = MAX(pos->y - radius, 0);
136
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,
141                                  r, g, b);
142                 return;
143         }
144
145         for (; y < MIN(pos->y + radius, screen->h); y++) {
146                 int offset;
147                 unsigned char *buf = screen->pixels;
148                 float y2 = (y - pos->y);
149
150                 y2 = sqrt(r2 - y2 * y2);
151                 x_start = pos->x - y2;
152                 x_end = pos->x + y2;
153                 x_start = MAX(0, x_start);
154                 x_end = MIN(x_end, screen->w);
155
156                 offset = y * screen->pitch + x_start * 4;
157                 if (transparent) {
158                         for (x = x_start; x < x_end; x++) {
159                                 buf[offset++] += b;
160                                 buf[offset++] += g;
161                                 buf[offset++] += r;
162                                 offset++;
163                         }
164                 } else {
165                         for (x = x_start; x < x_end; x++) {
166                                 buf[offset++] = b;
167                                 buf[offset++] = g;
168                                 buf[offset++] = r;
169                                 offset++;
170                         }
171                 }
172         }
173 }
174
175 void draw_planet(SDL_Surface *screen, struct planet *p,
176                  const struct camera *cam)
177 {
178         struct vector pos;
179         float radius = p->radius * cam->zoom;
180         int i;
181
182         vector_sub(&p->tree.pos, &cam->pos, &pos);
183         vector_scale(&pos, cam->zoom, &pos);
184         pos.x += screen->w / 2;
185         pos.y += screen->h / 2;
186
187         if (draw_lines) {
188                 for (i = 0; i < 4; i++) {
189                         if (!p->tree.child[i])
190                                 continue;
191
192                         struct planet *q = tree_to_planet(p->tree.child[i]);
193
194                         draw_line(screen, &p->tree.pos, &q->tree.pos,
195                                   p->r, p->g, p->b, cam);
196                 }
197         }
198
199         draw_circle(screen, &pos, radius, p->r, p->g, p->b, 0);
200 }
201
202 int gravitize_planets(struct planet *a, struct planet *b, const double time)
203 {
204         struct vector distance, sum;
205         double dist, f, acc;
206
207         vector_sub(&a->tree.pos, &b->tree.pos, &distance);
208
209         dist = vector_abs(&distance);
210
211         /* Return true in case of a collision */
212         if (dist < (a->radius + b->radius))
213                 return 1;
214
215         vector_div(&distance, dist, &distance);
216
217         f = a->mass * b->mass / (dist * dist) * time;
218
219         acc = f / b->mass;
220         vector_scale(&distance, acc, &sum);
221         vector_add(&b->speed, &sum, &b->speed);
222
223         acc = f / a->mass;
224         vector_scale(&distance, acc, &sum);
225         vector_sub(&a->speed, &sum, &a->speed);
226
227         return 0;
228 }
229
230 /*
231  * Merge planets a and b into planet a
232  *
233  * It is left for the caller to deal with the scrap planet b
234  */
235 static void _merge_planets(struct planet *a, struct planet *b)
236 {
237         struct vector pa, pb, p;
238         float mass;
239
240         vector_scale(&a->speed, a->mass, &pa);
241         vector_scale(&b->speed, b->mass, &pb);
242         vector_add(&pa, &pb, &p);
243         mass = a->mass + b->mass;
244
245         if (a->mass < b->mass)
246                 a->tree.pos = b->tree.pos;
247
248         a->r = (a->r * a->mass + b->r * b->mass) / mass;
249         a->g = (a->g * a->mass + b->g * b->mass) / mass;
250         a->b = (a->b * a->mass + b->b * b->mass) / mass;
251
252         a->mass = mass;
253         reshape_planet(a);
254         vector_div(&p, a->mass, &a->speed);
255 }
256
257 /*
258  * Merge planets a and b into a the new planet a, which pointer is
259  * returned to the caller. Planet b is removed from the linked list
260  * and it's memory is freed. The merged planet will retain in the
261  * list.
262  */
263 struct planet *merge_planets(struct planet *a, struct planet *b)
264 {
265         _merge_planets(a, b);
266
267         list_del(&b->list);
268         quadtree_del(&b->tree, &planet_ops);
269
270         free(b);
271         return a;
272 }
273
274 struct planet *move_planet(struct planet *p, const double time)
275 {
276         struct vector tmp, new_pos;
277
278         vector_scale(&p->speed, time, &tmp);
279         vector_add(&p->tree.pos, &tmp, &new_pos);
280
281         return tree_to_planet(quadtree_move(&p->tree, new_pos, &planet_ops));
282 }
283
284 void print_planet(const struct planet *p)
285 {
286         printf("pos: (%f,%f), speed: (%f,%f), mass: %f, radius %f\n",
287                p->tree.pos.x, p->tree.pos.y, p->speed.x, p->speed.y, p->mass, p->radius);
288 }
289
290 int planet_search_rectangular(struct quadtree *node,
291                               struct quadtree_iterator *itr)
292 {
293         struct planet_search_iterator *it = qt_itr_to_planet_itr(itr);
294         struct planet *p = tree_to_planet(node);
295         int direction = 0, i;
296         int up = 0, left = 0, right = 0, down = 0;
297
298         for (i = 0; i < 2; i++) {
299                 if (it->limit[i].x < p->tree.pos.x)
300                         left = 1;
301                 else
302                         right = 1;
303                 if (it->limit[i].y < p->tree.pos.y)
304                         up = 1;
305                 else
306                         down = 1;
307         }
308
309         if (left && up)
310                 direction |= QUADTREE_UPLEFT;
311         if (right && up)
312                 direction |= QUADTREE_UPRIGHT;
313         if (left && down)
314                 direction |= QUADTREE_DOWNLEFT;
315         if (right && down)
316                 direction |= QUADTREE_DOWNRIGHT;
317         if (direction == (QUADTREE_UPLEFT | QUADTREE_UPRIGHT |
318                           QUADTREE_DOWNLEFT | QUADTREE_DOWNRIGHT))
319                 direction |= QUADTREE_SELF;
320
321         return direction;
322 }
323
324 void planet_draw_iterator(struct quadtree *node, struct quadtree_iterator *it)
325 {
326         struct planet *p = tree_to_planet(node);
327         struct planet_search_iterator *i = qt_itr_to_planet_itr(it);
328
329         draw_planet(i->screen, p, i->cam);
330 }
331
332 static void planet_recalculate_stats(struct quadtree *node)
333 {
334         struct planet *p = tree_to_planet(node), *c;
335         struct vector vect;
336         double dist;
337         int i;
338
339         p->tree_area = 0;
340         p->tree_mass = p->mass;
341
342         for (i = 0; i < 4; i++) {
343                 if (!node->child[i])
344                         continue;
345
346                 c = tree_to_planet(node->child[i]);
347                 p->tree_mass += c->tree_mass;
348
349                 vector_sub(&p->tree.pos, &c->tree.pos, &vect);
350                 dist = vector_abs(&vect);
351                 dist += c->tree_area;
352                 p->tree_area = MAX(p->tree_area, dist);
353         }
354 }
355
356 struct quadtree_ops planet_ops = {
357         .recalculate_stats = planet_recalculate_stats,
358 };
359