Skip to content

Instantly share code, notes, and snippets.

@skeeto
Last active December 21, 2025 17:29
Show Gist options
  • Select an option

  • Save skeeto/95abbf23b15af7a751f84496b6801698 to your computer and use it in GitHub Desktop.

Select an option

Save skeeto/95abbf23b15af7a751f84496b6801698 to your computer and use it in GitHub Desktop.
Physics demo
// $ eval cc -g3 -o demo demo.c $(pkg-config --cflags --libs sdl2)
// Ref: https://old.reddit.com/r/C_Programming/comments/1prwizy
// Ref: https://github.com/Avery-Personal/Jubi
#define JUBI_IMPLEMENTATION
#include "Jubi.h"
#include "SDL.h"
static Sint32 randn(Uint64 *s, Sint32 lo, Sint32 hi)
{
*s = *s*0x3243f6a8885a308d + 1;
return (Sint32)((*s>>32)*(Uint64)(hi - lo)>>32) + lo;
}
static SDL_FRect tofrect(Body2D *b)
{
return (SDL_FRect){
b->Position.x - b->_Size.x/2,
b->Position.y - b->_Size.y/2,
b->_Size.x,
b->_Size.y,
};
}
int main(int, char **)
{
int x = SDL_WINDOWPOS_UNDEFINED;
int y = SDL_WINDOWPOS_UNDEFINED;
int width = 800;
int height = 800;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *w = SDL_CreateWindow("Jubi", x, y, width, height, 0);
SDL_Renderer *r = SDL_CreateRenderer(w, -1, SDL_RENDERER_PRESENTVSYNC);
JubiWorld2D world = Jubi_CreateWorld2D();
Body2D *floor = JBody2D_CreateBox(
&world,
(Vector2){(float)width/2, (float)height-5},
(Vector2){(float)width, 10},
BODY_STATIC, 1000
);
enum { N = 16 };
Uint64 rng = 1234;
Body2D *box[N] = {};
Sint32 colors[N] = {};
for (int i = 0; i < N; i++) {
float x = (float)randn(&rng, 0, width);
float y = (float)randn(&rng, 0, height);
float w = (float)randn(&rng, width/30, width/10);
float h = (float)randn(&rng, width/30, width/10);
Sint32 r = randn(&rng, 0, 256) | 0x40;
Sint32 g = randn(&rng, 0, 256) | 0x40;
Sint32 b = randn(&rng, 0, 256) | 0x40;
box[i] = JBody2D_CreateBox(
&world,
(Vector2){x, y},
(Vector2){w, h},
BODY_DYNAMIC, 1
);
colors[i] = r<<16 | g<<8 | b;
}
Sint64 last = (Sint64)SDL_GetTicks64();
for (;;) {
for (SDL_Event e; SDL_PollEvent(&e);) {
switch (e.type) {
case SDL_QUIT:
return 0;
}
}
SDL_SetRenderDrawColor(r, 0, 0, 0, 0);
SDL_RenderClear(r);
SDL_SetRenderDrawColor(r, 255, 0, 0, 255);
SDL_FRect fr = tofrect(floor);
SDL_RenderFillRectF(r, &fr);
for (int i = 0; i < N; i++) {
SDL_FRect fr = tofrect(box[i]);
SDL_SetRenderDrawColor(
r,
(Uint8)(colors[i]>>16),
(Uint8)(colors[i]>> 8),
(Uint8)(colors[i]>> 0),
255
);
SDL_RenderFillRectF(r, &fr);
}
SDL_RenderPresent(r);
SDL_Delay(1);
Sint64 now = (Sint64)SDL_GetTicks64();
Jubi_StepWorld2D(&world, 1 / (float)(now - last));
last = now;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment