特定の速度、重力速度、時間間隔で発射体を発射し、各時間間隔で速度/位置ベクトルを追跡して表示する小さな物理エンジンを構築しています。
現在、プログラムを実行すると、y
座標が正常に更新されます。ただし、私の座標x
とz
座標は同じように動作し、z
座標の計算が正しくないと確信しています。(でも間違ってるかも)
この問題は、軸上の位置ベクトルと速度ベクトルの両方で同じx
ですz
。
これが私のコードです:
include <iostream>
using namespace std;
struct velocityVector {
float vx = 10.0;
float vy = 14.14;
float vz = 10.0;
};
struct gravityVector {
float gx = 0.0;
float gy = -9.81;
float gz = 0.0;
};
struct positionVector {
float px = 0;
float py = 0;
float pz = 0;
};
int main() {
float deltaT = 0.01;
positionVector posAccess; // object for positionVectors
gravityVector gravAccess; // object for gravityVectors
velocityVector velAccess; // object for velocityVectors
while (deltaT < 1) {
deltaT += 0.01; // increment deltaT
cout << "Velocity vector = ";
// Display Velocity x,y,z
cout << velAccess.vx << " ";
cout << velAccess.vy << " ";
cout << velAccess.vz << " ";
cout << '\n';
cout << "Position vector = ";
// Display Position x,y,z
cout << posAccess.px << " ";
cout << posAccess.py << " ";
cout << posAccess.pz << " ";
cout << '\n' << endl;
// Update Velocity
velAccess.vx += deltaT * gravAccess.gx;
velAccess.vy += deltaT * gravAccess.gy;
velAccess.vz += deltaT * gravAccess.gz;
// Update Position
posAccess.px += deltaT * velAccess.vx;
posAccess.py += deltaT * velAccess.vy;
posAccess.pz += deltaT * velAccess.vz;
getchar(); // so I can go through each interval manually
}
}
それが役立つ場合。これが私の仕事です:
(10.0,14.14,- 10.0) の発射速度ベクトルを持つ発射体の 3D での軌道。時間ステップ = 0.01 秒。重力ベクトルは (0.0, -9.81, 0.0) です。
デモンストレーションの目的で、位置ベクトルと速度ベクトルをコンソールに表示します。