0

我一直在使用自己的游戏引擎有一段时间了,我一直在努力让输入和控制更像是一款 AAA FPS 游戏,或者至少是一款不错的独立游戏。我过去在这个问题上发布了几个关于制作平滑相机的主题,并且在发布它们时,我对结果感到满意。然而,现在我觉得它不够流畅,我已经将整个引擎切换到 SDL,所以我可以控制循环。(以前我使用的是 GLUT)。全部改成SDL后,鼠标流畅如黄油,但摄像机移动(行走)依然卡顿,看起来很糟糕。我已经实现了 Dwitter 的最后一个游戏循环,带有插值的循环,这里是相关代码:

int main (int argc,  char** argv)
{
    arg1 = argc;
    arg2 = argv;
    engineInit();
    //the loop has to stay here
    //kill all extra threads so they don't cause problems after we quit
    //gameloop
    SDL_Event event;
    bool running = true;

    const int TPS = 20;
    const int SKIP_TICKS = 1000 / TPS;
    const int MAX_FRAMESKIP = 5;
    int loops;
    long lastSec = 0;
    long nextGameTick = SDL_GetTicks();

    while (running)
    {
        while (SDL_PollEvent(&event)) {
            //do crap with events
            switch (event.type)
            {
                int x,y,button;
                case SDL_QUIT:
                SDL_GL_DeleteContext(glContext);
                SDL_DestroyWindow(window);
                SDL_Quit();
                cout << "The window has been closed.\n";
                running = false;
                    break;
                case SDL_MOUSEMOTION:
                    SDL_GetMouseState(&x, &y);
                    passiveMouse(x,y);
                    break;
                case SDL_MOUSEBUTTONDOWN :
                   button =  SDL_GetMouseState(&x, &y);
                    mouseFunc(button,1,x,y);
                    break;
                 case SDL_KEYDOWN:
                    keyboardDownFunc(event.key.keysym.sym);
                    break;
                case SDL_KEYUP:
                    keyboardUpFunc(event.key.keysym.sym);
                    break;
                default:

                    break;
            }

        }
        loops = 0;
        while (SDL_GetTicks()> nextGameTick && loops < MAX_FRAMESKIP) {
            nextGameTick+=SKIP_TICKS;
            loops++;
            TickHandler.tps++;
            TickHandler.onTick();


            int tickTime = int(SDL_GetTicks()/1000);
            if (tickTime > lastSec+1)
            {
                TickHandler.tps = 0;
                lastSec = tickTime;
            }
        }
        TickHandler.interpolation = double(SDL_GetTicks() + SKIP_TICKS - nextGameTick )
        / double( SKIP_TICKS );
        TickHandler.onRender();
        render();
    }
    Console.consoleActivated = false;
    SDL_GL_DeleteContext(glContext);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
    }

TickHandler.onRender() 调用了一些插值函数,这里是控制相机移动的函数:

void renderTick(float intp)
{
    if (cameraPlayer == true)
    {
        Physics.pos3 = -camy;
        Physics.collisions();
        Input.applyGravity();
        if (Input.walking == true)
            Input.moveCameraFirstPerson(1*intp);
        else
        {
            roll = 0;
            Input.change = false;
        }
    }

}

这是移动相机的第一人称:

void inputs::moveCameraFirstPerson(float speed)
{
    speed = speed*walkspeed;
    float radx = ((yaw+addedAngle)*MPI/180);
    camx -= (sinf(radx)/10)*speed;
    camz += (cosf(radx)/10)*speed;

    Physics.pos1 = -camx;
    Physics.pos2 = -camz;

    if (Physics.collided == true)
    {float radx = ((yaw+Input.addedAngle)*3.1415926535/180);
        camx += (sinf(radx)/20)*speed;
        camz -= (cosf(radx)/20)*speed;
        Physics.collided = false;

    }
    Client.x = camx;
    Client.z = camz;
    Client.y = camy;

    Projectile.x = camx;
    Projectile.z = camz;
}

如果我能把这一切整理好,我会很高兴,有什么帮助或参考吗?

4

0 回答 0