Jump to content

Ball Bounce Physics Test


CoderDunn
 Share

Recommended Posts

Inspired from my last script, I made this one to see if I could make a ball bounce like it would in real life. I know there are probably much better ways at doing this so if you have a better way please show me! You can customize the gravity, bounce ratio, and time per tick by editing the first few variables in the script. I dunno why I'm still using a golf ball but who cares ...

I have never taken a physics course and I'm not that great at math so grade lightly :D

~ Hallman

Ball_Bounce_Test.au3

Edited by Hallman
Link to comment
Share on other sites

Thats really good, now use that in maybe a connon game and you might get somewhere with it!

[center]"When you look at old, classic games like Snake, you often put it off because it's such a simple game, but it's only when you actually try and create your own unique game from scratch, do you finally appreciate those games."[/center][center]Don't ask for answers if you haven't TRIED yet![/center][center]Most answers can be answered in the help file! Use it![/center]

Link to comment
Share on other sites

I was interested in this script so I played around with it a little. Heres what I got try it out.

#include <GUIConstants.au3>

; Bounce Test by Hallman

; Change these variables to whatever you want
Global $Gravity = 1.4
; Has to be greater than 1 (1.4 = default)

Global $Bounce_Ratio = 0.7 ;
; Should be >0 and <1 (0.7 = default). If you set it to 1, then the ball will bounce to the same height that it was dropped from.

Global $TimePerTick = 30
; How long each tick is in miliseconds. Bigger number = slower ball movement (30 = default)


; -----------------------------------------------------------------------------------------------------------------------------------
; -----------------------------------------------------------------------------------------------------------------------------------

Dim $BallPos[4]
$BallPos[0] = 30
$BallPos[1] = 0
$BallPos[2] = 50
$BallPos[3] = 50

Dim $Current_Tick[6]
;~  $Current_Tick[0] = Active? (0 or 1)
;~  $Current_Tick[1] = Timer
;~  $Current_Tick[2] = Movement So Far
;~  $Current_Tick[3] = Total Movement

Global $Speed = 0

$Main_Window = GUICreate("Ball Test",$BallPos[2], $BallPos[3], 0, @DesktopHeight / 10, $WS_POPUP, $WS_EX_LAYERED + $WS_EX_TOOLWINDOW)

$Ball_Ctrl = GUICtrlCreatePic("Golf Ball.gif", 0, 0, $BallPos[2], $BallPos[3])

GuiSetState()

WinSetOnTop($Main_Window, "", 1)

$Win_Pos = WinGetPos($Main_Window, "")

$BallPos[0] = $Win_Pos[0]
$BallPos[1] = $Win_Pos[1]

Sleep(2000)

$Loop_Timer = TimerInit()
$Time_Per_Loop = 1

$Speed = 1

$Current_Tick[0] = 0

While 1
    $Time_Per_Loop = TimerDiff($Loop_Timer)
    $Loop_Timer = TimerInit()
    $BallPos[0] += 4
    If $Current_Tick[0] = 1 Then

        $Current_Tick_Time = TimerDiff($Current_Tick[1])
        
        If $Current_Tick_Time > $TimePerTick Then
            $BallPos[1] = $Current_Tick[3]
            WinMove($Main_Window, "", $BallPos[0], $BallPos[1], $BallPos[2], $BallPos[3])
            $Current_Tick[0] = 0
        Else
            If $Speed > 0 Then
                $int = ($TimePerTick - $Current_Tick_Time) / $Time_Per_Loop
                $Movement = ($Current_Tick[3] - $Current_Tick[2]) / $int
            
                $Current_Tick[2] += $Movement
                If $Current_Tick[2] >= $Current_Tick[3] Then
                    $BallPos[1] = $Current_Tick[3]
                    WinMove($Main_Window, "", $BallPos[0], $BallPos[1], $BallPos[2], $BallPos[3])
                    $Current_Tick[0] = 0
                Else
                    $BallPos[1] = $Current_Tick[2]
                    WinMove($Main_Window, "", $BallPos[0], $BallPos[1], $BallPos[2], $BallPos[3])
                EndIf
            Else
                $int = ($TimePerTick - $Current_Tick_Time) / $Time_Per_Loop
                $Movement = ($Current_Tick[3] - $Current_Tick[2]) / $int
            
                $Current_Tick[2] += $Movement
                If $Current_Tick[2] <= $Current_Tick[3] Then
                    $BallPos[1] = $Current_Tick[3]
                    WinMove($Main_Window, "", $BallPos[0], $BallPos[1], $BallPos[2], $BallPos[3])
                    $Current_Tick[0] = 0
                Else
                    $BallPos[1] = $Current_Tick[2]
                    WinMove($Main_Window, "", $BallPos[0], $BallPos[1], $BallPos[2], $BallPos[3])
                EndIf
            EndIf
        EndIf
    Else
        
        If $BallPos[1] = (@DesktopHeight - $BallPos[3]) Then
            $Speed = $Speed * -1
            $Speed = $Speed * $Bounce_Ratio
            If $Speed > -0.2 and $Speed < 0.2 Then
                Sleep(2000)
                Exit
            EndIf
        Else
            If $Speed > 0 Then
                $Speed = $Speed * $Gravity
            ElseIf $Speed < 0 Then
                $Speed = $Speed / $Gravity
                If $Speed > -2 Then $Speed = $Speed * -1
            EndIf
        EndIf
        
        $Current_Tick[2] = $BallPos[1]
        
        $Current_Tick[3] = $BallPos[1] + $Speed
        
        If $Current_Tick[3] > (@DesktopHeight - $BallPos[3]) Then
            $Current_Tick[3] = (@DesktopHeight - $BallPos[3])
        EndIf
        
        $Current_Tick[1] = TimerInit()
        
        $Current_Tick[0] = 1
    EndIf
    sleep(1)
WEnd

www.itoady.com

A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding

Link to comment
Share on other sites

Very nice Toady!!

Hmm, why does the program keeps running when the ball is offscreen?

Anyway, Can someone add a check in the program for the picture? :D

I had twice that I where wondering why the program didn't do anything :D

The program is running while ball goes off screen because the ball is still bouncing. I have multi-monitor so I can see if bounce to the other screen. To make it stay on your screen just change the line

$BallPos[0] += 4oÝ÷ ÚÚºÚ"µÍÌÍÐ[ÜÖÌH
ÏH
Edited by Toady

www.itoady.com

A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding

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...