Need help making my paddle contact

Hi Y’all,

I’m trying to make a vertical, one player version of pong for android. I’ve made png horizontally before but i’m fairly new to this whole thing and changing axis has me thrown off. I just need help making the ball contact the paddle and ceiling only, and fall off the bottom.

Here’s my code:

void contact(){
  if (y > paddleY && x >= paddleX && x <= paddleY + paddleW){
    if (velocityY > 0) {
      velocityY = -velocityY;

y is the y axis of my ball and x is the x axis.

Any help is appreciated!

1 Like

It’s import to remember that the screen coordinates go from 0,0 (upper left corner) to width, length (bottom right corner). So a ball rising up toward the top is negative change in Y position and falling down to the bottom is positive change in Y position. Unless the ball is moving super slow a regular checking against a fixed coordinate for basic collisions won’t work every time.

If your ball is round then you can test collision of the outer edge or the center point of it with the paddle/wall. In either case you want to know if that point (x,y) exceeds a certain value/coordinate on at least one axis or if it is possibly contained with a shape (like a rectangular paddle). You could attempt to predict collision based on direction of movement and the expect change in position on the next update. Regardless once you’ve detected a collision you’ll have to decide what should happen (like bouncing off) and implement it.

What exactly do paddleX and paddleY represent? I get that paddle W is probably the width of the paddle here.

In any case you need to make sure you’re tracking position, movement direction, speed, and maybe acceleration for the ball and the paddle.

It might be helpful to you to call it ballY (or bY) and ballX (or bX) to differentiate it. You could also create a class and make those fields, but it’s not critical for this.

1 Like

PaddleX and paddleY just represent the startpoint of the paddle. My issue is that i can’t make it bounce in the bounds of the paddle only, meaning i can make it bounce off where the paddle is, but it bounces at that height regardless of paddle position. It doesn’t fall off the edge if the paddle isn’t under it.

how do i keep it in bounds of the paddle only and fall off the bottom otherwise?

this should not work horizontal or vertical,
for a situation where a ball falls down its
y > paddleY
( or better as @Nathan mentioned: (y+br) ) if y is ball center and br is ball radius )
and the x in range of paddleX ( not paddleY )

if (y > paddleY && x >= paddleX && x <= paddleX + paddleW){