我一直在使用 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;
}
完整代码在这里:https ://docs.google.com/open?id=0B79vmyWxBr-kTnUtQURPUlVidzQ
谢谢你的帮助!