Physics Engine
The .NET Gaming Core Physics Engine provides robust and efficient tools for simulating realistic physical interactions within your game world. It leverages modern algorithms and hardware acceleration where possible to deliver high-fidelity results for collision detection, rigid body dynamics, and more.
Key Concepts
- Rigid Body Dynamics: Simulates the motion and interaction of solid objects, considering mass, inertia, forces, and torques.
- Collision Detection: Identifies when objects intersect or come into contact. Supports various collision shapes (spheres, boxes, capsules, meshes) and broad-phase/narrow-phase algorithms for performance.
- Collision Resolution: Determines how objects react upon collision, including impulses, friction, and restitution (bounciness).
- Constraints: Allows you to define relationships between rigid bodies, such as joints (hinge, ball-socket) and motors.
- Raycasting: Efficiently casts rays into the physics world to detect intersections, useful for hit detection and line-of-sight checks.
Core Components
PhysicsWorld
The central hub for managing all physics simulations. You'll add rigid bodies, collision shapes, and constraints to the PhysicsWorld
and step it forward in time to update object states.
using NetGaming.Physics;
// Initialize the physics world
var physicsWorld = new PhysicsWorld();
// Add a static ground plane
var groundBody = new RigidBody(isStatic: true);
groundBody.AddCollider(new BoxCollider(new Vector3(100f, 1f, 100f)));
physicsWorld.AddBody(groundBody);
// Add a dynamic sphere
var sphereBody = new RigidBody(mass: 1f);
sphereBody.Position = new Vector3(0f, 10f, 0f);
sphereBody.AddCollider(new SphereCollider(radius: 0.5f));
physicsWorld.AddBody(sphereBody);
// Game loop update
void Update(float deltaTime)
{
// Apply forces or impulses as needed
// sphereBody.ApplyForce(new Vector3(0f, -9.81f * sphereBody.Mass, 0f));
// Step the physics simulation
physicsWorld.Step(deltaTime);
// Read updated positions and rotations from sphereBody
}
RigidBody
Represents an object that can be simulated physically. It holds properties like mass, velocity, angular velocity, and can be associated with one or more colliders.
Collider
Defines the shape of an object for collision detection. Common types include:
SphereCollider
BoxCollider
CapsuleCollider
MeshCollider
(for complex geometry)
CollisionInfo
Provides details about a collision, such as the contact points, normal, and the two bodies involved.
Advanced Features
- Raycasting API: Perform precise raycasts for gameplay mechanics.
- Character Controller: Built-in component for common character movement and collision handling.
- Continuous Collision Detection (CCD): Reduces tunneling for fast-moving objects.
- Physics Materials: Define friction and restitution properties for surfaces.
MeshCollider
s sparingly, especially for dynamic objects, as they can be computationally expensive. Consider using simpler shapes like boxes or spheres for common collision checks.
RigidBody
positions and rotations after stepping the physics simulation.
API Reference
Class/Method | Description |
---|---|
PhysicsWorld.Step(float deltaTime) |
Advances the physics simulation by the given time delta. |
RigidBody.ApplyForce(Vector3 force) |
Applies a linear force to the rigid body. |
RigidBody.ApplyTorque(Vector3 torque) |
Applies an angular force to the rigid body. |
PhysicsWorld.Raycast(Vector3 origin, Vector3 direction, float maxDistance) |
Performs a raycast and returns information about the first hit. |
PhysicsMaterial |
Defines physical properties like friction and bounciness. |