Visual C#方法使用太多内存调用?

时间:2015-07-26 11:59:18

标签: c# memory floating-point xna

我正在使用XNA 4.0框架编写游戏。我编写了一组方法,将2D鼠标坐标转换为3d世界中的一条线,然后检查该线是否与平面相交,以及交点是否在该平面中的面的边界内

数学运算正常,但出于某种原因,当我每帧进行500次这样的计算时,它会使程序停止运行。在垃圾收集决定清理之前,我可以看到内存使用率从15 MB开始攀升到大约130 MB。我特别知道这是在这段代码中,因为当我发表评论时,其他一切都顺利进行。

我会在下面粘贴我的代码,任何见解都会有所帮助,谢谢!

The Loop:

            GraphicObject me = new GraphicObject();
            Intersection intersect;
            double? dist = null;

            foreach (GraphicObject obj in GraphicObjects)
            {
                intersect = obj.intersectMe(line);
                if (intersect.Distance != null)
                {
                    if (intersect.Distance < dist || dist == null)
                    {
                        dist = intersect.Distance;
                        me = obj;
                    }
                    else
                    {
                        obj.Highlight(false);
                    }
                }
                else
                {
                    obj.Highlight(false);
                }
            }

            if (dist != null)
            {
                me.Highlight(true);
            }

intersectMe:

    public override Intersection intersectMe(Ray _line)
    {
        GraphicHelper.Intersects(_line, rect.Vertices[0].Normal, rect.Vertices[0].Position, intersect);

        if (intersect.Distance != null)
        {
            if (!rect.PointOnMe(intersect.X - position.X, intersect.Y - position.Y, intersect.Z - position.Z))
            {
                intersect.Distance = null;
            }
        }

        return intersect;
    }

GraphicsHelper.Intersects:

    // _l = line, _n = normal to plane, _p = point on the plane
    public static void Intersects(Ray _l, Vector3 _n, Vector3 _p, Intersection _i)
    {
        _i.Distance = null;

        float num = (_n.X * (_p.X - _l.Position.X) + _n.Y * (_p.Y - _l.Position.Y) + _n.Z * (_p.Z - _l.Position.Z));

        float denom = (_n.X * _l.Direction.X + _n.Y * _l.Direction.Y + _n.Z * _l.Direction.Z);

        if (denom != 0 && num != 0)
        {
            float t = num / denom;

            if (t > 0)
            {
                _i.X = _l.Position.X + _l.Direction.X * t;
                _i.Y = _l.Position.Y + _l.Direction.Y * t;
                _i.Z = _l.Position.Z + _l.Direction.Z * t;

                _i.Distance = _i.X * _i.X + _i.Y * _i.Y + _i.Z * _i.Z;
            }
        }
    }

PointOnMe:

    public bool PointOnMe(float _x, float _y, float _z)
    {
        float ex = _x - Vertices[3].Position.X;
        float ey = _y - Vertices[3].Position.Y;
        float ez = _z - Vertices[3].Position.Z;

        float ae = a.X * ex + a.Y * ey + a.Z * ez;
        float be = b.X * ex + b.Y * ey + b.Z * ez;

        ex = _x - Vertices[1].Position.X;
        ey = _y - Vertices[1].Position.Y;
        ez = _z - Vertices[1].Position.Z;

        float ce = c.X * ex + c.Y * ex + c.Z * ez;
        float de = d.X * ex + d.Y * ey + d.Z * ez;

        if (ae > 0 && be > 0 && ce > 0 && de > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

1 个答案:

答案 0 :(得分:1)

谢谢大家花些时间为我看这个。这个错误实际上就是我如何处理obj.Highlight(),TaW的屁股让我得到了一个分析器设置帮我解决了这个问题。

    public override void Highlight(bool toggle)
    {
        if(toggle)
        {
            rect.Texture = new Texture2D(GraphicsManager.Graphics.GraphicsDevice, 1, 1);
            rect.Texture.SetData<Color>(new Color[] { Color.Yellow });
        }
        else
        {
            rect.Texture = new Texture2D(GraphicsManager.Graphics.GraphicsDevice, 1, 1);
            rect.Texture.SetData<Color>(new Color[] { squareColor });
        }
    }

所有obj的每一帧都生成了新的纹理。做事的可怕方式。