首先,我正在使用非常接近欧拉方法的方法来计算我的星球的位置。我知道这不是最准确的方法,但我一直在使用velocity verlet 近一个星期,但无法让它发挥作用。我的问题是我无法让我的星球围绕太阳循环,x 或 y 位置不断增加。任何帮助表示赞赏!谢谢!这是我的代码:
void updatePosition(CelestialObject object1, CelestialObject object2 )
{ // -----------------------------X calculations-----------------------------------
//calc force
float forceX = forceFuncX(object1.getX(), object2.getX(),object1.getY(), object2.getY(), object1.getMass(), object2.getMass());`
//accel calc
float AX = accelerationFuncX(forceX,object1.getX(), object2.getX(),object1.getMass());
agk::PrintC("Accel X: ");
float AXprint = AX*dt;
agk::Print(AXprint);
//velocity
float VX = object1.getVX();
VX = VX + AX*dt;
agk::PrintC("Velocity X: ");
agk::Print(VX);
//positionCalc
float X = object1.getX();
X = X + VX*dt;
agk::PrintC("Position X: ");
agk::Print(X);
//-------------------------Y calculations------------------------------------
//force
float forceY = forceFuncY(object1.getX(), object2.getX(),object1.getY(), object2.getY(), object1.getMass(), object2.getMass());
//accel
float AY = accelerationFuncY(forceY,object1.getY(),object2.getY(), object1.getMass()); //y
agk::PrintC("Accel Y: ");
float AYprint = AY*dt;
agk::Print(AYprint);
//velocity
float VY = object1.getVY();
VY = VY + AY*dt;
agk::PrintC("Velocity Y: ");
agk::Print(VY);
//position
float Y = object1.getY();
Y = Y + VY*dt;
agk::PrintC("Position Y: ");
agk::Print(Y);
object1.setPosition(X, Y);
agk::CreateParticles(X,Y);
}
这是它调用的函数:
double forceFuncX(float object1x,float object2x,float object1y, float object2y, double mass1, double mass2)
{
float d = object1x - object2x;
float r = sqrt(pow(object2x - object1x,2) + pow(object2y-object1y,2));;
//float r = sqrt(pow(object1x-object2x,2)+pow(object1y-object2y,2));
//double F = (G*(mass1*mass2))/pow(d,2);
float F = (G*(mass1*mass2))/(r*r);
return F;
}
double forceFuncY(float object1x,float object2x,float object1y, float object2y, double mass1, double mass2)
{
float d = object1y - object2y;
float r = sqrt(pow(object2x - object1x,2) + pow(object2y-object1y,2));;
//float r = sqrt(pow(object1x-object2x,2)+pow(object1y-object2y,2));
//double F = (G*(mass1*mass2))/pow(d,2);
float F = (G*(mass1*mass2))/(r*r);
return F;
}
和
float accelerationFuncX(float force, float object1x, float object2x, double mass) //gives the acceleration of an object
{
float accel = (force*(object2x-object1x))/mass;
return accel;
}
float accelerationFuncY(float force, float object1y, float object2y, double mass)
{
float accel =(force*(object2y-object1y))/mass;
return accel;
}