Jump to content
Sign in to follow this  
CoderDunn

Ball Bounce Physics Test

Recommended Posts

CoderDunn

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

Share this post


Link to post
Share on other sites
Vindicator209

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]

Share this post


Link to post
Share on other sites
BrettF
DexterMorgan

wow.. really cool

i opened like 10 of them at the same time very fun!


code

Share this post


Link to post
Share on other sites
Toady

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

Share this post


Link to post
Share on other sites
Triblade

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


My active project(s): A-maze-ing generator (generates a maze)

My archived project(s): Pong3 (Multi-pinger)

Share this post


Link to post
Share on other sites
Toady

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

Share this post


Link to post
Share on other sites
monoceres

You could make a nice little game out of this, just let the ball bounce up when you click on it and then your goal is to keep it in the air as long as possible :D


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
mafioso

@AdmiralAlex

Same for me, and I have changed the path to the ball...

Share this post


Link to post
Share on other sites
JustinReno

Are you sure the picture is in the script directory and named "Golf Ball.gif" ?

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.