]> git.itanic.dy.fi Git - sdl-planets/commitdiff
planets: First attemt to keep quadtree in order while moving the planets
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Mon, 5 Apr 2010 12:37:07 +0000 (15:37 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Mon, 5 Apr 2010 12:37:07 +0000 (15:37 +0300)
There are no guarantees that the implementation actually works..

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
planet.c
planet.h

index 75674f4cb1e5e16ab80ed11f9137516711ff43ab..9cb991dbbb8f7f58c9af49459198f3e8a7bab171 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -79,14 +79,15 @@ void create_planets(struct planet *p, int num, double total_mass, double radius)
                init_planet(new_planet);
 
                list_add(&new_planet->list, &p->list);
-               quadtree_add(&new_planet->tree, &p->tree,
-                            planet_spatial_compare);
 
                setup_planet(new_planet,
                             total_mass / num * 2 * get_random_double(),
                             total_mass,
                             radius);
 
+               quadtree_add(&p->tree, &new_planet->tree,
+                            planet_spatial_compare);
+
                sum += new_planet->mass;
        }
 }
@@ -195,15 +196,23 @@ struct planet *merge_planets(struct planet *a, struct planet *b)
        _merge_planets(a, b);
 
        list_del(&b->list);
+       quadtree_del(&b->tree, planet_spatial_compare);
+
        free(b);
        return a;
 }
 
-void move_planet(struct planet *p, const double time)
+struct planet *move_planet(struct planet *p, const double time)
 {
        struct vector tmp;
+       struct quadtree *tree_parent;
+
        vector_scale(&p->speed, time, &tmp);
        vector_add(&p->pos, &tmp, &p->pos);
+
+       tree_parent = quadtree_del(&p->tree, planet_spatial_compare);
+       quadtree_add(tree_parent, &p->tree, planet_spatial_compare);
+       return tree_to_planet(tree_parent);
 }
 
 void print_planet(const struct planet *p)
index 0ab556bf067badea4e80e89bf3950dacfc0b8ed3..248615a83f122f41dd9b12e76b8222caad0527f9 100644 (file)
--- a/planet.h
+++ b/planet.h
@@ -28,7 +28,7 @@ void create_planets(struct planet *p, int num, double total_mass,
 void draw_planet(SDL_Surface *screen, struct planet *p, const struct camera *);
 int gravitize_planets(struct planet *a, struct planet *b, const double time);
 struct planet *merge_planets(struct planet *a, struct planet *b);
-void move_planet(struct planet *p, const double time);
+struct planet *move_planet(struct planet *p, const double time);
 void print_planet(const struct planet *p);
 
 int planet_spatial_compare(struct quadtree *a, struct quadtree *b);