MSDN .NET Gaming Core APIs

Comprehensive documentation for developing high-performance games with .NET.

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

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:

CollisionInfo

Provides details about a collision, such as the contact points, normal, and the two bodies involved.

Advanced Features

Performance Tip: Use MeshColliders sparingly, especially for dynamic objects, as they can be computationally expensive. Consider using simpler shapes like boxes or spheres for common collision checks.
Best Practice: Always synchronize your game objects' transforms with their corresponding 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.