Jump to content

Anyone good at coding or physics? Here's a start...


magician13134
 Share

Recommended Posts

Well, I'm writing a VERY simple physics demo, but then I realized that I know very little about coding, and even less about physics... so... yeah... :whistle:

Here's what I've got so far, all you can create are balls, nothing else, they only bounce, the collide only works right 1/4 of the time, and there's lots of bugs, but hey, if you're bored, take a look, maybe fix a bug or two... thanks!

#include <GUIConstants.au3>
#include <misc.au3>

Dim $ball[64][12], $EditObject[64], $MonitorObject[64], $OutOfBounds[64], $stopped[64], $ObjectToWatch, $running
$AreaWidth = 590
$AreaHeight = 781
$count = 0

$GUI = GUICreate("A VERY simple gravity test", 600, 820, -1, -1, BitOr($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU, $WS_MAXIMIZEBOX))
$file = GUICtrlCreateMenu("File")
    $new = GUICtrlCreateMenu("New", $file)
        $NewObject = GUICtrlCreateMenu("Object", $new)
            $NewBall = GUICtrlCreateMenuitem("Ball", $NewObject)
        $NewProject = GUICtrlCreateMenuitem("Project", $new)
    $open = GUICtrlCreateMenuitem("Open", $file)
    $save = GUICtrlCreateMenuitem("Save", $file)
    GUICtrlCreateMenuitem("", $file)
    $close = GUICtrlCreateMenuitem("Close", $file)
    $exit = GUICtrlCreateMenuitem("Exit", $file)
$project = GUICtrlCreateMenu("Project")
    $start = GUICtrlCreateMenuitem("Start", $project)
    $stop = GUICtrlCreateMenuitem("Stop", $project)
    $reset = GUICtrlCreateMenuitem("Reset", $project)
    GUICtrlCreateMenuitem("", $project)
    $edit = GUICtrlCreateMenu("Edit", $project)
        $NoObjects = GUICtrlCreateMenuitem("No objects", $edit)
        GUICtrlSetState(-1, $GUI_DISABLE)
    $monitor = GUICtrlCreateMenu("Monitor speed", $project)
        $NoObjects2 = GUICtrlCreateMenuitem("No objects", $monitor)
        GUICtrlSetState(-1, $GUI_DISABLE)
    
$group = GUICtrlCreateGroup("", 5, 0, $AreaWidth, $AreaHeight)
GUICtrlSetResizing(-1, 102)
GUICtrlSetState($group, $GUI_DISABLE)
GUICtrlSetState($stop, $GUI_DISABLE)
GUICtrlSetState($start, $GUI_DISABLE)
GUICtrlSetState($reset, $GUI_DISABLE)
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then _exit()
    If $msg = $NewBall Then NewBall()
    If $msg = $start Then _run()
    If $msg = $save Then save()
    If $msg = $open Then open()
    If $msg = $reset Then reset()
    If $msg = $close Then close()
    If $msg = $exit Then _exit()
    For $i = 1 to $count
        If $msg = $EditObject[$i] Then EditObject($i)
        If $msg = $MonitorObject[$i] Then $ObjectToWatch = $i
    Next
    If $msg = $GUI_EVENT_MAXIMIZE Then
        $size = ControlGetPos("A VERY simple gravity test", "", $group)
        $AreaWidth = $size[2]
        $AreaHeight = $size[3]
    EndIf
    If $msg = $GUI_EVENT_RESTORE Then
        $size = ControlGetPos("A VERY simple gravity test", "", $group)
        $AreaWidth = $size[2]
        $AreaHeight = $size[3]
        For $i = 1 to $count
            If $ball[$count][1]+25 > $AreaWidth-5 Then $ball[$count][1] = $AreaWidth-30   ; If the object is out of bounds
            If $ball[$count][10]+25 > $AreaWidth-5 Then $ball[$count][10] = $AreaWidth-30 ; when the window is restored
            If $ball[$count][2]+25 > $AreaHeight-5 Then $ball[$count][2] = $AreaWidth-30  ; this is SUPPOSED to fix it,
            If $ball[$count][11]+25 > $AreaHeight-5 Then $ball[$count][11] = $AreaWidth-30; but it doesn't
        Next
    EndIf
WEnd

Func NewBall($x = 300, $y = 20, $XInc = 0, $YInc = 0, $elas = .45, $mass = 100)
    $count+=1
    GUICtrlDelete($NoObjects)
    GUICtrlDelete($NoObjects2)
    GUICtrlSetState($start, $GUI_ENABLE)
    GUICtrlSetState($reset, $GUI_ENABLE)
    $EditObject[$count] = GUICtrlCreateMenuitem("Object #" & $count & " (ball)", $edit)
    $MonitorObject[$count] = GUICtrlCreateMenuitem("Object #" & $count & " (ball)", $monitor)
    $ball[$count][0] = GUICtrlCreatePic("ball.bmp", $x, $y, 0, 0)
    GUICtrlSetResizing(-1, 802)
    $ball[$count][1] = $x ; X
    $ball[$count][2] = $y ; Y
    $ball[$count][3] = $XInc ; X increment
    $ball[$count][4] = $YInc ; Y inc.
    $ball[$count][5] = $elas ; elasticity
    $ball[$count][6] = 0 ; finished or not
    $ball[$count][7] = $ball[$count][1] ; X backup
    $ball[$count][8] = $ball[$count][2] ; Y backup
    $ball[$count][9] = $mass ; Mass
    $ball[$count][10] = $ball[$count][3] ; X inc. backup
    $ball[$count][11] = $ball[$count][4] ; Y inc. backup
EndFunc 

Func EditObject($i)
    reset()
    $win = WinGetPos("A VERY simple gravity test")
    $EditGUI = GUICreate("Edit object #" & $i, 350, 175)
    $move = GUICtrlCreateButton("Move", 1, 3, 50)
    $material = GUICtrlCreateCombo("Material", 1, 35, 100)
    GUICtrlSetData(-1, "Metal|Rubber|Wood|Clay|Other")
    $elasticity = GUICtrlCreateInput($ball[$count][5], 110, 35, 100, 18)
    GUICtrlCreateLabel("Elasticity", 220, 35)
    $mass = GUICtrlCreateInput($ball[$count][9], 110, 55, 100, 18)
    $XForce = GUICtrlCreateInput(abs($ball[$i][10]), 1, 77, 100, 18, $ES_READONLY)
    GUICtrlSetBkColor(-1, 0xFFFFFF)
    GUICtrlCreateUpdown(-1)
    GUICtrlSetLimit(-1, 500, 0)
    $XDirection = GUICtrlCreateCombo("Left", 110, 75)
    If $ball[$i][10] > 0 Then
        GUICtrlSetData(-1, "Right", "Right")
    Else
        GUICtrlSetData(-1, "Right")
    EndIf
    $YForce = GUICtrlCreateInput(abs($ball[$i][11]), 1, 102, 100, 18, $ES_READONLY)
    GUICtrlSetBkColor(-1, 0xFFFFFF)
    GUICtrlCreateUpdown(-1)
    GUICtrlSetLimit(-1, 500, 0)
    $YDirection = GUICtrlCreateCombo("Up", 110, 100)
    If $ball[$i][11] > 0 Then
        GUICtrlSetData(-1, "Down", "Down")
    Else
        GUICtrlSetData(-1, "Down")
    EndIf
    $done = GUICtrlCreateButton("Ok", 175, 145, 50)
    GUICtrlCreateLabel("g", 220, 55)
    GUISetState(@SW_SHOW)
    While 1
        $EditMsg = GUIGetMsg()
        If $EditMsg = $GUI_EVENT_CLOSE Then
            GUIDelete($EditGUI)
            Return
        EndIf
        If GUICtrlRead($material) = "Metal" Then
            If GUICtrlRead($elasticity) <> .45 Then GUICtrlSetData($elasticity, .45)
            If GUICtrlRead($mass) <> 100 Then GUICtrlSetData($mass, 100)
        ElseIf GUICtrlRead($material) = "Rubber" Then
            If GUICtrlRead($elasticity) <> .7 Then GUICtrlSetData($elasticity, .7)
            If GUICtrlRead($mass) <> 20 Then GUICtrlSetData($mass, 20)  
        ElseIf GUICtrlRead($material) = "Wood" Then
            If GUICtrlRead($elasticity) <> .25 Then GUICtrlSetData($elasticity, .25)
            If GUICtrlRead($mass) <> 50 Then GUICtrlSetData($mass, 50)
        ElseIf GUICtrlRead($material) = "Clay" Then
            If GUICtrlRead($elasticity) <> .05 Then GUICtrlSetData($elasticity, .05)
            If GUICtrlRead($mass) <> 25 Then GUICtrlSetData($mass, 25)
        EndIf
        If $EditMsg = $move Then
            GUISetState(@SW_HIDE)
            MouseMove($win[0]+$ball[$i][1], $win[1]+$ball[$i][2], 0)
            While Not _IsPressed(01)
                ToolTip("X:" & $ball[$i][1] & "  Y:" & $ball[$i][2])
                GUIGetMsg()
                $mouse = MouseGetPos()
                $ball[$i][1] = $mouse[0]-$win[0]
                $ball[$i][7] = $mouse[0]-$win[0]
                $ball[$i][2] = $mouse[1]-$win[1]
                $ball[$i][8] = $mouse[1]-$win[1]
                If $ball[$i][1] >= 7 and $ball[$i][1] <= $AreaWidth-28 and $ball[$i][2] >= 8 and $ball[$i][2] <= $AreaHeight-28 Then
                    ControlMove("A VERY simple gravity test", "", $ball[$i][0], $ball[$i][1], $ball[$i][2])
                EndIf
            WEnd
            ToolTip('')
            GUISetState(@SW_SHOW)
        EndIf
        If $EditMsg = $done Then
            ;If Number(GUICtrlRead($XForce)) <> 0 Then ; Ha ha, oops, what if you want no acceleration? ;)
                $ball[$i][10] = Number(GUICtrlRead($XForce))
                If GUICtrlRead($XDirection) = "Left" Then $ball[$i][10]*=-1
            ;EndIf
            ;If Number(GUICtrlRead($YForce)) <> 0 Then
                $ball[$i][11] = Number(GUICtrlRead($YForce))
                If GUICtrlRead($YDirection) = "Up" Then $ball[$i][11]*=-1
            ;EndIf
            If GUICtrlRead($elasticity) <= 0 or GUICtrlRead($elasticity) > 1 Then
                MsgBox(48, "Will not produce accurate results", "No known object has an elasticity of 0 or less or greater than 1." & @CRLF & "The results will be inaccurate.")
            EndIf
            $ball[$i][5] = GUICtrlRead($elasticity)
            $ball[$i][9] = GUICtrlRead($mass)
            GUIDelete($EditGUI)
            Return
        EndIf
    WEnd
EndFunc
Func reset()
    GUICtrlDelete($running)
    For $i = 1 to $count
        $ball[$i][1] = $ball[$i][7]
        $ball[$i][2] = $ball[$i][8]
        $ball[$i][3] = $ball[$i][10]
        $ball[$i][4] = $ball[$i][11]
        ControlMove("A VERY simple gravity test", "", $ball[$i][0], $ball[$i][1], $ball[$i][2])
    Next
EndFunc 

Func _run()
    GUICtrlSetState($stop, $GUI_ENABLE)
    For $i = 1 to $count
        $OutOfBounds[$i] = 0
        GUICtrlSetState($EditObject[$i], $GUI_DISABLE)
        $ball[$i][1] = $ball[$i][7]
        $ball[$i][2] = $ball[$i][8]
        $ball[$i][3] = $ball[$i][10]
        $ball[$i][4] = $ball[$i][11]
        $ball[$i][6] = 0
        $stopped[$i] = 0
    Next
    $done = 0
    $running = GUICtrlCreateLabel("Running", 10, $AreaHeight+5, 500, 15)
    GUICtrlSetResizing(-1, 2)
    While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then _exit()
        If $msg = $stop Then
            GUICtrlDelete($running)
            GUICtrlSetState($stop, $GUI_DISABLE)
            For $i = 0 to $count
                GUICtrlSetState($EditObject[$i], $GUI_ENABLE)
            Next
            reset()
            ExitLoop
        EndIf
        For $i = 1 to $count
            If abs($ball[$i][4]) <= .1 Then
                $stopped[$i]+=1
            Else
                $stopped[$i] = 0
            EndIf
            If $stopped[$i] >= 5 Then
                If $ball[$i][6] <> 1 Then
                    $done+=1
                    $ball[$i][6] = 1
                EndIf
            EndIf
            If $ObjectToWatch <> 0 Then GUICtrlSetData($running, "Running      X:" & $ball[$ObjectToWatch][3] & "     Y:" & $ball[$ObjectToWatch][4])
            If $ball[$i][6] = 0 Then
                ;If $ball[$i][3] > 0 Then $ball[$i][3]-=.01 ; I guess this is wrong
                ;If $ball[$i][3] < 0 Then $ball[$i][3]+=.01 ; In physics, does the X inc stays the same?
                $ball[$i][4]+=.098
                $ball[$i][1]+=$ball[$i][3]
                $ball[$i][2]+=$ball[$i][4]
                If $ball[$i][2]+25 >= $AreaHeight-3 Then ; Hits bottom
                    If $ball[$i][2]+25 > $AreaHeight-1 Then $OutOfBounds[$i]+=1
                    If $ball[$i][4] >= .1 Then ; Bounce
                        $ball[$i][2] = $AreaHeight-1-25
                        GUICtrlSetImage($ball[$i][0], "ball.bmp")
                        $ball[$i][4]*=-$ball[$i][5]
                        $ball[$i][3]*=$ball[$i][5]
                    Else ; Stop
                        If $ball[$i][4] <= -.1 Then
                            sleep(1)
                        Else
                            $done+=1
                            $ball[$i][6] = 1
                        EndIf
                    EndIf
                Else
                    $OutOfBounds[$i] = 0
                EndIf
                If $ball[$i][2] <= 4 Then ; Hits top
                    If $ball[$i][2] < 0 Then $OutOfBounds[$i]+=1
                    ;If $ball[$i][4] >= .1 Then ; Bounce ; These are commented out because it should ALWAYS bounce on a side or top
                        $ball[$i][2] = 4                 ; I forgot that when I first coded this, it causes errors to uncomment them
                        GUICtrlSetImage($ball[$i][0], "ball.bmp")
                        $ball[$i][4]*=-$ball[$i][5]
                        $ball[$i][3]*=$ball[$i][5]
                    ;EndIf
                Else
                    $OutOfBounds[$i] = 0
                EndIf
                If $ball[$i][1] <= 7 Then ; Hits left
                    If $ball[$i][1] < 5 Then $OutOfBounds[$i]+=1
                    ;If $ball[$i][4] >= .1 Then ; Bounce
                        $ball[$i][1] = 7
                        GUICtrlSetImage($ball[$i][0], "ball.bmp")
                        $ball[$i][4]*=$ball[$i][5]
                        $ball[$i][3]*=-$ball[$i][5]
                    ;EndIf
                Else
                    $OutOfBounds[$i] = 0
                EndIf
                If $ball[$i][1]+25 >= $AreaWidth-2 Then ; Hits right
                    If $ball[$i][1]+25 > $AreaWidth Then $OutOfBounds[$i]+=1
                    ;If $ball[$i][4] >= .1 Then ; Bounce
                        $ball[$i][1] = $AreaWidth-2-25
                        GUICtrlSetImage($ball[$i][0], "ball.bmp")
                        $ball[$i][4]*=$ball[$i][5]
                        $ball[$i][3]*=-$ball[$i][5]
                    ;EndIf
                Else
                    $OutOfBounds[$i] = 0
                EndIf
                For $j = 1 to $count
                    For $k = 1 to $count
                        If $j <> $k Then
                            If abs($ball[$j][1]-$ball[$k][1]) < 26 and abs($ball[$j][2]-$ball[$k][2]) < 26 Then ; collide
                                If $ball[$k][6] = 1 Then ; If one ball is at rest
                                    $ball[$j][4]*=-$ball[$j][5]
                                Else ; If both are moving
                                    $ball[$j][3]*=-$ball[$j][5];*abs($ball[$k][9]-$ball[$j][9]) ; SOMETHING needs to go here...
                                    $ball[$k][3]*=-$ball[$k][5];*abs($ball[$j][9]-$ball[$k][9]) ; but obviously not this!
                                    $ball[$j][4]*=-$ball[$j][5];*abs($ball[$k][9]-$ball[$j][9])
                                    $ball[$k][4]*=-$ball[$k][5];*abs($ball[$j][9]-$ball[$k][9])
                                    
                                    $ball[$j][1]+=$ball[$j][3]
                                    $ball[$k][2]+=$ball[$k][4]
                                    $ball[$j][1]+=$ball[$j][3]
                                    $ball[$k][2]+=$ball[$k][4]
                                EndIf
                                GUICtrlSetImage($ball[$i][0], "ball.bmp")
                            EndIf
                        EndIf
                    Next
                Next
                ControlMove("A VERY simple gravity test", "", $ball[$i][0], $ball[$i][1], $ball[$i][2])
            EndIf
        Next
        If $done >= $count Then
            If $ObjectToWatch <> 0 Then
                GUICtrlSetData($running, "X:" & $ball[$ObjectToWatch][3] & "     Y:" & $ball[$ObjectToWatch][4])
            Else
                GUICtrlDelete($running)
            EndIf
            For $i = 0 to $count
                GUICtrlSetState($EditObject[$i], $GUI_ENABLE)
            Next
            GUICtrlSetState($stop, $GUI_DISABLE)
            ExitLoop
        EndIf
    WEnd
EndFunc

Func save()
    $SavedFile = FileSaveDialog("Where would you like to save?", @WorkingDir, "Gravity Emulator File (*.gef)", 16, "*.gef")
    If FileExists($SavedFile) Then FileDelete($SavedFile)
    For $i = 1 to $count
        FileWrite($SavedFile, '#')
        FileWrite($SavedFile, $ball[$i][1] & ";" & $ball[$i][2] & ";" & $ball[$i][10] & ";" & $ball[$i][11] & ";" & $ball[$i][5] & ";" & $ball[$i][9]) 
    Next
EndFunc

Func close()
    GUICtrlSetState($start, $GUI_DISABLE)
    GUICtrlSetState($reset, $GUI_DISABLE)
    For $i = 1 to $count
        GUICtrlDelete($EditObject[$count])
        GUICtrlDelete($MonitorObject[$count])
        GUICtrlDelete($ball[$i][0])
    Next
    $done = 0
    $count = 0
    $i = 0
EndFunc

Func open()
    $OpenedFile = FileOpenDialog("Open a file", @WorkingDir, "Gravity emulator files (*.gef)")
    If $OpenedFile = "" Then Return
    $file = StringSplit(FileRead($OpenedFile), '#')
    close()
    For $i = 2 to $file[0]
        $line = StringSplit($file[$i], ";")
        NewBall($line[1], $line[2], $line[3], $line[4], $line[5], $line[6])
    Next
EndFunc

Func _exit()
    Exit
EndFunc

Oops! Forgot the required image!

ball.bmp

Edited by magician13134
Link to comment
Share on other sites

Nice, fun to mess around with!

Link to comment
Share on other sites

doesant seem to to work for me how do you use it :whistle:

never mind i fixed it my screen revs vars to small nice script if you made the ball in color and exta balls diff colors that would be beter

Edited by thenewkid

some of my scripts check them out and give feedback so i can learn from them :)autoclicker a autoclickernote taker a script to take notes with

Link to comment
Share on other sites

Yeah, I have lots of different colors, but I was thinking textures would be nice for materials...

@smstroble, download the attached image

Oh, but if you want to have some fun, try adding some acceleration without the image :whistle:

@thenewkid, sorry, I should've used vars based on screen size

Thanks, I wasn't expecting any positive feedback. Feel free to post any modified source.

Edited by magician13134
Link to comment
Share on other sites

  • Moderators

I don't understand... How do you lose a ball?

You're not married are you? :whistle:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

In the menu under the edit submenu it will show only Ball #1 and Ball #3 when there are 4 balls. Ball #2 & 4 are effectivly missing, I cant change their properties.

Nope.

YOU'VE LOST 2 BALLS ??!!

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Link to comment
Share on other sites

In the menu under the edit submenu it will show only Ball #1 and Ball #3 when there are 4 balls. Ball #2 & 4 are effectivly missing, I cant change their properties.

I don't get that... I just added 5 balls, and they all show up... I ran it too, can you maybe save the GEF file and upload it?

Link to comment
Share on other sites

Hi,

I just LOVE it, i's great. i just filled in 1.1 elasiticity and it was funny to see, would be cool if it was for real :whistle:

I will show this to my physics teacher one a day

Arjan

Link to comment
Share on other sites

Here is some stuff for you to look at.

P.S. i cant always get a ball to vanish from the edit menu but i got the ones here to vanish be creating them then edited them (i moved it then set its elasticity for 1) and viola gone.

Also i renamed the .gef file to a .txt because i cant upload .gef

Another thing of this nature i just discovered. If you close a project the balls remain in the menu and loading another project adds to them.

post-9526-1171913897_thumb.jpg

post-9526-1171913913_thumb.jpg

Problem.gef.txt

Edited by smstroble

MUHAHAHAHAHA

Link to comment
Share on other sites

Ok, the thing that annoyed me the most was the fact that the balls did no appear to be round because the color grey was different from the color of the GUI. I just used AutoIt Window Info tool to get the exact color and edited the image... here's my new ball image:

ball.bmp

Otherwise, great program there are some pretty neat things you can do with this, and I didn't know it was even possible in AutoIt... I guess if you are determined you can make just about anything with AutoIt eh? Nice work, keep the updates coming.

EDIT: Lol, this one is super glitchy the balls keep getting stuck together...

stuck_together.gef.txt

Edited by dandymcgee

- Dan [Website]

Link to comment
Share on other sites

@magician13134

You might be interested in reading this:

http://www.harveycartel.org/metanet/tutorials.html

It's a tutorial that explains the collision detection system used in Metanet's game N, which is itself pretty damn cool. I think the essence is that they don't run precise CD algorithms until objects are REALLY close.

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...