]> git.itanic.dy.fi Git - sdl-planets/commitdiff
planet.c: merge_planet: Keep quadtree spatially sorted
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Fri, 24 Dec 2010 21:11:08 +0000 (23:11 +0200)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Fri, 24 Dec 2010 21:11:08 +0000 (23:11 +0200)
If we move planet in the quadtree, we must call quadtree_move() in
order to keep the tree sorted. As the tree ordering might change, the
merge planet must also return the possibly new parent node of the
tree.

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

index 12d0b34571603cb719f751f6884f2429d29d4910..2c7d1435ff09fe8df676735e29f4ba2c3fe445d5 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -232,8 +232,9 @@ int gravitize_planets(struct planet *a, struct planet *b, const double time)
  *
  * It is left for the caller to deal with the scrap planet b
  */
-static void _merge_planets(struct planet *a, struct planet *b)
+static struct planet *_merge_planets(struct planet *a, struct planet *b)
 {
+       struct quadtree *parent = &a->tree;
        struct vector pa, pb, p;
        float mass;
 
@@ -243,7 +244,7 @@ static void _merge_planets(struct planet *a, struct planet *b)
        mass = a->mass + b->mass;
 
        if (a->mass < b->mass)
-               a->tree.pos = b->tree.pos;
+               parent = quadtree_move(&a->tree, b->tree.pos, &planet_ops);
 
        a->r = (a->r * a->mass + b->r * b->mass) / mass;
        a->g = (a->g * a->mass + b->g * b->mass) / mass;
@@ -252,6 +253,8 @@ static void _merge_planets(struct planet *a, struct planet *b)
        a->mass = mass;
        reshape_planet(a);
        vector_div(&p, a->mass, &a->speed);
+
+       return tree_to_planet(quadtree_find_parent(parent));
 }
 
 /*
@@ -262,13 +265,14 @@ static void _merge_planets(struct planet *a, struct planet *b)
  */
 struct planet *merge_planets(struct planet *a, struct planet *b)
 {
+       struct quadtree *p;
        _merge_planets(a, b);
 
        list_del(&b->list);
-       quadtree_del(&b->tree, &planet_ops);
+       p = quadtree_del(&b->tree, &planet_ops);
 
        free(b);
-       return a;
+       return tree_to_planet(p);
 }
 
 struct planet *move_planet(struct planet *p, const double time)