小编Aeo*_*dyn的帖子

C#重力模拟代码表现得很奇怪

我一直在使用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;
    } …
Run Code Online (Sandbox Code Playgroud)

c# simulation gravity

4
推荐指数
1
解决办法
1309
查看次数

标签 统计

c# ×1

gravity ×1

simulation ×1