From a Pong game, I've getted the logic to make the ball rebound on the walls of the X-axis. This is the logic that makes this:
Main method:
protected void handleBounces(float px, float py) {
/*Rebound on X-axis*/
if(mBall.x <= Ball.RADIUS || mBall.x >= getWidth() - Ball.RADIUS) {
mBall.bounceWall();
if(mBall.x == Ball.RADIUS)
mBall.x++;
else
mBall.x--;
}
}
Ball logic:
public void bounceWall() {
setAngle(3 * Math.PI - mAngle);
}
public void setAngle(double angle) {
mAngle = angle % (2 * Math.PI);
mAngle = boundAngle(mAngle);
findVector();
}
protected double boundAngle(double angle) {
return boundAngle(angle, angle >= Math.PI);
}
protected void findVector() {
vx = (float) (speed * Math.cos(mAngle));
vy = (float) (speed * Math.sin(mAngle));
}
Now, I need to do the same, but for the Y-axis's top side, this is. I'm making a mod to the pong game, that consists on just one paddle (at the bottom) and the ball must rebound on the other 3 walls.
As said, I make it rebound on the X-axis walls, but I need now make it rebound on the Top side of the screen (Y-axis top side).
I tried several ways, but the ball doesn't rebound. I think that with an implementation like this, it should work, but doesn't work, the ball continues and dissapears at the top:
Main method:
protected void handleTopFastBounce(float px, float py) {
if(mBall.goingUp() == false) return;
if(mBall.y <= Ball.RADIUS) {
mBall.bounceWall();
playSound(mWallSFX);
if (mBall.y == Ball.RADIUS) {
mBall.y++;
}
}
}
Ball logic: Same as for the method above, but adding this method:
public boolean goingUp() {
return mAngle >= Math.PI;
}