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

Username:   Password: 
 RegisterRegister   
 Ball Bouncing Screensaver
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xmen




PostPosted: Thu Mar 25, 2004 11:48 pm   Post subject: 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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Mar 26, 2004 12:57 am   Post subject: (No subject)

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




PostPosted: Fri Mar 26, 2004 1:40 am   Post subject: (No subject)

circular collisions is my entire compsci life Laughing I still can barely get them to work Razz

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 thoughtful's pool tutorial for some circle collision data.
xmen




PostPosted: Fri Mar 26, 2004 3:17 pm   Post subject: (No subject)

im in grade 10 in canada and yes this is an assignment
Hunter007




PostPosted: Thu Apr 01, 2004 4:15 pm   Post subject: (No subject)

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...

code:

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 <= x2 + radius then
            for yCheck : y1 - radius .. y1 + radius
                if yCheck >= y2 - radius and yCheck <= y2 + radius then
                    result (true)
                end if
            end for
        end if
    end for
    result (false)
end collided

% WHEN TO BOUNCE OFF WALL
loop
    x1 := x1 + dx1
    x2 := x2 + dx2
    y1 := y1 + dy1
    y2 := y2 + dy2

    if x1 > 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




PostPosted: Thu Apr 01, 2004 5:38 pm   Post subject: (No subject)

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




PostPosted: Sat Apr 03, 2004 2:16 pm   Post subject: (No subject)

Yes there is.

tony wrote:

If the distance between two centers is equal to the sum of two radii, then balls are just touching each other.


code:

if Math.Distance (circle1x, circle1y, circle2x, circle2y) < circle1radius + circle2radius then
the_short1




PostPosted: Thu Apr 08, 2004 10:52 pm   Post subject: (No subject)

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...
Sponsor
Sponsor
Sponsor
sponsor
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 1  [ 8 Posts ]
Jump to:   


Style:  
Search: