A lightweight 2D physics engine written in C++. Stellar provides a simple and efficient foundation for physics-based games and simulations.
- C++ compiler with C++11 support
- SDL2 library
brew install sdl2
sudo apt-get install libsdl2-dev
# Build with SDL2 visualization
g++ test_game.cpp -std=c++11 -o game -I/opt/homebrew/include -L/opt/homebrew/lib -lSDL2
./game
int main() {
World world;
initWorld(world);
int circleId = addCircle(world, {0, 10}, 1.0, 1.0);
int boxId = addBox(world, {5, 10}, {2, 2}, 2.0);
Body &circle = world.bodies[circleId];
std::cout << "Circle area: " << calculateArea(circle) << "\n";
std::cout << "Circle MOI: " << calculateMOI(circle) << "\n";
double fixedTimeStep = 1.0 / 60.0; // 60 Hz simulation
for (int i = 0; i < 100; i++) {
stepWorld(world, fixedTimeStep);
std::cout << "Step " << i << ":\n";
std::cout << "Circle position: " << world.bodies[circleId].position.x
<< ", " << world.bodies[circleId].position.y << "\n";
std::cout << "Box position: " << world.bodies[boxId].position.x << ", "
<< world.bodies[boxId].position.y << "\n\n";
}
return 0;
}
ToDo
Vector Implementations are Done!
Vector2D struct definition (x, y components)Vector operation functions:vec2_add, vec2_sub, vec2_mulvec2_dot, vec2_crossvec2_normalizevec2_rotatevec2_length, vec2_length_squared
Basic Motion Implementation is Done!
Body struct (position, velocity, acceleration)Force functions:apply_forceapply_impulse
Integration functions:integrate_linear_motionapply_gravity
World struct to hold all bodies
Circle struct (position, radius)AABB struct (min, max points)- Polygon struct (vertex array, vertex count)
Shape union struct with type enumPoint containment test functions
- Broad phase functions:
grid_system struct and functions- quadtree struct and functions
- Collision pair struct
- Narrow phase functions:
check_circle_circlecheck_aabb_aabbcheck_circle_aabb- check_polygon_polygon (SAT)
- Contact struct (normal, depth, points)
- Resolution functions:
- resolve_collision_positions
- resolve_collision_velocities
- apply_momentum_conservation
- resolve_penetration
- Material struct:
- restitution
- friction_static
- friction_dynamic
- density
- Material resolution functions
- Constraint struct with type enum
- Constraint functions:
- solve_distance_constraint
- solve_point_constraint
- solve_rotation_constraint
- solve_angle_limits
- Spring struct and functions
- Rope struct and functions
- Joint struct variations
- Joint solver functions
- Force generator struct
- Force application functions:
- apply_drag
- apply_buoyancy
- apply_wind
- Raycast struct and functions
- Body state flags (active/sleeping)
- Island struct for connected bodies
- Continuous collision functions
- Spatial partitioning optimization
- Spring-mass struct
- Soft body solver functions
- Cloth struct and functions
- Rope simulation functions
- Particle struct
- Particle system functions
- Particle emitter struct
- Fluid particle functions