Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
ball bouncing help
Author Message
Stormtrooper

Posted: Tue Apr 15, 2003 12:58 pm   Post subject: ball bouncing help

ok i made this code and i want the balls to bounce when they hit eachother what should i add to the programm and where to put it? also my 2 balls are in each other how do i make them to be by them selves and move into different directions?

 code: setscreen ("graphics") var c, dx, dy, r, cx, cy, b, a, d, e, f, g : int := 0 var q : string (1) var strc : string var colours : array 0 .. 15 of string := init ("black", "dark blue", "dark green", "dark cyan", "dark red", "dark magneta", "brown", "white", "grey", "blue", "green", "cyan", "red", "magenta",     "yellow", "bright white") loop     put " Enter Radius of ball 1 and 2 "     get r, a     put " Enter a Colour of ball 1 "     get strc : *     for i : 0 .. 15         if strc = colours (i) then             c := i         end if     end for     put " Enter a Colour of ball 2 "     get strc : *     for i : 0 .. 15         if strc = colours (i) then             c := i         end if     end for     cx := Rand.Int (r, maxx - r)     cy := Rand.Int (r, maxy - r)     dx := 1     dy := 1     d := Rand.Int (a, maxx - a)     e := Rand.Int (a, maxy - a)         f := -1     g := -1     loop         exit when hasch         drawfilloval (cx, cy, r, r, c)         delay (10)         drawfilloval (cx, cy, r, r, white)         drawfilloval (d, e, a, a, b)         delay (10)         drawfilloval (d, e, r, r, white)         d += f         e += g         cx += dx         cy += dy         if d >= maxx - a or d <= a then             f := -f             g := -g         end if         if cx >= maxx - r or cx <= r then             dx := -dx             dy := -dy         end if         if e > maxy - a or e <= a then             g := -g             f := -f         end if         if cy > maxy - r or cy <= r then             dy := -dy             dx := -dx         end if     end loop end loop

Tony

Posted: Tue Apr 15, 2003 1:44 pm   Post subject: (No subject)

well the basic idea is to change the direction of movement after the collision.

You should read a tutorial on circular collisions.

To realistically bounce balls from each other, read a tutorial on "making a pool game". It describes the physics behind ball colision and gives some code examples of how to code it. You should use it as a guide only though since it was programmed as an animation so it would bug if you just copy/paste it into your program.

You can find all the reading material in the "tutorials" section of this site.
Tony's programming blog. DWITE - a programming contest.
Homer_simpson

Posted: Fri Apr 25, 2003 10:41 pm   Post subject: (No subject)

i believe this is what you have been trying to make ?...
 code: var x, y := 200 var dirx, diry := true View.Set ("offscreenonly") loop     drawfilloval (x, y, 10, 10, 9)     View.Update     delay (1)     cls     case dirx of         label true :             x += 2         label false :             x -= 2     end case     case diry of         label true :             y -= 1         label false :             y += 1     end case     if x >= 630 or x <= 0 then         dirx := not dirx     end if     if y >= 390 or y <= 0 then         diry := not diry     end if end loop
SilverSprite

Posted: Sat Apr 26, 2003 11:07 am   Post subject: (No subject)

What does this line do?

dirx := not dirx
Martin

Posted: Sat Apr 26, 2003 11:08 am   Post subject: (No subject)

Toggle's dirX

not true = false
not false = true
therefor, not dirX = true if dirX = false, and vise versa
void

Posted: Thu May 01, 2003 5:33 pm   Post subject: paddle game

okay....i have this thing for paddle ball game where i tell the porgram that if the lowest point in the ball touches any part of the top of the paddle....then bounce it back....and i have everything perfectly fine.....(i used the same program structure that i saw on here before for the bouncing balls...but i edited it so that if its below 0 on the y-axis...then exit the loop thats drawing the ball....but now im stuck....i need an idea that will allow me to repeat the process for 3 times (the user has three balls to use).....any ideas .....im up for anything
Tony

Posted: Thu May 01, 2003 5:51 pm   Post subject: (No subject)

well put that loop inside another loop. Every time you exit your inside loop you add one to used up lifes counter. When the counter reaches 3, that means you used up your 3rd ball and its game over. Otherwise your outside loop returns you to the beginning of your inside loop
Tony's programming blog. DWITE - a programming contest.
Homer_simpson

Posted: Thu May 01, 2003 6:27 pm   Post subject: (No subject)

or u can make a variable and everytime ball misses add the value of the variable by 1 and exit loop when variable = 3

Homer_simpson

Posted: Thu May 01, 2003 6:35 pm   Post subject: (No subject)

 code: function distance (x1, y1, x2, y2 : int) : int     result round (((x1 - x2) ** 2 + (y1 - y2) ** 2) ** .5) end distance var x1, y1 := 100 var x2, y2 := 10 var dirx1, diry1 := false var dirx2, diry2 := true View.Set ("offscreenonly") loop     drawfilloval (x1, y1, 50, 50, 9)     drawfilloval (x2, y2, 50, 50, 4)     View.Update     delay (1)     cls     case dirx1 of         label true :             x1 += 2         label false :             x1 -= 2     end case     case diry1 of         label true :             y1 -= 1         label false :             y1 += 1     end case     if x1 >= 630 or x1 <= 0 then         dirx1 := not dirx1     end if     if y1 >= 390 or y1 <= 0 then         diry1 := not diry1     end if     case dirx2 of         label true :             x2 += 1         label false :             x2 -= 1     end case     case diry2 of         label true :             y2 -= 2         label false :             y2 += 2     end case     if x2 >= 630 or x2 <= 0 then         dirx2 := not dirx2     end if     if y2 >= 390 or y2 <= 0 then         diry2 := not diry2     end if     %collide     if distance (x1, y1, x2, y2) < 100 then         dirx2 := not dirx2         diry2 := not diry2         dirx1 := not dirx1         diry1 := not diry1     end if end loop

here's another one that balls change direction when they hit eachother
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 9 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: