star-sim

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

commit 0081d6f2d6c3e20c7bb6e37ad4984f9ba3a39fbb
parent f258d192d0be5a79c9c4c28bf5010eec187dd33f
Author: amin <dev@aminmesbah.com>
Date:   Sat,  5 Aug 2017 08:32:48 +0000

Add the ability to pan.

Uses simple delta x and delta y values to offsets the location of each
rendered pixel. Note that this doesn't play nice with trace mode.

FossilOrigin-Name: 98baf0a02db9dbb484a44b6ff33ec31e09ebc8c6182586f9978aa803f76bf2bc
Diffstat:
MREADME.md | 3+++
Msrc/platform_sdl.c | 41+++++++++++++++++++++++++++++++++++++++++
Msrc/star_garden.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++--------------------
Msrc/star_garden.h | 11+++++++++--
4 files changed, 102 insertions(+), 22 deletions(-)

diff --git a/README.md b/README.md @@ -10,6 +10,9 @@ efficiently calculate the forces acting on each star. ### Keyboard Shortcuts +- `WASD`: Pan. +- `Arrow Keys`: Slow Pan. +- `Home`: Center View. - `b`: Toggle bounding box rendering. - `f`: Toggle brute-force mode. Stars turn blue when this mode is active, and gravitational attraction is calculated for every single pair of stars. The diff --git a/src/platform_sdl.c b/src/platform_sdl.c @@ -204,6 +204,47 @@ int main(int argc, char *argv[]) SDL_PumpEvents(); dimension = sdl_get_window_dimension(window); + + const uint8_t *keystate = SDL_GetKeyboardState(0); + + if (keystate[SDL_SCANCODE_A]) + { + sim_state.view.dx += 5; + } + if (keystate[SDL_SCANCODE_D]) + { + sim_state.view.dx -= 5; + } + if (keystate[SDL_SCANCODE_W]) + { + sim_state.view.dy += 5; + } + if (keystate[SDL_SCANCODE_S]) + { + sim_state.view.dy -= 5; + } + if (keystate[SDL_SCANCODE_LEFT]) + { + sim_state.view.dx++; + } + if (keystate[SDL_SCANCODE_RIGHT]) + { + sim_state.view.dx--; + } + if (keystate[SDL_SCANCODE_UP]) + { + sim_state.view.dy++; + } + if (keystate[SDL_SCANCODE_DOWN]) + { + sim_state.view.dy--; + } + if (keystate[SDL_SCANCODE_HOME]) + { + sim_state.view.dx = 0; + sim_state.view.dy = 0; + } + struct OffscreenBuffer buffer; // WARNING: these pointers are aliased until the end of the // loop diff --git a/src/star_garden.c b/src/star_garden.c @@ -18,11 +18,15 @@ void sim_init(struct SimState *sim_state, int field_width, int field_height) } sim_state->num_stars = NUM_STARS; + sim_state->bounding_box.center_x = field_width / 2; sim_state->bounding_box.center_y = field_height / 2; sim_state->bounding_box.side_length_x = 0; sim_state->bounding_box.side_length_y = 0; + sim_state->view.dx = 0; + sim_state->view.dy = 0; + float min_x = field_width / 2; float max_x = field_width / 2; float min_y = field_height / 2; @@ -177,6 +181,8 @@ void sim_render(struct OffscreenBuffer *buffer, float dt, struct SimState *sim_s return; } + float dx = sim_state->view.dx; + float dy = sim_state->view.dy; int num_stars = sim_state->num_stars; struct Star *stars = sim_state->stars; struct QuadTree *qt = sim_state->qt; @@ -187,8 +193,8 @@ void sim_render(struct OffscreenBuffer *buffer, float dt, struct SimState *sim_s { sim_set_pixel( buffer, - stars[i].x + (sinf(stars[i].angle) * stars[i].speed) * dt, - stars[i].y - (cosf(stars[i].angle) * stars[i].speed) * dt, + (stars[i].x + (sinf(stars[i].angle) * stars[i].speed) * dt) + dx, + (stars[i].y - (cosf(stars[i].angle) * stars[i].speed) * dt) + dy, COLOR_CYAN); } } @@ -198,19 +204,19 @@ void sim_render(struct OffscreenBuffer *buffer, float dt, struct SimState *sim_s { sim_set_pixel( buffer, - stars[i].x + (sinf(stars[i].angle) * stars[i].speed) * dt, - stars[i].y - (cosf(stars[i].angle) * stars[i].speed) * dt, + (stars[i].x + (sinf(stars[i].angle) * stars[i].speed) * dt) + dx, + (stars[i].y - (cosf(stars[i].angle) * stars[i].speed) * dt) + dy, stars[i].color); } } if (RENDER_GRID) { - sim_grid_render(buffer, qt->root, COLOR_GREEN); + sim_grid_render(buffer, qt->root, COLOR_GREEN, dx, dy); } if (RENDER_BOUNDING_BOX) { - sim_bounding_box_render(buffer, &sim_state->bounding_box, COLOR_RED); + sim_bounding_box_render(buffer, &sim_state->bounding_box, COLOR_RED, dx, dy); } } @@ -237,7 +243,7 @@ void sim_set_pixel(struct OffscreenBuffer *buffer, uint32_t x, uint32_t y, uint3 } -void sim_grid_render(struct OffscreenBuffer *buffer, struct QuadTreeNode *node, uint32_t color) +void sim_grid_render(struct OffscreenBuffer *buffer, struct QuadTreeNode *node, uint32_t color, float dx, float dy) { if (!buffer) { @@ -251,25 +257,25 @@ void sim_grid_render(struct OffscreenBuffer *buffer, struct QuadTreeNode *node, x <= (node->cell->center_x + node->cell->distance_x); ++x) { - sim_set_pixel(buffer, x, node->cell->center_y, color); + sim_set_pixel(buffer, x + dx, node->cell->center_y + dy, color); } for (int y = (node->cell->center_y - node->cell->distance_y); y <= (node->cell->center_y + node->cell->distance_y); ++y) { - sim_set_pixel(buffer, node->cell->center_x, y, color); + sim_set_pixel(buffer, node->cell->center_x + dx, y + dy, color); } - sim_grid_render(buffer, node->ne, color); - sim_grid_render(buffer, node->nw, color); - sim_grid_render(buffer, node->sw, color); - sim_grid_render(buffer, node->se, color); + sim_grid_render(buffer, node->ne, color, dx, dy); + sim_grid_render(buffer, node->nw, color, dx, dy); + sim_grid_render(buffer, node->sw, color, dx, dy); + sim_grid_render(buffer, node->se, color, dx, dy); } } -void sim_bounding_box_render(struct OffscreenBuffer *buffer, struct SimBounds *bounding_box, uint32_t color) +void sim_bounding_box_render(struct OffscreenBuffer *buffer, struct SimBounds *bounding_box, uint32_t color, float dx, float dy) { if (!buffer) { @@ -284,13 +290,20 @@ void sim_bounding_box_render(struct OffscreenBuffer *buffer, struct SimBounds *b x <= bounding_box->center_x + reticle_radius; x++) { - sim_set_pixel(buffer, x, bounding_box->center_y, color); + sim_set_pixel( + buffer, x + dx, + bounding_box->center_y + dy, + color); } for (int y = bounding_box->center_y - reticle_radius; y <= bounding_box->center_y + reticle_radius; y++) { - sim_set_pixel(buffer, bounding_box->center_x, y, color); + sim_set_pixel( + buffer, + bounding_box->center_x + dx, + y + dy, + color); } float half_length_x = bounding_box->side_length_x / 2; @@ -300,16 +313,32 @@ void sim_bounding_box_render(struct OffscreenBuffer *buffer, struct SimBounds *b x <= (bounding_box->center_x + half_length_x); ++x) { - sim_set_pixel(buffer, x, bounding_box->center_y - half_length_y, color); - sim_set_pixel(buffer, x, bounding_box->center_y + half_length_y, color); + sim_set_pixel( + buffer, + x + dx, + bounding_box->center_y - half_length_y + dy, + color); + sim_set_pixel( + buffer, + x + dx, + bounding_box->center_y + half_length_y + dy, + color); } for (int y = (bounding_box->center_y - half_length_y); y <= (bounding_box->center_y + half_length_y); ++y) { - sim_set_pixel(buffer, bounding_box->center_x - half_length_x, y, color); - sim_set_pixel(buffer, bounding_box->center_x + half_length_x, y, color); + sim_set_pixel( + buffer, + bounding_box->center_x - half_length_x + dx, + y + dy, + color); + sim_set_pixel( + buffer, + bounding_box->center_x + half_length_x + dx, + y + dy, + color); } } } diff --git a/src/star_garden.h b/src/star_garden.h @@ -62,10 +62,17 @@ struct SimBounds float side_length_y; }; +struct SimView +{ + float dx; + float dy; +}; + struct SimState { int num_stars; struct SimBounds bounding_box; + struct SimView view; struct QuadTree *qt; struct Star stars[NUM_STARS]; }; @@ -84,8 +91,8 @@ void sim_update(struct SimState *sim_state, int field_width, int field_height); void sim_bounding_box_update(struct SimBounds *bounds, float min_x, float min_y, float max_x, float max_y); void sim_render(struct OffscreenBuffer *buffer, float dt, struct SimState *sim_state); void sim_set_pixel(struct OffscreenBuffer *buffer, uint32_t x, uint32_t y, uint32_t color); -void sim_bounding_box_render(struct OffscreenBuffer *buffer, struct SimBounds *bounding_box, uint32_t color); -void sim_grid_render(struct OffscreenBuffer *buffer, struct QuadTreeNode *node, uint32_t color); +void sim_bounding_box_render(struct OffscreenBuffer *buffer, struct SimBounds *bounding_box, uint32_t color, float dx, float dy); +void sim_grid_render(struct OffscreenBuffer *buffer, struct QuadTreeNode *node, uint32_t color, float dx, float dy); void sim_cleanup(struct SimState *sim_state); #define STAR_GARDEN_H