基于小型物理程序的无限循环

kac*_*ous 0 c physics

这是一个模拟网球被抛出50米建筑物侧面的程序.程序应在每个时间步输出x,y和速度值.但是,我似乎得到了无限循环.

 #include<stdio.h>
 #include<math.h>

 int main() {

     //Intial values 
     float ax = 0; //acceleration in the horizontal direction
     float ay = -9.8; //acceleration in the downward direction
     float x = 0; //top of building at position 0 
     float y = 50; //building is height 50 m
     float vx = 10*cos(30); //velocity in the horizontal direction = 10 m/s * cos(30); 
     float vy = 10*sin(30); //velocity in the vertical direction = 10 m/s * sin(30);     
     int time = 0; //time starts at 0 seconds
     float deltaTime = 0.001; //increment time by .001 each iteration

     //while ball is greater than 0, or above the ground which is at position 0
     while(y > 0) {

     time = time + deltaTime;
     vx = vx + ax*deltaTime;
     vy = vy + ay*deltaTime;
     x = x + vx*deltaTime + (1/2*ax*deltaTime*deltaTime);
     y = y + vy*deltaTime + (1/2*ay*deltaTime*deltaTime);     

     printf("x = %f, y = %f, vx = %f, vy = %f, time = %d, ", x,y,vx,vy,time);

     }
     system ("PAUSE"); 
     return 0;

 }
Run Code Online (Sandbox Code Playgroud)

我的猜测是y永远不会小于0,但由于我有限的物理知识,我不知道如何解决它.

Cod*_*aos 8

1/2 == 0不是0.5

由于1和2都是整数,因此使用整数除法,它截断为最接近的整数.使用0.5f获得float或者只是0.5得到一个双.