Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Ball Bounce Off Of Paddle....how!?!?!?!
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
gergysun




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
gergysun




PostPosted: 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
Tony




PostPosted: 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?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
gergysun




PostPosted: 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
gergysun




PostPosted: 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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Nick




PostPosted: 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
gergysun




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: 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.
gergysun




PostPosted: 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
Degensquared




PostPosted: 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
gergysun




PostPosted: 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
Degensquared




PostPosted: 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.
CodeMonkey2000




PostPosted: 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.
Wolf_Destiny




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 23 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: