运动变慢一段时间后

时间:2015-08-15 10:57:23

标签: c++ directx

你好每个人:玩家运动问题。

我在空场景中有一个玩家角色从A点移动到B并以恒定速度返回到A,在游戏运行的第一个小时它是OKAY,三小时后运动变得缓慢和缓慢,感谢事先

Game.h

D3DXMATRIX  Player_Matrix ;         //main player matrix .
D3DXVECTOR3 PlayerPos;              //main player position .
D3DXVECTOR3 PlayerLook;             //main player Look at position . 

Game.cpp

//Initialize()  
     D3DXMatrixIdentity(&Player_Matrix);    
     PlayerPos =  D3DXVECTOR3(10.0f,0.0f,10.0f);
     PlayerLook = D3DXVECTOR3(0.0f,0.0f,1.0f);

//MovePlayer()    
        //declarations
        static float angle = D3DXToRadian(0);
        float Speed = 70.0f ;

       PlayerPos += ( PlayerLook * ( Speed * (m_timeDelta)) );      

       if(PlayerPos.x >= 320)         //      320:(B)
       {
            angle = D3DXToRadian(180);                  
       }
       if(PlayerPos.x <= 0)            //      0:(A)
       {
            angle = D3DXToRadian(180); 
       }


    //Setting up player matrixes
    D3DXMATRIX TransMat , RotMat , TempMat , ;  

    D3DXMatrixIdentity(&TempMat);
    D3DXMatrixIdentity(&RotMat);
    D3DXMatrixIdentity(&TransMat);  

    //Setup Rotation matrix .
    D3DXMatrixRotationY(&RotMat,angle);
    angle = 0.0f ;

    //Attach PlayerLook Vector to rotation matrix
    D3DXVec3TransformCoord(&PlayerLook,&PlayerLook,&RotMat);

    //gathering rotation matrix with player matrix 
    D3DXMatrixMultiply(&Player_Matrix,&Player_Matrix,&RotMat);      

    //transmat is an empty matrix to collect new player position 
    D3DXMatrixTranslation(&TransMat, PlayerPos.x,PlayerPos.y, PlayerPos.z);     

    //multiply new position matrix with main player matrix 
    D3DXMatrixMultiply(&TempMat,&Player_Matrix,&TransMat);

    d3ddev->SetTransform(D3DTS_WORLD,&TempMat);
    Main_Player->Render();

2 个答案:

答案 0 :(得分:0)

我会研究你是如何计算m_timeDelta的。也许您的方法允许浮点错误建立。

以下是关于此主题的文章:https://randomascii.wordpress.com/2012/02/13/dont-store-that-in-a-float/

我和Tom Forsyth在一起,认为64位整数是绝对时间(http://home.comcast.net/~tom_forsyth/blog.wiki.html#[[A%20matter%20of%20precision]])的最佳存储类型,但是double也能正常工作。

答案 1 :(得分:0)

我刚刚发现问题及其解决方案:

正如我之前所解释的那样,所有应用程序都运行良好,问题是:运动(只有x ++,y ++,z ++)在一段时间后变得缓慢而缓慢。首先我认为这是一个内存泄漏,但动画和增量时间工作正常。在那里,试图找到导致这种情况的合理问题。 我将应用程序发布到其他PC,经过一段时间应用程序工作正常,此时我看到FPS在每秒内达到60.0帧,在DXUT内的MS DirectX SDK中搜索后我发现了一个结构哪个控制FPS,有一个讲GPU和加速的Doc,他们建议控制FPS及其限制,这是代码:

//-----------------------------------------------------------------------------
// Name: LockFrameRate()
// Desc: Limit The frame Rate to specified 
//-----------------------------------------------------------------------------
bool LockFrameRate(int frame_rate , float SecPerCnt )
{
    static __int64 StartTime = 0 ;

    __int64 CurTime = 0 ; 
    QueryPerformanceCounter((LARGE_INTEGER*)&CurTime);

    float CurrentSecond = (float)((CurTime - StartTime )* SecPerCnt ) ;

    // Get the elapsed time by subtracting the current time from the last time
    // If the desired frame rate amount of seconds has passed -- return true (ie Blit())
    if( CurrentSecond > (1.0f / frame_rate) )
    {
        // Reset the last time
        StartTime = CurTime;    
        return true;
    }

    return false;
}

// int WINAPI WinMain(....)

//***************************
    // Initialize Timing Preformance  . *
    //***************************

    //Store Counts per second 
    __int64 CountPerSec = 0 ; 

    //Gets how many counts does the CPU do per second 
    QueryPerformanceFrequency((LARGE_INTEGER*)&CountPerSec); 

    //Gets second per count to preform it with different typs of CPUs
    float SecondPerCount = 1.0f / CountPerSec ; 

    //Initial Previous Time 
    __int64 PrevTime = 0 ;
    QueryPerformanceCounter((LARGE_INTEGER*)&PrevTime);

while(msg.message != WM_QUIT)                               // while not quit message , go on    
    {
        if(PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))
        {                               
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else if(LockFrameRate(60 , SecondPerCount)) // If it is time to draw, do so , I selected 60 fps to limit
        {
            //Capture Current Time
            __int64 CurTime = 0 ;
            QueryPerformanceCounter((LARGE_INTEGER*)&CurTime);

            //Calculate Delta Time 
            float DeltaTime =(float) ((CurTime - PrevTime) * SecondPerCount)  ;         
            //Engine loop
            Engine->Engine_Run(DeltaTime , SecondPerCount );                        

            //After Frame Ends set Pervious Time to Current Time 
            PrevTime = CurTime ; 
        }
        //else
            //Sleep(1); // Give the OS a little bit of time to process other things
    }

我评论睡眠(1),因为我科学地知道它只是CPU的负担,但我把它放在科学的信心,在App和其他人之间有一个等待每个人的空闲过程其他这是现今的计算机技术。 如果你想尝试它,你可以感觉到一些屏幕停止发生这些是不需要的

谢谢Stack Over Flow 。谢谢你们。