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

Username:   Password: 
 RegisterRegister   
 Many Balls bouncing
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
Paul




PostPosted: Sun Feb 15, 2004 10:53 pm   Post subject: Many Balls bouncing

Can anyone show me an easy way to have many balls bouncing around in a box, preferably without using alot of different variables and not alot of flashing?
Sponsor
Sponsor
Sponsor
sponsor
Homer_simpson




PostPosted: Sun Feb 15, 2004 11:26 pm   Post subject: (No subject)

check out my particle enegine with physics post...
Cervantes




PostPosted: Mon Feb 16, 2004 3:52 pm   Post subject: (No subject)

Well I think he wants something simpler than that Confused
You need to learn to use arrays. If you can't understand them from this code and from the help file and any tutorials on here (You'll get it from all those I'm sude) PM me or ask in this thread. Smile

code:
var howmany : int
put "How many balls? : " ..
get howmany
var x, y, dx, dy : array 1 .. howmany of int
for i : 1 .. howmany
    dx (i) := Rand.Int (-4, 4)
    dy (i) := Rand.Int (-4, 4)
    x (i) := maxx div 2
    y (i) := maxy div 2
end for
setscreen ("offscreenonly")
cls
loop
    cls
    for k : 1 .. howmany
        x (k) += dx (k)
        y (k) += dy (k)
        Draw.FillOval (x (k), y (k), 5, 5, brightred)
    end for
    View.Update
    delay (5)
end loop
zylum




PostPosted: Mon Feb 16, 2004 4:03 pm   Post subject: (No subject)

you forgot to add the bit of code which checks whether the ball has reached the screen...

code:

x (k) += dx (k)
y (k) += dy (k)
if x (k) < 0 or x (k) > maxx then
    dx (k) *= -1
end if
if y (k) < 0 or y (k) > maxy then
    dx (k) *= -1
end if
Cervantes




PostPosted: Mon Feb 16, 2004 4:08 pm   Post subject: (No subject)

Rolling Eyes
Paul is a very compitent programmer who can do that himself...
All he needed to know was how to use an array.
we64




PostPosted: Mon Feb 16, 2004 6:37 pm   Post subject: (No subject)

I changed it based on Cervantes' code... Now it really bouncing around screen...
code:

var howmany : int
put "How many balls? : " ..
get howmany
var x, y, dx, dy : array 1 .. howmany of int
for i : 1 .. howmany
    dx (i) := Rand.Int (-4, 4)
    dy (i) := Rand.Int (-4, 4)
    x (i) := maxx div 2
    y (i) := maxy div 2
end for
setscreen ("offscreenonly")
cls
loop
    cls
    for k : 1 .. howmany
        x (k) += dx (k)
        y (k) += dy (k)
        Draw.FillOval (x (k), y (k), 5, 5, brightred)
        if x (k) >= maxx or x (k) <= 0 then
            dx (k) := -dx (k)
        elsif y (k) >= maxy or y (k) <= 0 then
            dy (k) := -dy (k)
        end if
    end for
    View.Update
    delay (5)
end loop
Paul




PostPosted: Mon Feb 16, 2004 6:46 pm   Post subject: (No subject)

Hehe, thanks, this kinda looks like the other guy's explosion don't you think? And if you use over 100 balls, it looks vaguely 3Dish.
jonos




PostPosted: Mon Feb 16, 2004 6:51 pm   Post subject: (No subject)

we64's thing looks crazy. its like a kaliedoscope. +bits

jonos edit:
nm, bits thing isn't workin.
Cervantes




PostPosted: Mon Feb 16, 2004 7:02 pm   Post subject: (No subject)

....
jonos why are you trying to give we64 bits for merging code that I wrote with a snippet that zylum wrote?

anyways here is a game that I made using thoughtful's collision from his pool tutorial.

code:

View.Set ("position:centre;centre,graphics:300;300,title:Bouncing Balls,nobuttonbar")
var totalballs : int := 15 %NOTE : to increase how many balls will spawn this number must be at least equal to the max number of balls
var x, y, dx, dy : array 1 .. totalballs of int
var dx_temp, dy_temp : array 1 .. totalballs of real
var movedist1, movedist2, collangle, mass, a1, a2, nX, nY, optimisedP : real

var px, py : int
px := maxx div 2
py := maxy div 2
var keys : array char of boolean
var hit : boolean := false
var respawn : boolean := true
var respawnx, respawny : int
var lives : int := 3
const ballradius := 8
const ballcollidedistance := ballradius * 2
var timer : int
setscreen ("offscreenonly")
mass := 1

for i : 1 .. totalballs
    x (i) := Rand.Int (20 + ballradius, maxx - 20 - ballradius)
    y (i) := Rand.Int (20 + ballradius, maxy - 20 - ballradius)
    dx (i) := Rand.Int (-3, 3)
    dy (i) := Rand.Int (-3, 3)
end for

proc drawscreen
    for i : 1 .. maxx by 20
        Draw.FillStar (i, 0, i + 20, 20, 2)
        Draw.FillStar (i, maxy - 20, i + 20, maxy, 2)
    end for
    for k : 20 .. maxy - 20 by 20
        Draw.FillStar (0, k, 20, k + 20, 2)
        Draw.FillStar (maxx - 20, k, maxx, k + 20, 2)
    end for
    locate (3, 3)
end drawscreen

proc ball_movement
    for i : 1 .. totalballs
        %wall bouncing
        if x (i) > maxx - 20 - ballradius then
            dx (i) := -dx (i)
            x (i) += dx (i)
        end if
        if x (i) < 20 + ballradius then
            dx (i) := -dx (i)
            x (i) += dx (i)
        end if
        if y (i) > maxy - 20 - ballradius then
            dy (i) := -dy (i)
            y (i) += dy (i)
        end if
        if y (i) < 20 + ballradius then
            dy (i) := -dy (i)
            y (i) += dy (i)
        end if

        x (i) += dx (i)
        y (i) += dy (i)
        drawfilloval (x (i), y (i), ballradius, ballradius, blue)
    end for
end ball_movement

proc balls_collide
    for i : 1 .. totalballs
        for k : i .. totalballs

            if k not= i then
                if Math.Distance (x (i), y (i), x (k), y (k)) < ballcollidedistance then
         %CREDIT :  THOUGHTFUL
                    if y (k) - y (i) not= 0 and x (k) - x (i) not= 0 then
                        collangle := arctand ((y (k) - y (i)) / ((x (k) - x (i))))

                        nX := cosd (collangle)
                        nY := sind (collangle)

                        a1 := x (i) * nX + y (i) * nY
                        a2 := x (k) * nX + y (k) * nY

                        optimisedP := (2.0 * (a1 - a2)) / (mass + mass)

                        x (i) := x (i) - (round (optimisedP) * round (mass) * round (nX))
                        y (i) := y (i) - (round (optimisedP) * round (mass) * round (nY))
                        x (k) := x (k) + (round (optimisedP) * round (mass) * round (nX))
                        y (k) := y (k) + (round (optimisedP) * round (mass) * round (nY))
                        % moves the balls forward a step so they dont get stuck with each other( but the balls will still stick)
                        x (i) += dx (i)
                        y (i) += dy (i)
                        x (k) += dx (k)
                        y (k) += dy (k)
                    end if
                end if
            end if
        end for
    end for
end balls_collide

proc player_control
    Input.KeyDown (keys)
    if keys (KEY_UP_ARROW) then
        py += 2
    end if
    if keys (KEY_DOWN_ARROW) then
        py -= 2
    end if
    if keys (KEY_RIGHT_ARROW) then
        px += 2
    end if
    if keys (KEY_LEFT_ARROW) then
        px -= 2
    end if
    drawfilloval (px, py, ballradius, ballradius, brightred)
end player_control

proc player_boundaries
    if px > maxx - 50 - ballradius then
        px := maxx - 50 - ballradius
    end if
    if px < 50 + ballradius then
        px := 50 + ballradius
    end if
    if py > maxy - 50 - ballradius then
        py := maxy - 50 - ballradius
    end if
    if py < 50 + ballradius then
        py := 50 + ballradius
    end if
end player_boundaries

proc relocate
    lives -= 1
    delay (1000)
    px := 20 + ballradius
    py := 20 + ballradius
end relocate


loop
    timer := Time.Elapsed
    totalballs := round (timer / 5000)
    exit when totalballs > 20 or hit = true
    cls
    drawscreen

    player_control
    player_boundaries

    ball_movement
    balls_collide
    for k : 1 .. totalballs
        if Math.Distance (px, py, x (k), y (k)) < ballcollidedistance then
            hit := true
        end if
    end for
    View.Update
    delay (10)
end loop

delay (500)
cls
locate (7, 5)

put "Thanks for playing,\n       -Cervantes"


its not nearly done but w/e just for fun Smile
Paul




PostPosted: Mon Feb 16, 2004 7:06 pm   Post subject: (No subject)

Very nice, bahaha, you slow ninja's will never catch me. Grr... it took 10 of them. Have you realized that the player can't reach the corners?
Cervantes




PostPosted: Mon Feb 16, 2004 7:08 pm   Post subject: (No subject)

yes I have Razz I did that to prevent those cheap players from hiding in the corner. Takes a long time for you to die when you hide in the corners Confused
jonos




PostPosted: Mon Feb 16, 2004 7:10 pm   Post subject: (No subject)

i believe that i can give bits who anyone who i think deserves them, and my opinion on who deserves them does not have to be the same as your. i liked what he did with it, so i thought that i would give him bits.
Cervantes




PostPosted: Mon Feb 16, 2004 7:15 pm   Post subject: (No subject)

Jonos you are totally entitled to you're opinion and your beliefs.
As for my beliefs oh who deserves bits, I think no one here. That's really and truly not a hard program to make Laughing

But while you're enjoying your freedom of opinion allow me to enjoy mine: le cut... le paste. Razz
jonos




PostPosted: Mon Feb 16, 2004 7:26 pm   Post subject: (No subject)

the code was not difficult compared to your skill level, but to mine it was.
Paul




PostPosted: Mon Feb 16, 2004 7:33 pm   Post subject: (No subject)

No, I don't think the code is difficult, or complicated, it just takes experience plus intelligence to come up with it Very Happy
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  [ 29 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: