【发布时间】:2017-01-07 10:52:12
【问题描述】:
我正在构建一个小型物理引擎,它以给定的角度和速度发射弹丸,并在每个时间间隔跟踪和显示速度/位置矢量。目前,我的仓位值vars.posNew 似乎正在更新,但我的vars.x 和vars.y 值无法更新。
这是我的代码:
#include <iostream>
using namespace std;
#define PI 3.14159265359
struct vecVariables {
float v = 0, a = -9.81;
float posNew = 0, posOld = 0;
float x, y;
float theta = 45; // our start angle is 45
float u = 20; // our start velocity is 20
};
int main() {
float deltaT = 0.01;
vecVariables vars; // creates an object for Variables to be used
while (deltaT <= 1) {
deltaT += 0.01;
vars.v = vars.u + vars.a * deltaT; // gets the velocity V
vars.posNew = vars.posOld + vars.v * deltaT; // gets position D
vars.x = vars.u * cos(vars.theta * PI / 180); // <-- I'm going wrong somewhere here
vars.y = vars.u * sin(vars.theta* PI / 180);
cout << "velocity vec = [" << vars.x << " , " << vars.y << "]" << endl; // velocity on x,y
cout << "pos = "<< vars.posNew << endl; // display position
vars.posOld = vars.posNew;
getchar();
}
}
我知道vars.x 和vars.y 中的值是常量值,这让我简单地认为我应用了错误的公式来计算这些值,或者我只是遗漏了一件事?
【问题讨论】:
标签: c++ vector visual-studio-2015 physics