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) {
{
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;
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)
{
#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);