]> git.itanic.dy.fi Git - sdl-planets/commitdiff
Crash and burn
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 10 Apr 2010 18:36:19 +0000 (21:36 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 10 Apr 2010 18:36:19 +0000 (21:36 +0300)
planet.c

index a51d3cb0f2f39b9dda1a2a15b7a1b409eba9e5c8..6172cdbf4281de696a945ab61b43a40a2ab562e2 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -209,16 +209,31 @@ struct planet *merge_planets(struct planet *a, struct planet *b)
 
 struct planet *move_planet(struct planet *p, const double time)
 {
-       struct vector tmp;
+       struct vector tmp, new_pos;
        struct quadtree *tree_parent;
+       struct planet_search_iterator it;
 
        vector_scale(&p->speed, time, &tmp);
-       vector_add(&p->pos, &tmp, &p->pos);
+       vector_add(&p->pos, &tmp, &new_pos);
        vector_scale(&p->speed, pow(0.99, time), &p->speed);
 
-       tree_parent = quadtree_del(&p->tree, planet_spatial_compare);
-       quadtree_add(tree_parent, &p->tree, planet_spatial_compare);
-       return tree_to_planet(tree_parent);
+       /* 
+        * Update the quadtree if there are any planets between the
+        * old and new position. If there is nothing between the old
+        * and new location, there is no need to modify the tree at
+        * all.
+        */
+       it.limit[0] = p->pos;
+       it.limit[1] = new_pos;
+       it.qt_iterator.head = quadtree_find_parent(&p->tree);
+
+       if (walk_quadtree(&it.qt_iterator)) {
+               tree_parent = quadtree_del(&p->tree, planet_spatial_compare);
+               quadtree_add(tree_parent, &p->tree, planet_spatial_compare);
+               return tree_to_planet(tree_parent);
+       }
+
+       return tree_to_planet(it.qt_iterator.head);
 }
 
 void print_planet(const struct planet *p)