star-sim

Barnes-Hut gravity simulation.
git clone git://git.amin.space/star-sim.git
Log | Files | Refs | README | LICENSE

commit dcbbcfe4a96d152f53febe84ed130314e5d80d15
parent 14141ebd956b12819490ac9d043fc758547e2720
Author: amin <dev@aminmesbah.com>
Date:   Sat, 17 Jun 2017 05:40:28 +0000

Fix center-of-mass calculation.

FossilOrigin-Name: bfbf229909b5655811f94d8c6e9864817a7827ca2a0c9651f4ee24f3b5a7737d
Diffstat:
Mbarnes_hut.h | 17++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/barnes_hut.h b/barnes_hut.h @@ -237,20 +237,21 @@ void quad_tree_set_virtual_stars(struct QuadTreeNode *node, struct Star virtual_ } else if (*current_num < max_virtual_stars) { - // TODO: handle mass more intelligently float x_sum = 0; float y_sum = 0; float mass_sum = 0; for (int i = 0; i < node->cell->num_stars; ++i) { - x_sum += node->cell->stars[i]->x; - y_sum += node->cell->stars[i]->y; - mass_sum += node->cell->stars[i]->mass; + struct Star *s = node->cell->stars[i]; + + mass_sum += s->mass; + x_sum += s->x * s->mass; + y_sum += s->y * s->mass; } - float x_avg = x_sum / node->cell->num_stars; - float y_avg = y_sum / node->cell->num_stars; + float x_avg = x_sum / mass_sum; + float y_avg = y_sum / mass_sum; virtual_stars[*current_num].x = x_avg; virtual_stars[*current_num].y = y_avg; @@ -259,7 +260,9 @@ void quad_tree_set_virtual_stars(struct QuadTreeNode *node, struct Star virtual_ } else { - printf("Virtual_stars buffer overflow.\n"); + printf("Virtual_stars buffer overflow:\n\tcurrent_num: %d\n\tmax_virtual_stars: %d\n", + *current_num, + max_virtual_stars); } }