Computer Science Canada

Need Help- ball won't stay on scroll bar

Author:  The_Triangle [ Sat Nov 12, 2005 7:38 am ]
Post subject:  Need Help- ball won't stay on scroll bar

i been trying forever to make this ball bounce off of the paddle (its not pong, but something like it) and its just not happening.

i tried to make that if the ball_x is equal to the scroll's x, the ball_y would remain on the same y cordinate as the scrolls y.

New Code:
code:

View.Set ("graphics:max,max,offscreenonly")
%-------------------------------------- ball -------------------------------------------

proc ball (var x, y, mx, my, mb, loadBall, size : int)

    colourback (black)
    cls
    var speed : int := 10

    %Pic.Draw (loadBall, mx - 7, 74, picCopy)
    drawfillbox (mx - 45, 74, 1000, 100, black)     % erases first ball
    y -= 2         % gravity
    drawfilloval (mx + 30, y, size, size, brightblue)
    mousewhere (mx, my, mb)     % click3 to bounce ball off the scrollbar(paddle)
    if mb = 1 then
        y += speed
        %Pic.Draw (loadBall, mx - 7, y, picCopy)
    end if

end ball

%-------------------------------------- moveScroll -----------------------------------------

proc moveScroll (mx, scroll : int)

    drawfillbox (mx + 21, 20, maxx, 80, black)           % old drawBox scroll
    drawfillbox (mx, 20, -20, 80, black)
    drawfillbox (mx, 20, mx + 70, 80, blue)

end moveScroll

%-------------------------------------- collisionDetect ------------------------------------

proc collisionDetect (y, font1 : int)

    if y <= 1 then
        Font.Draw ("You Lose", 250, 200, font1, black)
    end if

end collisionDetect

%-------------------------------------- Mainline -------------------------------------------
var x, y : int := 100
var size : int := 10
var mx, my, mb : int
var scroll : int := Pic.FileNew ("scroll.JPG")
var loadBall : int := Pic.FileNew ("ball.JPG")
var font1 : int := Font.New ("verdana:50:bold")

loop
    mousewhere (mx, my, mb)
    ball (x, y, mx, my, mb, loadBall, size)
    moveScroll (mx, scroll)
    collisionDetect (y, font1)
    delay (10)
    View.Update
end loop

Author:  MysticVegeta [ Sat Nov 12, 2005 12:55 pm ]
Post subject: 

Please post the necessary images and zip it please. Thank you.

Author:  Cervantes [ Sat Nov 12, 2005 1:20 pm ]
Post subject: 

Think about storing the velocity of the ball in variables.

    ball_x
    ball_y
    ball_vx
    ball_vy

That way, you can use a little Collision Detection to check if the ball has hit the paddle. If it has, you reverse the velocity that is perpendicular to the plane of the paddle. In other words, if the paddle moves from left to right and the ball hits the top of it, reverse its y velocity.

Author:  The_Triangle [ Sat Nov 12, 2005 3:31 pm ]
Post subject: 

Cervantes wrote:
Think about storing the velocity of the ball in variables.

    ball_x
    ball_y
    ball_vx
    ball_vy

That way, you can use a little Collision Detection to check if the ball has hit the paddle. If it has, you reverse the velocity that is perpendicular to the plane of the paddle. In other words, if the paddle moves from left to right and the ball hits the top of it, reverse its y velocity.


that would help if i knew what velocity was... Embarassed

Author:  The_Triangle [ Sat Nov 12, 2005 3:32 pm ]
Post subject: 

MysticVegeta wrote:
Please post the necessary images and zip it please. Thank you.


I'll re-edit the code , so that there are no pictures Smile

Author:  [Gandalf] [ Sat Nov 12, 2005 4:26 pm ]
Post subject: 

Please don't double post.

Velocity is just speed with direction. So if you had a speed of 10km/h then an equal velocity could be 10km/h east. Translating this to more programmable terms, you have an x velocity and a y velocity. Now, each of those has a direction, so it can be either + or -.

Lets say your ball hits the paddle going vy=0 and vx=4, then once it hits the paddle vx=vx*-1 or vx=-4.

Author:  The_Triangle [ Sun Nov 13, 2005 9:23 am ]
Post subject: 

[Gandalf] wrote:
Please don't double post.

Velocity is just speed with direction. So if you had a speed of 10km/h then an equal velocity could be 10km/h east. Translating this to more programmable terms, you have an x velocity and a y velocity. Now, each of those has a direction, so it can be either + or -.

Lets say your ball hits the paddle going vy=0 and vx=4, then once it hits the paddle vx=vx*-1 or vx=-4.


Ok, say I use ball_vx and ball_vy.

What use would I have for my ball_x and my ball_y variables ?

Author:  Cervantes [ Sun Nov 13, 2005 10:17 am ]
Post subject: 

You would add the x velocity to the x position and the y velocity to the y position, each time though the loop:

code:

loop
   x := x + vx   %Note this could be rewritten as x += vx
   y += vy
   %other things
end loop

Author:  Albrecd [ Mon Nov 14, 2005 2:54 pm ]
Post subject: 

Right now, your only collision detection is that if the ball hits the bottom of the screen then it puts "you lose." You need another procedure or proc or whatever you want to use (procedure would be the best) to determine if the ball has hit the paddle.

Ex:
code:
if x_ball >= x_paddle and x_ball <= x_paddle + paddlewidth any y_ball = y_Paddle then %put whatever you want to happen next here.


Also, you should probably not have x_ball be equal to x_paddle (which it currently is) or you can't lose.

Author:  Albrecd [ Mon Nov 14, 2005 3:03 pm ]
Post subject: 

Sorry, that "any" in the code is supposed to be "and." I would have edited it but for some reason It won't give me that option Confused sorry

Author:  do_pete [ Mon Nov 14, 2005 3:04 pm ]
Post subject: 

or you could go
code:
function Hit:boolean
     if Ball_X>=Paddle_X and Ball_X<=Paddle_X+Paddle_Width and
          Ball_Y<=Paddle_Y+PaddleWidth then
               result true
     end if
end Hit


: