在c++控制台中基于实时的增量/递减
Real-time based increment / decrement in C++ console
有没有一种方法可以在不使用第三方库的情况下,在简单的c++控制台应用程序中根据实时情况增加/减少变量的值?
例如,在Unity3D游戏引擎中,有一个叫做时间的东西。DeltaTime表示完成上一帧所需的时间。现在,我知道在控制台应用程序中没有绘制更新功能或帧,但是我要做的是能够增加/减少变量的值,这样,它就会随着时间而变化,比如,
变量= 0
变量=变量+时间。DeltaTime
使变量值每秒递增。在c++ 11中可能有这样的功能吗?例如,如果speed为5,那么5秒后,变量的值为5。
基本上,我正在创建一个简单的粒子系统,我希望粒子在达到MAX_AGE后死亡。我不确定如何在一个简单的c++控制台应用程序中实现。
对于简单的计时,可以使用std::chrono::system_clock。tbb::tick_count是一个更好的增量计时器,但是您说没有第三方库。
您已经知道,增量时间是最终时间减去原始时间。
dt= t-t0
这个时间就是速度变化时所经过的时间。
函数的导数表示该函数相对于其中一个变量的无限小变化。函数对变量的导数定义为
f(x + h) - f(x)
f'(x) = lim -----------------
h->0 h
首先得到一个时间,i。e NewTime = timeGetTime()
.
然后用刚得到的新时间减去旧时间。称之为时间,dt
。
OldTime = NewTime;
dt = (float) (NewTime - OldTime)/1000;
现在将dt
求和为totaltime
。
TotalTime += dt
所以当TotalTime
达到5时,你就结束了粒子的生命。
if(TotalTime >= 5.0f){
//particles to die after their MAX_AGE is reached
TotalTime=0;
}
…
更有趣的阅读:
http://gafferongames.com/game-physics/http://gafferongames.com/game-physics/integration-basics/…
Windows示例代码:
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include<windows.h>
#pragma comment(lib,"winmm.lib")
void gotoxy(int x, int y);
void StepSimulation(float dt);
int main(){
int NewTime = 0;
int OldTime = 0;
int StartTime = 0;
int TimeToLive = 5000; //// TimeToLive 5 seconds
float dt = 0;
float TotalTime = 0;
int FrameCounter = 0;
int RENDER_FRAME_COUNT = 60;
// reset TimeToLive
StartTime = timeGetTime();
while(true){
NewTime = timeGetTime();
dt = (float) (NewTime - OldTime)/1000; //delta time
OldTime = NewTime;
// print to screen TimeToLive
gotoxy(1,1);
printf("NewTime - StartTime = %d ", NewTime - StartTime );
if ( (NewTime - StartTime ) >= TimeToLive){
// reset TimeToLive
StartTime = timeGetTime();
}
// The rest of the timestep and 60fps lock
if (dt > (0.016f)) dt = (0.016f); //delta time
if (dt < 0.001f) dt = 0.001f;
TotalTime += dt;
if(TotalTime >= 5.0f){
TotalTime=0;
StepSimulation(dt);
}
if(FrameCounter >= RENDER_FRAME_COUNT){
// draw stuff
//Render();
gotoxy(1,3);
printf("OldTime = %d n",OldTime);
printf("NewTime = %d n",NewTime);
printf("dt = %f n",dt);
printf("TotalTime = %f n",TotalTime);
printf("FrameCounter = %d fpsn",FrameCounter);
printf(" n");
FrameCounter = 0;
}
else{
gotoxy(22,7);
printf("%d ",FrameCounter);
FrameCounter++;
}
}
return 0;
}
void gotoxy(int x, int y){
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}
void StepSimulation(float dt){
// calculate stuff
//vVelocity += Ae * dt;
}
相关文章:
- 从数据库实时显示QT c++中的数据
- 为什么此派生对象无法访问基类的后递减方法?
- 在 Windows 8/10 技术中完全实时的屏幕捕获,没有延迟
- 有没有办法使用 c++ 实时阅读文本?
- 在本地网络中通过OpenCV(C++)实时流式传输图像
- 将相机数据从服务器实时流式传输到客户端
- 将实时(非静态)放在qt(c ++)上
- 线程之间的实时数据共享
- 如何在实时应用程序中锁定线程
- 迭代器在递增和递减时是否保证会变异?
- OpenCV - 来自相机的实时馈送不流畅
- 如何在单行语句中C++递减多个变量?
- 如何提高 OpenCV 实时检测器应用程序的帧率?
- 使用 Opencv 将姿势值实时写入文本文件
- Firebase C++ Windows 上的实时数据库连接丢失
- 我在 IDA 或 dbg 或 olly 上看到的内存是否与我在 RAM 上实时加载的内存相同?
- 递减 std::vector::begin 是否未定义,即使它从未被使用过?
- 媒体基金会:WavSink以比实时消耗率更快的速度处理音频-是否可以实时限制后台处理
- 如何实时捕获相机的视频?
- 在c++控制台中基于实时的增量/递减