PHI is a compact 2D physics and rendering library built on SDL2. It is designed to support basic physics objects, constraints, rigid structures, and compound shapes such as ropes and cloths.
The library is organized around a single public include file:
lib/phi2d.h
This header pulls in the core physics, shape, rendering, and utility modules, making it easy to use the library as a self-contained engine.
- 2D physics simulation with Verlet integration
- Bodies and shapes:
CircleRectWallPoint
- Constraints and structure support:
StickRodJointRigidBodyRigidStructure
- Compound shape helpers:
RopeCloth
- SDL2-based renderer and simple UI / statistics overlay
- Spatial grid optimization for collision handling
- Shape texture caching via
ShapeCache
lib/
phi2d.h— main public include file
lib/math/
vector2d.h— 2D vector math utilitiesconstrain2d.h— constraint-related math helperscollision2d.h— collision helper utilities
lib/colors/
color.h— color definitions and helperscolors.cpp— color initialization and utilities
lib/core/
world.h— world configuration and global physics settingsengine.h/engine.cpp— main SDL2 engine, update loop, rendering, event handlingshape_cache.h/shape_cache.cpp— cached texture generation for shapeskey.h— keyboard and input helper definitions
lib/components/
physics_object.h— base physics object classinfo.h— collision result helper
lib/components/shapes/
point.h,point.cppcircle.h,circle.cpprect.h,rect.cppwall.h,wall.cpp
lib/components/constraints/
stick.h,stick.cpprod.h,rod.cppjoint.h,joint.cppnode.h,node.cpp
lib/components/structures/
rigid_body.h,rigid_body.cpprigid_structure.h,rigid_structure.cppjoint_body.h
lib/compound_shapes/
rope.h— rope helper that creates points and stickscloth.h— cloth helper that creates point/ stick grids
World— simulation settings, gravity/force vector, boundariesEngine— window creation, render loop, input handling, object managementPhysicsObject— base class for physical entitiesCircle,Rect,Wall— renderable physics shapesStick,Rod,Joint,Node— constraint objectsRigidBody,RigidStructure— composite rigid objectsRope,Cloth— compound shape helpers built from points and sticks
lib/phi2d.hincludes.cppfiles directly, so it works as a single-include entry point for simple projects.- The engine depends on SDL2 and SDL2_ttf for rendering and text output.
- Font assets are located in
lib/assets/font/, includingsfpro.ttf.
#include "lib/phi2d.h"
int main(int argc, char *argv[])
{
World world("Demo World", 800, 600);
Engine engine(world, true);
Circle *ball = new Circle(200, 100, 1.0f, 24.0f, COLORS::RED);
Rect *box = new Rect(400, 300, 1.0f, 120, 80, COLORS::BLUE);
engine.add(ball);
engine.add(box);
engine.run();
return 0;
}- Link against SDL2 and SDL2_ttf.
- Add
src/includeor your SDL2 include path to the compiler include directories. - Add
src/libor your SDL2 library path to the linker directories.
If you are using the project from this workspace, the existing task definitions show commands that use C:\msys64\mingw64\bin\g++.exe and the SDL2 library set from a local MSYS2 environment.
The demo/ folder contains example programs that exercise the library:
performance.cpp
These examples demonstrate dynamic bodies, cloth, rigid bodies, joints, wind effects, and performance behavior.