Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7036239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:24:01+00:00 2026-05-28T01:24:01+00:00

So, I am creating the game breakout in processing (programming language) but can’t quite

  • 0

So, I am creating the game breakout in processing (programming language) but can’t quite figure out a function to check for collision against the bat.

So far the section I have written for collision against the bat only collides the ball against the base and returns it in the opposite direction. For now, the game is a never ending phenomenon where the ball just collides with the walls. What I am trying to do is, collide the ball against the bat.

Oh this is my homework, so just please point me in the right direction instead of doing it for me.

Here’s the code:

// Basic Breakout game
// Code from Matthre Yee-King

// brick position
float brickX;
float brickY;

// brick width and height
float brickH;
float brickW;

// ball position
float ballX;
float ballY;

// ball diameter
float ballD;

// ball direction
float ballDx;
float ballDy;

// bat position
float batX;

//bat width
float batW;
float batH;

//bat colour
float batB;

void setup() {
  size (500, 500, P2D);

  // set sizes of game items
  brickW = 100;
  brickH = 50;
  batW = 100;
  batH = 25;
  ballD = 25;
  batB = 255;

  // random brick position
  brickX = random(0, width - brickW);
  brickY = random (0, height / 2);

  // bat in the centre
  batX = (width/2) - (batW/2);

  // ball atop bat
  ballX = batX + (batW/2);
  ballY = height - batH - (ballD/2);

  // ball movement
  ballDx = random(-5, 5);
  ballDy = -5;
  rectMode(CORNER);
  ellipseMode(CENTER);
}

void draw() {
  // check for ball collision
  // with top or sides of bat
  checkBallAgainstBat();

  // check for ball collision with
  // left right and top walls
  // and bounce
  checkBallAgainstWalls();

  // check ball against brick
  checkBallAgainstBrick();

  // move the ball
  ballX += ballDx;
  ballY += ballDy;
  background(0);

  // draw the bat
  fill(0, 255, 0);
  rect(batX, height - batH, batW, batH);

  // draw the brick
  fill(0, 0, batB);
  batB = (batB + 10) % 255;
  rect(brickX, brickY, brickW, brickH);

  // draw the ball
  fill(255, 0, 0);
  ellipse(ballX, ballY, ballD, ballD);

  if (keyCode == 37) { // left cursor key
    batX -= 10;

    // keep it on the screen
    if (batX < 0) {
      batX = 0;
    }
  }

  if (keyCode == 39) {
    batX += 10;
    if (batX > (width - batW)) {
      batX = width - batW;
    }
  }
}

// when they let go of the key, reset the keyCode
void keyReleased() {
  keyCode = -1;
}

// this function checks if the ball has hit the top or sides of the bat and
// updates its direction as appropriate so the ball bouncs off the bat
void checkBallAgainstBat() {
  if (ballY + ballD > height - batH) {
    ballDy *= -1;
  }
}

// this function checks if the ball has hit the brick. It should bounce off
// the brick and return true if so
boolean checkBallAgainstBrick() {
  return false;
}

// this function checks if the ball has hit the top, left or right
// walls and update its
// direction as appropriate so the ball bounces off the walls
void checkBallAgainstWalls() {
  if (ballX + ballD > width) {
    ballDx *= -1;
  }
  if (ballX - ballD < 0) {
    ballDx *= -1;
  }
  if (ballY - ballD < 0) {
    ballDy *= -1;
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:24:02+00:00Added an answer on May 28, 2026 at 1:24 am

    Since the bat in breakout is a fixed width, your collision detection can be quite simple (in pseudo-code):

    if (lower_edge(ball) > top_edge(bat)) { 
       // the ball has entered territory where it might have collided
       if ((left_edge(ball) <= right_edge(bat)) && (right_edge(ball) >= left_edge(bat))) {
          // the ball's within the horizontal bounds of the bat, so it's a "hit"
          ... calculate deflection ...
       } else {
          // oops, ball's gone past the bat and wasn't hit
          strike_out();
    } else {
      // ball's still above the bat somewhere. do nothing
    }
    

    In english: If the ball’s lower edge has gone past where the top edge of the bat is, we’ve POSSIBLY got a collision. This is only checking the vertical axis of the play field. You then check if the left or right edges of the ball fall within the horizontal location of the bat. If neither side of the ball overlaps the bat, then you’ve lost. Otherwise you’ve collided and you do the collision detection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a game where players can make an alloy. To make it less
I'm creating a game, and am quite new to Python generally. I created a
I m creating a game, in that i want to use some animation, but
I'm creating a simple breakout game. However, the KeyAdapter isn't receiving the input. The
Im creating flash game that have the functionality to capture/record its gameplay that can
I am creating a game in which a player can challenge a random player
I'm creating a game where the world is formed out of cubes (like in
I'm creating a game for my sister, and I want a function to return
I'm creating a game app using cocos2d Chipmunk. The app is working normally but
ok im creating a game but it uses too much cpu but it doesn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.