stride3d/stride · error · Exception

Both RigidBodies must be on the same simulation

Error message

Both RigidBodies must be on the same simulation

What it means

When a constraint links two rigid bodies, both must belong to the same physics Simulation instance, because Bullet constraints are created within one dynamics world. CreateConstraintInternal compares rigidBodyB.Simulation with rigidBodyA.Simulation and throws if they differ (only when rigidBodyB is non-null).

Solutions

  1. Ensure both rigid bodies live in scenes/components served by the same Simulation before constraining them.
  2. If the bodies must interact, move one entity into the other body's scene or merge the simulations.
  3. Check rigidBodyA.Simulation == rigidBodyB.Simulation in setup code before creating the constraint.

Example fix

// before
Simulation.CreateConstraint(ConstraintTypes.Point2Point, bodyA, bodyB, frameA, frameB); // bodyB in another scene
// after
if (bodyA.Simulation != bodyB.Simulation)
    throw new InvalidOperationException("Constraint bodies must share one simulation");
Simulation.CreateConstraint(ConstraintTypes.Point2Point, bodyA, bodyB, frameA, frameB);
Defensive patterns

Strategy: validation

Validate before calling

if (rigidBodyA.Simulation != rigidBodyB.Simulation) throw new InvalidOperationException("Constraint bodies must share one simulation");

Type guard

bool SameSimulation(RigidbodyComponent a, RigidbodyComponent b) => a?.Simulation != null && ReferenceEquals(a.Simulation, b?.Simulation);

Try / catch

try { var c = Simulation.CreateConstraint(t, a, b, fa, fb); } catch (Exception ex) when (ex.Message.Contains("same simulation")) { /* relocate body or skip constraint */ }

Prevention

When it happens

Trigger: Calling Simulation.CreateConstraint or the two-body CreateHingeConstraint with bodies whose RigidbodyComponent.Simulation properties point to different Simulation objects (e.g. one body in a different scene's physics world).

Common situations: Cross-scene joint setups; entities moved between scenes after their bodies were registered; multiple physics simulations in one game and bodies mixed across them.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/0f827d267d535a11. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Physics/Simulation.cs:696

        /// <param name="pivotInB">Pivot point in body b.</param>
        /// <param name="axisInB">Axis in body b.</param>
        /// <param name="useReferenceFrameA">if set to <c>true</c> [use reference frame a].</param>
        /// <exception cref="System.Exception">
        /// Cannot perform this action when the physics engine is set to CollisionsOnly
        /// or
        /// Both RigidBodies must be valid
        /// </exception>
        public static HingeConstraint CreateHingeConstraint(RigidbodyComponent rigidBodyA, Vector3 pivotInA, Vector3 axisInA, RigidbodyComponent rigidBodyB, Vector3 pivotInB, Vector3 axisInB, bool useReferenceFrameA = false)
        {
            if (rigidBodyA == null || rigidBodyB == null) throw new Exception("Both RigidBodies must be valid");
            return CreateHingeConstraintInternal(rigidBodyA, rigidBodyB, pivotInA, pivotInB, axisInA, axisInB, useReferenceFrameA);
        }


        static Constraint CreateConstraintInternal(ConstraintTypes type, RigidbodyComponent rigidBodyA, Matrix frameA, RigidbodyComponent rigidBodyB = null, Matrix frameB = default, bool useReferenceFrameA = false)
        {
            if (rigidBodyA == null) throw new Exception($"{nameof(rigidBodyA)} must be valid");
            if (rigidBodyB != null && rigidBodyB.Simulation != rigidBodyA.Simulation) throw new Exception("Both RigidBodies must be on the same simulation");

            Constraint constraintBase;
            var rbA = rigidBodyA.InternalRigidBody;
            var rbB = rigidBodyB?.InternalRigidBody;
            switch (type)
            {
                case ConstraintTypes.Point2Point:
                {
                    var constraint = new Point2PointConstraint
                    {
                        InternalPoint2PointConstraint = 
                            rigidBodyB == null ? 
                            new BulletSharp.Point2PointConstraint(rbA, frameA.TranslationVector ) :
                            new BulletSharp.Point2PointConstraint(rbA, rbB, frameA.TranslationVector, frameB.TranslationVector),
                    };
                    constraintBase = constraint;

                    constraint.InternalConstraint = constraint.InternalPoint2PointConstraint;

View on GitHub (pinned to 96fad776d2)