
-----------------------------------
xmen
Thu Mar 25, 2004 11:48 pm

Ball Bouncing Screensaver
-----------------------------------
hey apparently i hav made a program where a ball (or happy face) bounces around the screen.....but what i need for this screensaver is to have two balls bouncing around the screen instead of just one, also when the two balls meet they will also bounce away

my question is......does anyone of you hav a program similar to this?? or can anyone plzzzzz help me help with this as to how i can modify my program

plz reply asap thankyou

-----------------------------------
Tony
Fri Mar 26, 2004 12:57 am


-----------------------------------
well you employ circular collision detection to check if balls are coliding or not.

If the distance between two centers is equal to the sum of two radii, then balls are just touching each other.

Then you figure out how balls bounce back... We've got some nice pool programs posted and I think even some discussion on the physics of collisions.

-----------------------------------
Cervantes
Fri Mar 26, 2004 1:40 am


-----------------------------------
circular collisions is my entire compsci life :lol: I still can barely get them to work :P

is this an assignment?  if it is, and if you're in grade 10, I think you are probably doing a harder question than asked for.  that, or your teacher is a madman.  if it isn't an assignment and you're doing this for fun, and you are in grade 10, it would probably be wise to make the project easier. 

Check out [url=http://www.compsci.ca/v2/viewtopic.php?t=3240]thoughtful's pool tutorial for some circle collision data.

-----------------------------------
xmen
Fri Mar 26, 2004 3:17 pm


-----------------------------------
im in grade 10 in canada and yes this is an assignment

-----------------------------------
Hunter007
Thu Apr 01, 2004 4:15 pm


-----------------------------------
Hey thought I'd help you out... I did this project last semester, but no winking faces. The faces are just circles inside bigger circles. 

Heres the code, reply if you need more help...


View.Set ("graphics:600;400,offscreenonly")

% VARIABLES
var Color : int
var x1, y1, dx1, dy1, x2, y2, dy2, dx2 : int
var count : int := 0
var tempx, tempy : int

% TOP AND BOTTOM BORDER
for i : 1 .. maxx by 20
    Color := Rand.Int (1, 60)
    if Color = 31 then
        Color := Rand.Int (1, 60)
    end if
    Draw.FillStar (i, 0, i + 20, 20, Color)
    Draw.FillStar (i, maxy - 20, i + 20, maxy, Color)
end for

% LEFT AND RIGHT BORDER
for k : 20 .. maxy - 20 by 20
    Color := Rand.Int (1, 60)
    if Color = 31 then
        Color := Rand.Int (1, 60)
    end if
    Draw.FillStar (0, k, 20, k + 20, Color)
    Draw.FillStar (maxx - 20, k, maxx, k + 20, Color)
end for
x1 := maxx - 41
y1 := maxy - 61
x2 := maxy - 41
y2 := maxy - 41
dx1 := 4
dy1 := 0
dx2 := 0
dy2 := 4

% COLLISION DETECTION
function collided (x1, x2, y1, y2, radius : int) : boolean
    for xCheck : x1 - radius .. x1 + radius
        if xCheck >= x2 - radius and xCheck = y2 - radius and yCheck  maxx - 41 or x1 < 41 then
        dx1 := -dx1
    end if

    if y1 > maxy - 41 or y1 < 41 then
        dy1 := -dy1
    end if

    if x2 > maxx - 41 or x2 < 41 then
        dx2 := -dx2
    end if

    if y2 > maxy - 41 or y2 < 41 then
        dy2 := -dy2
    end if
    
    % WHEN THEY COLLIDE BALLS BOUNCE IN DIFFERENT DIRECTIONS
    if collided (x1, x2, y1, y2, 20) then
        tempx := dx1
        tempy := dy1
        dx1 := -dx1
        dy1 := -dy1
        dx1 := round (dx1 / 2 + dx2 / 2)
        dy1 := round (dy1 / 2 + dy2 / 2)
        dx2 := -dx2
        dy2 := -dy2
        dx2 := round (dx2 / 2 + tempx / 2)
        dy2 := round (dy2 / 2 + tempy / 2)
        count := count + 1
    end if

    locate (maxrow -2,4)
    colour (blue)
    put "Collisions : ", count ..
    colour (black)

    % THE BALLS
    Draw.FillOval (x1, y1, 20, 20, blue)
    Draw.FillOval (x2, y2, 20, 20, black)
    View.Update
    delay (10)
    Draw.FillOval (x1, y1, 20, 20, 0)
    Draw.FillOval (x2, y2, 20, 20, 0)
end loop


-----------------------------------
Jodo Yodo
Thu Apr 01, 2004 5:38 pm


-----------------------------------
Is there a way to make the collision detection circular instead of in a box shape?  Because right now, it the program works well because the circles are small.  But if the circles were bigger, it would be more obvious that the collision detection is in the shape of a square.

-----------------------------------
Cervantes
Sat Apr 03, 2004 2:16 pm


-----------------------------------
Yes there is.


If the distance between two centers is equal to the sum of two radii, then balls are just touching each other.



if Math.Distance (circle1x, circle1y, circle2x, circle2y) < circle1radius + circle2radius then


-----------------------------------
the_short1
Thu Apr 08, 2004 10:52 pm


-----------------------------------
now i know why tony is adicted to taht... !!!! GOOD STUFF!!! i like math.distance now.... ehehhehe..... thanks for the information ppl.....

now i can make my jezzball ten times better...
