{"record":{"id":"cbe858a429ab3eb1","repo":"stride3d/stride","slug":"a-gear-constraint-always-needs-two-rigidbodies-to-be-created","errorCode":null,"errorMessage":"A Gear constraint always needs two rigidbodies to be created.","messagePattern":"A Gear constraint always needs two rigidbodies to be created\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Physics/Simulation.cs","lineNumber":793,"sourceCode":"                    var constraint = new Generic6DoFSpringConstraint\n                    {\n                        InternalGeneric6DofSpringConstraint =  \n                            rigidBodyB == null ? \n                                new BulletSharp.Generic6DofSpringConstraint(rbA, frameA, useReferenceFrameA) :\n                                new BulletSharp.Generic6DofSpringConstraint(rbA, rbB, frameA, frameB, useReferenceFrameA),\n                    };\n                    constraintBase = constraint;\n\n                    constraint.InternalConstraint = constraint.InternalGeneric6DofConstraint = constraint.InternalGeneric6DofSpringConstraint;\n                    break;\n                }\n                case ConstraintTypes.Gear:\n                {\n                    var constraint = new GearConstraint\n                    {\n                        InternalGearConstraint =  \n                            rigidBodyB == null ? \n                                throw new Exception(\"A Gear constraint always needs two rigidbodies to be created.\") :\n                                new BulletSharp.GearConstraint(rbA, rbB, frameA.TranslationVector, frameB.TranslationVector),\n                    };\n                    constraintBase = constraint;\n\n                    constraint.InternalConstraint = constraint.InternalGearConstraint;\n                    break;\n                }\n                default:\n                    throw new ArgumentException(type.ToString());\n            }\n\n            if(rigidBodyB != null)\n            {\n                constraintBase.RigidBodyB = rigidBodyB;\n                rigidBodyB.LinkedConstraints.Add(constraintBase);\n            }\n            constraintBase.RigidBodyA = rigidBodyA;\n            rigidBodyA.LinkedConstraints.Add(constraintBase);","sourceCodeStart":775,"sourceCodeEnd":811,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Physics/Simulation.cs#L775-L811","documentation":"When creating a constraint via Simulation's constraint creation API, a Gear constraint requires two rigid bodies to be constructed because the underlying BulletSharp.GearConstraint needs both rbA and rbB. If rigidBodyB is null the library throws this Exception rather than building a one-sided constraint. Gear constraints couple the rotation of two bodies, so a second body is mandatory.","triggerScenarios":"Calling the constraint creation switch path with ConstraintTypes.Gear while passing null for rigidBodyB (only rigidBodyA supplied).","commonSituations":"Developers create gear constraints programmatically and forget to fetch/assign the second body (e.g. body not yet spawned, or lookup by name returned null), unlike simpler joint types where a single-body frame is sometimes acceptable.","solutions":["Ensure rigidBodyB is a valid, registered RigidbodyComponent before requesting a Gear constraint","Verify the lookup of the second body (by entity/component reference) actually succeeded and is not null","Use a different constraint type if a single-body constraint was intended"],"exampleFix":"// before\nvar c = simulation.CreateConstraint(ConstraintTypes.Gear, bodyA, null, frameA, frameB);\n// after\nif (bodyB == null) throw new InvalidOperationException(\"Gear constraint requires bodyB\");\nvar c = simulation.CreateConstraint(ConstraintTypes.Gear, bodyA, bodyB, frameA, frameB);","handlingStrategy":"validation","validationCode":"if (rigidBodyB == null) throw new InvalidOperationException(\"Gear constraint requires two rigidbodies\");","typeGuard":null,"tryCatchPattern":"try { sim.CreateConstraint(ConstraintTypes.Gear, bodyA, bodyB, fa, fb); } catch (Exception e) { Log.Error(e.Message); }","preventionTips":["Assert both bodies are non-null and registered before creating any constraint","Prefer constraint types with single-body support when a second body may be absent"],"tags":["physics","constraints","null-argument"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}