Table of Contents

Simple constraint

This example demonstrates how to create a physics-based constraint between two objects using the BepuPhysics integration in Stride. The code illustrates:

  • Setting up a basic 3D scene with skybox, profiler, and ground reference grid
  • Creating a capsule and two spheres as visual objects in the scene
  • Implementing a distance servo constraint that maintains a fixed distance between two spheres
  • Using the DistanceServoConstraintComponent to define the relationship between physics bodies

The distance servo constraint connects the two spheres and enforces a target distance of 3.0 units between them, causing them to pull toward or push away from each other to maintain this distance. This example showcases how constraints can be used to create physically-driven connections between objects, which is useful for creating joints, hinges, ropes, and other interconnected mechanical systems.

Stride UI Example

View on GitHub.

using Stride.BepuPhysics;
using Stride.BepuPhysics.Constraints;
using Stride.CommunityToolkit.Bepu;
using Stride.CommunityToolkit.Engine;
using Stride.CommunityToolkit.Rendering.ProceduralModels;
using Stride.CommunityToolkit.Scripts.Utilities;
using Stride.CommunityToolkit.Skyboxes;
using Stride.Core.Mathematics;
using Stride.Engine;

// Initialize the game instance
using var game = new Game();

// Run the game loop with the Start method
game.Run(start: Start);

void Start(Scene scene)
{
    // Set up a basic 3D scene with skybox, profiler, and a ground gizmo
    game.SetupBase3DScene();
    game.AddSkybox();
    game.AddProfiler();
    game.AddGroundGizmo(new(-5, 0, -5), showAxisName: true);

    // Left mouse picks either sphere up; the servo drags the other one along (GrabberScript, see E05_3D_Grabber).
    game.GetCameraEntity().Add(new GrabberScript());

    var overlay = DebugOverlay.GetOrCreate(game);
    overlay.SectionGap = 0;
    overlay.AddSection("Constraint", static () =>
    [
        new("Left mouse", "Pick a sphere up, carry it, throw it", Color.Yellow),
        new("Mouse wheel", "Carry distance", Color.Yellow),
        new("T", "Hold and move the mouse to turn the held sphere", Color.Yellow),
        new(""),
        new("A distance servo holds the spheres 3 units apart", Color.LightGray),
        new("Carry one and the other follows", Color.LightGray),
    ]);

    // Create an additional capsule for visual reference
    var entity = game.Create3DPrimitive(PrimitiveModelType.Capsule);
    entity.Transform.Position = new Vector3(0, 3, 0);
    entity.Scene = scene;

    // Create a sphere with a golden material
    var sphere = game.Create3DPrimitive(PrimitiveModelType.Sphere, new()
    {
        Material = game.CreateMaterial(Color.Gold)
    });
    sphere.Transform.Position = new Vector3(0.1f, 5, -0.3f);

    // Create a second sphere to demonstrate a connected constraint
    var connectedSphere = game.Create3DPrimitive(PrimitiveModelType.Sphere);
    connectedSphere.Transform.Position = new Vector3(-2f, 1, -2f);

    // Set up a distance servo constraint between the sphere and connected sphere