我试图模拟一个蹦极跳,给定主体的质量,弹力绳的弹簧常数,绳索的初始长度,以及找到加速度,速度和距离下降所需的其他值。学科。我必须使用基本类和算术才能完成这个项目。
这是我创建的课程:
#include "FallingObject.h"
#include <cmath>
#include <iostream>
using namespace std;
FallingObject::FallingObject(float mass, float surfaceArea, float bungeeSpringConstant, float bungeeUnstretchedLength) {
this->mass = mass;
this->surfaceArea = surfaceArea;
this->bungeeSpringConstant = bungeeSpringConstant;
this->bungeeUnstretchedLength = bungeeUnstretchedLength;
}
float FallingObject::getMass(){
return this->mass;
}
float FallingObject::getSurfaceArea(){
return this->surfaceArea;
}
float FallingObject::getBungeeSpringConstant(){
return this->bungeeSpringConstant;
}
float FallingObject::getBungeeUnstretchedLength(){
return this->bungeeUnstretchedLength;
}
void FallingObject::simulateTimeStep(float deltaT){
float g = 9.81, timeStep = 0.0;
float fWeight, fFriction, fSpring, fTotal;
float currentV, currentD, currentA;
velocity = 0.0;
distance = 0.0;
acceleration = 0.0;
//Acceleration @ timeStep = 0:
fWeight = mass * g;
fFriction = (-0.65) * surfaceArea * velocity * abs(velocity);
fSpring = (-1) * bungeeSpringConstant * distance;
fTotal = fWeight + fFriction + fSpring;
currentA = fTotal / mass;
//Velocity @ timeStep = 0
currentV = acceleration * timeStep;
//Distance @ timeStep = 0
currentD = velocity * timeStep;
//increment values
timeStep += deltaT;
acceleration += currentA;
velocity += currentV;
distance += currentD;
}
float FallingObject::getFallDistance(){
return this->distance;
}
float FallingObject::getVelocity(){
return this->velocity;
}
float FallingObject::getAcceleration(){
return this->acceleration;
}
这是主要功能:
int _tmain(int argc, _TCHAR* argv[])
{
// arguments in order:
// mass (70kg)
// surface area (0.2m^2)
// spring constant for the bungee cord (21.7)
// unstretched length of bungee cord (30m)
FallingObject fallingObject(70.0f, 0.2f, 21.7f, 30.0f);
vector<float> elapsedTimes;
vector<float> distances;
vector<float> velocities;
vector<float> accelerations;
ofstream outFile("bungee_data.txt");
// time step zero
elapsedTimes.push_back(0.0f);
distances.push_back(fallingObject.getFallDistance());
velocities.push_back(fallingObject.getVelocity());
accelerations.push_back(fallingObject.getAcceleration());
float simulationTime = 60; // 60 seconds
float deltaT = 0.01; // 0.01 seconds
int timeSteps = (int)(simulationTime / deltaT);
for (unsigned int t = 1; t < timeSteps; t++) {
fallingObject.simulateTimeStep(deltaT);
elapsedTimes.push_back(t * deltaT);
distances.push_back(fallingObject.getFallDistance());
velocities.push_back(fallingObject.getVelocity());
accelerations.push_back(fallingObject.getAcceleration());
}
for (int i = 0; i < timeSteps; i++){
outFile << elapsedTimes[i] << ",";
outFile << distances[i] << ",";
outFile << velocities[i] << ",";
outFile << accelerations[i] << endl;
}
return 0;
}
我遇到的问题是值没有更新,我无法确定原因。
任何人都可以对此问题提供一些帮助吗?
答案 0 :(得分:1)
您正在使用timeStep
执行计算,deltaT
是函数的本地计算,并且每次调用时都设置为零。
由于currentV = acceleration * deltaT;
currentD = velocity * deltaT;
是时间步,所以只需使用:
deltaT
(如果您要继续将velocity = 0.0;
distance = 0.0;
acceleration = 0.0;
添加到&#34;时间步骤&#34;,它将不是一个步骤,而是总耗用时间。)
您还会在每次通话时重置这些变量:
{{1}}
答案 1 :(得分:0)
每次FallingObject::simulateTimeStep
来电,您都会重置状态描述值velocity
和distance
。你必须永久保存它们。