简体   繁体   English

3D / FPS相机问题-SDL2

[英]3D / FPS Camera Issues - SDL2

I got a problem with my code, I'm trying to make a First Person 3D Camera. 我的代码有问题,我正在尝试制作第一人称3D相机。 I use SDL_GetKeyboardState(NULL) to get the pressed keys. 我使用SDL_GetKeyboardState(NULL)来获取按下的键。

When I press one of the defined keys nothing happens, why? 当我按下已定义的键之一时,什么也没发生,为什么?

Camera (Controll): 相机(控制):

void Control(float movevel, float mousevel, bool mi, SDL_Window* window)
{
if (mi)  //if the mouse is in the screen
{
    int MidX = 640 / 2;   //middle of the screen
    int MidY = 480 / 2;
    SDL_ShowCursor(SDL_DISABLE);    //we don't show the cursor
    int tmpx, tmpy;
    SDL_GetMouseState(&tmpx, &tmpy); //get the current position of the cursor
    camYaw += mousevel*(MidX - tmpx);   //get the rotation, for example, if the mouse current position is 315, than 5*0.2, this is for Y
    camPitch += mousevel*(MidY - tmpy); //this is for X
    lockCamera();
    //SDL_WarpMouse(MidX, MidY);       //move back the cursor to the center of the screen
    SDL_WarpMouseInWindow(window, MidX, MidY);

    const Uint8* kstate = SDL_GetKeyboardState(NULL);

    if (kstate[SDLK_w])
    {
        if (camPitch != 90 && camPitch != -90)
        {       //if we are facing directly up or down, we don't go forward, it will be commented out, when there will be gravity
            moveCamera(movevel, 0.0);        //move forward
        }

        moveCameraUp(movevel, 0.0);      //move up/down
    }

    if (kstate[SDLK_s])
    {
        //same, just we use 180 degrees, so we move at the different direction (move back)
        if (camPitch != 90 && camPitch != -90)
        {
            moveCamera(movevel, 180.0);
        }

        moveCameraUp(movevel, 180.0);
    }

    if (kstate[SDLK_a])
    {       //move left
        moveCamera(movevel, 90.0);
    }

    if (kstate[SDLK_d])
    {  //move right
        moveCamera(movevel, 270);
    }
}

glRotatef(-camPitch, 1.0, 0.0, 0.0);
glRotatef(-camYaw, 0.0, 1.0, 0.0);
}

Main (Loop) 主(循环)

while (running)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    SDL_Event ev;
    while (SDL_PollEvent(&ev))
    {
        switch (ev.type)
        {
            case SDL_QUIT:
                running = false;
                break;
            case SDL_KEYDOWN:
                int x, y;

                SDL_GetMouseState(&x, &y);
                Main::handleKeys(ev.key.keysym.scancode, x, y);
                break;
        }
    }

    Main::update(window);
    Main::render(window);

    SDL_GL_SwapWindow(window);
}

Main (Update): 主要(更新):

void Main::update(SDL_Window* window)
{
Control(0.2, 0.2, mousein, window);
UpdateCamera(0.2); //move the camera to the new location
}

您应该在平移相机之前调用glRotatef函数,否则它将绕原点而不是其位置旋转

Use SDL_PumpEvents() to update the state array. 使用SDL_PumpEvents()更新状态数组。 (From the SDL_GetKeyboardState() wiki). (来自SDL_GetKeyboardState()Wiki)。 I believe that if you do: 我相信,如果您这样做:

SDL_PumpEvents();
SDL_GetKeyboardState(NULL);

you should get the results you are looking for. 您应该得到想要的结果。 (You need to SDL_PumpEvents every loop as well as SDL_GetKeyboardState obviously) (显然,每个循环都需要SDL_PumpEvents以及SDL_GetKeyboardState)

(EDIT) Another option: (编辑)另一个选择:

    case SDL_KEYDOWN:
        switch(event.key.keysym.sym){
            case SDLK_w
                (... code)
                break;
            case SDLK_s:
                (... code)
                break;
            case SDLK_a:
                (... code)
                break;
            case SDLK_d:
                (... code)
                break;

You can also send the event.key.keysym.sym to a function and use it their ofcourse. 您也可以将event.key.keysym.sym发送给函数,并在过程中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM