Skip to content

ProwlEngine/Prowl.Drift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

22 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Github top languages GitHub version GitHub license GitHub issues GitHub stars Discord

Table of Contents

  1. About The Project
  2. Features
  3. Getting Started
  4. Contributing
  5. Contributors
  6. License

🎱 About The Project 🎱

Prowl.Drift is an open-source, MIT-licensed 2D physics engine built for the Prowl Game Engine. It provides a lightweight, high-performance physics simulation with support for rigid body dynamics, collision detection, and constraint solving.

Prowl.Drift follows a simple and intuitive API design, making it easy to integrate physics into your games and applications. Whether you're building a platformer, a physics puzzle game, or need realistic object interactions, Prowl.Drift has you covered.

Here's a basic example:

// Create a physics space
var space = new Space();

// Create a dynamic body with a box shape
var body = new Body(Body.BodyType.Dynamic, new Vector2(100, 50));
var boxShape = ShapePoly.CreateBox(0, 0, 32, 32);
body.AddShape(boxShape);
space.AddBody(body);

// Apply a force
body.ApplyForceToCenter(new Vector2(0, -500));

// Step the simulation (with solver iterations)
space.Step(1f / 60f, 8, 3, true); // dt, velocity iterations, position iterations, warm starting

✨ Features ✨

  • General:

    • Cross-Platform! Windows, Linux & Mac!
    • 100% C#!
    • Built on System.Numerics for performance
    • Supports .NET 6, 7, 8, and 9
    • Highly Portable
      • Very easy to integrate
    • Lightweight and fast
  • Physics Features:

    • Rigid Body Dynamics
      • Static, Kinematic, and Dynamic bodies
      • Mass, velocity, and angular velocity
      • Force and torque accumulation
      • Linear and angular damping
      • Fixed rotation support
    • Shape Support
      • Circles
      • Polygons (convex)
      • Line segments (With Rounded Thickness)
      • Multiple shapes per body (Compound)
    • Collision Detection
      • Broad-phase spatial hashing (Very Simple)
      • Narrow-phase SAT (Separating Axis Theorem)
      • Contact manifold generation
    • Constraint Solving
    • Joint System
      • Distance joints
      • Revolute joints (pin joints)
      • Prismatic joints (slider joints)
      • Weld joints
      • Wheel joints
      • Rope joints
      • Angle joints
      • Mouse joints (for dragging)

(back to top)

πŸš€ Getting Started πŸš€

Installation

dotnet add package Prowl.Drift

Basic Usage

Prowl.Drift is designed to be simple to use. Create a space, add bodies with shapes, and step the simulation.

using Prowl.Drift;
using System.Numerics;

// Create a physics space
var space = new Space();
space.Gravity = new Vector2(0, -10); // Set gravity

// Create the ground (static body)
var ground = new Body(Body.BodyType.Static, new Vector2(0, 400));
var groundShape = ShapePoly.CreateBox(0, 0, 800, 20);
ground.AddShape(groundShape);
space.AddBody(ground);

// Create a falling box (dynamic body)
var box = new Body(Body.BodyType.Dynamic, new Vector2(100, 50));
var boxShape = ShapePoly.CreateBox(0, 0, 32, 32);
box.AddShape(boxShape);
space.AddBody(box);

// Main game loop
while (gameRunning)
{
    // Step the physics simulation (60 FPS)
    space.Step(1f / 60f, 8, 3, true);

    // Use body.Position and body.Angle for rendering
    RenderBox(box.Position, box.Angle);
}

Creating Bodies

Bodies are the fundamental objects in the physics simulation. They can be static (immovable), kinematic (movable but not affected by forces), or dynamic (fully simulated).

// Static body (walls, ground, platforms)
var staticBody = new Body(Body.BodyType.Static, position);
space.AddBody(staticBody);

// Kinematic body (moving platforms, elevators)
var kinematicBody = new Body(Body.BodyType.Kinematic, position);
kinematicBody.LinearVelocity = new Vector2(50, 0); // Moves at constant velocity
space.AddBody(kinematicBody);

// Dynamic body (players, projectiles, physics objects)
var dynamicBody = new Body(Body.BodyType.Dynamic, position);
space.AddBody(dynamicBody);
dynamicBody.ApplyForceToCenter(new Vector2(0, -500)); // Apply force

Shapes and Collision

Bodies need shapes to participate in collision detection. Multiple shapes can be attached to a single body.

// Box shape
var boxShape = ShapePoly.CreateBox(0, 0, width, height);
body.AddShape(boxShape);

// Circle shape
var circleShape = new ShapeCircle(0, 0, radius);
body.AddShape(circleShape);

// Polygon shape (must be convex)
var vertices = new Vector2[] {
    new Vector2(-16, -16),
    new Vector2(16, -16),
    new Vector2(16, 16),
    new Vector2(-16, 16)
};
var polyShape = new ShapePoly(vertices);
body.AddShape(polyShape);

// Line segment
var segmentShape = new ShapeSegment(new Vector2(-50, 0), new Vector2(50, 0), 2); // thickness = 2
body.AddShape(segmentShape);

// Set material properties
boxShape.Density = 1.0f;
boxShape.Friction = 0.7f;
boxShape.Elasticity = 0.2f; // Restitution/bounciness

Joints and Constraints

Joints connect bodies together with various constraints.

// Distance joint - maintains fixed distance
var distanceJoint = new DistanceJoint(bodyA, bodyB, anchorA, anchorB);
space.AddJoint(distanceJoint);

// Revolute joint - pin joint, allows rotation
var revoluteJoint = new RevoluteJoint(bodyA, bodyB, worldAnchor);
revoluteJoint.CollideConnected = false; // Disable collision between connected bodies
space.AddJoint(revoluteJoint);

// Rope joint - maximum distance constraint
var ropeJoint = new RopeJoint(bodyA, bodyB, anchorA, anchorB);
ropeJoint.CollideConnected = false;
space.AddJoint(ropeJoint);

// Weld joint - rigid connection
var weldJoint = new WeldJoint(bodyA, bodyB, worldAnchor);
space.AddJoint(weldJoint);

(back to top)

🀝 Contributing 🀝

Check our Contributing guide to see how to be part of this team.

(back to top)

πŸ“œ License πŸ“œ

Distributed under the MIT License. See LICENSE for more information.

(back to top)


Discord

About

A MIT Licensed .NET 2D Physics Library

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages