我一直在网上寻找我正在尝试编写有趣代码的物理引擎的集成方法(一定要喜欢那里的书呆子:P)。我找到了 Euler 的方法、RK4 和 Verlet(以及时间更正的版本)。我也一直在尝试想出一些我自己的方法。我想知道您是否知道其他任何您认为直观或有帮助的人。谢谢。
编辑:感谢您迄今为止的所有帮助。至于澄清:也许我的意思是数字积分。令人惊讶的是,在我所有的研究中,我没有找到我想要做的事情的技术名称!也许描述我的具体问题会使我的问题更清楚。假设我想模拟一个球在一个圆形(或一旦我实现 3d 的球形)引力场中移动。该球将遇到力矢量,该矢量可用于计算球在该特定刻度上所在点的相应加速度矢量。从你的物理课上,你知道速度 = 加速度 * 时间,但我的问题是球在技术上只是在那一刻,在微积分中用 dt 表示。明显地,
这是我实现欧拉数值积分方法的(成功)尝试:
//For console output. Note: I know I could just put "using namespace std;" but I hate doing that.
#include <iostream>
using std::cout;
using std::system;
using std::endl;
//Program entry
int main (void)
{
//Variable decleration;
double time = 0;
double position = 0;
double velocity = 0;
double acceleration = 2;
double dt = 0.000001; //Here is the "instantanious" change in time I was talking about.
double count = 0; //I use count to make sure I am only displaying the data at whole numbers.
//Each irritation of this loop is one tick
while (true)
{
//This next bit is a simplified form of Euler's method. It is what I want to "upgrade"
velocity += acceleration * dt;
position += velocity * dt;
if (count == 1/dt) //"count == 1/dt" will only return true if time is a whole number.
{
//Simple output to console
cout << "Time: " << time << endl;
cout << "Position: " << position << endl;
cout << "------------------" << endl;
system ("pause");
count = 0; //To reset the counter.
}
//Update the counters "count" and "time"
count++;
time += dt;
}
return 1; //Program exit
}
因为加速度是恒定的,而且这个微分实际上是可以解决的(为什么我用它来测试,解决方案是位置 = 时间 ^ 2,这是相当准确的,但是如果你让它变得更复杂一点,例如,使加速度随着时间的推移,算法会非常迅速地失去准确性。再次感谢!