在c++控制台中基于实时的增量/递减

Real-time based increment / decrement in C++ console

本文关键字:递减 实时 控制台 c++ 于实时      更新时间:2023-10-16

有没有一种方法可以在不使用第三方库的情况下,在简单的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;
}