stride3d/stride · error · Exception

Both RigidBodies must be valid

Error message

Both RigidBodies must be valid

What it means

CreateConstraint builds a physics constraint between two rigid bodies; both bodies are mandatory inputs used to create the native Bullet constraint. Passing null for either body would produce a native crash, so the public API validates and throws before reaching BulletSharp.

Solutions

  1. Obtain valid RigidbodyComponent references for both bodies before calling CreateConstraint.
  2. Ensure the referenced entities/components are loaded and enabled (non-null) at constraint creation time.
  3. Defer constraint creation until after scene/entity initialization completes.

Example fix

// before
var c = Simulation.CreateConstraint(ConstraintTypes.Point2Point, bodyA, bodyB, frameA, frameB); // bodyB null
// after
if (bodyA == null || bodyB == null)
    throw new InvalidOperationException("Constraint requires both rigid bodies to be resolved");
var c = Simulation.CreateConstraint(ConstraintTypes.Point2Point, bodyA, bodyB, frameA, frameB);
Defensive patterns

Strategy: validation

Validate before calling

if (rigidBodyA == null || rigidBodyB == null) throw new InvalidOperationException("CreateConstraint requires both bodies non-null");

Type guard

bool CanConstrain(RigidbodyComponent a, RigidbodyComponent b) => a != null && b != null;

Try / catch

try { var c = Simulation.CreateConstraint(t, a, b, fa, fb); } catch (Exception ex) when (ex.Message == "Both RigidBodies must be valid") { /* resolve body refs and retry setup */ }

Prevention

When it happens

Trigger: Calling Simulation.CreateConstraint(type, rigidBodyA, rigidBodyB, frameA, frameB) with rigidBodyA or rigidBodyB set to null (including via default/omitted arguments upstream).

Common situations: Resolving body references via late-bound lookups (Get<T> on entities) that return null; wiring constraints before the rigid body components are initialized.

Related errors


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

Appendix: source

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

        
        /// <summary>
        /// Creates the constraint.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="rigidBodyA">The rigid body a.</param>
        /// <param name="rigidBodyB">The rigid body b.</param>
        /// <param name="frameA">The frame a.</param>
        /// <param name="frameB">The frame b.</param>
        /// <param name="useReferenceFrameA">if set to <c>true</c> [use reference frame a].</param>
        /// <returns></returns>
        /// <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 Constraint CreateConstraint(ConstraintTypes type, RigidbodyComponent rigidBodyA, RigidbodyComponent rigidBodyB, Matrix frameA, Matrix frameB, bool useReferenceFrameA = false)
        {
            if (rigidBodyA == null || rigidBodyB == null) throw new Exception("Both RigidBodies must be valid");
            return CreateConstraintInternal(type, rigidBodyA, frameA, rigidBodyB, frameB, useReferenceFrameA);
        }

        /// <summary>
        /// Creates a hinge constraint using a specialized constructor.
        /// </summary>
        /// <param name="rigidBodyA">The rigid body a.</param>
        /// <param name="pivotInA">Pivot point in body a.</param>
        /// <param name="axisInA">Axis in body a.</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
        /// RigidBody must be valid
        /// </exception>
        public static HingeConstraint CreateHingeConstraint(RigidbodyComponent rigidBodyA, Vector3 pivotInA, Vector3 axisInA, bool useReferenceFrameA = false)
        {
            if (rigidBodyA == null) throw new Exception("RigidBody must be valid");

View on GitHub (pinned to 96fad776d2)