Particle physics with force integration for orbital mechanics.
Particle physics with force integration for orbital mechanics.Particle physics simulates objects with forces that change over time. Unlike kinematics (constant acceleration), particles accumulate forces each frame and integrate to update velocity and position. Use particles when:
The particle loop: accumulate forces → integrate → clear forces → repeat This example simulates a solar system where gravitational forces constantly change as planets move, creating realistic orbital mechanics.
#include <stdlib.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define CANVAS_WIDTH SCREEN_WIDTH
#define CANVAS_HEIGHT SCREEN_HEIGHT
#define FB_PIXEL_MODE BJ_PIXEL_MODE_XRGB8888
#define G_SUN BJ_F(120.0)
#define SOFTENING BJ_F(6.0)
#define M_SUN BJ_F(1000.0)
#define M_MERCURY BJ_F(0.055)
#define M_VENUS BJ_F(0.815)
#define M_EARTH BJ_F(1.0)
#define M_MARS BJ_F(0.107)
#define M_JUPITER BJ_F(317.8)
typedef struct {
uint32_t color;
#define N_PLANETS 5
#define N_ASTEROIDS 800
}
}
}
}
const bj_real r = rmin + (rmax - rmin) * t;
}
}
}
}
}
}
}
}
}
static void*
setup(
struct bj_app* app,
void* init_data) {
(void)init_data;
srand((unsigned)time(NULL));
return 0;
}
return 0;
}
void* user_data
) {
(void)w; (void)dirty; (void)user_data;
}
(void)app; (void)user_data;
}
static void step(
struct bj_app* app,
struct bj_tick_info tick,
void* user_data) {
(void)tick; (void)user_data;
}
}
static void teardown(
struct bj_app* app,
void* user_data) {
(void)user_data;
}
int main(
int argc,
char* argv[]) {
(void)argc; (void)argv;
}
Application lifecycle: callback-driven setup, step, and teardown.
Assertion facility for Banjo API.
int main(int argc, char *argv[])
static void step(struct bj_app *app, struct bj_tick_info tick, void *user_data)
static void teardown(struct bj_app *app, void *user_data)
static void * setup(struct bj_app *app, void *init_data)
Header file for Bitmap type.
static void on_draw(struct bj_window *w, struct bj_render_target *target, const struct bj_rect *dirty, void *user_data)
Header file for Bitmap drawing functions.
void draw(bj_bitmap *bmp)
Sytem event management API.
int bj_run_app(bj_app_setup_fn setup, bj_app_step_fn step, bj_app_fixed_step_fn fixed_step, bj_app_teardown_fn teardown, void *init_data)
Drive the application lifecycle.
void bj_set_fixed_step_rate(struct bj_app *app, int hz)
Configure the fixed-step rate (in Hz) for app.
void bj_quit_app(struct bj_app *app, int exit_code)
Signal the given application to exit on the next iteration.
Timing snapshot handed to the step and fixed-step callbacks.
void bj_clear_bitmap(struct bj_bitmap *bitmap)
Fills the entire bitmap with the clear colour.
void bj_put_pixel(struct bj_bitmap *bitmap, size_t x, size_t y, uint32_t value)
Change the pixel colour at given coordinate.
struct bj_render_target bj_render_target
struct bj_bitmap bj_bitmap
struct bj_window bj_window
void bj_draw_filled_circle(struct bj_bitmap *bitmap, int cx, int cy, int radius, uint32_t color)
Draw a filled circle onto a bitmap.
void bj_dispatch_events(void)
Poll and dispatch all pending events.
void bj_close_on_escape(struct bj_window *window, const struct bj_key_event *event, void *user_data)
Handle the ESC key to close a window.
bj_key_callback_fn bj_set_key_callback(bj_key_callback_fn callback, void *user_data)
Set the global callback for keyboard key events.
#define BJ_TAU
TAU in the selected bj_real precision.
static struct bj_vec3 bj_mat3_transform_vec3(const struct bj_mat3x3 *restrict M, struct bj_vec3 v)
Multiply a 3×3 matrix by a 3D vector: r = M * v.
#define BJ_VEC2_ZERO
Zero 2D vector literal {0, 0}.
static void bj_mat3_set_ortho(struct bj_mat3x3 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t)
Build a 2D orthographic projection into a 3×3 matrix.
#define BJ_FZERO
Zero constant in bj_real.
static void bj_mat3_mul(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
Matrix product: out = A * B.
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
#define bj_sqrt
Square root.
float bj_real
Selected real type for float configuration.
static void bj_mat3_set_viewport(struct bj_mat3x3 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
Build a 2D viewport transform into a 3×3 matrix.
Axis-aligned rectangle: a top-left corner plus a width and height.
3D vector of bj_real components.
void bj_step_particle_2d(struct bj_particle_2d *particle, bj_real dt)
Semi-implicit Euler step for a particle.
void bj_apply_point_gravity_softened_2d(struct bj_particle_2d *restrict particle_from, const struct bj_particle_2d *restrict particle_to, const bj_real gravity_factor, const bj_real epsilon)
Apply softened point gravity to avoid singularities at small r.
2D point mass state and physical properties.
uint32_t bj_get_pixel_value(enum bj_pixel_mode mode, uint8_t red, uint8_t green, uint8_t blue)
Returns an opaque value representing a pixel colour, given its RGB composition.
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
void bj_set_draw_callback(struct bj_window *window, bj_window_draw_fn fn, void *user_data)
Register the redraw callback for window.
static void bj_invalidate_window(struct bj_window *window)
Mark the whole window as needing a repaint.
struct bj_window * bj_bind_window(const char *title, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t flags, struct bj_error **error)
Create a new struct bj_window with the specified attributes.
struct bj_bitmap * bj_render_target_bitmap(struct bj_render_target *target)
Reach the software framebuffer behind a render target.
bj_bool bj_should_close_window(struct bj_window *window)
Get the close flag state of a window.
void bj_unbind_window(struct bj_window *window)
Deletes a struct bj_window object and releases associated memory.
static void fixed_step(struct bj_app *app, struct bj_tick_info tick, void *ud)
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Matrix types and operations for 2D and 3D transforms.
Physics helpers (SI units, but dimensionally consistent with any unit system).
Physics helpers (SI units, but dimensionally consistent with any unit system).
static void update_projection()
static void init_planet(planet_t *p, bj_real r, bj_real mass, uint32_t color, bj_real draw_r, bj_real phase)
static void init_asteroids()
static bj_real orbital_speed_soft(bj_real G, bj_real M, bj_real r, bj_real eps)
bj_particle_2d asteroids[800]
static void physics(bj_real dt)
Header file for general pixel manipulation facilities.
Header file for system interactions.
Header file for time manipulation utilities.
Fixed-size vector types (2D, 3D, 4D) and inline operations.
Header file for bj_window type.