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

发布于 2024-11-09 01:52:21 字数 1193 浏览 0 评论 0原文

这是一个模拟网球从 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;

 }

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

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 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;

 }

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青瓷清茶倾城歌 2024-11-16 01:52:21

1/2 == 0 而不是 0.5

由于 1 和 2 都是整数,因此使用整数除法截断为最接近的整数。使用 0.5f 获取 float 或仅使用 0.5 获取双精度值。

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.

回梦 2024-11-16 01:52:21

time 是一个 int,而不是 float;那么它不会永远保持为零吗?

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

_畞蕅 2024-11-16 01:52:21

将时间声明为 float,而不是 int:因此,它在当前代码中根本不会改变。
中的三角函数接受弧度,而不是度数。

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.

独留℉清风醉 2024-11-16 01:52:21

看一下这个词

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

Look at the term

y = y + vy*deltaTime + (1/2*ay*deltaTime*deltaTime);   
゛时过境迁 2024-11-16 01:52:21

如果您有兴趣了解您的代码如何在 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();

我所做的唯一修改是对 Cos 和 Sin 的强制转换以及更改浮动时间。
并在一些初始值(如转换)后添加“f”。

这可能不是 C 的真正答案,但它可能是一条线索?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文