小编use*_*732的帖子

计算行星之间的吸引力方向

我正在尝试使用C在opengl中建立太阳系的模拟.我似乎无法弄清楚如何编写用于计算另一个行星施加在行星上的力的方向的代码.这就是我到目前为止:

void attraction(planet self, planet other, float *direction)
{
   float d = vecDistance(self.p, other.p);

   //calc the force of attraction
   float f = G * ((self.mass * other.mass) / (pow(d, 2)));

   //calc the direction of the force
   float vectorDist[3];
   vecSub(self.p, other.p, vectorDist);

   direction[0] = f*vectorDist[0] / d;
   direction[1] = f*vectorDist[1] / d;
   direction[2] = f*vectorDist[2] / d;
}



float vecDistance( float *pV1, float *pV2 )
{
    float fLen=0.0f;

    if(pV1 && pV2)
    {
        float av[3];

        vecSub(pV2, pV1, av);
        fLen=vecLength(av);
    }

    return fLen; …
Run Code Online (Sandbox Code Playgroud)

c math physics

5
推荐指数
1
解决办法
206
查看次数

标签 统计

c ×1

math ×1

physics ×1