I’ve built a cute little pong game using previous knowledge, however moving collision was not something I wanted to spend a lot of time on (I have made a bouncing ball example). Using code online, I was able to get a (mostly) stable pong experience. However, I am porting it to my phone, and the pong glitch is starting to get on my nerves. Unfortunately, I just started learning Processing two weeks ago (high school class), and I don’t know how I can fix the issue. Can someone please walk me through the steps needed?
The paddle’s “hit box” is shifted down by (x) amount of pixels. What should connect at the top doesn’t connect, and what should go below the paddle bounces off as if the paddle was there.
I want to correct this, so that the ball actually bounces off the paddle correctly.
Without double-checking, my guess is that the culprit is probably rectMode(CENTER); – you are interpreting your paddle coordinates in some places as CORNER, and in other places as CENTER. Pick one.
you have to change the if clauses depending on your rectMode
with CENTER it’s different than with CORNER.
Basically with center you give the center position to the rect command and with corner the upper left corner.
Hence, in the first case you want to check if the ball is > position - size/2 && ball is < position + size/2.
In the 2nd case you just check if ball is > Position && ball is < Position + size.
That’s pseudo code of course just to show the principle.
The other thing
The other thing is that you check left and right border of the screen with the same if clause and just say * -1 here. It’s how it’s done in the examples so I can’t blame you for this.
This can lead to stuttering in the screen border area though (making bouncingBallX positive and negative back and forth). Better check the borders separately and use abs() instead. The function abs() gives you the absolute value which is always positive.
// left
if (bouncingBallX <= 0) {
bouncingBallSpeedX = abs(bouncingBallSpeedX); // abs makes this always positive
}
// right
if (bouncingBallX >= width) {
bouncingBallSpeedX = abs(bouncingBallSpeedX) * -1; // this is always negative
}
When you read the tutorial about objects you could make a class Paddle and a class Ball and work from there. Just to learn object oriented programming.