弹丸不会以负角向下
Projectile not going downwards on negative angle
我正在构建一个小型物理引擎,它根据用户的一组发射参数(角度、高度、时间间隔和初始速度)发射弹丸,然后显示一些信息,例如总距离或角度它在空中的每个时间间隔:
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
总计 T = 10
我的程序显示了预期的结果,即我的射弹先升后降。但是,如果我为 thetaDegrees 输入相同的期望值 -40,我的射弹 应该直接向下 ,而不是 它只是上升然后下降.
我的代码哪里出错了?
正如 Igor 所说,在 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;
}
你要么必须改变
xNew = xOld + velocityX * time; // works out displacement for X
到
xNew = xOld + velocityX * deltaTime; // works out displacement for X
或删除
xOld = xNew;
最后,因为两者都保持不变,您将得到二次方加速的 x 坐标,而不是等速的线性运动。
我正在构建一个小型物理引擎,它根据用户的一组发射参数(角度、高度、时间间隔和初始速度)发射弹丸,然后显示一些信息,例如总距离或角度它在空中的每个时间间隔:
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
总计 T = 10
我的程序显示了预期的结果,即我的射弹先升后降。但是,如果我为 thetaDegrees 输入相同的期望值 -40,我的射弹 应该直接向下 ,而不是 它只是上升然后下降.
我的代码哪里出错了?
正如 Igor 所说,在 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;
}
你要么必须改变
xNew = xOld + velocityX * time; // works out displacement for X
到
xNew = xOld + velocityX * deltaTime; // works out displacement for X
或删除
xOld = xNew;
最后,因为两者都保持不变,您将得到二次方加速的 x 坐标,而不是等速的线性运动。