使用OpenGL平滑相机移动

时间:2013-09-06 20:32:18

标签: loops opengl camera smooth

我已经使用自己的游戏引擎了一段时间了,我一直在努力让输入和控制更像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;
}

如果我能把这一切都整理好,任何帮助或参考资料,我都会喜欢吗?

0 个答案:

没有答案
相关问题