{"record":{"id":"bc689e1e1bb7b4d8","repo":"MonoGame/MonoGame","slug":"nearplanedistance-farplanedistance","errorCode":null,"errorMessage":"nearPlaneDistance >= farPlaneDistance","messagePattern":"nearPlaneDistance >= farPlaneDistance","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework/Matrix.cs","lineNumber":927,"sourceCode":"        /// </summary>\r\n        /// <param name=\"width\">Width of the viewing volume.</param>\r\n        /// <param name=\"height\">Height of the viewing volume.</param>\r\n        /// <param name=\"nearPlaneDistance\">Distance to the near plane.</param>\r\n        /// <param name=\"farPlaneDistance\">Distance to the far plane, or <see cref=\"float.PositiveInfinity\"/>.</param>\r\n        /// <param name=\"result\">The new projection <see cref=\"Matrix\"/> for perspective view as an output parameter.</param>\r\n        public static void CreatePerspective(float width, float height, float nearPlaneDistance, float farPlaneDistance, out Matrix result)\r\n        {\r\n            if (nearPlaneDistance <= 0f)\r\n\t\t    {\r\n\t\t        throw new ArgumentException(\"nearPlaneDistance <= 0\");\r\n\t\t    }\r\n\t\t    if (farPlaneDistance <= 0f)\r\n\t\t    {\r\n\t\t        throw new ArgumentException(\"farPlaneDistance <= 0\");\r\n\t\t    }\r\n\t\t    if (nearPlaneDistance >= farPlaneDistance)\r\n\t\t    {\r\n\t\t        throw new ArgumentException(\"nearPlaneDistance >= farPlaneDistance\");\r\n\t\t    }\r\n\r\n            var negFarRange = float.IsPositiveInfinity(farPlaneDistance) ? -1.0f : farPlaneDistance / (nearPlaneDistance - farPlaneDistance);\r\n\r\n            result.M11 = (2.0f * nearPlaneDistance) / width;\r\n            result.M12 = result.M13 = result.M14 = 0.0f;\r\n            result.M22 = (2.0f * nearPlaneDistance) / height;\r\n            result.M21 = result.M23 = result.M24 = 0.0f;            \r\n            result.M33 = negFarRange;\r\n            result.M31 = result.M32 = 0.0f;\r\n            result.M34 = -1.0f;\r\n            result.M41 = result.M42 = result.M44 = 0.0f;\r\n            result.M43 = nearPlaneDistance * negFarRange;\r\n        }\r\n\r\n        /// <summary>\r\n        /// Creates a new projection <see cref=\"Matrix\"/> for perspective view with field of view.\r\n        /// </summary>\r","sourceCodeStart":909,"sourceCodeEnd":945,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework/Matrix.cs#L909-L945","documentation":"Thrown as ArgumentException by Matrix.CreatePerspective when nearPlaneDistance is greater than or equal to farPlaneDistance. The near plane must be strictly in front of the far plane for a valid frustum.","triggerScenarios":"Passing near >= far (e.g. near=10, far=10, or near=100, far=50); swapped near/far arguments; equal values from config.","commonSituations":"Swapping near and far when calling; config typos making near larger than far; dynamically computed planes that cross.","solutions":["Ensure nearPlaneDistance < farPlaneDistance (e.g. near=0.1, far=1000).","Swap the arguments if they were reversed.","Validate and clamp near to be strictly less than far before calling."],"exampleFix":"// before\nMatrix.CreatePerspective(w, h, 1000f, 0.1f, out proj); // swapped\n\n// after\nMatrix.CreatePerspective(w, h, 0.1f, 1000f, out proj);","handlingStrategy":"validation","validationCode":"if (nearPlaneDistance >= farPlaneDistance)\n    throw new ArgumentException(\"near must be less than far.\");\nMatrix.CreatePerspective(w, h, nearPlaneDistance, farPlaneDistance, out proj);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass near < far.","Double-check argument order (near before far).","Validate dynamic plane computations do not cross."],"tags":["monogame","math","matrix","camera","argument"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}