Skip to content

Instantly share code, notes, and snippets.

@erincatto
erincatto / lazy_sleep.md
Created December 9, 2025 22:13
Lazy Sleeping

Each thread has a bit set called awakeIslandBitSet. Each bit corresponds the awake island index. Before the solve, this bitset is all zeros.

When bodies are finalized, every body that is not sleepy will turn on the corresponding awakeIslandBitSet for the owning island. So this island cannot sleep.

On the other hand, if this body is sleepy and it is the sleepiest body, then it will make the owning island a candidate for splitting if the island has removed constraints.

This is the related code:

@erincatto
erincatto / circle_impulse_trace.md
Last active December 8, 2025 17:47
Circle drop impulse trace

Solver loop

for each sub step
  apply forces to update velocity (gravity)
  apply warm starting impulses
  apply impulses with position bias
  use velocity to update position
  apply impulses without position bias
end
@erincatto
erincatto / segment_pool.md
Last active December 6, 2025 21:33
Segmented Array Pool
// Pool allocator with block size doubling
// https://danielchasehooper.com/posts/segment_array/

// The first block can hold this many items. Each subsequent block has double capacity.
#define BASE_BLOCK_ITEM_COUNT 32

// With 20 blocks the final block can hold 32 * 19^2 = 16,777,216 items
#define MAX_POOL_BLOCKS 20
@erincatto
erincatto / static_shapes.md
Last active March 25, 2024 19:47
Static Shapes

Option 1: static bodies

typedef struct b2StaticBodyDef
{
	/// The world position of the body. Avoid creating bodies at the origin
	/// since this can lead to many overlapping shapes.
	b2Vec2 position;

	/// The world angle of the body in radians.
	float angle;
@erincatto
erincatto / soft_constraint_test.m
Last active January 24, 2024 05:29
Soft Constraint Octave Script
% This tests a two particle mass-spring-damper system with particle 1 connect to ground and
% particle 2 connected to particle 1. Particle 2 is much heavier than particle 1 and the
% simulation can go unstable if the soft constraint is made too stiff.
% Stability is improved by adding a relaxation step that applies the rigid constraint after
% the position update.
% no relax stable up to 13.5 Hz
% with relax stable up to 20.5 Hz
relax = 1;
hertz = 20.5;
@erincatto
erincatto / choice.c
Last active January 28, 2023 05:47
Data layout choices
struct b2CircleShape
{
// circle stuff
};
struct b2PolygonShape
{
// polygon stuff
};
@erincatto
erincatto / test_macros.h
Created January 8, 2023 17:40
Simple Unit Testing
// SPDX-FileCopyrightText: 2022 Erin Catto
// SPDX-License-Identifier: MIT
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#define RUN_TEST(T) \
do { \
int result = T(); \
@erincatto
erincatto / const_correctness.md
Last active July 26, 2022 23:53
const correctness
struct Foo
{
   int* data;
};

void DoStuff(const Foo& f)
{
   f.data[3] = 10;
}
Given J*JT * x = b, solve for x in linear time and space.
J is m by n
x is m by 1
b is m by 1
m < n
J is a rectangular matrix that represents a tree with equal and opposite entries.
For example: