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

Incorrect distance being produced by my catapult game

本文关键字:距离 不正确 产生了 弹射器 游戏 我的      更新时间:2023-10-16

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

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

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

附言:我想问题可能是由我的弧度转换引起的。以下是我的代码片段:

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

RADIAN的值应该是PI/180
(PI弧度)/(180度)*(角度,以度为单位)将以radian为单位给出结果
进行更改,然后根据需要进行调试。