A 2D physics engine implemented in C# (.NET 9) using SFML for rendering.
- Refactored to separate game engine from implementation - the physics engine is now a standalone library
- Added constraint system with WeldConstraint and AxisConstraint
- Added demo games showcasing engine capabilities
- Integrated with ProjectorSegmentation for body tracking via MJPEG stream or webcam
- Demo video: https://youtu.be/Lo-g24-rv4k
- Arbitrary Polygon Physics: Support for complex polygon shapes beyond simple circles and boxes
- Object Sleep States: Performance optimization that puts inactive objects to sleep
- Spatial Hashing: Efficient broad-phase collision detection algorithm
- Multiple Collision Types: Circle-Circle, Circle-Polygon, and Polygon-Polygon collision detection
- Rotational Physics: Support for rotational dynamics and angular momentum
- Constraint System: Joints and connections between objects (WeldConstraint, AxisConstraint)
- Friction and Restitution: Configurable material properties for objects
- SFML Rendering: Hardware-accelerated rendering with shader support
- UI System: Basic UI elements for debugging and information display
- Game Engine Architecture: Clean separation between physics engine and game implementations via
IGameinterface
Older screenshots from development - UI and features may have changed
Arbitrary Polygon physics:
Object Sleep States (WIP):
Earlier development screenshots:
- Clone the repository
- Open the solution file in Visual Studio
- Restore NuGet packages
- Build and run
SharpPhysics.Demoproject
sharpPhysics/
├── physics/ # Core Physics Engine Library (SharpPhysics.Engine)
│ └── Engine/
│ ├── Classes/ # Data structures and object definitions
│ │ ├── Objects/ # PhysicsObject and related classes
│ │ └── Templates/ # ObjectTemplates, ActionTemplates
│ ├── Constraints/ # WeldConstraint, AxisConstraint implementations
│ ├── Core/ # GameEngine, IGame interface
│ ├── Helpers/ # Math utilities (PhysMath, CollisionHelpers, etc.)
│ ├── Input/ # InputManager, KeyState
│ ├── Player/ # PlayerController
│ ├── Rendering/ # SFML-based renderer
│ │ └── UI/ # UI elements (buttons, sliders, etc.)
│ ├── Shaders/ # SFML shader implementations
│ ├── Shapes/ # Circle, Box, Polygon shape definitions
│ └── Structs/ # AABB and other basic structures
│
├── SharpPhysics.Demo/ # Demo Application
│ ├── DemoGame.cs # Physics sandbox demo
│ ├── MenuGame.cs # Main menu
│ ├── BubblePopGame.cs # Bubble pop mini-game
│ ├── PlatformerGame.cs # Platformer demo
│ ├── RainCatcherGame.cs # Rain catcher mini-game
│ ├── SettingsGame.cs # Settings screen
│ ├── DemoProps/ # Demo-specific props (e.g., DemoGameCar)
│ ├── Helpers/ # Demo helpers (SkeletonRenderer)
│ ├── Integration/ # Person detection bridge
│ ├── Settings/ # Game settings
│ └── Resources/ # Fonts, images, etc.
│
└── SharpPhysics.Tests/ # Unit Tests
├── CollisionTests.cs
├── CollisionHelperTests.cs
└── Vec2Tests.cs
The engine uses spatial hashing for efficient broad-phase collision detection. This algorithm divides the world into a grid of cells and assigns objects to cells based on their position. Only objects in the same or adjacent cells are checked for collisions.
For narrow-phase collision detection, the engine uses different algorithms depending on the shapes involved:
- Circle vs Circle: Simple distance check between centers
- Polygon vs Circle: Find closest point on polygon to circle center
- Polygon vs Polygon: Separating Axis Theorem (SAT)
The engine uses impulse-based collision resolution. When a collision is detected, impulses are applied to the objects to resolve the collision. The impulse magnitude depends on the objects' masses, velocities, and restitution (bounciness).
To optimize performance, objects that have been stationary for a while are put to sleep. Sleeping objects don't participate in physics calculations until they're woken up by a collision or user interaction.
Implement the IGame interface and register it with the GameEngine:
public class MyGame : IGame
{
public void Initialize(GameEngine engine) { /* Setup your game */ }
public void Update(float deltaTime, KeyState keyState) { /* Game logic */ }
public void Render(Renderer renderer) { /* Custom rendering */ }
public void Shutdown() { /* Cleanup */ }
}- SFML.Net - Simple and Fast Multimedia Library for .NET
- ProjectorSegmentation (optional) - Body tracking for interactive demos
- Custom 2D Physics Engine Tutorial - Barebones C++ engine tutorial (up to rotational calculations)
- SFML.Net - Rendering library
- SFML UI Implementation - UI system reference
- Ten Minute Physics - Spatial Hashing algorithm
- OpenAI models used for bug fixing and brainstorming features






