]> git.itanic.dy.fi Git - sdl-planets/commitdiff
Generate plantes in a "starsystem" like form
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 20 Mar 2010 13:14:48 +0000 (15:14 +0200)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 20 Mar 2010 13:14:48 +0000 (15:14 +0200)
Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
main.c
planet.c
planet.h

diff --git a/main.c b/main.c
index f9c587576b6b7e1a6023e90864cde1580430c654..e18621b844ba441e983b551db9a72cba0651a252 100644 (file)
--- a/main.c
+++ b/main.c
@@ -105,13 +105,7 @@ static void loop(SDL_Surface *screen, int num_of_planets)
 
        planet = malloc(sizeof(*planet));
        init_planet(planet);
-
-       for (i = 1; i < num_of_planets; i++) {
-               struct planet *new_planet;
-               new_planet = malloc(sizeof(*planet));
-               init_planet(new_planet);
-               list_add(&new_planet->list, &planet->list);
-       }
+       create_planets(planet, num_of_planets, 50000, 500);
 
        ticks = SDL_GetTicks();
        while (1) {
index 166d8400f1c3269a2640850f21b8e8fdc16bcd35..eb2881b50649c1c0daada225f064de42e29743fa 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -25,9 +25,9 @@ void init_planet(struct planet *p)
 {
        p->speed.x = 0;
        p->speed.y = 0;
-       p->pos.x = (signed)(get_random() % 1000) - 500;
-       p->pos.y = (signed)(get_random() % 1000) - 500;
-       p->mass = get_random() % 1000 + 100;
+       p->pos.x = 0;
+       p->pos.y = 0;
+       p->mass = 0;
        reshape_planet(p);
        p->r = get_random() % 256;
        p->g = get_random() % 256;
@@ -36,6 +36,58 @@ void init_planet(struct planet *p)
        INIT_LIST_HEAD(&p->list);
 }
 
+/**
+ * setup_planet - set the planet on a "solarsystem"
+ * @p:         pointer to struct planet to set up
+ * @mass:      mass of the planet to set up
+ * @total_mass: total mass of the system
+ * @radius:    maximum radius of the system
+ */
+
+static void setup_planet(struct planet *p, double mass, double total_mass,
+                        double radius)
+{
+       double angle = M_PI * 2 * get_random_double();
+       double velocity;
+       double distance;
+
+       distance = radius * get_random_double();
+       velocity = sqrt(total_mass / radius);
+
+       velocity *= pow(distance / radius, 0.2);
+
+       p->pos.x = cos(angle) * distance;
+       p->pos.y = sin(angle) * distance;
+
+       p->speed.x = -sin(angle) * velocity;
+       p->speed.y = cos(angle) * velocity;
+
+       p->mass = mass;
+
+       reshape_planet(p);
+}
+
+void create_planets(struct planet *p, int num, double total_mass, double radius)
+{
+       int i;
+       double sum = 0;
+       struct planet *new_planet;
+
+       for (i = 1; i < num; i++) {
+               new_planet = malloc(sizeof(*new_planet));
+               init_planet(new_planet);
+               list_add(&new_planet->list, &p->list);
+               setup_planet(new_planet,
+                            total_mass / num * 2 * get_random_double(),
+                            total_mass,
+                            radius);
+
+               sum += new_planet->mass;
+       }
+
+       printf("Requested mass: %f, got %f\n", total_mass, sum);
+}
+
 void draw_planet(SDL_Surface *screen, struct planet *p,
                 const struct camera *cam)
 {
index 30934f5be0c2e454b9220b8a40d60cf39e04ad81..4b6d3f42e4d87ec3c1f1ad08578b54dcd425b1e8 100644 (file)
--- a/planet.h
+++ b/planet.h
@@ -20,6 +20,8 @@ struct planet {
 #define list_to_planet(list_head) container_of((list_head), struct planet, list)
 
 void init_planet(struct planet *p);
+void create_planets(struct planet *p, int num, double total_mass,
+                   double radius);
 void draw_planet(SDL_Surface *screen, struct planet *p, const struct camera *);
 int gravitize_planets(struct planet *a, struct planet *b, const double time);
 struct planet *merge_planets(struct planet *a, struct planet *b);