gpex,
If I am correct, the ball is getting stuck onto the paddle because, when the collision takes place, the direction of the ball is the only calculation being made. However, a second calculation is needed. This is, to reposition the ball in front of the paddle, because otherwise the ball continues to touch the paddle, therefore the collision code(ball changing direction) is happening over and over, rapdily, 'without moving'. Once repositioned, it is then free of the collision of the paddle.
The reason the ball stays on the paddle until the mouse is shaken, is because of the above, and that once the paddle, in one sync, has a chance to leave the area of the ball(the mouse being shaken heavily), the ball is then not touching the paddle, having then a chance to move.
About a changing direction of the ball. A 'direction' method is quite a simple way to go about it. Think about this, an x coordinate direction would be left or right. A y coordinate direction would be up or down. Knowing this, let's make two direction variables, to use for boolean operations, 1 or 0. Let's say that a value of 1, on the x coordinate, means "right", as to add to the ball's x coordinate. A value of 0 should then equal to "left", as to subtract from the ball's x coordinate. Also, a value of 1, on the y coordinate should equal to "down", as adding, and 0 equal "up", as subtracting.
If the ball hits any vertical surface(player paddle, side wall), then the x coordinate direction should be swapped. If it was 1, it should now be 0. Therefore, if the ball traveling right(1), it will now be going left(0).
If the ball hits any horizontal surface(ceiling, floor), then the y coordinate direction should be swapped. If it was 1, it should now be 0. Therefore, if the ball was traveing down(1), it will now be traveling up(0).
We then use these boolean values to either increase or decrease the value of the specified coordinate. To make the ball go right on the x coordinate, it's value must be added to. A value of 0 would mean to subtract from the position value. Same goes for the y coordinate, except up and down.
if xdir = 1 then inc xpos,2
if xdir = 0 then dec xpos,2
if ydir = 1 then inc ypos,2
if ydir = 0 then dec ypos,2
Try this program:
set display mode 800,600,32
sync on
sync rate 60
REM << set random start position and direction values
xpos = rnd(800)
ypos = rnd(600)
xdir = rnd(1)
ydir = rnd(1)
radius = 10
ink rgb(255,255,255),0
REM <<<<<<< main loop >>>>>>>>
do
REM << collision of left wall
if xpos < 0
REM << reposition ball
xpos = radius
REM << if xpos < 0 then ball's direction was obviously "left"(0);swap direction
xdir = 1
endif
REM << collision of right wall
if xpos > 800
REM << reposition ball
xpos = 800 - radius
REM << if xpos > 800 then ball's direction was obviously "right"(1);swap direction
xdir = 0
endif
REM << collision of ceiling
if ypos < 0
REM reposition ball
ypos = radius
REM << if ypos < 0 then ball's direction was obviously "up"(0);swap direction
ydir = 1
endif
REM << collision of floor
if ypos > 600
REM << reposition ball
ypos = 600 - radius
REM << if ypos > 800 then ball's direction was obviously "down"(1);swap direction
ydir = 0
endif
REM << make ball move according to it's two directions
REM << move right
if xdir = 1 then inc xpos,2
REM << move left
if xdir = 0 then dec xpos,2
REM << move down
if ydir = 1 then inc ypos,2
REM << move up
if ydir = 0 then dec ypos,2
REM << draw ball to screen
circle xpos,ypos,radius * 2
sync
cls
loop
+NanoBrain+