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

Username:   Password: 
 RegisterRegister   
 Fun ball physics
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
petree08




PostPosted: Thu Oct 26, 2006 9:03 am   Post subject: Fun ball physics

In this program there are 4 balls. If you bump into a ball with your mouse then the ball you hit will bounce off of it. Eventually all the balls will start bouncing off each other and the walls.
you can stop the balls by pressing the keys ...

1, 2, 3, 4, 5


code:


var X1, Y1, X2, Y2, X3, Y3 : int
var X4, Y4 : int
var X, Y, Clicker : int % mouse
var Dx1, Dy1, Dx2, Dy2, Dx3, Dy3 : int := 0
var Dx4, Dy4 : int := 0
var Size : int := 15
var Key : array char of boolean
randint (X1, 20, maxx - 20)
randint (X2, 20, maxx - 20)
randint (X3, 20, maxx - 20)
randint (X4, 20, maxx - 20)
randint (Y1, 20, maxy - 20)
randint (Y2, 20, maxy - 20)
randint (Y3, 20, maxy - 20)
randint (Y4, 20, maxy - 20)

setscreen ("graphics:max,max,nobuttonbar,offscreenonly")
colorback (7)
loop
    cls
    X1 += Dx1
    Y1 += Dy1

    X2 += Dx2
    Y2 += Dy2

    X3 += Dx3
    Y3 += Dy3
    X4 += Dx4
    Y4 += Dy4

    mousewhere (X, Y, Clicker)
    if Clicker = 1 then
        Size := Size + 1
    else
        if Size > 15 then
            Size := Size - 1
        end if
    end if
    Input.KeyDown (Key)
    if Key ('1') then
        Dx1 := 0
        Dy1 := 0
    end if
    if Key ('2') then
        Dx2 := 0
        Dy2 := 0
    end if
    if Key ('3') then
        Dx3 := 0
        Dy3 := 0
    end if
    if Key ('4') then
        Dx1 := 0
        Dy1 := 0
        Dx2 := 0
        Dy2 := 0
        Dx3 := 0
        Dy3 := 0
        Dx4 := 0
        Dy4 := 0
    end if
    drawline (X, Y, X1, Y1, 10)
    drawline (X, Y, X2, Y2, 10)
    drawline (X, Y, X3, Y3, 10)
    drawline (X, Y, X4, Y4, 10)
    drawfilloval (X, Y, 5, 5, 10)
    drawoval (X, Y, Size, Size, 10)
    drawfilloval (X1, Y1, 5, 5, 12)
    drawfilloval (X2, Y2, 5, 5, 12)
    drawfilloval (X3, Y3, 5, 5, 12)
    drawfilloval (X4, Y4, 5, 5, 12)
    drawoval (X1, Y1, 15, 15, 12)
    drawoval (X2, Y2, 15, 15, 12)
    drawoval (X3, Y3, 15, 15, 12)
    drawoval (X4, Y4, 15, 15, 12)
    if X1 + 15 >= maxx or X1 - 15 <= 1 then
        Dx1 := -Dx1
    elsif Y1 + 15 >= maxy or Y1 - 15 <= 1 then
        Dy1 := -Dy1
    end if

    if X2 + 15 >= maxx or X2 - 15 <= 1 then
        Dx2 := -Dx2
    elsif Y2 + 15 >= maxy or Y2 - 15 <= 1 then
        Dy2 := -Dy2
    end if

    if X3 + 15 >= maxx or X3 - 15 <= 1 then
        Dx3 := -Dx3
    elsif Y3 + 15 >= maxy or Y3 - 15 <= 1 then
        Dy3 := -Dy3
    end if


    if X4 + 15 >= maxx or X4 - 15 <= 1 then
        Dx4 := -Dx4
    elsif Y4 + 15 >= maxy or Y4 - 15 <= 1 then
        Dy4 := -Dy4
    end if

    if X1 + 15 + Size > X and X1 - 30 - Size < X and Y1 + 15 + Size > Y and Y1 - 15 - Size < Y then
        Dx1 := (X1 - X) div (Size div 3)
        Dy1 := (Y1 - Y) div (Size div 3)
    end if


    if X2 + 15 + Size > X and X2 - 30 - Size < X and Y2 + 15 + Size > Y and Y2 - 15 - Size < Y then
        Dx2 := (X2 - X) div (Size div 3)
        Dy2 := (Y2 - Y) div (Size div 3)
    end if

    if X3 + 15 + Size > X and X3 - 30 - Size < X and Y3 + 15 + Size > Y and Y3 - 15 - Size < Y then
        Dx3 := (X3 - X) div (Size div 3)
        Dy3 := (Y3 - Y) div (Size div 3)
    end if


    if X4 + 15 + Size > X and X4 - 30 - Size < X and Y4 + 15 + Size > Y and Y4 - 15 - Size < Y then
        Dx4 := (X4 - X) div (Size div 3)
        Dy4 := (Y4 - Y) div (Size div 3)
    end if

    if X3 + 30 > X1 and X3 - 30 < X1 and Y3 + 30 > Y1 and Y3 - 30 < Y1 then
        Dx3 := (X3 - X1) div 4
        Dy3 := (Y3 - Y1) div 4
        Dx1 := (X1 - X3) div 4
        Dy1 := (Y1 - Y3) div 4
    end if


    if X3 + 30 > X2 and X3 - 30 < X2 and Y3 + 30 > Y2 and Y3 - 30 < Y2 then
        Dx3 := (X3 - X2) div 4
        Dy3 := (Y3 - Y2) div 4
        Dx2 := (X2 - X3) div 4
        Dy2 := (Y2 - Y3) div 4
    end if

    if X1 + 30 > X2 and X1 - 30 < X2 and Y1 + 30 > Y2 and Y1 - 30 < Y2 then
        Dx1 := (X1 - X2) div 4
        Dy1 := (Y1 - Y2) div 4
        Dx2 := (X2 - X1) div 4
        Dy2 := (Y2 - Y1) div 4
    end if



    if X4 + 30 > X2 and X4 - 30 < X2 and Y4 + 30 > Y2 and Y4 - 30 < Y2 then
        Dx4 := (X4 - X2) div 4
        Dy4 := (Y4 - Y2) div 4
        Dx2 := (X2 - X4) div 4
        Dy2 := (Y2 - Y4) div 4
    end if

    if X4 + 30 > X1 and X4 - 30 < X1 and Y4 + 30 > Y1 and Y4 - 30 < Y1 then
        Dx4 := (X4 - X1) div 4
        Dy4 := (Y4 - Y1) div 4
        Dx1 := (X1 - X4) div 4
        Dy1 := (Y1 - Y4) div 4
    end if

    if X4 + 30 > X3 and X4 - 30 < X3 and Y4 + 30 > Y3 and Y4 - 30 < Y3 then
        Dx4 := (X4 - X3) div 4
        Dy4 := (Y4 - Y3) div 4
        Dx3 := (X3 - X4) div 4
        Dy3 := (Y3 - Y4) div 4
    end if



    drawline (X1, Y1, X2, Y2, 12)
    drawline (X1, Y1, X3, Y3, 12)
    drawline (X2, Y2, X3, Y3, 12)
    drawline (X1, Y1, X4, Y4, 12)
    drawline (X2, Y2, X4, Y4, 12)
    drawline (X3, Y3, X4, Y4, 12)


    % if balls get stuck
    if X1 > maxx then
        X1 := maxx - 30
    elsif X1 < 1 then
        X1 := 30
    end if
    if Y1 > maxy then
        Y1 := maxy - 30
    elsif Y1 < 1 then
        Y1 := 30
    end if

    if X2 > maxx then
        X2 := maxx - 30
    elsif X2 < 1 then
        X2 := 30
    end if
    if Y2 > maxy then
        Y2 := maxy - 30
    elsif Y2 < 1 then
        Y2 := 30
    end if

    if X3 > maxx then
        X3 := maxx - 30
    elsif X3 < 1 then
        X3 := 30
    end if
    if Y3 > maxy then
        Y3 := maxy - 30
    elsif Y3 < 1 then
        Y3 := 30
    end if

    View.Update
end loop

Sponsor
Sponsor
Sponsor
sponsor
zylum




PostPosted: Thu Oct 26, 2006 11:40 pm   Post subject: (No subject)

pretty cool Wink although i did notice that your collision detection is a bit off. i just skimmed through your code and it doesnt seem like you are using circular collision detection...

if youre interested, these tutorials will help you write a program that does the same thing but you will write it faster, with less effort and it will your program will be significantly shorter:

procedures: http://www.compsci.ca/v2/viewtopic.php?t=407

records: http://www.compsci.ca/v2/viewtopic.php?t=9636
Mathwiz




PostPosted: Tue Oct 31, 2006 9:00 am   Post subject: Re: Fun ball physics

that was cool

Rolling Eyes Borg Borg



petree08 wrote:
In this program there are 4 balls. If you bump into a ball with your mouse then the ball you hit will bounce off of it. Eventually all the balls will start bouncing off each other and the walls.
you can stop the balls by pressing the keys ...

1, 2, 3, 4, 5


code:


var X1, Y1, X2, Y2, X3, Y3 : int
var X4, Y4 : int
var X, Y, Clicker : int % mouse
var Dx1, Dy1, Dx2, Dy2, Dx3, Dy3 : int := 0
var Dx4, Dy4 : int := 0
var Size : int := 15
var Key : array char of boolean
randint (X1, 20, maxx - 20)
randint (X2, 20, maxx - 20)
randint (X3, 20, maxx - 20)
randint (X4, 20, maxx - 20)
randint (Y1, 20, maxy - 20)
randint (Y2, 20, maxy - 20)
randint (Y3, 20, maxy - 20)
randint (Y4, 20, maxy - 20)

setscreen ("graphics:max,max,nobuttonbar,offscreenonly")
colorback (7)
loop
    cls
    X1 += Dx1
    Y1 += Dy1

    X2 += Dx2
    Y2 += Dy2

    X3 += Dx3
    Y3 += Dy3
    X4 += Dx4
    Y4 += Dy4

    mousewhere (X, Y, Clicker)
    if Clicker = 1 then

        Size := Size + 1
    else
        if Size > 15 then
            Size := Size - 1
        end if
    end if
    Input.KeyDown (Key)
    if Key ('1') then
        Dx1 := 0
        Dy1 := 0
    end if
    if Key ('2') then
        Dx2 := 0
        Dy2 := 0
    end if
    if Key ('3') then
        Dx3 := 0
        Dy3 := 0
    end if
    if Key ('4') then
        Dx1 := 0
        Dy1 := 0
        Dx2 := 0
        Dy2 := 0
        Dx3 := 0
        Dy3 := 0
        Dx4 := 0
        Dy4 := 0
    end if
    drawline (X, Y, X1, Y1, 10)
    drawline (X, Y, X2, Y2, 10)
    drawline (X, Y, X3, Y3, 10)
    drawline (X, Y, X4, Y4, 10)
    drawfilloval (X, Y, 5, 5, 10)
    drawoval (X, Y, Size, Size, 10)
    drawfilloval (X1, Y1, 5, 5, 12)
    drawfilloval (X2, Y2, 5, 5, 12)
    drawfilloval (X3, Y3, 5, 5, 12)
    drawfilloval (X4, Y4, 5, 5, 12)
    drawoval (X1, Y1, 15, 15, 12)
    drawoval (X2, Y2, 15, 15, 12)
    drawoval (X3, Y3, 15, 15, 12)
    drawoval (X4, Y4, 15, 15, 12)
    if X1 + 15 >= maxx or X1 - 15 <= 1 then
        Dx1 := -Dx1
    elsif Y1 + 15 >= maxy or Y1 - 15 <= 1 then
        Dy1 := -Dy1
    end if

    if X2 + 15 >= maxx or X2 - 15 <= 1 then
        Dx2 := -Dx2
    elsif Y2 + 15 >= maxy or Y2 - 15 <= 1 then
        Dy2 := -Dy2
    end if

    if X3 + 15 >= maxx or X3 - 15 <= 1 then
        Dx3 := -Dx3
    elsif Y3 + 15 >= maxy or Y3 - 15 <= 1 then
        Dy3 := -Dy3
    end if


    if X4 + 15 >= maxx or X4 - 15 <= 1 then
        Dx4 := -Dx4
    elsif Y4 + 15 >= maxy or Y4 - 15 <= 1 then
        Dy4 := -Dy4
    end if

    if X1 + 15 + Size > X and X1 - 30 - Size < X and Y1 + 15 + Size > Y and Y1 - 15 - Size < Y then
        Dx1 := (X1 - X) div (Size div 3)
        Dy1 := (Y1 - Y) div (Size div 3)
    end if


    if X2 + 15 + Size > X and X2 - 30 - Size < X and Y2 + 15 + Size > Y and Y2 - 15 - Size < Y then
        Dx2 := (X2 - X) div (Size div 3)
        Dy2 := (Y2 - Y) div (Size div 3)
    end if

    if X3 + 15 + Size > X and X3 - 30 - Size < X and Y3 + 15 + Size > Y and Y3 - 15 - Size < Y then
        Dx3 := (X3 - X) div (Size div 3)
        Dy3 := (Y3 - Y) div (Size div 3)
    end if


    if X4 + 15 + Size > X and X4 - 30 - Size < X and Y4 + 15 + Size > Y and Y4 - 15 - Size < Y then
        Dx4 := (X4 - X) div (Size div 3)
        Dy4 := (Y4 - Y) div (Size div 3)
    end if

    if X3 + 30 > X1 and X3 - 30 < X1 and Y3 + 30 > Y1 and Y3 - 30 < Y1 then
        Dx3 := (X3 - X1) div 4
        Dy3 := (Y3 - Y1) div 4
        Dx1 := (X1 - X3) div 4
        Dy1 := (Y1 - Y3) div 4
    end if


    if X3 + 30 > X2 and X3 - 30 < X2 and Y3 + 30 > Y2 and Y3 - 30 < Y2 then
        Dx3 := (X3 - X2) div 4
        Dy3 := (Y3 - Y2) div 4
        Dx2 := (X2 - X3) div 4
        Dy2 := (Y2 - Y3) div 4
    end if

    if X1 + 30 > X2 and X1 - 30 < X2 and Y1 + 30 > Y2 and Y1 - 30 < Y2 then
        Dx1 := (X1 - X2) div 4
        Dy1 := (Y1 - Y2) div 4
        Dx2 := (X2 - X1) div 4
        Dy2 := (Y2 - Y1) div 4
    end if



    if X4 + 30 > X2 and X4 - 30 < X2 and Y4 + 30 > Y2 and Y4 - 30 < Y2 then
        Dx4 := (X4 - X2) div 4
        Dy4 := (Y4 - Y2) div 4
        Dx2 := (X2 - X4) div 4
        Dy2 := (Y2 - Y4) div 4
    end if

    if X4 + 30 > X1 and X4 - 30 < X1 and Y4 + 30 > Y1 and Y4 - 30 < Y1 then
        Dx4 := (X4 - X1) div 4
        Dy4 := (Y4 - Y1) div 4
        Dx1 := (X1 - X4) div 4
        Dy1 := (Y1 - Y4) div 4
    end if

    if X4 + 30 > X3 and X4 - 30 < X3 and Y4 + 30 > Y3 and Y4 - 30 < Y3 then
        Dx4 := (X4 - X3) div 4
        Dy4 := (Y4 - Y3) div 4
        Dx3 := (X3 - X4) div 4
        Dy3 := (Y3 - Y4) div 4
    end if



    drawline (X1, Y1, X2, Y2, 12)
    drawline (X1, Y1, X3, Y3, 12)
    drawline (X2, Y2, X3, Y3, 12)
    drawline (X1, Y1, X4, Y4, 12)
    drawline (X2, Y2, X4, Y4, 12)
    drawline (X3, Y3, X4, Y4, 12)


    % if balls get stuck
    if X1 > maxx then
        X1 := maxx - 30
    elsif X1 < 1 then
        X1 := 30
    end if
    if Y1 > maxy then
        Y1 := maxy - 30
    elsif Y1 < 1 then
        Y1 := 30
    end if

    if X2 > maxx then
        X2 := maxx - 30
    elsif X2 < 1 then
        X2 := 30
    end if
    if Y2 > maxy then
        Y2 := maxy - 30
    elsif Y2 < 1 then
        Y2 := 30
    end if

    if X3 > maxx then
        X3 := maxx - 30
    elsif X3 < 1 then
        X3 := 30
    end if
    if Y3 > maxy then
        Y3 := maxy - 30
    elsif Y3 < 1 then
        Y3 := 30
    end if

    View.Update
end loop

Rolling Eyes Borg Borg
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: