I have a ball sprite that is generated via generateSprite(); and also a platform sprite that is generated the same way. I need to make the ball bounce off the platform. This is a breakout style game so I have rectangles that are NOT sprites that the ball needs to hit, bounce off of and make them disappear. How would I do this?
To make the ball bounce off the platform this is what I tried
ballvx = 2;
ballvy = 2;
ballx += ballvx;
bally += ballvy;
if (ballx > platformx && ballx < platformx + 200 && bally > platformy) {
if (bally < platformy + 20) {
ballvy *= -1;
} else {
ballvx *= -1;
}
}
if (ballx < 0 || ballx > width) {
ballvx *= -1;
}
if (bally < 0 || bally > height) {
state=LOSE;
}
The problem here is that the ball simply goes straight through the platform instead of bouncing off of it.
For the rectangles I have no idea how to make them both disappear and have the ball bounce off them at the same time. Here is the code for the rectangles:
fill(r, g, b);
rect(width*.01, height/28, w, h);
fill(r, g, b);
rect(width*.128, height/28, w, h);
fill(r, g, b);
rect(width*.25, height/28, w, h);
fill(r, g, b);
rect(width*.35, height/28, w, h);
fill(r, g, b);
rect(width*.45, height/28, w, h);
fill(r, g, b);
rect(width*.55, height/28, w, h);
fill(r, g, b);
rect(width*.65, height/28, w, h);
fill(r, g, b);
rect(width*.75, height/28, w, h);
fill(r, g, b);
rect(width*.85, height/28, w, h);
fill(r, g, b);
rect(width*.95, height/28, w, h);
fill(r, g, b);
rect(width*.01, height/4, w, h);
fill(r, g, b);
rect(width*.128, height/4, w, h);
fill(r, g, b);
rect(width*.25, height/4, w, h);
fill(r, g, b);
rect(width*.35, height/4, w, h);
fill(r, g, b);
rect(width*.45, height/4, w, h);
fill(r, g, b);
rect(width*.55, height/4, w, h);
fill(r, g, b);
rect(width*.65, height/4, w, h);
fill(r, g, b);
rect(width*.75, height/4, w, h);
fill(r, g, b);
rect(width*.85, height/4, w, h);
fill(r, g, b);
rect(width*.95, height/4, w, h);
The following demo uses classes for a paddle, ball, and brick to illustrate circle/rectangle collisions. Collision detection was taken from this reference: https://www.jeffreythompson.org/collision-detection/circle-rect.php. A rectangle array with a 'show' parameter is used to display the rectangles which are hidden following a collision (brick[id].show = false;). Only rectangles still being shown are tested for future collisions. Paddle movement is controlled by the four arrow keyboard keys. The demo has lots of room for improvement but does illustrate making rectangles disappear following a collision.
Ball class(separate tab):
Brick class(separate tab):
Paddle class(separate tab):