-2

StackOverflowException在我的 C# 程序中得到了一个。

Cmodel.cs

public class CModel
{
    public Vector3 Position { get; set; }

    public Vector3 Rotation { get; set; }

    public Vector3 Scale { get; set; }

    public Model Model { get; private set; }

    public BoundingSphere BoundingSphere
    {
        get
        {
            // no need for rotation, as this is a sphere
            Matrix worldTransform = Matrix.CreateScale(Scale) *
            Matrix.CreateTranslation(Position); // THIS IS WHERE THE EXCEPTION OCCURS

            BoundingSphere transformed = BoundingSphere;
            transformed = transformed.Transform(worldTransform);

            return transformed;
        }
    }
    private Matrix[] modelTransforms;
    private GraphicsDevice graphicsDevice;
    private BoundingSphere boundingsphere;
    public CModel(Model Model, Vector3 Position, Vector3 Rotation,
    Vector3 Scale, GraphicsDevice graphicsDevice)
    {
    this.Model = Model;

    modelTransforms = new Matrix[Model.Bones.Count];
    Model.CopyAbsoluteBoneTransformsTo(modelTransforms);

    buildBoundingSphere();
    }


    public void Draw(Matrix View, Matrix Projection)
    {
        // Calculate the base transformation by combining
        // translation, rotation, and scaling
        Matrix baseWorld = Matrix.CreateScale(Scale)
        * Matrix.CreateFromYawPitchRoll(
        Rotation.Y, Rotation.X, Rotation.Z)
        * Matrix.CreateTranslation(Position);
        foreach (ModelMesh mesh in Model.Meshes)
        {
            Matrix localWorld = modelTransforms[mesh.ParentBone.Index]
            * baseWorld;
            foreach (ModelMeshPart meshPart in mesh.MeshParts)
            {
                BasicEffect effect = (BasicEffect)meshPart.Effect;
                effect.World = localWorld;
                effect.View = View;
                effect.Projection = Projection;
                effect.EnableDefaultLighting();
            }
            mesh.Draw();
        }
    }
        private void buildBoundingSphere()
        {
            BoundingSphere sphere = new BoundingSphere(Vector3.Zero, 0);

            // Merge all the model's built in bounding spheres
            foreach (ModelMesh mesh in Model.Meshes)
            {
                BoundingSphere transformed = mesh.BoundingSphere.Transform(
                    modelTransforms[mesh.ParentBone.Index]);

                sphere = BoundingSphere.CreateMerged(sphere, transformed);
            }

            this.boundingsphere = sphere;
        }
    }
}
4

4 回答 4

4

您的 getter 中有一个递归调用,它将调用自身并导致StackOverflowException

public BoundingSphere BoundingSphere
{
    get
    {
       ...
       BoundingSphere transformed = BoundingSphere;
       ...
    }
}

尚不完全清楚您要写什么- 但如果您想保留任何状态,您将需要一个支持字段来存储边界球体。

于 2012-06-03T15:00:51.797 回答
1

您的 get 方法调用自身:BoundingSphere调用BoundingSphere.

private BoundingSphere _boundingsphere = null;
public BoundingSphere BoundingSphere
{
    get
    {
        // no need for rotation, as this is a sphere
        **Matrix worldTransform = Matrix.CreateScale(Scale)
            * Matrix.CreateTranslation(Position);**

        BoundingSphere transformed = _boundingsphere;
        transformed = transformed.Transform(worldTransform);

        return transformed;
    }
    set
    {
        _boundingsphere = value;
    }
}

当您使用以下表格时:

public BoundingSphere BoundingSphere { get; set }

您不需要指定变量来存储实际值,但是当您显式实现 get 或 set 时,您应该声明附加变量并在 get、set 实现中使用它。

于 2012-06-03T15:05:10.487 回答
-1

我认为你需要改变

get 
{ 
    ...
    BoundingSphere transformed = BoundingSphere; // public property
    ...
}

对此

get 
{ 
    ...
    BoundingSphere transformed = boundingsphere; // private variable
    ...
} 
于 2012-08-20T19:06:19.590 回答
-1
  1. 查看调用堆栈或异常的堆栈跟踪。
  2. 识别回路。
  3. 弄清楚如何打破循环。
于 2012-06-03T15:03:35.433 回答