]> git.itanic.dy.fi Git - sdl-planets/blob - planet.c
Non-optimized version of quadtree is now working fully
[sdl-planets] / planet.c
1 #include <math.h>
2
3 #include "random.h"
4 #include "planet.h"
5 #include "utils.h"
6
7 static void putpixel(struct SDL_Surface *screen, const int x, const int y,
8                      const unsigned char r, const unsigned char g,
9                      const unsigned char b)
10 {
11         int offset = y * screen->pitch + x * 4;
12         unsigned char *buf = screen->pixels;
13
14         buf[offset++] = b;
15         buf[offset++] = g;
16         buf[offset]   = r;
17 }
18
19 static void reshape_planet(struct planet *p)
20 {
21         p->radius = pow(p->mass / 100, 1 / 3.0);
22 }
23
24 void init_planet(struct planet *p)
25 {
26         p->speed.x = 0;
27         p->speed.y = 0;
28         p->pos.x = 0;
29         p->pos.y = 0;
30         p->mass = 0;
31         reshape_planet(p);
32         p->r = get_random() % 256;
33         p->g = get_random() % 256;
34         p->b = get_random() % 256;
35
36         INIT_LIST_HEAD(&p->list);
37         init_quadtree(&p->tree);
38 }
39
40 /**
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
46  */
47
48 static void setup_planet(struct planet *p, double mass, double total_mass,
49                          double radius)
50 {
51         double angle = M_PI * 2 * get_random_double();
52         double velocity;
53         double distance;
54
55         distance = radius * pow(get_random_double(), 2);
56         velocity = sqrt(total_mass / radius);
57
58         velocity *= pow(distance / radius, 0.2);
59
60         p->pos.x = cos(angle) * distance;
61         p->pos.y = sin(angle) * distance;
62
63         p->speed.x = -sin(angle) * velocity;
64         p->speed.y = cos(angle) * velocity;
65
66         p->mass = mass;
67
68         reshape_planet(p);
69 }
70
71 void create_planets(struct planet *p, int num, double total_mass, double radius)
72 {
73         int i;
74         double sum = 0;
75         struct planet *new_planet;
76
77         setup_planet(p,
78                      total_mass / num * 2 * get_random_double(),
79                      total_mass,
80                      radius);
81
82         for (i = 0; i < num; i++) {
83                 new_planet = malloc(sizeof(*new_planet));
84                 init_planet(new_planet);
85
86                 list_add(&new_planet->list, &p->list);
87
88                 setup_planet(new_planet,
89                              total_mass / num * 2 * get_random_double(),
90                              total_mass,
91                              radius);
92
93                 quadtree_add(&p->tree, &new_planet->tree,
94                              planet_spatial_compare);
95
96                 sum += new_planet->mass;
97         }
98 }
99
100 void draw_planet(SDL_Surface *screen, struct planet *p,
101                  const struct camera *cam)
102 {
103         struct vector pos;
104         float radius = p->radius * cam->zoom;
105         float r2 = radius * radius;
106         int x, x_start, y, x_end;
107
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;
112
113         y = MAX(pos.y - radius, 0);
114
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,
119                                  p->r, p->g, p->b);
120                 return;
121         }
122
123         for (; y < MIN(pos.y + radius, screen->h); y++) {
124                 int offset;
125                 unsigned char *buf = screen->pixels;
126                 float y2 = (y - pos.y);
127
128                 y2 = sqrt(r2 - y2 * y2);
129                 x_start = pos.x - y2;
130                 x_end = pos.x + y2;
131                 x_start = MAX(0, x_start);
132                 x_end = MIN(x_end, screen->w);
133
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;
139                         offset++;
140                 }
141         }
142 }
143
144 int gravitize_planets(struct planet *a, struct planet *b, const double time)
145 {
146         struct vector distance, sum;
147         double dist, f, acc;
148
149         vector_sub(&a->pos, &b->pos, &distance);
150
151         dist = vector_abs(&distance);
152
153         /* Return true in case of a collision */
154         if (dist < (a->radius + b->radius))
155                 return 1;
156
157         vector_div(&distance, dist, &distance);
158
159         f = a->mass * b->mass / (dist * dist) * time;
160
161         acc = f / b->mass;
162         vector_scale(&distance, acc, &sum);
163         vector_add(&b->speed, &sum, &b->speed);
164
165         acc = f / a->mass;
166         vector_scale(&distance, acc, &sum);
167         vector_sub(&a->speed, &sum, &a->speed);
168
169         return 0;
170 }
171
172 /*
173  * Merge planets a and b into planet a
174  *
175  * It is left for the caller to deal with the scrap planet b
176  */
177 static void _merge_planets(struct planet *a, struct planet *b)
178 {
179         struct vector pa, pb, p;
180
181         vector_scale(&a->speed, a->mass, &pa);
182         vector_scale(&b->speed, b->mass, &pb);
183         vector_add(&pa, &pb, &p);
184
185         if (a->mass < b->mass)
186                 a->pos = b->pos;
187
188         a->mass += b->mass;
189         reshape_planet(a);
190         vector_div(&p, a->mass, &a->speed);
191 }
192
193 /*
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
197  * list.
198  */
199 struct planet *merge_planets(struct planet *a, struct planet *b)
200 {
201         _merge_planets(a, b);
202
203         list_del(&b->list);
204         quadtree_del(&b->tree, planet_spatial_compare);
205
206         free(b);
207         return a;
208 }
209
210 struct planet *move_planet(struct planet *p, const double time)
211 {
212         struct vector tmp;
213         struct quadtree *tree_parent;
214
215         vector_scale(&p->speed, time, &tmp);
216         vector_add(&p->pos, &tmp, &p->pos);
217
218         tree_parent = quadtree_del(&p->tree, planet_spatial_compare);
219         quadtree_add(tree_parent, &p->tree, planet_spatial_compare);
220         return tree_to_planet(tree_parent);
221 }
222
223 void print_planet(const struct planet *p)
224 {
225         printf("pos: (%f,%f), speed: (%f,%f), mass: %f, radius %f\n",
226                p->pos.x, p->pos.y, p->speed.x, p->speed.y, p->mass, p->radius);
227 }
228
229 int planet_spatial_compare(struct quadtree *ta, struct quadtree *tb)
230 {
231         struct planet *a, *b;
232         int up, left;
233         a = tree_to_planet(ta);
234         b = tree_to_planet(tb);
235
236         up = b->pos.y < a->pos.y;
237         left = b->pos.x < a->pos.x;
238
239         if (up && left)
240                 return 0;
241         if (up && !left)
242                 return 1;
243         if (left)
244                 return 2;
245         return 3;
246 }
247
248 int planet_search_rectangular(struct quadtree *node,
249                               struct quadtree_iterator *itr)
250 {
251         struct planet_search_iterator *it = qt_itr_to_planet_itr(itr);
252         struct planet *p = tree_to_planet(node);
253         int direction = 0, i;
254         int up = 0, left = 0, right = 0, down = 0;
255
256         for (i = 0; i < 2; i++) {
257                 if (it->limit[i].x < p->pos.x)
258                         left = 1;
259                 else
260                         right = 1;
261                 if (it->limit[i].y < p->pos.y)
262                         up = 1;
263                 else
264                         down = 1;
265         }
266
267         if (left && up)
268                 direction |= QUADTREE_UPLEFT;
269         if (right && up)
270                 direction |= QUADTREE_UPRIGHT;
271         if (left && down)
272                 direction |= QUADTREE_DOWNLEFT;
273         if (right && down)
274                 direction |= QUADTREE_DOWNRIGHT;
275         if (direction == (QUADTREE_UPLEFT | QUADTREE_UPRIGHT |
276                           QUADTREE_DOWNLEFT | QUADTREE_DOWNRIGHT))
277                 direction |= QUADTREE_SELF;
278
279         return direction;
280 }
281
282 void planet_draw_iterator(struct quadtree *node, struct quadtree_iterator *it)
283 {
284         struct planet *p = tree_to_planet(node);
285         struct planet_search_iterator *i = qt_itr_to_planet_itr(it);
286
287         draw_planet(i->screen, p, i->cam);
288 }