开发者

Infinite Loop in small physics based program

开发者 https://www.devze.com 2023-03-07 02:36 出处:网络
Here is a program that models a tennis ball being thrown off the side of a 50 meter building. The program should output the x, y, and velocity values at each time step.

Here is a program that models a tennis ball being thrown off the side of a 50 meter building. The program should output the x, y, and velocity values at each time step. However, I seem to be getting an infinite loop.

 #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 del开发者_StackOverflow中文版taTime = 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;

 }

My guess is that y will never become smaller than 0, but because of my limited physics knowledge, I don't know how I could fix it.


1/2 == 0 not 0.5

Since 1 and 2 are both integers this uses integer division which truncates to the closest integral number. Use 0.5f to get a float or just 0.5 to get a double.


time is an int, not a float; so won't that stay zero forever?


Declare time as float, not int: it's not changing at all in your current code because of this. Trigonometric functions in <math.h> accept radians, not degrees.


Look at the term

y = y + vy*deltaTime + (1/2*ay*deltaTime*deltaTime);   


If you are interested in finding out how your code, with little modification finishes without a killer loop in C#:

            float ax = 0; //acceleration in the horizontal direction
            float ay = -9.8f; //acceleration in the downward direction
            float x = 0; //top of building at position 0 
            float y = 50; //building is height 50 m
            float vx = 10f * (float)Math.Cos(30); //velocity in the horizontal direction = 10 m/s * cos(30); 
            float vy = 10 * (float)Math.Sin(30); //velocity in the vertical direction = 10 m/s * sin(30);     
            float time = 0; //time starts at 0 seconds
            float deltaTime = 0.001f; //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);

                Console.WriteLine("x = {0}, y = {1}, vx = {2}, vy = {3}, time = {4}, ", x, y, vx, vy, time);

            }
            Console.ReadKey();

The only modification I did are the casts on Cos and Sin and changing time to float. And added the 'f' after some initial values (like casting).

It may not be a real answer for C, but it could be a clue?

0

精彩评论

暂无评论...
验证码 换一张
取 消