gpt4 book ai didi

c# - Visual C# 方法调用使用过多内存?

转载 作者:行者123 更新时间:2023-11-30 17:42:47 24 4
gpt4 key购买 nike

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

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

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

循环:

            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);
}

与我相交:

    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;
}
}
}

指向我:

    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;
}
}

最佳答案

谢谢大家花时间帮我看看这个。错误实际上出在我处理 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 });
}
}

所有对象的每一帧都会生成新的纹理。一种糟糕的做事方式。

关于c# - Visual C# 方法调用使用过多内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31636719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com