Skip to content

dankelley2/sharpPhysics

Repository files navigation

SharpPhysics

A 2D physics engine implemented in C# (.NET 9) using SFML for rendering.

Recent Updates (2025)

  • 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

Features

  • 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 IGame interface

Screenshots

Older screenshots from development - UI and features may have changed

Arbitrary Polygon physics:

image image

Object Sleep States (WIP):

IMG_6923

Earlier development screenshots:

image image image image

Installation and Setup

  1. Clone the repository
  2. Open the solution file in Visual Studio
  3. Restore NuGet packages
  4. Build and run SharpPhysics.Demo project

Project Structure

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

Physics Algorithms

Broad-Phase Collision Detection

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.

Narrow-Phase Collision Detection

For narrow-phase collision detection, the engine uses different algorithms depending on the shapes involved:

  1. Circle vs Circle: Simple distance check between centers
  2. Polygon vs Circle: Find closest point on polygon to circle center
  3. Polygon vs Polygon: Separating Axis Theorem (SAT)

Collision Resolution

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).

Object Sleep States

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.

Creating Your Own Game

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 */ }
}

Dependencies

Credits and References

About

2D physics engine in C#

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages