#Breakout Game Ball Gets Stuck Bouncing Up and Down or Left to Right

1 messages · Page 1 of 1 (latest)

tawny nebula
#

Hello I have created a breakout game however the ball awkwardly bounces up and down in place or left to right every once in a while is there a way to fix this?

sharp gazelleBOT
#

<@&987246487241105418> please have a look, thanks.

tawny nebula
sharp gazelleBOT
steep sorrel
#

I imagine it has something to do with x or y component being zero. You are basing your ball direction change solely on some multiplication of x or y and because zero times anything is zero, it remains zero.

tawny nebula
steep sorrel
#

Was just giving ideas. Possibly here.

                         ballXdir = (int) Math.round(speed * Math.cos(angle)); // Convert angle to x-direction
                            ballYdir = (int) Math.round(speed * Math.sin(angle)); // Convert angle to y-direction

which is right after this

double angle = Math.toRadians(Math.random() * 120 + 30); // Get a random angle between 30 and 150 degrees
#

Seems like you increment position by this ball dir. So if ball dir is zero then that axis stays at the same value.

#

Maybe don't let ballXdir and ballYdir be zero.