我的弹射游戏产生的距离不正确

时间:2015-11-17 08:16:27

标签: c++ formula physics

我似乎在我的弹射游戏中遇到了一个涉及距离方程的严重问题。该等式用于计算由具有给定速度和发射角度的弹射器发射的射弹的距离。通常距离总是高得离谱。

速度是通过某些用户输入密度计算的,因此也可能存在问题。我禁用了角度有效性检查并将触发角度设置为90(这不会给我0距离)并且我得到了相当高的数字。

我无法验证导致程序问题的原因是因为我不接受物理学,我会知道等式中的任何错误(我们的老师为我们提供了公式)。我想知道是否有任何精通物理学的程序员可以帮助我修复这个游戏?

P.S。我认为这个问题可能是由我的弧度转换引起的。这是我的代码片段:

          cout << "Choose a material to launch:" << endl
               << "1) BIRCH WOOD (670 kg/m^3)\n2) ICE (917 kg/m^3)\n3) FLAMING COAL (1500 kg/m^3)\n4) ASBESTOS (2450 kg/m^3)" << endl;
          cin  >> materialChoice;
          materialChoice = int(materialChoice);
          while (materialChoice < 1 || materialChoice > 4)
          {
                cout << "Please choose a valid material type:" << endl
                     << "1) BIRCH WOOD (670 kg/m^3)\n2) ICE (917 kg/m^3) 3) FLAMING COAL (1500 kg/m^3)\n4) ASBESTOS (2450 kg/m^3)" << endl;
                cin  >> materialChoice;
                materialChoice = int(materialChoice);
          }

          switch (materialChoice)
          {
          case 1:
               density = 670;
               break;
          case 2:
               density = 917;
               break;
          case 3:
               density = 1500;
               break;
          default: 
               density = 2450;
          }

          cout << "Now select a launch angle between 20 and 70 degrees:" << endl;
          cin  >> angleChoice;

          while (angleChoice < 20 || angleChoice > 70)
          {
              cout << "Please select a valid launch angle between 20 and 70 degrees:" << endl;
              cin  >> angleChoice;
          }    

          cout << "Now select the radius of the projectile between 6 cm and 10 cm:" << endl;
          cin  >> radius;
          while (radius < 6 || radius > 10)
          {
                cout << "Please select a valid radius for the projectile between 6 cm and 10 cm:" << endl;
                cin  >> radius;
          } 
          velocity = 360000.0/(density * radius);
          distance = 2 * sin(RADIAN * (angleChoice)) * velocity * cos(RADIAN * (angleChoice)) * velocity/9.8;

          cout << "The projectile goes flying! Press any number to check where it landed" << endl;
          cin  >> null;
          shortDiff = (abs(distance - 24));

1 个答案:

答案 0 :(得分:3)

RADIAN的值应为PI/180
(PI弧度)/(180度)*(以度为单位的ANGLE)将为您提供RADIAN的结果。
进行更改,然后根据需要进行调试。