]> git.itanic.dy.fi Git - sdl-planets/commitdiff
Move position vector from planet to quadtree
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 30 Oct 2010 16:57:19 +0000 (19:57 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 30 Oct 2010 16:57:19 +0000 (19:57 +0300)
Moving the position information inside the quadtree structure will
make it eventually much easier to do varios tree operations within the
quadtree code. Most of the quadtree callback functions can be moved
inside the quadtree code. This patch implements just the first part of
the change, moving the coordinates inside quadtree.

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

index a79c085a8ef714d6e67bb10036247292929d1245..39f8c35b3a7e8084303bcdc76f04b263c6f724d5 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -86,8 +86,8 @@ static void setup_planet(struct planet *p, double mass, double total_mass,
 
        velocity *= pow(distance / radius, 0.2);
 
 
        velocity *= pow(distance / radius, 0.2);
 
-       p->pos.x = cos(angle) * distance;
-       p->pos.y = sin(angle) * distance;
+       p->tree.pos.x = cos(angle) * distance;
+       p->tree.pos.y = sin(angle) * distance;
 
        p->speed.x = -sin(angle) * velocity;
        p->speed.y = cos(angle) * velocity;
 
        p->speed.x = -sin(angle) * velocity;
        p->speed.y = cos(angle) * velocity;
@@ -179,7 +179,7 @@ void draw_planet(SDL_Surface *screen, struct planet *p,
        float radius = p->radius * cam->zoom;
        int i;
 
        float radius = p->radius * cam->zoom;
        int i;
 
-       vector_sub(&p->pos, &cam->pos, &pos);
+       vector_sub(&p->tree.pos, &cam->pos, &pos);
        vector_scale(&pos, cam->zoom, &pos);
        pos.x += screen->w / 2;
        pos.y += screen->h / 2;
        vector_scale(&pos, cam->zoom, &pos);
        pos.x += screen->w / 2;
        pos.y += screen->h / 2;
@@ -191,7 +191,7 @@ void draw_planet(SDL_Surface *screen, struct planet *p,
 
                        struct planet *q = tree_to_planet(p->tree.child[i]);
 
 
                        struct planet *q = tree_to_planet(p->tree.child[i]);
 
-                       draw_line(screen, &p->pos, &q->pos,
+                       draw_line(screen, &p->tree.pos, &q->tree.pos,
                                  p->r, p->g, p->b, cam);
                }
        }
                                  p->r, p->g, p->b, cam);
                }
        }
@@ -204,7 +204,7 @@ int gravitize_planets(struct planet *a, struct planet *b, const double time)
        struct vector distance, sum;
        double dist, f, acc;
 
        struct vector distance, sum;
        double dist, f, acc;
 
-       vector_sub(&a->pos, &b->pos, &distance);
+       vector_sub(&a->tree.pos, &b->tree.pos, &distance);
 
        dist = vector_abs(&distance);
 
 
        dist = vector_abs(&distance);
 
@@ -243,7 +243,7 @@ static void _merge_planets(struct planet *a, struct planet *b)
        mass = a->mass + b->mass;
 
        if (a->mass < b->mass)
        mass = a->mass + b->mass;
 
        if (a->mass < b->mass)
-               a->pos = b->pos;
+               a->tree.pos = b->tree.pos;
 
        a->r = (a->r * a->mass + b->r * b->mass) / mass;
        a->g = (a->g * a->mass + b->g * 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;
@@ -280,11 +280,11 @@ static int planet_search_when_moving(struct quadtree *node,
        int up = 0, left = 0, right = 0, down = 0;
 
        for (i = 0; i < 2; i++) {
        int up = 0, left = 0, right = 0, down = 0;
 
        for (i = 0; i < 2; i++) {
-               if (it->limit[i].x < p->pos.x)
+               if (it->limit[i].x < p->tree.pos.x)
                        left = 1;
                else
                        right = 1;
                        left = 1;
                else
                        right = 1;
-               if (it->limit[i].y < p->pos.y)
+               if (it->limit[i].y < p->tree.pos.y)
                        up = 1;
                else
                        down = 1;
                        up = 1;
                else
                        down = 1;
@@ -325,19 +325,19 @@ struct planet *move_planet(struct planet *p, const double time)
        int modify = 0;
 
        vector_scale(&p->speed, time, &tmp);
        int modify = 0;
 
        vector_scale(&p->speed, time, &tmp);
-       vector_add(&p->pos, &tmp, &new_pos);
+       vector_add(&p->tree.pos, &tmp, &new_pos);
 
        /* Check if we have crossed any of the parents */
        parent = p->tree.parent;
        while (parent) {
                pa = tree_to_planet(parent);
 
        /* Check if we have crossed any of the parents */
        parent = p->tree.parent;
        while (parent) {
                pa = tree_to_planet(parent);
-               if (p->pos.x < pa->pos.x && new_pos.x > pa->pos.x)
+               if (p->tree.pos.x < pa->tree.pos.x && new_pos.x > pa->tree.pos.x)
                        modify = 1;
                        modify = 1;
-               if (p->pos.x > pa->pos.x && new_pos.x < pa->pos.x)
+               if (p->tree.pos.x > pa->tree.pos.x && new_pos.x < pa->tree.pos.x)
                        modify = 1;
                        modify = 1;
-               if (p->pos.y < pa->pos.y && new_pos.y > pa->pos.y)
+               if (p->tree.pos.y < pa->tree.pos.y && new_pos.y > pa->tree.pos.y)
                        modify = 1;
                        modify = 1;
-               if (p->pos.y > pa->pos.y && new_pos.y < pa->pos.y)
+               if (p->tree.pos.y > pa->tree.pos.y && new_pos.y < pa->tree.pos.y)
                        modify = 1;
 
                if (!modify) {
                        modify = 1;
 
                if (!modify) {
@@ -346,7 +346,7 @@ struct planet *move_planet(struct planet *p, const double time)
                }
 
                tree_parent = quadtree_del(&p->tree, &planet_ops);
                }
 
                tree_parent = quadtree_del(&p->tree, &planet_ops);
-               p->pos = new_pos;
+               p->tree.pos = new_pos;
                quadtree_add(tree_parent, &p->tree, &planet_ops);
                return tree_to_planet(tree_parent);
        }
                quadtree_add(tree_parent, &p->tree, &planet_ops);
                return tree_to_planet(tree_parent);
        }
@@ -357,13 +357,13 @@ struct planet *move_planet(struct planet *p, const double time)
                 * them into correct place within the tree.
                 */
                it.qt_iterator.head = &p->tree;
                 * them into correct place within the tree.
                 */
                it.qt_iterator.head = &p->tree;
-               it.limit[0] = p->pos;
+               it.limit[0] = p->tree.pos;
                it.limit[1] = new_pos;
                it.qt_iterator.direction = planet_search_when_moving;
                it.qt_iterator.callback = planet_move_iterator;
                walk_quadtree(&it.qt_iterator);
        }
                it.limit[1] = new_pos;
                it.qt_iterator.direction = planet_search_when_moving;
                it.qt_iterator.callback = planet_move_iterator;
                walk_quadtree(&it.qt_iterator);
        }
-       p->pos = new_pos;
+       p->tree.pos = new_pos;
 
        return tree_to_planet(quadtree_find_parent(&p->tree));
 }
 
        return tree_to_planet(quadtree_find_parent(&p->tree));
 }
@@ -371,7 +371,7 @@ struct planet *move_planet(struct planet *p, const double time)
 void print_planet(const struct planet *p)
 {
        printf("pos: (%f,%f), speed: (%f,%f), mass: %f, radius %f\n",
 void print_planet(const struct planet *p)
 {
        printf("pos: (%f,%f), speed: (%f,%f), mass: %f, radius %f\n",
-              p->pos.x, p->pos.y, p->speed.x, p->speed.y, p->mass, p->radius);
+              p->tree.pos.x, p->tree.pos.y, p->speed.x, p->speed.y, p->mass, p->radius);
 }
 
 int planet_spatial_compare(struct quadtree *ta, struct quadtree *tb)
 }
 
 int planet_spatial_compare(struct quadtree *ta, struct quadtree *tb)
@@ -381,8 +381,8 @@ int planet_spatial_compare(struct quadtree *ta, struct quadtree *tb)
        a = tree_to_planet(ta);
        b = tree_to_planet(tb);
 
        a = tree_to_planet(ta);
        b = tree_to_planet(tb);
 
-       up = b->pos.y < a->pos.y;
-       left = b->pos.x < a->pos.x;
+       up = b->tree.pos.y < a->tree.pos.y;
+       left = b->tree.pos.x < a->tree.pos.x;
 
        if (up && left)
                return 0;
 
        if (up && left)
                return 0;
@@ -402,11 +402,11 @@ int planet_search_rectangular(struct quadtree *node,
        int up = 0, left = 0, right = 0, down = 0;
 
        for (i = 0; i < 2; i++) {
        int up = 0, left = 0, right = 0, down = 0;
 
        for (i = 0; i < 2; i++) {
-               if (it->limit[i].x < p->pos.x)
+               if (it->limit[i].x < p->tree.pos.x)
                        left = 1;
                else
                        right = 1;
                        left = 1;
                else
                        right = 1;
-               if (it->limit[i].y < p->pos.y)
+               if (it->limit[i].y < p->tree.pos.y)
                        up = 1;
                else
                        down = 1;
                        up = 1;
                else
                        down = 1;
@@ -452,7 +452,7 @@ static void planet_recalculate_stats(struct quadtree *node)
                c = tree_to_planet(node->child[i]);
                p->tree_mass += c->tree_mass;
 
                c = tree_to_planet(node->child[i]);
                p->tree_mass += c->tree_mass;
 
-               vector_sub(&p->pos, &c->pos, &vect);
+               vector_sub(&p->tree.pos, &c->tree.pos, &vect);
                dist = vector_abs(&vect);
                dist += c->tree_area;
                p->tree_area = MAX(p->tree_area, dist);
                dist = vector_abs(&vect);
                dist += c->tree_area;
                p->tree_area = MAX(p->tree_area, dist);
index 89f232ac3d706dcc9d7f924e73f1434c40d7b5d5..9f71b99fa679259ccae61692b83b4f2ed6d7755b 100644 (file)
--- a/planet.h
+++ b/planet.h
@@ -11,7 +11,6 @@
 
 struct planet {
        struct vector speed;
 
 struct planet {
        struct vector speed;
-       struct vector pos;
        struct list_head list;
        struct quadtree tree;
        double mass;
        struct list_head list;
        struct quadtree tree;
        double mass;
index 0784147d50c58264ee08b02296b59eea3bbde3b9..87f3b8c0ba2f536dc8b2276f47ec19fc315b13e8 100644 (file)
@@ -4,8 +4,10 @@
 #include <string.h>
 
 #include "utils.h"
 #include <string.h>
 
 #include "utils.h"
+#include "vector.h"
 
 struct quadtree {
 
 struct quadtree {
+       struct vector pos;
        struct quadtree *child[4];
        struct quadtree *parent;
 
        struct quadtree *child[4];
        struct quadtree *parent;