C ++ SDL2 /滚动碰撞问题

时间:2016-08-27 07:03:57

标签: c++ sdl-2

正如标题所示,我试图通过滚动实现碰撞。 这里的代码不是最整洁的,它不使用面向对象的编程。但是这个代码是我正在制作的游戏中碰撞的原型,所以我可以在那个游戏中重用。 无论如何,当我编写这段代码并且没有实现滚动时,碰撞工作正常。但是,当我实现滚动时,突然间,代表玩家的矩形的一个像素卡在我用来测试碰撞的矩形中。

这是代码。

Main.cpp的:

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include "SDL_ttf.h"
using namespace std;

int main(int argc, char *argv[]) {

    SDL_Init(SDL_INIT_EVERYTHING);
    IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);
    Mix_Init(MIX_INIT_MP3);
    Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 640);
    TTF_Init();

    SDL_Window *Window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 600, SDL_WINDOW_SHOWN);
    SDL_Renderer *Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    bool quit = false;
    SDL_Event Event;

    SDL_Rect Box;
    Box.w = 25;
    Box.h = 25;
    Box.x = 500 - int(Box.w / 2);
    Box.y = 300 - int(Box.h / 2);

    int CAMERA_X = 0;
    int CAMERA_Y = 0;

    int xVel = 0;
    int yVel = 0;

    SDL_Rect RECTS[3];
    RECTS[0].x = 50;
    RECTS[0].y = 50;
    RECTS[0].w = 50;
    RECTS[0].h = 100;
    RECTS[1].x = 150;
    RECTS[1].y = 50;
    RECTS[1].w = 50;
    RECTS[1].h = 100;
    RECTS[2].x = 250;
    RECTS[2].y = 50;
    RECTS[2].w = 50;
    RECTS[2].h = 100;

    SDL_Rect CAM_RECTS[3];
    CAM_RECTS[0].x = 50;
    CAM_RECTS[0].y = 50;
    CAM_RECTS[0].w = 50;
    CAM_RECTS[0].h = 100;
    CAM_RECTS[1].x = 150;
    CAM_RECTS[1].y = 50;
    CAM_RECTS[1].w = 50;
    CAM_RECTS[1].h = 100;
    CAM_RECTS[2].x = 250;
    CAM_RECTS[2].y = 50;
    CAM_RECTS[2].w = 50;
    CAM_RECTS[2].h = 100;

    const Uint8 *Input = NULL;

    while (!quit) {

        Input = SDL_GetKeyboardState(NULL);

        while (SDL_PollEvent(&Event)) {

            if (Event.type == SDL_QUIT) {

                quit = true;

            }

        }

        if (Input[SDL_SCANCODE_RIGHT]) {

            xVel = -7;

        } else if (Input[SDL_SCANCODE_LEFT]) {

            xVel = 7;

        } else {

            xVel = 0;

        }

        if (Input[SDL_SCANCODE_UP]) {

            yVel = 7;

        } else if (Input[SDL_SCANCODE_DOWN]) {

            yVel = -7;

        } else {

            yVel = 0;

        }

        for (unsigned int i = 0; i < 3; i++) {

            CAM_RECTS[i].x = RECTS[i].x + CAMERA_X;
            CAM_RECTS[i].y = RECTS[i].y + CAMERA_Y;

        }

        CAMERA_X += xVel;

        for (unsigned int i = 0; i < 3; i++) {

            if (Box.x <= CAM_RECTS[i].x + CAM_RECTS[i].w && Box.x + Box.w >= CAM_RECTS[i].x && Box.y <= CAM_RECTS[i].y + CAM_RECTS[i].h && Box.y + Box.h >= CAM_RECTS[i].y) {

                if (Box.x <= CAM_RECTS[i].x + CAM_RECTS[i].w && Box.x + Box.w >= CAM_RECTS[i].x) {

                    CAMERA_X += -xVel;

                }

            }

        }

        CAMERA_Y += yVel;

        for (unsigned int i = 0; i < 3; i++) {

            if (Box.x <= CAM_RECTS[i].x + CAM_RECTS[i].w && Box.x + Box.w >= CAM_RECTS[i].x && Box.y <= CAM_RECTS[i].y + CAM_RECTS[i].h && Box.y + Box.h >= CAM_RECTS[i].y) {

                if (Box.y <= CAM_RECTS[i].y + CAM_RECTS[i].h && Box.y + Box.h >= CAM_RECTS[i].y) {

                    CAMERA_Y += -yVel;

                }

            }

        }

        SDL_SetRenderDrawColor(Renderer, 0, 0, 0, 255);
        SDL_RenderClear(Renderer);

        SDL_SetRenderDrawColor(Renderer, 255, 255, 255, 255);
        SDL_RenderFillRect(Renderer, &Box);

        SDL_SetRenderDrawColor(Renderer, 255, 255, 255, 255);

        for (unsigned int i = 0; i < 3; i++) {

            SDL_RenderFillRect(Renderer, &CAM_RECTS[i]);

        }

        SDL_RenderPresent(Renderer);

    }

    Input = NULL;

    SDL_DestroyRenderer(Renderer);
    SDL_DestroyWindow(Window);

    TTF_Quit();
    Mix_CloseAudio();
    Mix_Quit();
    IMG_Quit();
    SDL_Quit();

    return 0;

}

请帮忙。 非常感谢你:))

1 个答案:

答案 0 :(得分:0)

好的,我改变了答案,现在它应该没有抖动。

这绝对不是最好的解决方案,但这取决于你的计划的更广泛的概念。

int sign(int x) 
{
    return (x > 0) - (x < 0);
}

/* Movement values are updated by referenced arguments.*/
void check_collisions(SDL_Rect* obj_rects, unsigned num_of_obj, 
        SDL_Rect obstacle, int& horizontal_move, int& vertical_move)
{
    for (unsigned int i = 0; i < num_of_obj; i++) 
    {

        int obj_new_x = obj_rects[i].x + horizontal_move;

        // Horizontal collision
        if (obj_new_x < (obstacle.x + obstacle.w) 
            && (obj_new_x + obj_rects[i].w) > obstacle.x 
            && obj_rects[i].y < (obstacle.y + obstacle.h) 
            && (obj_rects[i].h + obj_rects[i].y) > obstacle.y)
        {
            // Calculate maximal possible horizontal movement.
            if (sign(horizontal_move) == 1)
                horizontal_move = obstacle.x - (obj_rects[i].x + obj_rects[i].w);
            else if (sign(horizontal_move) == -1)
                horizontal_move = (obstacle.x + obstacle.w) - obj_rects[i].x;
        }

        int obj_new_y = obj_rects[i].y + vertical_move;

        // Vertical collision
        if (obj_rects[i].x < (obstacle.x + obstacle.w) 
            && (obj_rects[i].x + obj_rects[i].w) > obstacle.x 
            && obj_new_y < (obstacle.y + obstacle.h)
            && (obj_new_y + obj_rects[i].h) > obstacle.y)
        {
            if (sign(vertical_move) == 1)
                vertical_move = obstacle.y - (obj_rects[i].y + obj_rects[i].h);
            else if (sign(vertical_move) == -1)
                vertical_move = (obstacle.y + obstacle.h) - obj_rects[i].y;
        }
    }
}

int cam_velocity = 5;
...

while (!quit) {
...
    if (Input[SDL_SCANCODE_RIGHT]) 
    {
        x_movement = cam_velocity;
    }
    else if (Input[SDL_SCANCODE_LEFT]) 
    {
        x_movement = -cam_velocity;
    }

    if (Input[SDL_SCANCODE_UP]) 
    {
        y_movement = -cam_velocity;
    }
    else if (Input[SDL_SCANCODE_DOWN]) 
    {
        y_movement = cam_velocity;
    }

    check_collisions(CAM_RECTS, 3, Box, x_movement, y_movement);

    CAMERA_X += x_movement;
    CAMERA_Y += y_movement;

    for (unsigned int i = 0; i < 3; i++) {

        CAM_RECTS[i].x = RECTS[i].x + CAMERA_X;
        CAM_RECTS[i].y = RECTS[i].y + CAMERA_Y;
    }
...