【问题标题】:Debugging a simple collision processor调试一个简单的碰撞处理器
【发布时间】:2012-01-22 19:34:20
【问题描述】:

我处理碰撞的方法是将速度向量分成两个分量,法线轴和切线轴,切线轴垂直于两个法线轴。法线轴平行于连接两个对象中心的线。

然后我将速度分量的法向量分量转换为动量。

Momentum = Velocity * Mass.

然后我将物体 A 的动量传递给 B,然后将 B 传递给 A。

然后我根据切轴上交换的动量和不变的速度找到A和B的对应速度。

在我看来是合理的,而且有效!

但问题是它有时会出错。

我使用 Visual Studio。

我对调试工具知之甚少,但无法很好地利用它来查找故障。

拜托,如果有人能帮忙,我将不胜感激。

这是我第四次从头开始编写整个代码。

但同样的方法总是会出错。

所以我开始从头开始写整个事情,希望我可以通过组织以某种方式解决它。请...帮助。

class Vector
{

    public double X = 0;
    public double Y = 0;
    public double Z = 0;
    public Vector direction
    {
        get { return new Vector(0.00000001 + X / Math.Abs(X), 0.00000001 + Y / Math.Abs(Y), 0.00000001 + Z / Math.Abs(Z)); }
    }

    public double angle { get { return Math.Atan2(Y, Z); } }

    //scholar operation
    public static Vector operator + (Vector v1 , double s) {
        return new Vector(v1.X + s , v1.Y + s , v1.Z +s );
    }
    public static Vector operator -(Vector v1, double s)
    {
        return new Vector(v1.X - s, v1.Y - s, v1.Z - s);
    }
    public static Vector operator *(Vector v, double s)
    {
        return new Vector(v.X * s, v.Y * s, v.Z * s);
    }
    //vector to vector operation
    public static Vector operator +(Vector a, Vector b)
    {
        return new Vector(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
    }
    public static Vector operator -(Vector a, Vector b)
    {
        return new Vector(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
    }
    public static Vector operator *(Vector a, Vector b)
    {
        return new Vector(a.X * b.X, a.Y * b.Y, a.Z *b.Z);
    }
    //other operations
    public static Vector operator %(Vector a, Vector b)
    {
        return new Vector(a.Y*b.Z - a.Z*b.Y, a.Z*b.X - a.X*b.Z , a.X*b.Y - a.Y*b.X);
    }
    public static double dotProduct(Vector a, Vector b)
    {
        Vector v = a*b;
        return a.X + a.Y + a.Z;
    }
    public static void makeOrthonormalBasic(Vector a, Vector b, Vector c)
    {
        c = b % a;
        if (c.squareMagnitude() != 0)
        {
            a = c % b;
        }
    }

    public Vector()
    {
        X = 0;
        Y = 0;
        Z = 0;
    }
    public Vector(double x, double y, double z)
    {
        X = x;
        Y = y;
        Z = z;
    }
    public Vector (double x , double y )
    {
        X= x;
        Y = y;
    }

    public void flip()
    {
        X *= -1;
        Y *= -1;
        Z *= -1;
    }
    public Vector fliped_vector()
    {
        Vector v = new Vector(X * -1, Y * -1, Z * -1);
        return v;
    }
    public double magnitude()
    {
        return Math.Sqrt(X * X + Y * Y + Z * Z);            
    }
    public double squareMagnitude() {
        return X * X + Y * Y + Z * Z;
    }
    public Vector normalize()
    {
        double m = magnitude();
        if (m > 0)
        {
            Vector v = new Vector();
            v.X = this.X;
            v.Y = this.Y;
            v *= 1 / m;
            return v;
        }
        return new Vector() ;
    }

    public void zero()
    {
        X = 0;
        Y = 0;
    }
    public void set(double x, double y)
    {
        X = x;
        Y = y;
    }
    public void add(double x, double y)
    {
        X += x;
        Y += y;
    }

}

class KCircle
{
    double size = 30;
    #region instant variables
    public Ellipse shape = new Ellipse();
    public Vector force = new Vector();
    public Vector velocity = new Vector();
    public Vector acceleration = new Vector();
    public Vector position = new Vector();
    #endregion
    public double Vector
    {
        get { return Math.Sqrt(velocity.X * velocity.X + velocity.Y * velocity.Y); }
    }
    public double Angle
    {
        get { return Math.Atan2(velocity.Y, velocity.X); }
    }
    public double radius
    {
        get { return shape.Width / 2; }
    }
    public double mass
    {
        get { return Math.PI * radius * radius; }
    }

    public KCircle()
    {
        initialize();
    }
    public void initialize()
    {
        color_normal();
        shape.StrokeThickness = 5;
        shape.Width = shape.Height = size;
    }

    public void change_color()
    {
        shape.Fill = System.Windows.Media.Brushes.Red;
        if (shape.Stroke == System.Windows.Media.Brushes.Red)
        {
            shape.Stroke = System.Windows.Media.Brushes.Blue;
            return;
        }
        shape.Stroke = System.Windows.Media.Brushes.Red;
    }
    public void color_normal()
    {
        shape.Stroke = System.Windows.Media.Brushes.Black;
        shape.Fill = System.Windows.Media.Brushes.White;
    }
}

private void process_object_interactions(){
        for (int i = 0; i < circles.Count; i++)
        {
            KCircle a = circles[i];
            for (int z = i + 1; z < circles.Count; z++)
            {
                KCircle b = circles[z];
                if (collide(a, b)  )
                {
                    a.change_color();
                    b.change_color();

                    Vector d = distance_between(a, b);
                    double angle_n = Math.Atan2(d.Y, d.X);
                    double angle_t = angle_n + 90;
                    double direction_n = 1;
                    double direction_t = 1;
                    if (angle_n < 0)
                    {
                        angle_n += 360;
                    }

                    if (angle_n > 180)
                    {
                        direction_t = -1;
                    }
                    if (angle_n > 90 && angle_n < 270)
                    {
                        angle_t = angle_n - 90;
                        angle_n += 180;
                        direction_n = -1;
                    }
                    if (angle_n > 360)
                    {
                        angle_n -= 360;
                    }
                    KCircle[] c = new KCircle[2];
                    c[0] = a;
                    c[1] = b;
                    double[] direction_n_array = new double[2];
                    direction_n_array[0] = direction_n;
                    direction_n_array[1] = direction_n * -1;
                    double[] direction_t_array = new double[2];
                    direction_t_array[0] = direction_t;
                    direction_t_array[1] = direction_t * -1;
                    double[] velocity_t = new double[2];
                    double[] velocity_n = new double[2];

                    for (int v = 0; v < 2; v++)
                    {
                        double angle = Math.Abs(c[i].velocity.angle - angle_n);
                        velocity_n[v] =(direction_n_array[v]) * (c[v].velocity.magnitude() * Math.Cos(angle));
                        velocity_t[v] = (direction_t_array[v]) *(c[v].velocity.magnitude() * Math.Sin(angle));
                    }
                    Vector[] new_vector_n = new Vector[2];
                    Vector[] momentum = new Vector[2];
                    for (int v = 0; v < 2; v++)
                    {
                        new_vector_n[v] = new Vector(velocity_n[v] * Math.Cos(angle_n), (velocity_n[v] * Math.Sin(angle_n)));
                        momentum[v] = new_vector_n[v];
                        momentum[v] *= c[v].mass;
                    }
                    /**
                    velocity_n_new[0] = normal_velocity_exchange(velocity_n[0], velocity_n[1], c[0].mass, c[1].mass);
                    velocity_n_new[1] = normal_velocity_exchange(velocity_n[1], velocity_n[0], c[1].mass, c[0].mass);
                     * */
                    for (int v = 0; v < 2; v++)
                    {
                        c[v].velocity.X = velocity_t[v] * Math.Cos( angle_t);
                        c[v].velocity.Y = -1* ((velocity_t[v] )* Math.Sin(angle_t)) ;
                    }
                    c[0].velocity += momentum[1] * (1 / c[0].mass);
                    c[1].velocity += momentum[0] * (1 / c[1].mass);
            }
        }
    }
}

【问题讨论】:

  • 您能描述一下您遇到的错误吗?它会影响最佳的调试策略。
  • 可以给我们介绍一下KCircle和Vector的实现吗?
  • 它的 wpf.program 刚刚停止,这就是我知道我遇到错误的方式
  • 当然我会尽快发布它们
  • 完成发布 Kcircle 和 Vector 类

标签: c# wpf visual-studio debugging collision


【解决方案1】:

已提出的问题分为两部分。

  1. 如何修复代码以正常运行?
  2. 如何使用 Visual Studio 调试器?

这个答案将解决第二个问题。

首先从 Visual Studio 以调试模式运行程序。如果顶部的工具栏中有一个条目显示“发布”,请将其拉​​下并将其更改为“调试”。单击绿色三角形启动程序。

遇到错误时,Visual Studio 应显示异常助手对话框,如下所述:http://msdn.microsoft.com/en-us/library/2ww37f14.aspx

这个对话框的标题是什么?它应该说明遇到了什么类型的异常。

在“操作”部分中,单击“查看详细信息”以查看有关异常的更多信息。

展开详细信息并找到堆栈跟踪。下拉右侧的 Stack Trace 以展开它。 Stack Trace 的第一行会告诉你是哪一行源代码导致了错误,以及之前的方法调用顺序是什么。

使用此基本调试信息来帮助确定问题所在,或使用它为您的问题添加更具体的错误信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多