From f403584b331fc7deb8b67787fd00b0f7443f6fca Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Fri, 24 Dec 2010 23:11:08 +0200 Subject: [PATCH] planet.c: merge_planet: Keep quadtree spatially sorted 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 --- planet.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/planet.c b/planet.c index 12d0b34..2c7d143 100644 --- 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) -- 2.45.0