]> git.itanic.dy.fi Git - sdl-planets/commitdiff
gravitize_planets: Return true in even of a collision
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 7 Mar 2010 11:18:19 +0000 (13:18 +0200)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 7 Mar 2010 11:18:19 +0000 (13:18 +0200)
Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
planet.c
planet.h

index 99e6bfad1167e4beec0e6ac83506d38245469f7b..5c3fdaa33e337c64dd1c214ff97d9bdd1309284e 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -45,7 +45,7 @@ void draw_planet(SDL_Surface *screen, struct planet *p)
                        putpixel(screen, x, y, p->r, p->g, p->b);
 }
 
-void gravitize_planets(struct planet *a, struct planet *b, const double time)
+int gravitize_planets(struct planet *a, struct planet *b, const double time)
 {
        struct vector distance, sum;
        double dist, f, acc;
@@ -53,6 +53,11 @@ void gravitize_planets(struct planet *a, struct planet *b, const double time)
        vector_sub(&a->pos, &b->pos, &distance);
 
        dist = vector_abs(&distance);
+
+       /* Return true in case of a collision */
+       if (dist < (a->size + b->size))
+               return 1;
+
        vector_div(&distance, dist, &distance);
 
        f = a->mass * b->mass / (dist * dist + 5) * time;
@@ -64,6 +69,8 @@ void gravitize_planets(struct planet *a, struct planet *b, const double time)
        acc = f / a->mass;
        vector_scale(&distance, acc, &sum);
        vector_sub(&a->speed, &sum, &a->speed);
+
+       return 0;
 }
 
 void move_planet(struct planet *p, const double time)
index ac0d8e37d77412750ce6395d020dfbae9edd8daf..78cf937ef6626bd4aaa1baa2447dcf48643efbd5 100644 (file)
--- a/planet.h
+++ b/planet.h
@@ -20,7 +20,7 @@ struct planet {
 
 void init_planet(const SDL_Surface *screen, struct planet *p);
 void draw_planet(SDL_Surface *screen, struct planet *p);
-void gravitize_planets(struct planet *a, struct planet *b, const double time);
+int gravitize_planets(struct planet *a, struct planet *b, const double time);
 void move_planet(struct planet *p, const double time);
 void print_planet(const struct planet *p);