English 中文(简体)
C## 重力模拟代码变怪异
原标题:C# gravity simulation code acting weird

我一直在使用 C# 和 Winforms 进行重力模拟( 类似), 我得到了一些非常怪异的行为。 它在单击时几乎会形成一个物体, 并且被其他物体所吸引。 问题是, 除非它们距离很远, 否则它们不会被正面的方向吸引( 右、 下. ), 但是它们会被吸引到向上和向左 。

这是更新代码 :

    public Vector GetGravEffect(GravObject other)
    {
        if ((Math.Abs(X - other.X) <= Mass * Form.DrawScale + other.Mass * Form.DrawScale) && 
            (Math.Abs(Y - other.Y) <= Mass * Form.DrawScale + other.Mass * Form.DrawScale))
        {
            return new Vector(0, 0);
        }

        double tAngle = Math.Atan2((other.Y - Y), (other.X - X));

        double tMagnitude = (GravModifier * Mass * other.Mass / ((Math.Pow((other.X - X), 2)) + (Math.Pow((other.Y - Y), 2))) * 1000);

        Complex c = Complex.FromPolarCoordinates(tMagnitude, tAngle);

        Vector r = new Vector(c.Real, c.Imaginary);

        return r;
    }

完整代码在此 : < a href=" https://docs.google.com/open? id= 0B79vmyWxBr- knUtQuurPUlVidzQ" rel="nofol" >https://docs.google.com/open?id= 0B79vmyWxBr- knUtQurPUlVidz/a >

谢谢你的帮助!

最佳回答

问题不在于 GetGravEffect 方法( 它提供正确的结果), 而是在于 更新 方法 。 它完全忽略物理法则 。 您无法将 GetGravEffect 返回的值相加, 并且将它视为速度 。 获得一个对象吸引另一个对象的GravEffect 返回力 。 您必须对这些力量进行权衡, 然后做额外的计算, 包括惯性、 加速和计算结果速度的时间 。 另外, 将 X 和 Y 投进到一起并不是一个好主意, 因为您会松散很多精确性, 特别是对于慢速度 。 如果遵循正确的方法, 它效果很好 :

    public void Update() {
        Vector Force = new Vector(0, 0);
        foreach (GravObject g in Form.Objects) {
            if (this != g)
                Force += GetGravEffect(g);
        }

        double TimeElapsedSinceLastUpdate = Form.Timer.Interval * 0.001;
        Vector acceleration = Force / Mass;
        Velocity += acceleration*TimeElapsedSinceLastUpdate;

        X = (X + Velocity.X * Form.VelocityScale);
        Y = (Y + Velocity.Y * Form.VelocityScale);

        if (X + Mass * Form.DrawScale >= Form.Panels.Panel2.Width || X - Mass * Form.DrawScale <= 0)
            Velocity.X *= -1;
        if (Y + Mass * Form.DrawScale >= Form.Panels.Panel2.Height || Y - Mass * Form.DrawScale <= 0)
            Velocity.Y *= -1;
    }
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签