}
/*
- * Merge planets a and b into planet a
+ * Merge planets a and b together
*
- * It is left for the caller to deal with the scrap planet b
+ * The smaller planet will be deleted, matter will be transferred to
+ * the bigger planet.
*/
-static void _merge_planets(struct planet *a, struct planet *b)
+static struct planet *_merge_planets(struct planet *a, struct planet *b)
{
+ struct planet *rem, *nr;
struct vector pa, pb, p;
float mass;
vector_add(&pa, &pb, &p);
mass = a->mass + b->mass;
- //if (a->mass < b->mass)
- //a->tree.pos = b->tree.pos;
+ if (a->mass < b->mass) {
+ rem = a;
+ nr = a;
+ } else {
+ rem = b;
+ nr = a;
+ }
+
+ nr->r = (a->r * a->mass + b->r * b->mass) / mass;
+ nr->g = (a->g * a->mass + b->g * b->mass) / mass;
+ nr->b = (a->b * a->mass + b->b * b->mass) / mass;
- a->r = (a->r * a->mass + b->r * b->mass) / mass;
- a->g = (a->g * a->mass + b->g * b->mass) / mass;
- a->b = (a->b * a->mass + b->b * b->mass) / mass;
+ nr->mass = mass;
+ reshape_planet(nr);
+ vector_div(&p, nr->mass, &nr->speed);
- a->mass = mass;
- reshape_planet(a);
- vector_div(&p, a->mass, &a->speed);
+ return rem;
}
/*
- * Merge planets a and b into a the new planet a, which pointer is
- * returned to the caller. Planet b is removed from the linked list
- * and it's memory is freed. The merged planet will retain in the
- * list.
+ * Merge planets a and b into a the new planet, which pointer is
+ * returned to the caller. The other planet is removed from the linked
+ * list and it's memory is freed. The merged planet will be kept in
+ * the list.
*/
struct planet *merge_planets(struct planet *a, struct planet *b)
{
- _merge_planets(a, b);
+ struct planet *r;
+
+ r = _merge_planets(a, b);
//list_del(&b->list);
//quadtree_del(&b->tree, &planet_ops);
//free(b);
- b->to_be_deleted = 1;
- return a;
+ if (r == a)
+ a->to_be_deleted = 1;
+ else
+ b->to_be_deleted = 1;
+
+ return r;
}
static void gravitize_planet(struct planet *p, struct planet *pt, double time)
void planet_move_iterator(struct quadtree *node, struct quadtree_iterator *it)
{
- struct quadtree *parent;
struct planet_search_iterator *itr = qt_itr_to_planet_itr(it);
if (node == it->head)
return;