Trouble with Ball bat collision in SDL2 breakout game C++

  c++, game-development, game-physics, sdl, sdl-2

Hello I’m making a simple break out game using SDL 2 for Switch using C++ and Homebrew sdk. I was able to implement Ball colliding with screen boundaries.But I’m unable to do ball bat collision. Here is my code.You can ignore some switch initialisation code.

My idea was if ball.x == bat.x and ball.y == bat.y then make ball_vel_y = -ball_vel_y

But my idea didn’t work out. Is there is any other way to do it.

#include<iostream>
#include<SDL2/SDL.h>
#include<switch.h>
// some switch buttons
#define JOY_A     0
#define JOY_B     1
#define JOY_X     2
#define JOY_Y     3
#define JOY_PLUS  10
#define JOY_MINUS 11
#define JOY_LEFT  12
#define JOY_UP    13
#define JOY_RIGHT 14
#define JOY_DOWN  15
#define KEY_LSTICK_UP 17
#define KEY_LSTICK_DOWN 19
#define KEY_LSTICK_LEFT 16
#define KEY_LSTICK_RIGHT 18

bool Quit = false;
SDL_Event event;
int ball_x = 20;
int ball_y = 50;
int ball_vel_x = 1;
int ball_vel_y = 1;

int bkw = 1920;
int bkh = 1080;
int bkwmin = 0;
int bkhmin = 0;

int bat_x = bkw /2 ;
int bat_y = bkh - 300;;





void BallCollision()
{
    if(ball_x <= bkwmin || ball_x >= bkw - 60)
    {
        ball_vel_x = -ball_vel_x;
    }
    if(ball_y <= bkhmin || ball_y >= bkh - 300)
    {
        ball_vel_y = -ball_vel_y;
    }

    //ball bat collision here
   
  
}




void MoveBall()
{
    ball_x += ball_vel_x * 5;
    ball_y += ball_vel_y * 5;
}
void EventHandler()
{
     if(SDL_PollEvent(&event))
    {
            if(event.type == SDL_QUIT)
            {
  
       
                         Quit = true;
        
             }

            else if(event.type == SDL_JOYBUTTONDOWN)
            {
                 if(event.jbutton.button == KEY_LSTICK_LEFT && bat_x > 0)
                 {
                    bat_x -= 2*50;
                 }
                 else if(event.jbutton.button == KEY_LSTICK_RIGHT && bat_x < bkw - 100)
                 {
                     bat_x += 2*50;
                 }
            }
    }
}
int main()
{

     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
     SDL_Window* Window = SDL_CreateWindow("HelloWorld",0,0,1920, 1080,SDL_WINDOW_OPENGL);
     SDL_Renderer* Renderer = SDL_CreateRenderer(Window,0,SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);
     
     romfsInit();
     
    
    

    for (int i = 0; i < 2; i++) {
        if (SDL_JoystickOpen(i) == NULL) {
            SDL_Log("SDL_JoystickOpen: %sn", SDL_GetError());
            SDL_Quit();
            return -1;
        }
    }

   
    SDL_Rect bkrect;
    bkrect.x = 0;
    bkrect.y = 0;
    bkrect.h = 1080;
    bkrect.w = 1920;

    
   
    SDL_Surface* ball = SDL_LoadBMP("romfs:/resources/ball.bmp");
    SDL_Texture* balltexture = SDL_CreateTextureFromSurface(Renderer,ball);

    SDL_Surface* BackgroundImage = SDL_LoadBMP("romfs:/resources/bk.bmp");
    SDL_Texture* BackgroundTexture = SDL_CreateTextureFromSurface(Renderer,BackgroundImage); 

    SDL_Surface* bat = SDL_LoadBMP("romfs:/resources/bat.bmp");
    SDL_Texture* battexture = SDL_CreateTextureFromSurface(Renderer,bat);

    
    
    

    while(!Quit)
    {
          
    
    EventHandler();
     MoveBall();
     BallCollision();

    
    SDL_Rect ballrect;
    ballrect.x = ball_x;
    ballrect.y = ball_y;
    ballrect.h = 60;
    ballrect.w = 60;

    SDL_Rect batrect;
    batrect.x = bat_x;
    batrect.y = bat_y;
    batrect.w = 100;
    batrect.h = 50;
    
    SDL_RenderCopy(Renderer,BackgroundTexture,NULL,&bkrect);
    SDL_RenderCopy(Renderer,balltexture,NULL,&ballrect);
    SDL_RenderCopy(Renderer,battexture,NULL,&batrect);
    
    SDL_RenderPresent(Renderer);
    SDL_RenderClear(Renderer);
   
  

    
    }
    

    SDL_DestroyRenderer(Renderer);
    romfsExit();
    SDL_DestroyWindow(Window);
    SDL_Quit();

    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT