2016-11-27 5 views
-1

私は小さな物理エンジンを構築しています。この物理エンジンは、ユーザーによる起動パラメータのセット(角度、高さ、時間間隔、初期速度)で投射し、それは、空気中だと、すべての時間間隔で総距離や角度:投射物が負の角度で下向きにならない

bool heightCheck = false; 
double theta; 
double initialVelocity, velocity; 
double yNew = 0.0, xNew, xOld = 0.0, yOld = 0.0; 
const double time = 0.1; 
const double gravitiyHalf = 9.8/2; 
double velocityX = 0.0, velocityY = 0.0; 
double angle = 0.0; 
double totalT = 0; 
double maxHeight = 0.0; 
double thetaDegrees = 0; 
#define PI 3.14159265l // constant for PI 

cout << "Insert a lanuch Angle (theta): "; 
cin >> thetaDegrees;  
cout << "Insert a launch height: ";  
cin >> yOld;    
cout << "Insert an initial velocity: ";  
cin >> initialVelocity;  
cout << "Time (DeltaT) in seconds: "; 
cin >> totalT; 

for (double deltaTime = 0.0; deltaTime < totalT; deltaTime += 0.1) { 

    const double squared = deltaTime * deltaTime;  // squared constant for deltaTime squared 

    theta = thetaDegrees * PI/180; // converts theta to a degrees value 

    velocityX = initialVelocity * cos(theta); // calculates Vx 
    velocityY = initialVelocity * sin(theta); // calculates Vy 

    // apply initialV to velocity 
    velocity = initialVelocity + 9.8 * time; 

    xNew = xOld + velocity * time; // works out displacement for X 

    yNew = yOld + velocity * deltaTime - gravitiyHalf/0.5 * (squared); // calculates Y 

    velocityY = velocity - 9.8 * deltaTime; // includes gravity to Y 

    angle = atan2(yNew, xNew) * 180/PI; // convert angle to degrees 

    cout << "\nHeight: " << yNew << endl; 
    cout << "Distance in Meters: " << xNew << "m" << endl; 
    cout << "Angle: " << angle << endl; 
    cout << "Time: " << deltaTime << "s " << endl; 

    if (heightCheck == false) { 
     maxHeight = yOld; 
     // keep maxheight equal to previous height 
    } 

    if (yNew < yOld && heightCheck == false) { 
     heightCheck = true; 
     // if projectile is going down, trigger maxheight 
    } 

    cout << "Maximum height: " << maxHeight << endl; 

    if ((yNew < 0) || (deltaTime == totalT)) { 
     getchar();  // stops if delta t = total T or projectile landed 
    } 

    yOld = yNew; // refresh x & y 
    xOld = xNew; 
} 

私は私のプログラムの開始時に以下の値を入力した場合:

theteDegrees = 45

yOld = 0

initialVelocity = 20

totalT = 10

私のプログラムは、私の発射は、その後ダウンし、上がって表示され、期待される結果を表示します。しかし、同じ値を入力してthetaDegreesの-40を入力すると、私の弾丸のように真っ直ぐになり、のようになります。のようになります。

私のコードでどこが間違っていますか?

+2

'thetaDegrees'は' velocityX'と 'velocityY'の計算に入ります - 後で実際にはどちらも使われません。効果的に45度の撮影角度をハードコードします。 –

+0

コンパイラツールセットに付属のデバッガを使用していますか?あなたのような質問は、デバッガを使うと簡単に解決できます。デバッガの使い方を学ぶことは、プログラムを書く方法を学ぶ過程で必須です。 – PaulMcKenzie

+0

@IgorTandetnikちょうどそれを固定しました。私は誤って 'velocity'を' yNew'と 'xNew'の両方に使用していたことを気付かず、' velocityX'と 'velocityY'を使用していたはずです。 –

答えて

0

イゴール氏によると、XとYの距離計算では、velocityXとvelocityYを考慮していません。

xNew = xOld + velocity * time; // works out displacement for X 
    yNew = yOld + velocity * deltaTime - gravitiyHalf/0.5 * (squared); // calculates Y 

いくつかの冗長な計算があります。もっと単純化されたバージョンはこれのようなものです。

theta = thetaDegrees * PI/180; // converts theta to a degrees value 

velocityX = initialVelocity * cos(theta); // calculates Vx 
velocityY = initialVelocity * sin(theta); // calculates Vy 

//cout<<velocityX<<endl<<velocityY<<endl; 

for (double deltaTime = 0.0; deltaTime < totalT; deltaTime += 0.1) { 

const double squared = deltaTime * deltaTime;  // squared constant for deltaTime squared 

xNew = xOld + velocityX * time; // works out displacement for X 

yNew = yOld + velocityY * deltaTime - gravitiyHalf/0.5 * (squared); // calculates Y 

velocityY = velocityY - 9.8 * deltaTime; // includes gravity to Y 

angle = atan2(yNew, xNew) * 180/PI; // convert angle to degrees 

cout << "\nHeight: " << yNew << endl; 
cout << "Distance in Meters: " << xNew << "m" << endl; 
cout << "Angle: " << angle << endl; 
cout << "Time: " << deltaTime << "s " << endl; 

if (heightCheck == false) { 
    maxHeight = yOld; 
    // keep maxheight equal to previous height 
} 

if (yNew < yOld && heightCheck == false) { 
    heightCheck = true; 
    // if projectile is going down, trigger maxheight 
} 

cout << "Maximum height: " << maxHeight << endl; 

if ((yNew < 0) || (deltaTime == totalT)) { 
    getchar();  // stops if delta t = total T or projectile landed 
} 

yOld = yNew; // refresh x & y 
xOld = xNew; 
} 
0

あなたはどちらかが

xNew = xOld + velocityX * time; // works out displacement for X 

xNew = xOld + velocityX * deltaTime; // works out displacement for X 

を変更したり、残して、最後に

xOld = xNew; 

を削除する必要があり、両方のあなたの二次の高速化を与えるだろう変わらずx座標ではなく、一定速度の直線運動。

関連する問題