# pset4 velocity & ball's angle

I'm stuck in step 6. I have understood how to make the ball bounce on the window's edges but I cannot figure out how to make the ball move in an angle. I assume that it has to be some combination of Y-axis and X-axis movement, but I cannot figure it out. To be more specific, the ball is moving in an angle but I don't think this is the right way. Here's a part of my code:

``````                    //bouncing up-down
move(ball, 0, velocity);
if (getY(ball) + getHeight(ball) >= getHeight(window))
velocity = -velocity;
else if (getY(ball) <= 0 )
velocity = -velocity;
pause(10);`
//bouncing left-right
move(ball, velocity, 0);
if (getX(ball) + getWidth(ball) >= getWidth(window))
velocity = -velocity;
else if (getX(ball) <= 0 )
velocity = -velocity;
pause(10);
``````

I also don't get why I have to use drand48. Some pseudo code and a little bit of explanation would be really useful!

Here is some of my code.When this block is executed, I am able to move the ball as long as I move the paddle.Else (in the while (true) loop) the ball wont move.``` while (lives > 0 && bricks > 0) { while (true) { GEvent event = getNextEvent(MOUSE_EVENT); if (event != NULL) { if (getEventType(event) == MOUSE_MOVED) { double x = getX(event) - getWidth(paddle) / 2; double y = HEIGHT - PADDLE_HEIGHT - getHeight(ball); if (x < 0) x = 0; else if (x >= (WIDTH - PADDLE_WIDTH)) x = WIDTH - PADDLE_WIDTH; setLocation(paddle, x, y); move( ball, vx, vy ); } }```

I think you may need a separate velocity variable for each dimension (e.g., Vx and Vy). I think this will clear up the angle problem.

• I cannot understand where to define the vx and vy speed. I do it outside both while loops, but then the ball moves only when I move the mouse (which means the paddle as well). I can't get the ball to move when there is a mouse click. Oct 5 '14 at 21:31
• I do not think you need two while loops to do this and I suspect that solving your problem may involve getting everything into one while loop. You should probably post more of your code. I do not see any while loops in what is posted. Oct 6 '14 at 23:43
• I've edited my post. If you could give some advice I'd appreciate it.I a little stuck here... Oct 10 '14 at 5:54

Pseudo code should be something like this:
While (Lives and Blocks are both greater than 0)
If (x<0 or x>WIDTH) then reverse velocity on x-axis
If (y<0 or y>HEIGHT) then reverse velocity on y-axis

If you are having trouble moving the paddle independently of the ball moving then you probably have more than one While Loop and you should only be using one.

Another thing you need to is have the ball moving in both the x and y directions when the program is initiated.

• So the ball's movement and the bouncing is implemented outside forever loop ? Because both move ball and move paddle should be inside a loop (at least this is what I understood). Oct 18 '14 at 21:02
• I think the only thing that should be outside the while loop is the initial movement - set the ball moving and then enter the While loop where paddle movement and bouncing can be checked for. Oct 19 '14 at 0:35