Pong Game Collision Help
in
Programming Questions
•
1 year ago
Hello all!
I'm fairly new to processing and need some help with collision.
I am trying to figure out how to make the ball correctly bounce off of the paddle and the walls.
I have looked at the pong example game and it helps but I can't grasp how collisions work in programming.
Any help with my code or explaining how collisions work would be appreciated.
Sorry about the lack of objects but I tried to keep the code as clean as possible.
Any responses are appreciated.
Here's the code.
int paddleWidth;
int paddleHeight;
int paddleX;
int paddleY;
float paddleSpeed;
int ballRadius;
float ballX;
float ballY;
float dirX;
float dirY;
void setup () {
size(640, 480);
rectMode(CENTER);
ellipseMode(CENTER);
paddleWidth = 120;
paddleHeight = 10;
paddleX = width/2;
paddleY = height - paddleHeight;
paddleSpeed = 10;
ballRadius = 10;
ballX = width/2;
ballY = -height + height - ballRadius;
dirY = 1;
dirX = 0;
}
void draw() {
BackScreen();
Paddle();
Ball();
BallMovement();
PaddleControl();
PaddleContain();
Miss();
}
void BackScreen() {
rectMode(CORNER);
noStroke();
fill(0);
rect(0, 0, width, height);
}
void Paddle() {
rectMode(CENTER);
fill(255);
noStroke();
rect(paddleX, paddleY, paddleWidth, paddleHeight);
}
void Ball() {
fill(255);
noStroke();
smooth();
ellipse(ballX, ballY, ballRadius*2, ballRadius*2);
}
void BallMovement() {
ballY += dirY * 5.0;
ballX += dirX;
}
void BallBounce() {
if(ballY < height - paddleHeight && ballX < paddleX + (paddleWidth/2)
&& ballX > paddleX - (paddleWidth/2)) {
dirY *= -1;
}
}
void PaddleControl() {
if(keyPressed && paddleX + (paddleWidth/2) < width) {
if(key == CODED) {
if(keyCode == RIGHT) {
paddleX += paddleSpeed;
}
}
}
if(keyPressed && paddleX - (paddleWidth/2) > width - width) {
if(key == CODED) {
if(keyCode == LEFT) {
paddleX -= paddleSpeed;
}
}
}
}
void PaddleContain() {
}
void Miss() {
if(ballY > height) {
ballY = height - height - (ballRadius*2);
}
}
I'm fairly new to processing and need some help with collision.
I am trying to figure out how to make the ball correctly bounce off of the paddle and the walls.
I have looked at the pong example game and it helps but I can't grasp how collisions work in programming.
Any help with my code or explaining how collisions work would be appreciated.
Sorry about the lack of objects but I tried to keep the code as clean as possible.
Any responses are appreciated.
Here's the code.
int paddleWidth;
int paddleHeight;
int paddleX;
int paddleY;
float paddleSpeed;
int ballRadius;
float ballX;
float ballY;
float dirX;
float dirY;
void setup () {
size(640, 480);
rectMode(CENTER);
ellipseMode(CENTER);
paddleWidth = 120;
paddleHeight = 10;
paddleX = width/2;
paddleY = height - paddleHeight;
paddleSpeed = 10;
ballRadius = 10;
ballX = width/2;
ballY = -height + height - ballRadius;
dirY = 1;
dirX = 0;
}
void draw() {
BackScreen();
Paddle();
Ball();
BallMovement();
PaddleControl();
PaddleContain();
Miss();
}
void BackScreen() {
rectMode(CORNER);
noStroke();
fill(0);
rect(0, 0, width, height);
}
void Paddle() {
rectMode(CENTER);
fill(255);
noStroke();
rect(paddleX, paddleY, paddleWidth, paddleHeight);
}
void Ball() {
fill(255);
noStroke();
smooth();
ellipse(ballX, ballY, ballRadius*2, ballRadius*2);
}
void BallMovement() {
ballY += dirY * 5.0;
ballX += dirX;
}
void BallBounce() {
if(ballY < height - paddleHeight && ballX < paddleX + (paddleWidth/2)
&& ballX > paddleX - (paddleWidth/2)) {
dirY *= -1;
}
}
void PaddleControl() {
if(keyPressed && paddleX + (paddleWidth/2) < width) {
if(key == CODED) {
if(keyCode == RIGHT) {
paddleX += paddleSpeed;
}
}
}
if(keyPressed && paddleX - (paddleWidth/2) > width - width) {
if(key == CODED) {
if(keyCode == LEFT) {
paddleX -= paddleSpeed;
}
}
}
}
void PaddleContain() {
}
void Miss() {
if(ballY > height) {
ballY = height - height - (ballRadius*2);
}
}
1