Computer Science Canada

Ball Bounce Off Of Paddle....how!?!?!?!

Author:  gergysun [ Thu Nov 29, 2007 10:23 pm ]
Post subject:  Ball Bounce Off Of Paddle....how!?!?!?!

Honeslty, lol

ive been searching of how to do that for about an hour now, and i dont understand it
all i want to do is have the ball bounce off my paddle....
here is my code so far...


Code edited out at OP's request

Author:  Tony [ Thu Nov 29, 2007 10:42 pm ]
Post subject:  RE:Ball Bounce Off Of Paddle....how!?!?!?!

It's the same way as bouncing off a wall or a ceiling, just like you're already doing
code:

elsif bally+50>=maxy then
rise:=-1

but instead of maxy, you're checking the ball's position against that of the paddle.

Author:  gergysun [ Thu Nov 29, 2007 10:50 pm ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

and how do i do that.....like what do i put

Author:  Tony [ Thu Nov 29, 2007 11:19 pm ]
Post subject:  RE:Ball Bounce Off Of Paddle....how!?!?!?!

Well how did you figure out how to bounce the ball off the walls?

Author:  gergysun [ Fri Nov 30, 2007 2:23 pm ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

well, i looked at the tutorials and managed to get something out of it, and thats how i got 2 of the sides to work, then i asked someone to help me and they gave me the answer of how it works and then i sort of understood it, then when the ball hit the bottom it didnt finish so i figured out how to make it finsih, and now i cant get the ball to bounce off the paddle

Author:  gergysun [ Fri Nov 30, 2007 7:35 pm ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

WILL SOMEONE HELP ME FOR CRYING OUT LOUD...I DONT UNDERSTAND WHAT TONY IS SAYING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Author:  Nick [ Fri Nov 30, 2007 7:39 pm ]
Post subject:  RE:Ball Bounce Off Of Paddle....how!?!?!?!

i think you just want someone to do the code for you I read through the help and its more than sufficent

Author:  gergysun [ Fri Nov 30, 2007 7:42 pm ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

i have, but i dont understand it...only the stupid rectangle-ball collision thing....i just dont get it.
and what does Toney mean by

Quote:
but instead of maxy, you're checking the ball's position against that of the paddle.

Author:  Clayton [ Fri Nov 30, 2007 9:42 pm ]
Post subject:  RE:Ball Bounce Off Of Paddle....how!?!?!?!

Instead of just checking the ball's x co-ordinate and y co-ordinate against maxx and maxy respectively, you want to check within your paddle's x and y co-ordinates instead.

Author:  gergysun [ Fri Nov 30, 2007 9:52 pm ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

ok, so what ur sayiung is instead of typing
" elsif bally + 50 >= maxxy then
rise := -1 "
i would type
"elsif x - 80 >= maxy then
rise := -1"


if not, please correct me
here is my full code

drawfill (1, 399, 150, 150)
% Ball is slding across title
var font1 : int %defiing font as an integer
font1 := Font.New ("Stencil:30") % defining font size
assert font1 > 0
Font.Draw ("Greg's Ping Pong Game", 160, 325, font1, red) % Main Title
Font.Free (font1)
colorback (6) % the background color
locate (7, 30)
var title1 : int %defines title1 as an integer
title1 := 10

loop %begins title loop
%Title wiper
drawfilloval (title1, 335, 20, 20, 12)
exit when title1 = 600
delay (50)%delay's playback 50 milliseconds
drawfilloval (title1, 335, 20, 20, 158) % draws circle and fills it
title1 := title1 + 10
end loop %ends title loop

cls % clears the screen
colorback (12)% defining background color
put " ---The intructions are ---- " % outputs "Intructions"
put " "% skips a line
put " Move the Paddle with mouse " % outputs actual instructions
put " Each time you keep the ball up in the air you reieve a point"% outputs actual instructions
put " If you miss the ball, you lose "% outputs actual instructions
put " "% skips a line
put " "% skips a line
put " "% skips a line
put " "% skips a line
put " "% skips a line

% press any key to continue
font1 := Font.New ("Stencil:20")%defines font1 as font.new and font size
Font.Draw ("~~Press any Key to Begin~~", 90, 125, font1, red)

Input.Pause % won't continue until user presses a key

colourback (blue)% defining bacground color
cls % Clears the screen
setscreen ("offscreenonly")

var rise, run, rad, u, yy, xx : int%defiing rise,run,rad,yy,xx as integers
var bally, ballx : int := 200% defining bally,ballx as integers
rad := 10 %defines Radius of ball
rise := 2% defines where the ball starts
run := 2 % defines where the ball starts

loop%begins main loop


var x, y, button : int %defines x,y,button as integers
Mouse.Where (x, y, button)%indicates where the mouse is
Draw.FillOval (ballx, bally, rad, rad, yellow)% draws pong ball
ballx := ballx + run
bally := bally + rise


if ballx + 50 >= maxx then
run := -1
elsif ballx - 50 <= 0 then
run := +1
elsif bally + 50 >= maxy then
rise := -1
elsif bally + 50 >= 140 then
rise := -1

elsif x-80 >=maxx then
rise:=-1
elsif x + 80 <= maxy then
rise := -1
elsif bally - 50 <= 0 then
exit

end if
View.Update
delay (10) % delay's playback 10 milliseconds
cls %Clears the screen
% Paddle
Draw.Box (50, 50, maxx - 50, maxy - 50, white)%draws main border
drawfill (50, 40, white, white)% draws inside-outise white box
drawfillbox (x - 80, 66, x + 80, 75, brightcyan) % Draws paddle
% outside rectangle + fill
drawline (40, 30, 40, 370, black)% draws line
drawline (40, 30, 600, 30, black)% draws line
drawline (600, 30, 600, 370, black)% draws line
drawline (600, 370, 40, 370, black)% draws line
drawfill (0, 10, brightred, black)%fills outside border brightred
end loop % ends the main loop

Author:  Degensquared [ Fri Nov 30, 2007 10:25 pm ]
Post subject:  RE:Ball Bounce Off Of Paddle....how!?!?!?!

Please use code tags >.<

So essentially, you just want the y increment to invert if it hits the paddle.

so instead of comparing anything to maxx or maxy you are looking for a relationship between the ball and the paddle right?

so....
code:

%Checks for a collision
if ballx >= x - 80 and ballx <= x + 80 and bally <= y + rad then
%Inverts the y velocity
rise *= 1
end if

Author:  gergysun [ Fri Nov 30, 2007 10:41 pm ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

ok, i gotcha, so basically, when ur doing the paddle and the ball, u just forget about the maxxy,and maxy..
but
you code still doesnt work Sad

Author:  Degensquared [ Fri Nov 30, 2007 11:03 pm ]
Post subject:  RE:Ball Bounce Off Of Paddle....how!?!?!?!

well yeah, I didn't test it, it was an example for you to work with, not to copy directly.

Author:  CodeMonkey2000 [ Fri Nov 30, 2007 11:23 pm ]
Post subject:  RE:Ball Bounce Off Of Paddle....how!?!?!?!

First thing you need to know is how far down the paddle is. Since you are keeping this constant, we just check to see if the ball is below this point. You can figure out how far down the paddle is by looking at the larger Y coordinate in the drawbox procedure for the paddle. Next we need to determine weather or not the ball's x coordinate is more than or equal to the lower(smaller) x coordinate of the paddle, and less than or equal to the larger x coordinate of the paddle (you can get these coordinates from the parameters in the drawbox procedure you used for the paddle). If all of these conditions are met then the ball has collided with the paddle. Remember to keep the radius in mind though.

Author:  Wolf_Destiny [ Sat Dec 01, 2007 12:19 am ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

gergysun @ Fri Nov 30, 2007 7:35 pm wrote:
WILL SOMEONE HELP ME FOR CRYING OUT LOUD...I DONT UNDERSTAND WHAT TONY IS SAYING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Just, you know strictly for the record and all, complaining about the help you've been given is not generally a great idea. Maybe try asking for clarification, and nicely at that, since no one actually has to help you at all. Tony was simply trying to lead you in the right direction of solving the problem yourself, because that's what programming is: problem solving.

So, you want the ball to bounce when it hits the paddle right? Well, logically when will it hit the paddle? Where will the ball be when it hits the paddle? Somewhere inside the paddle right? So then just do a simple check:
code:
if the ball's x coordinate is between the left-most x coord of the paddle and the right-most x coord then
if the ball's y coordinate is also between the lower and upper y coordinates of the paddle then
hey, they just collided!!
Do something about it!
end if
end if


Generally a good idea is to draw out your problem on paper. And just try to think out the different steps of the collision.
1. How do I tell when they collide?
Maybe I should compare their relative positions...
2. What do I do when they collide?
Well, I want the ball to bounce.
3. What would be the result of it bouncing?
It starts going up.

Anyways hope that helps
Quote:
ok, i gotcha, so basically, when ur doing the paddle and the ball, u just forget about the maxxy,and maxy..
but
you code still doesnt work

And again, don't expect us to do your assignment for you. If you don't do it yourself, you'll never be able to do it later. No one can help you on the exam, and I've seen people get zero on a compsci exam, after getting good but undeserved marks throughout the course.

P.S. As CodeMonkey stated, do keep the radius in mind, however I would personally worry about that after getting the collision working in the first place.

Author:  gergysun [ Mon Dec 03, 2007 12:19 pm ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

alright, This is what i got so far...(understanding for what you said)

code:
elsif ballx <= x + 80 and ballx <= x - 80 and bally >= 66 and bally >=75 then
        rise *= 1


is that right?

Author:  Tony [ Mon Dec 03, 2007 1:37 pm ]
Post subject:  RE:Ball Bounce Off Of Paddle....how!?!?!?!

You haven't even tried out this code in your program! I would have expected it to be easier for you to just click "run" and see if it works, rather than have us figure out if what you wrote is right.

Author:  LaZ3R [ Mon Dec 03, 2007 1:55 pm ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

You need to simply check the position of the ball relative to the players paddle...

There are SEVERAL ways of doing this. Think about it logically... there's no point in screaming at tony when you don't understand what he said.

Author:  gergysun [ Mon Dec 03, 2007 2:00 pm ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

umm.. i have....
i didnt work, so I was asking you if you could spot out the problem.... ( i know, i should have worded my last post differently)

I've actually managed to get the ball bounce off the paddle using the ssame techniwue as bouncing off the walls BUT now, when the ball goes to the left side, it goes right through..it doesnt bounce...
i just dont get it,

here is my code

code:
drawfill (1, 399, 150, 150)
% Ball is slding across title
var font1 : int %defiing font as an integer
font1 := Font.New ("Stencil:30") % defining font size
assert font1 > 0
Font.Draw ("Greg's Ping Pong Game", 160, 325, font1, red) % Main Title
Font.Free (font1)
colorback (6) % the background color
locate (7, 30)
var title1 : int %defines title1 as an integer
title1 := 10

loop %begins title loop
    %Title wiper
    drawfilloval (title1, 335, 20, 20, 12)
    exit when title1 = 600
    delay (50) %delay's playback 50 milliseconds
    drawfilloval (title1, 335, 20, 20, 158) % draws circle and fills it
    title1 := title1 + 10
end loop %ends title loop

cls % clears the screen
colorback (12) % defining background color
put " ---The intructions are ---- " % outputs "Intructions"
put " " % skips a line
put " Move the Paddle with mouse " % outputs actual instructions
put " Each time you keep the ball up in the air you reieve a point" % outputs actual instructions
put " If you miss the ball, you lose " % outputs actual instructions
put " " % skips a line
put " " % skips a line
put " " % skips a line
put " " % skips a line
put " " % skips a line

% press any key to continue
font1 := Font.New ("Stencil:20") %defines font1 as font.new and font size
Font.Draw ("~~Press any Key to Begin~~", 90, 125, font1, red)

Input.Pause % won't continue until user presses a key

colourback (blue) % defining bacground color
cls % Clears the screen
setscreen ("offscreenonly")

var rise, run, rad, u, yy, xx : int %defiing rise,run,rad,yy,xx as integers
var bally, ballx : int := 100 % defining bally,ballx as integers
rad := 10 %defines Radius of ball
rise := 2 % defines where the ball starts
run := 2 % defines where the ball starts

loop %begins main loop


    var x, y, button : int %defines x,y,button as integers
    Mouse.Where (x, y, button) %indicates where the mouse is
    Draw.FillOval (ballx, bally, rad, rad, yellow) % draws pong ball
    ballx := ballx + run
    bally := bally + rise


    if ballx + 50 >= maxx then
        run *= -1
    elsif ballx - 50 <= 0 then
        run *= +1
    elsif bally + 50 >= maxy then
        rise *= -1

    elsif bally - 50 <= 0 then
        exit
   

  elsif bally - 90 <= 0 then
        rise *=- 1
  end if

    View.Update
    delay (10) % delay's playback 10 milliseconds
    cls %Clears the screen
    % Paddle
    Draw.Box (50, 50, maxx - 50, maxy - 50, white) %draws main border
    drawfill (50, 40, white, white) % draws inside-outise white box
    drawfillbox (x + 80, 66, x - 80, 75, brightcyan) % Draws paddle
    % outside rectangle + fill
    drawline (40, 30, 40, 370, black) % draws line
    drawline (40, 30, 600, 30, black) % draws line
    drawline (600, 30, 600, 370, black) % draws line
    drawline (600, 370, 40, 370, black) % draws line
    drawfill (0, 10, brightred, black) %fills outside border brightred
end loop % ends the main loop


....
it just doesnt make any sence.... please correct my mistake
thanks

Author:  LaZ3R [ Mon Dec 03, 2007 2:58 pm ]
Post subject:  RE:Ball Bounce Off Of Paddle....how!?!?!?!

You still fail to understand the point being made here. You even said it yourself, you figured out how to get the ball to bounce off the paddle using the SAME technique. Apply this same code but alter it so it works for the left side of the screen as well.

The point of this site is to assist users, not do the work for them. You don't learn that way.

Author:  gergysun [ Mon Dec 03, 2007 5:10 pm ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

ARG
now that i have gotten the ball to bounce off paddle, the ball goes right through the left side boundery, and when the paddle misses the ball, it doesnt finish!!! I DONT GET IT
please someone, I know i may have been a pest on earlier posts, but ive been trying to get this thing to work for 5 days now...i must be stupid at this :S
anyways, can someone please help me?

Author:  gergysun [ Tue Dec 04, 2007 3:27 pm ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

Sorry to quadruple post guys, but i did it! Very Happy
everything thing seems to be working A ok.
For Future people who might need help on this, here is the code

code:
if bally >= 70 and bally <= 90 and ballx >= x - 30 and ballx <= x + 30 then % checks for ball collison on paddle
        rise *= -1 % reverses velocity

Author:  LaZ3R [ Tue Dec 04, 2007 7:33 pm ]
Post subject:  Re: Ball Bounce Off Of Paddle....how!?!?!?!

That method works. You'll learn new and more effective ways of doing collision detection later on as you do more programming.

Example: If your ball increases speed at any time during the game, you have created a gap of 20 pixels which means if the ball travels at 21 pixels down per move, it has a chance of jumping your paddle based on its last position. If the ball doesn't speed up, don't worry about this for now, but think about it for future games you might make.


: