Sign in to follow this  
Followers 0
andybiochem

Ballistics

3 posts in this topic

Hi Guys,

Thought I'd share this with you all.

It's a ballistics simulator, showing what happens when you throw a ball in the air. w00t!

To throw a ball, click and drag anywhere in the white portion of the GUI - preferably to the left hand side.

The longer you make your line the more energy (starting velocity) your ball has...so it will be thrown further.

#include <GuiConstantsEx.au3>
#include <Misc.au3>

Opt("MouseCoordMode",2)

;***********************************
; Declare Vars
;***********************************

Global $PI = 3.1415926536

Global $iWidth = 800
Global $iHeight = 600

Global $iStartX
Global $iStartY

Global $time
Global $distance
Global $height
Global $velocity
Global $angle

Global $m1[2]
Global $m2[2]

Global $dll = DllOpen("user32.dll")

Global $iGraphCount = 0


;***********************************
; GUI
;***********************************

$GUI = GUICreate("",$iWidth,$iHeight,Default,Default,Default,0x2000000)
GUISetOnEvent($GUI_EVENT_CLOSE,"_Close")


GUICtrlCreateLabel("",1,550,800,50)
GUICtrlSetBkColor(-1,0xe9e9e9)


;***********************************
; Graphic
;***********************************

$Graphic = GUICtrlCreateGraphic(0,0,$iWidth,$iHeight)
GUICtrlSetBkColor(-1,0xFFFFFF)



GUISetState()

While 1
;***********************************
; Set up potential + angle
;***********************************

    Sleep(100)
    
    If _IsPressed("01",$dll) = 1 Then
        
        $m1 = MouseGetPos()
        
        $Ball = GUICtrlCreateRadio("",$m1[0],$m1[1]-13,13,13)
        GUICtrlSetBkColor(-1,0xFFFFFF)
        
        
        Do
            _ClearGraphic()
            $m2 = MouseGetPos()
            GUICtrlSetGraphic($Graphic,$GUI_GR_MOVE,$m1[0],$m1[1])
            GUICtrlSetGraphic($Graphic,$GUI_GR_LINE,$m2[0],$m2[1])
            GUICtrlSetGraphic($Graphic,$GUI_GR_REFRESH)
            Sleep(1)
        Until _IsPressed("01",$dll) = 0
        
        $velocity = Sqrt((($m2[1] - $m1[1])^2) + (($m2[0] - $m1[0])^2))
        $angle = ASin((($m2[1] - $m1[1]) / $velocity)) * (180 / $PI)
    
        _ClearGraphic()
        _Shoot()

    EndIf
    
WEnd


Func _Shoot()
;***********************************
; Calculate projection + move radio
;***********************************

    $time = 0
    $distance = 0
    $height = 0
    
    $angle *= ($PI / 180)
    
    Do
        $time += 0.1
        $velocity -= 0.5
        
        $distance = $velocity * Cos($angle) * $time + $m1[0]
        $height = $m1[1] - ((($velocity * Sin($angle) * $time) - (9.8 * $time^2)) / 2) - 13
        
        GUICtrlSetPos($Ball,$distance,$height)
        
        If Abs($height) > (550 - 16) Then ExitLoop
        If $distance > (800 - 13) Or $distance < (0 - 16) Then ExitLoop
        
        Sleep(1)
        
    Until $height < 0
    
EndFunc



Func _ClearGraphic()
;***********************************
; Clears / deletes graphic
;***********************************
    
    $iGraphCount += 1
    
    GUICtrlSetGraphic($Graphic,$GUI_GR_MOVE,0,$iHeight / 2)
    GUICtrlSetGraphic($Graphic,$GUI_GR_PENSIZE,1000)
    GUICtrlSetGraphic($Graphic,$GUI_GR_COLOR,0xFFFFFF)
    GUICtrlSetGraphic($Graphic,$GUI_GR_LINE,$iWidth,$iHeight / 2)
    GUICtrlSetGraphic($Graphic,$GUI_GR_PENSIZE,1)
    GUICtrlSetGraphic($Graphic,$GUI_GR_COLOR,0x000000)
    
    If $iGraphCount > 100 Then
        GUISetState(@SW_LOCK)
        GUICtrlDelete($Graphic)
        $Graphic = GUICtrlCreateGraphic(0,0,$iWidth,$iHeight)
        GUICtrlSetBkColor(-1,0xFFFFFF)
        GUISetState(@SW_UNLOCK)
        $iGraphCount = 0
    EndIf
    
EndFunc



Func _Close()
;***********************************
; Bye Bye!!
;***********************************

    Exit
    
EndFunc

There is a slight bug/problem with the condition (line 110):

If Abs($height) > (550 - 16) Then ExitLoop

The ExitLoop causes the ball to stop moving. The problem is, if you fire a ball upwards it will 'stop' on the top of the screen, even though the position ($height) is not greater than (550 - 16).

Anyone know why this is happening?....something to do with it being a negative number initially? - I've used Abs to try to rectify this, but no luck.

Comments / critisism welcome!!!


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites



pretty cool..

Although it bugs and it doesn't work always

Share this post


Link to post
Share on other sites

one thing, you are missing

Opt("GUIOnEventMode", 1)

to exit the GUI

8)


NEWHeader1.png

Share this post


Link to post
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
Sign in to follow this  
Followers 0