Hello! I’m having a hard time getting a hit detection between my ball and paddle. I was able to get it to work, but it stopped working once I migrated my code into classes. I’ll include my code that works as well as the same code that I’m trying to make work with classes. Also, I’m trying to use a Boolean function for the hit detection.
I appreciate any feedback! Also, this is my first post, so I apologize in advance for any mistakes.
//--------------------------functioning code, but not organized in classes
float ychange = 0;
float paddleXPos = 550;
float paddleYPos = 250;
float paddleWidth = 20;
float paddleHeight = 100;
float CircX = 0;
float CircY = 300;
float CircWidth = 10;
float CircHeight = 10;
float CircSpeedX = 1;
float CircSpeedY = 2;
//makes ball play at a random speed each time
float r = random(15);
void setup() {
size (600, 600);
}
void draw() {
frameRate(30*r);
background (0);
//----------------------------------------------------------------------------//Paddle
paddleYPos += ychange;
paddleYPos = constrain(paddleYPos, paddleYPos/2, height-paddleHeight/2);
float centerRectY = constrain(mouseY, 0, height-paddleHeight);
rect(paddleXPos, centerRectY, paddleWidth, paddleHeight);
//---------------------------------------------------------------------------//Collision
//checks if Circ keeps moving in current X direction, will it collide with the Paddle
if (CircX + CircWidth > paddleXPos &&
CircX< paddleXPos + paddleWidth &&
CircY + CircHeight > centerRectY &&
CircY < centerRectY + paddleHeight) {
CircSpeedX *= -1;
}
//makes Circ bounce off left and right edges of screen
else if (CircX < 0 || CircX + CircWidth > width) {
CircSpeedX *= -1;
}
//checks if Circ keeps moving in current Y direction, will it collide with the Paddle
if (CircX + CircWidth > paddleXPos &&
CircX < paddleXPos + paddleWidth &&
CircY + CircHeight > centerRectY &&
CircY< centerRectY + paddleHeight) {
CircSpeedY = -1;
}
//bounce off top and bottom edges of screen
else if (CircY < 0 || CircY + CircHeight > height) {
CircSpeedY = -1;
}
CircX += CircSpeedX;
CircY += CircSpeedY;
ellipse(CircX, CircY, CircWidth2, CircHeight2);
}
//---------------------same code, but trying to organize in classes. Not working. I have the boolean for the hit detection under the Paddle class.
Paddle rightPaddle;
Ball puck;
// Global Variables
int score = 0;
float r = random(15);
void setup() {
size (600, 600);
noStroke();
// Create object and pass in parameters
rightPaddle = new Paddle();
puck = new Ball();
noCursor();
}
void draw() {
background (127);
puck.display();
rightPaddle.display();
}
class Ball {
// fields
float circX; // 0
float circY; // 300
float circHeight = 10; // Maybe make this a settable variable in the future?1
float circWidth= 10;
float circSpeedX;
float circSpeedY;
// constructor
Ball() {
startBall();
}
//makes ball play at a random speed each time
// methods
void startBall() {
circX =0;
circY = random(height);
circSpeedX = random(3, 5);
circSpeedY = random(-2, 2);
}
void display() {
ellipse(circX, circY, circWidth2, circHeight2);
circX += circSpeedX;
circY += circSpeedY;
// Edge Collision Detection
leftRightCheck();
topBottomCheck();
}
void leftRightCheck() {
if (circX > width) {
startBall();
}
if (circX < 0) {
reverseX();
}
}
void topBottomCheck() {
// TOP
if (circY < 0 + circHeight) {
reverseY();
}
// Bottom
if (circY > height - circHeight) {
reverseY();
}
}
void reverseX() {
circSpeedX *= -1;
}
void reverseY() {
circSpeedY *= -1;
}
}
class Paddle {
float yChange = 0;
float pXPos = 550;
float pYPos = 250;
float pWidth = 20;
float pHeight = 100;
float constrainPY;
Paddle() {
}
void display() {
pYPos += yChange;
pYPos = constrain(pYPos, pYPos/2, height-pHeight/2);
float constrainPY = constrain(mouseY, 0, height-pHeight);
rect(pXPos, constrainPY, pWidth, pHeight);
if (hitPaddle(pXPos, pYPos, pWidth, pHeight, constrainPY, puck.circX, puck.circY, puck.circHeight, puck.circWidth, puck.circSpeedX,
puck.circSpeedY)) {
}
}
boolean hitPaddle(float paddleX, float paddleY, float paddleWidth, float paddleHeight,
float constrainPY, float ballX, float ballY, float circWidth, float circHeight,
float circSpeedX, float circSpeedY) {
if (ballX +circWidth> paddleX &&
ballX< paddleX + paddleWidth &&
ballY + circHeight > constrainPY &&
ballY< constrainPY + paddleHeight) {
circSpeedX *= -1;
}
if (ballX+ circWidth > paddleX &&
ballX < paddleX + paddleWidth &&
ballY+ circHeight > constrainPY &&
ballY< constrainPY + paddleHeight) {
circSpeedY *= -1;
return true;
}
return false;
}
}