玩家没有与地面碰撞?

问题描述 投票:0回答:1

我正在检测左脚的碰撞,以防止自己从地面跌落。代码工作得很好,直到我将 gameLoop 函数移动到另一个文件来组织我的代码。这个问题似乎与我的碰撞逻辑无关,但我不确定。

这是我的代码的相关部分:

主要.cpp

#include <SDL2/SDL.h>

#include "player.h"
#include "game.h"
#include "sdl_setup.h"



int main(int argc, char* args[])
{
    
    SDLSetup setup;
    Game game;
    
    game.runGame();
    game.cleanUP();
    
    return 0;
}

这是我移动游戏循环的地方。游戏.cpp

#include <SDL2/SDL.h>

#include "game.h"

void Game::runGame()
{
    bool quit = false;
    
    while (!quit)
    {
        player.eventHandling();
        player.update();
        player.collision(setup.WINDOW_WIDTH, setup.WINDOW_HEIGHT); // <-
        
        player.setRenderDrawColor(setup.renderer, COLOR_PERIWINKLE_BLUE);
        SDL_RenderClear(setup.renderer);
        
        // Draw game objects here
        
        // Drawing the player
        player.drawPlayer(setup.renderer);
        SDL_RenderPresent(setup.renderer);
    }
}

void Game::cleanUP()
{
    SDL_DestroyRenderer(setup.renderer);
    SDL_DestroyWindow(setup.window);
    SDL_Quit();
}

player.cpp(物理)

void Player::update()
    {
        // Gravity conts
        const double GRAVITY = 0.5;
        const double MAX_FALL_SPEED = 10;
        
        yVelocity += GRAVITY;
        
        if (yVelocity > MAX_FALL_SPEED)
        {
            yVelocity = MAX_FALL_SPEED;
        }
        
        playerPos.x += xVelocity;
        playerPos.y += yVelocity;
        
        playerData.body.x = playerPos.x + 15;
        playerData.body.y = playerPos.y + 30;
        
        playerData.leftFoot.x = playerPos.x + 11;
        playerData.leftFoot.y = playerPos.y + 130;
        
        playerData.rightFoot.x = playerPos.x + 38;
        playerData.rightFoot.y = playerPos.y + 130;
        
        playerData.iris.x = playerPos.x + 30;
        playerData.iris.y = playerPos.y + 10;
        
        playerData.pupil.x = playerPos.x + 32;
        playerData.pupil.y = playerPos.y + 10;
    }
void Player::collision(int windowHeight, int windowWidth)
    {
        playerData.leftFoot.x = playerPos.x + 11;
           playerData.leftFoot.y = playerPos.y + 130;
        if (playerData.leftFoot.y + playerData.leftFoot.h >= windowHeight)
        {
            playerData.leftFoot.y = windowHeight - playerData.leftFoot.h;
            yVelocity = 0;
            
        }
        else{}
        if (playerPos.x + playerData.head.w >= windowWidth)
        {
            playerPos.x = windowWidth - playerData.head.w;
            xVelocity = 0;
            if (xVelocity == 0)
            {
                xVelocity = -1;
            }
            
        }
        else if (playerPos.x <= 0)
        {
            playerPos.x = 0;
            xVelocity = 0;
            if (xVelocity == 0)
            {
                xVelocity = 1;
            }
        }
    }

player.cpp(绘图函数)

    #include "player.h"
    #include "sdl_setup.h"
    #include "colors.h"

    Player::Player()
    {
        // Setting to the head
        playerPos.x = 100;
        playerPos.y = 20;
        // Constructor - Setting position and size of player characteristics
        playerData.head = {100, 20, 70, 70};
        playerData.body = {115, 50, 30, 100};
        playerData.leftFoot = {111, 150, 12, 12};
        playerData.rightFoot = {138, 150, 12, 12};
        playerData.iris = {130, 30, 15, 15};
        playerData.pupil = {132, 30, 10, 10};
        playerData.mouth = {125, 60, 30, 10};
        
        playerHair hair = initalizeLine(120, 20, 110, 40);
        playerHair hair2 = initalizeLine(127, 20, 106, 30);
        
        hairCollection.push_back(hair);
        hairCollection.push_back(hair2);
    }

    playerHair Player::initalizeLine(int startX, int startY, int endX, int endY)
    {
        playerHair playerhair;
        
        playerhair.startX = startX;
        playerhair.startY = startY;
        playerhair.endX = endX;
        playerhair.endY = endY;
        
        return playerhair;
    }

    void Player::setRenderDrawColor(SDL_Renderer* renderer, const SDL_Color& color)
    {
        SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
    }

    void Player::drawPlayer(SDL_Renderer* renderer)
    {
        setRenderDrawColor(renderer, COLOR_DEEP_ORANGE);
        
        SDL_Rect head = playerData.head;
        head.x = playerPos.x;
        head.y = playerPos.y;
        
        SDL_Rect body = playerData.body;
        body.x = playerPos.x + 15;
        body.y = playerPos.y + 30;
        
        SDL_RenderFillRect(renderer, &head);
        SDL_RenderFillRect(renderer, &body);
        
        setRenderDrawColor(renderer, COLOR_ROYAL_BLUE);
        
        SDL_Rect leftFoot = playerData.leftFoot;
        leftFoot.x = playerPos.x + 11;
        leftFoot.y = playerPos.y + 130;
        SDL_Rect rightFoot = playerData.rightFoot;
        rightFoot.x = playerPos.x + 38;
        rightFoot.y = playerPos.y + 130;
        
        SDL_RenderFillRect(renderer, &leftFoot);
        SDL_RenderFillRect(renderer, &rightFoot);

        setRenderDrawColor(renderer, COLOR_WHITE);
        
        SDL_Rect iris = playerData.iris;
        iris.x = playerPos.x + 30;
        iris.y = playerPos.y + 10;
        
        SDL_RenderFillRect(renderer, &iris);
        
        setRenderDrawColor(renderer, COLOR_BLACK);
        
        SDL_Rect pupil = playerData.pupil;
        pupil.x = playerPos.x + 32;
        pupil.y = playerPos.y + 10;
        
        drawHair(renderer, 20, 0, 10, 50, 0);
        drawHair(renderer, 27, 0, 6, 40, 1);
        
        SDL_RenderFillRect(renderer, &playerData.pupil);
        
        setRenderDrawColor(renderer, COLOR_SALMON);
        
        SDL_Rect mouth = playerData.mouth;
        mouth.x = playerPos.x + 25;
        mouth.y = playerPos.y + 40;
        
        SDL_RenderFillRect(renderer, &mouth);
    }
c++ sdl collision-detection game-development
1个回答
0
投票

确保在player.collision(setup.WINDOW_WIDTH, setup.WINDOW_HEIGHT);之前调用player.update();在你的游戏循环中。

© www.soinside.com 2019 - 2024. All rights reserved.