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.
#define BJ_AUTOMAIN_CALLBACKS
#include <stdlib.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define CANVAS_WIDTH SCREEN_WIDTH
#define CANVAS_HEIGHT SCREEN_HEIGHT
#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;
}
}
}
#define DT_CLAMP (BJ_F(1.0)/BJ_F(120.0))
}
}
}
}
}
}
int bj_app_begin(
void** user_data,
int argc,
char* argv[]) {
(void)user_data; (void)argc; (void)argv;
srand((unsigned)time(NULL));
return bj_callback_exit_error;
}
return bj_callback_continue;
}
(void)user_data;
? bj_callback_exit_success
: bj_callback_continue;
}
(void)user_data;
return status;
}
Assertion facility for Banjo API.
int bj_app_begin(void **user_data, int argc, char *argv[])
Definition audio_pcm.c:25
int bj_app_iterate(void *user_data)
Definition audio_pcm.c:60
int bj_app_end(void *user_data, int status)
Definition audio_pcm.c:84
Header file for Bitmap type.
bj_renderer * renderer
Definition bitmap_blit.c:25
bj_window * window
Definition bitmap_blit.c:24
Header file for Bitmap drawing functions.
Sytem event management API.
void bj_clear_bitmap(struct bj_bitmap *bitmap)
Fills the entire bitmap with the clear color.
uint32_t bj_make_bitmap_pixel(struct bj_bitmap *bitmap, uint8_t red, uint8_t green, uint8_t blue)
Returns an opaque value representing a pixel color, given its RGB composition.
void bj_put_pixel(struct bj_bitmap *bitmap, size_t x, size_t y, uint32_t value)
Change the pixel color at given coordinate.
struct bj_bitmap bj_bitmap
Definition api.h:270
struct bj_renderer bj_renderer
Definition api.h:288
struct bj_window bj_window
Definition api.h:296
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.
bj_real x
Definition vec.h:27
bj_real x
Definition vec.h:39
bj_real y
Definition vec.h:28
bj_real y
Definition vec.h:40
#define BJ_TAU
TAU in the selected bj_real precision.
Definition math.h:88
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.
Definition mat.h:209
#define BJ_VEC2_ZERO
Definition vec.h:32
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.
Definition mat.h:430
#define bj_sin
Sine.
Definition math.h:221
#define BJ_FZERO
Zero constant in bj_real.
Definition math.h:64
#define bj_pow
Power.
Definition math.h:219
#define bj_cos
Cosine.
Definition math.h:212
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.
Definition mat.h:183
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:53
#define bj_sqrt
Square root.
Definition math.h:222
float bj_real
Selected real type for float configuration.
Definition math.h:51
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.
Definition mat.h:457
struct bj_vec3: 3D vector of bj_real values.
Definition vec.h:38
struct bj_vec2 position
Definition physics_2d.h:79
bj_real damping
Definition physics_2d.h:83
struct bj_vec2 velocity
Definition physics_2d.h:80
struct bj_vec2 forces
Definition physics_2d.h:82
bj_real inverse_mass
Definition physics_2d.h:84
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.
Definition physics_2d.h:78
void bj_present(struct bj_renderer *renderer, struct bj_window *window)
Present the framebuffer to a window.
struct bj_renderer * bj_create_renderer(enum bj_renderer_type type, struct bj_error **error)
Create a new renderer instance.
struct bj_bitmap * bj_get_framebuffer(struct bj_renderer *renderer)
Get the renderer's framebuffer.
bj_bool bj_renderer_configure(struct bj_renderer *renderer, struct bj_window *window, struct bj_error **error)
Configure a renderer for a specific window.
void bj_destroy_renderer(struct bj_renderer *renderer)
Destroy a renderer and free associated resources.
@ BJ_RENDERER_TYPE_SOFTWARE
Software (CPU-based) renderer.
Definition renderer.h:35
bj_bool bj_begin(int systems, struct bj_error **error)
Initializes the system.
void bj_end(void)
De-initializes the system.
@ BJ_VIDEO_SYSTEM
Definition system.h:20
void bj_sleep(int milliseconds)
Suspends the current thread for a specified duration.
double bj_stopwatch_delay(const struct bj_stopwatch *stopwatch)
Returns the time in seconds since the last step.
double bj_stopwatch_elapsed(const struct bj_stopwatch *stopwatch)
Returns the elapsed time in seconds since the stopwatch was reset.
Structure representing a simple stopwatch.
Definition time.h:97
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.
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.
Logging utility functions.
Portable main substitution and application callback facilities.
C99 math shim with bj_real precision type and scalar utilities.
Physics helpers (SI units, but dimensionally consistent with any unit system).
Physics helpers (SI units, but dimensionally consistent with any unit system).
#define SCREEN_WIDTH
Definition physics_kinematics.c:30
bj_stopwatch stopwatch
Definition physics_kinematics.c:55
static void draw()
Definition physics_kinematics.c:136
#define SCREEN_HEIGHT
Definition physics_kinematics.c:31
static void update(bj_real dt)
Definition physics_kinematics.c:103
bj_bitmap * framebuffer
Definition physics_kinematics.c:35
static void update_projection()
Definition physics_particle.c:77
#define M_VENUS
Definition physics_particle.c:53
uint32_t color
Definition physics_particle.c:64
#define M_EARTH
Definition physics_particle.c:54
bj_particle_2d body
Definition physics_particle.c:62
#define DT_CLAMP
Definition physics_particle.c:166
static void init_planet(planet_t *p, bj_real r, bj_real mass, uint32_t color, bj_real draw_r, bj_real phase)
Definition physics_particle.c:105
#define N_PLANETS
Definition physics_particle.c:67
#define N_ASTEROIDS
Definition physics_particle.c:71
bj_real radius
Definition physics_particle.c:63
uint32_t asteroid_color
Definition physics_particle.c:73
#define M_MARS
Definition physics_particle.c:55
bj_particle_2d sun
Definition physics_particle.c:69
planet_t planets[5]
Definition physics_particle.c:68
#define M_JUPITER
Definition physics_particle.c:56
static void init_sun()
Definition physics_particle.c:94
static void init_asteroids()
Definition physics_particle.c:122
#define SOFTENING
Definition physics_particle.c:50
#define CANVAS_WIDTH
Definition physics_particle.c:37
static bj_real orbital_speed_soft(bj_real G, bj_real M, bj_real r, bj_real eps)
Definition physics_particle.c:88
bj_particle_2d asteroids[800]
Definition physics_particle.c:72
static void initialize()
Definition physics_particle.c:144
#define CANVAS_HEIGHT
Definition physics_particle.c:38
#define M_SUN
Definition physics_particle.c:51
#define M_MERCURY
Definition physics_particle.c:52
bj_mat3x3 projection
Definition physics_particle.c:43
static void physics(bj_real dt)
Definition physics_particle.c:184
#define G_SUN
Definition physics_particle.c:49
Definition physics_particle.c:61
Rendering backend interface.
Header file for system interactions.
Header file for time manipulation utilities.
Header file for bj_window type.