Skip to content

idcnys/phi2d

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

PHI

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.

Overview

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.

Key Features

  • 2D physics simulation with Verlet integration
  • Bodies and shapes:
    • Circle
    • Rect
    • Wall
    • Point
  • Constraints and structure support:
    • Stick
    • Rod
    • Joint
    • RigidBody
    • RigidStructure
  • Compound shape helpers:
    • Rope
    • Cloth
  • SDL2-based renderer and simple UI / statistics overlay
  • Spatial grid optimization for collision handling
  • Shape texture caching via ShapeCache

Library Structure

lib/

  • phi2d.h — main public include file

lib/math/

  • vector2d.h — 2D vector math utilities
  • constrain2d.h — constraint-related math helpers
  • collision2d.h — collision helper utilities

lib/colors/

  • color.h — color definitions and helpers
  • colors.cpp — color initialization and utilities

lib/core/

  • world.h — world configuration and global physics settings
  • engine.h / engine.cpp — main SDL2 engine, update loop, rendering, event handling
  • shape_cache.h / shape_cache.cpp — cached texture generation for shapes
  • key.h — keyboard and input helper definitions

lib/components/

  • physics_object.h — base physics object class
  • info.h — collision result helper

lib/components/shapes/

  • point.h, point.cpp
  • circle.h, circle.cpp
  • rect.h, rect.cpp
  • wall.h, wall.cpp

lib/components/constraints/

  • stick.h, stick.cpp
  • rod.h, rod.cpp
  • joint.h, joint.cpp
  • node.h, node.cpp

lib/components/structures/

  • rigid_body.h, rigid_body.cpp
  • rigid_structure.h, rigid_structure.cpp
  • joint_body.h

lib/compound_shapes/

  • rope.h — rope helper that creates points and sticks
  • cloth.h — cloth helper that creates point/ stick grids

Public API Summary

Main classes

  • World — simulation settings, gravity/force vector, boundaries
  • Engine — window creation, render loop, input handling, object management
  • PhysicsObject — base class for physical entities
  • Circle, Rect, Wall — renderable physics shapes
  • Stick, Rod, Joint, Node — constraint objects
  • RigidBody, RigidStructure — composite rigid objects
  • Rope, Cloth — compound shape helpers built from points and sticks

Notes

  • lib/phi2d.h includes .cpp files 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/, including sfpro.ttf.

Example Usage

#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;
}

Build / Integration Notes

  • Link against SDL2 and SDL2_ttf.
  • Add src/include or your SDL2 include path to the compiler include directories.
  • Add src/lib or 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.

Demos

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.

About

PHI is a compact, high-performance 2D physics engine built on SDL2, designed for C++ developers who need a self-contained, easy-to-integrate solution for simulations. It utilizes Verlet integration to provide stable physics calculations.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors