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

Username:   Password: 
 RegisterRegister   
 Multiple ball colision 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: Fri Nov 10, 2006 12:17 pm   Post subject: Multiple ball colision physics

Okay so a while a go i posted a ball physics thingy and it basically blew apple sauce.
So i went home ant wrote this little thing

it is sooo much better than my other one.


code:






const SIZE := 50
const MID_Y := maxy div 2
const MID_X := maxx div 2
const SPEED := 9
const SIZE_P := 10
const SIZE_C := 5
const DIV_F := 1



var X, Y, Dx, Dy, Sy, Sx, C : array 1 .. SIZE of int
var Length : array 1 .. SIZE of real4
var Key : array char of boolean
var XP, YP, Click : int
setscreen ("graphics:max,max,nobuttonbar,offscreenonly")

process Sound
    sound (100, 100)

end Sound

for Index : 1 .. SIZE
    randint (X (Index), 20, maxx - 20)
    randint (Y (Index), 20, maxy - 20)
    loop
        randint (C (Index), 1, 15)
        exit when C (Index) not= 7
    end loop
    Dx (Index) := 0
    Dy (Index) := 0

end for

XP := MID_X
YP := MID_Y
colorback (7)
loop

    drawfillbox (1, 1, maxx, maxy, 15)
    drawfillbox (20, 20, maxx - 20, maxy - 20, 7)
    mousewhere (XP, YP, Click)
    drawfilloval (XP, YP, SIZE_P, SIZE_P, 0)



    if Click = 1 then
        for Index : 1 .. SIZE
            Dx (Index) := 0
            Dy (Index) := 0
            % drawline (XP, YP, X (Index), Y (Index), 12)
        end for
        % else
        %     for Index : 1 .. SIZE
        %
        %         drawline (XP, YP, X (Index), Y (Index), C (Index))
        %     end for
    end if

    for Index : 1 .. SIZE

        drawfilloval (X (Index), Y (Index), SIZE_C, SIZE_C, C (Index))
        %drawfilloval (X (Index), Y (Index), SIZE_C, SIZE_C, 0)

        if X (Index) > maxx - 20 or X (Index) < 20 then
            Dx (Index) := -Dx (Index)
        end if

        if Y (Index) > maxy - 20 or Y (Index) < 20 then
            Dy (Index) := -Dy (Index)
        end if

        Length (Index) := sqrt (((XP - X (Index)) ** 2) + ((YP - Y (Index)) ** 2))


        if Length (Index) <= SIZE_C + SIZE_P then
            %  fork Sound
            Sy (Index) := - (YP - Y (Index))
            Sx (Index) := - (XP - X (Index))

            Dx (Index) := Sx (Index) div DIV_F
            Dy (Index) := Sy (Index) div DIV_F

        end if


        for Index2 : 1 .. SIZE
            Length (Index2) := sqrt (((X (Index2) - X (Index)) ** 2) + ((Y (Index2) - Y (Index)) ** 2))


            if Length (Index2) <= SIZE_C + SIZE_C and Index not= Index2 then
                %fork Sound
                Sy (Index2) := Y (Index2) - Y (Index)
                Sx (Index2) := X (Index2) - X (Index)

                Dx (Index2) := Sx (Index2) div DIV_F
                Dy (Index2) := Sy (Index2) div DIV_F

            end if
            %drawline (X (Index), Y (Index), X (Index2), Y (Index2), 0)
        end for



        X (Index) := X (Index) + Dx (Index)
        Y (Index) := Y (Index) + Dy (Index)

    end for



    View.Update
end loop





Sponsor
Sponsor
Sponsor
sponsor
DemonZ




PostPosted: Fri Nov 10, 2006 6:21 pm   Post subject: (No subject)

Very Nice.... This would be useful for a pool game.
Windsurfer




PostPosted: Fri Nov 10, 2006 7:54 pm   Post subject: (No subject)

The physics seem... weird. I'm going to be blunt here.
All the balls move at the same speed. It doesn't matter what angle their collisions are, they move at the same speed, just different directions. That's not proper.
Now for coding: Naming convention is nonexistent. In fact, some names are incorrect. "SIZE" should not be used to describe the number of balls, in my opinion. Also, although not a rule, it is common practice to name variables and arrays in all lowercase.
Don't use parallel arrays, use records instead.
Commenting should be used to explain code, not just disable a part of code.
...Good spacing. Keep that up.
Cervantes




PostPosted: Fri Nov 10, 2006 11:07 pm   Post subject: (No subject)

The telltale clue that something is wrong here is that if you give the system a little bit of energy at the start of the program by jostling a few balls, then move the white ball into the corner, what happens? Eventually all the balls are moving pretty fast. There's a lot of energy in the system. Way more than you gave the system in the beginning.
TripleBla




PostPosted: Mon Nov 13, 2006 1:58 pm   Post subject: (No subject)

Windsurfer wrote:
I am jesus.


fixed.
blaster009




PostPosted: Mon Nov 13, 2006 11:14 pm   Post subject: (No subject)

Windsurfer wrote:

Now for coding: Naming convention is nonexistent. In fact, some names are incorrect. "SIZE" should not be used to describe the number of balls, in my opinion. Also, although not a rule, it is common practice to name variables and arrays in all lowercase.


Common for who? I use camelCase. I personally find it a lot easier to read xSizeArrayForBunchOfRandomObjects than xsizearrayforbunchofrandomobjects (of course, I'd never really use a name that long...)

As for size as the number, why not? It is HIS code, and if that's how he chooses to remember it, so be it. Razz
Clayton




PostPosted: Mon Nov 13, 2006 11:21 pm   Post subject: (No subject)

I think he meant variable names not as MyArray, but as myArray for example. SIZE just doesn't really explain what it is. To me it tells me how big something is (ie the radius of the ball).

EDIT: WOOHOO! 700th post
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  [ 7 Posts ]
Jump to:   


Style:  
Search: