Jump to content

Math question


Recommended Posts

Hiya!

Im still struggling with math.

This time I can't figure out how 'gravity' works.

Could anyone help me fix my script? :mellow:

If you have no idea what im talking about, take a look at the reference: here.

#include <Math.au3>

Global $Gravity = 2
Global $Firepower = 0
Global $BulletInAir = False
Global $Angle = _ATan2(300 * -1, 400)
Global $StartX = 10
Global $StartY = 580
Global $DirX, $DirY
Global $X = $StartX, $Y = $StartY
Global $Arrow

$GUI = GUICreate("", 800, 600)
$Arrow = GUICtrlCreateLabel("", $StartX, $StartY, 10, 10)
GUICtrlSetBkColor(-1, 0x000000)

GUISetState()
While 1
    Switch GUIGetMsg()
        Case -3
            Exit
    EndSwitch

    Fire()
    ConsoleWrite("; ======================" & @CRLF)
    ConsoleWrite("; Firepower       : " & $Firepower & @CRLF)
    ConsoleWrite("; Angle           : " & $Angle & @CRLF)
    ConsoleWrite("; DirX            : " & $DirX & @CRLF)
    ConsoleWrite("; DirY            : " & $DirY & @CRLF)
    ConsoleWrite("; X               : " & $X & @CRLF)
    ConsoleWrite("; Y               : " & $Y & @CRLF)
    ConsoleWrite("; ======================" & @CRLF)

    Sleep(50)
WEnd

Func Fire()
    $Firepower = 45

    $DirX = Cos(($Angle * 3.1415926535) / 180) * $Firepower
    $DirY = Sin(($Angle * 3.1415926535) / 180) * $Firepower

    $X  += Round($DirX / 5, 0)
    $Y += Round($DirY / 5, 0)
    $DirY += $Gravity

    GUICtrlSetPos($Arrow, $X, $Y)
EndFunc

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

  • Moderators

AlmarM,

These equations are only valid in a vacuum:

#include <Math.au3>

$Pi = 3.14159265358979

Global $Gravity = 2
Global $Firepower = Random(25, 50, 1)
Global $BulletInAir = False
Global $Angle = Random(20, 60, 1) * $Pi / 180
Global $StartX = 10
Global $StartY = 580
Global $DirX, $DirY
Global $X = $StartX, $Y = $StartY
Global $Arrow

Global $InitVel_X = Cos($Angle) * $Firepower
Global $InitVel_Y = Sin($Angle) * $Firepower

$GUI = GUICreate("", 800, 600)
$Arrow = GUICtrlCreateLabel("", $StartX, $StartY, 10, 10)
GUICtrlSetBkColor(-1, 0)

$iBegin = TimerInit()

GUISetState()
While 1
    Switch GUIGetMsg()
        Case -3
            Exit
    EndSwitch

    If $Y >= -10 Then
        Fire()
    Else
        Sleep(1000)
        $X = 0
        $Y = 0
        $Firepower = Random(25, 50, 1)
        $Angle = Random(20, 60, 1) * $Pi / 180
        $iBegin = TimerInit()
    EndIf
    #cs
    ConsoleWrite("; ======================" & @CRLF)
    ConsoleWrite("; Firepower       : " & $Firepower & @CRLF)
    ConsoleWrite("; Angle           : " & $Angle & @CRLF)
    ConsoleWrite("; DirX            : " & $DirX & @CRLF)
    ConsoleWrite("; DirY            : " & $DirY & @CRLF)
    ConsoleWrite("; X               : " & $X & @CRLF)
    ConsoleWrite("; Y               : " & $Y & @CRLF)
    ConsoleWrite("; ======================" & @CRLF)
    #ce

    Sleep(50)
WEnd

Func Fire()

    $iTime = TimerDiff($iBegin) / 1000

    $X = ($InitVel_X * $iTime) ; horizontal position is dependent on time and intial velocity
    $Y = ($InitVel_Y * $iTime) - (0.5 * $Gravity * ($iTime ^ 2)) ; vertical position needs to take gravity into account

    GUICtrlSetPos($Arrow, $X, $StartY - $Y)

EndFunc

If you want air resistance to be taken into account, you need a bit more calculation. :mellow:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I came across his post at another forum, perhaps it can be 'translated' or helpful in some other way?

The formula for an object jumping is

s = (u * t ) + (0.5 * a * t ^2)

Where s is the current height above the ground;

u is the initial speed that the object leaves the ground at;

t is the time since the object started the jump;

a is the acceleration due to gravity (constant) (should be negative in this case otherwise your object will shoot off into space )

If s is in metres, u is in metres per second and t is in seconds, then a realistic numeric value for a will be -9.8 (but using -10 will be pretty good) and u = 2 . For a moon walk effect you could use a = -2

I knew 4 years of Physics would be handy someday :mellow:

Maybe this can help you out too.

Since the acceleration due to gravity of an object on earth's surface is about:

9.8 meters per second squared = one "g"

Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Link to comment
Share on other sites

Here my fast hack:

;Coded by UEZ Build 2010-06-23
#include <GDIPlus.au3>
#Include <Misc.au3>
Opt("GUIOnEventMode", 1)
Opt("MouseCoordMode", 2)
$width = 600
$height = $width
$hGUI = GUICreate("GDI+ Ball Trajectory by UEZ 2010", $width, $height)
GUISetState()

_GDIPlus_Startup()
$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphics)
$hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hBuffer, 2)
_GDIPlus_GraphicsClear($hBuffer, 0xFFF0F0F0)

$hPen = _GDIPlus_PenCreate(0xFF0000A0, 2)
$hBrush = _GDIPlus_BrushCreateSolid(0x8000A000)


GUISetOnEvent(-3, "_Exit")
$lmb = 0
$ball_x = 0
$ball_y = 0
$fire = 0
$power = 2
$max_power = 30
$gravity = 1
While Sleep(30)
    _GDIPlus_GraphicsClear($hBuffer, 0xA0F0F0F0)
    $mpos = MouseGetPos()
    If _IsPressed("01") And $fire = 0 Then
        $lmb = 1
        If $power < $max_power Then $power += 1
        _GDIPlus_PenSetWidth($hPen, $power)
        _GDIPlus_GraphicsDrawLine($hBuffer, 0, $width, $mpos[0], $mpos[1], $hPen)
        $ball_x = $mpos[0]
        $ball_y = $mpos[1]
        $dx = -$mpos[0] * 0.005 * $power
        $dy = ($height - $mpos[1]) * 0.005 * $power
    Else
        If $lmb = 1 Then
            $fire = 1
            $lmb = 0
        EndIf
        $power = 2
        _GDIPlus_PenSetWidth($hPen, $power)
        _GDIPlus_GraphicsDrawLine($hBuffer, 0, $width, $mpos[0], $mpos[1], $hPen)
    EndIf
    If $fire Then
        _GDIPlus_GraphicsFillEllipse($hBuffer, $ball_x, $ball_y, 15, 15, $hBrush)
        $ball_x -= $dx
        $ball_y -= $dy
        $dy -= $gravity
        If $ball_x > $width Or $ball_y > $height Or $ball_x < 0 Then $fire = 0
    EndIf
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0)
WEnd

Func _Exit()
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hBuffer)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
    Exit
EndFunc

Press lmb to shoot ball.

BR,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Got it as I want to have it. :mellow:

#include <Misc.au3>

Global $x = 10
Global $y = 580
Global $Fire = 0
Global $Shoot = 0
Global $Power = 1
Global $Max_Power = 25
Global $Fire_Inc = 1
Global $Gravity = 1
Global $Distance
Global $dx
Global $dy
Global $DiffX, $DiffY

Global $Tx = 0, $Ty = 0

$GUI = GUICreate("Shooting Test - AlmarM", 800, 600)
$Arrow = GUICtrlCreateLabel("", 10, 580, 10, 10)
GUICtrlSetBkColor(-1, 0x000000)

$Target = GUICtrlCreateLabel("", $Tx, $Ty, 10, 10)
GUICtrlSetBkColor(-1, 0xFF0000)

$Info = GUICtrlCreateLabel("", 10,  50, 150, 100)

$Do = GUICtrlCreateButton("Shoot!", 10, 10, 100)

GUISetState()
While Sleep(30)
    Switch GUIGetMsg()
        Case -3
            Exit

        Case $Do
            $Shoot = 1

    EndSwitch

    If _IsPressed(02) Then
        $WinPos = WinGetPos("Shooting Test - AlmarM")
        $Tx = (MouseGetPos(0) - $WinPos[0] - 8)
        $Ty = (MouseGetPos(1) - $WinPos[1] - 28)

        $Distance = $Tx

        GUICtrlSetPos($Target, $Tx, $Ty)
        Sleep(20)
    EndIf

    If _IsPressed(11) Then
        $Power += $Fire_Inc

        If $Power = $Max_Power Then $Fire_Inc *= -1
        If $Power = 1 Then $Fire_Inc *= -1

        Sleep(100)
    EndIf

    If $Shoot = 1 And $Fire = 0 Then
        $Shoot = 1
        $x = 10
        $y = 580
        $dx = -$Distance * 0.005 * $Power
        $dy = 250 * 0.005 * $Power
        $Fire = 1
    Else
        If $Shoot = 1 Then
            $Fire = 1
            $Shoot = 0
        EndIf
    EndIf

    If $Fire = 1 Then
        $x -= Round($dx, 0)
        $y -= Round($dy, 0)
        $dy -= $Gravity

        If $x > 800 Or $y > 600 Or $x < 0 Then
            $x = 10
            $y = 580
            $Fire = 0
            $Power = 1
        EndIf
    EndIf

    GUICtrlSetPos($Arrow, $x, $y)

    GUICtrlSetData($Info, "Fire     : " & $Fire & @CRLF & _
                                    "Shoot      : " & $Shoot & @CRLF & _
                                    "Power      : " & $Power & @CRLF & _
                                    "Distance   : " & $Distance & @CRLF & _
                                    "dx     : " & $dx & @CRLF & _
                                    "dy     : " & $dy & @CRLF & _
                                    "XY     : " & $x & ", " & $y)

WEnd

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

Have you not studied your SUVAT yet at school? My favourite is when people ask you what you are studying at school you say "Integration and differentiation and their applications to kinematics."

Im studing 'Game-Development'. And yeah, that includes math.

The only thing is *sounds selfish*, there are only 3 people in my group who actualy understand coding (which includes me). :P

Our teacher is never here when we have math. He has to 'pratice' himself... :mellow:

So im learning all the coding (XNA aka C#) myself. Including math and Physics.

I realy hope I can skip a year, it's to easy for me now... I need a challange.

:party:

I just woke up (like 2 min ago) so grammer mistakes are possible.

:party:

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

Im studing 'Game-Development'. And yeah, that includes math.

The only thing is *sounds selfish*, there are only 3 people in my group who actualy understand coding (which includes me). :P

Our teacher is never here when we have math. He has to 'pratice' himself... :mellow:

So im learning all the coding (XNA aka C#) myself. Including math and Physics.

I realy hope I can skip a year, it's to easy for me now... I need a challange.

:party:

I just woke up (like 2 min ago) so grammer mistakes are possible.

:party:

I did SUVAT as part of my maths AO level which I wrote the paper for a few weeks ago. We had to remember all the equations and know how to use them etc. Game developement sounds fun, but why don't you save yourself the maths and use lasers? :party:
Link to comment
Share on other sites

I did SUVAT as part of my maths AO level which I wrote the paper for a few weeks ago. We had to remember all the equations and know how to use them etc. Game developement sounds fun, but why don't you save yourself the maths and use lasers? :mellow:

Use lasers? :P

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

AlmarM,

These equations are only valid in a vacuum:

If you want air resistance to be taken into account, you need a bit more calculation. :mellow:

M23

This is especially relevant because these aren't clay pigeons: they're balloons. The whole point of a balloon really is to behave irregularly vis-a-vis gravity. Unless "Bloons" aren't supposed to behave like "balloons." :P

Link to comment
Share on other sites

I think one or more of us don't understand the other...

If you made a game that had lasers instead of bullets gravity wouldn't effect it :P

Ooh, just a straight line. :party:

Right?

:mellow:

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

I don't know whether you are still interested in my version but I modified my code from post#4 a little bit.

;Coded by UEZ Build 2010-06-25
#include <GDIPlus.au3>
#Include <Misc.au3>
Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)
Opt("MouseCoordMode", 2)
Local Const $width = 1024
Local Const $height = 600
Local $hGUI = GUICreate("GDI+ Ball Trajectory by UEZ 2010", $width, $height)
GUISetState()

_GDIPlus_Startup()
Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
Local $hBitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphics)
Local $hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hBuffer, 2)
_GDIPlus_GraphicsClear($hBuffer, 0xFFF0F0F0)

Local $hPen = _GDIPlus_PenCreate(0xFF0000A0, 2)
Local $hBrush = _GDIPlus_BrushCreateSolid(0xA000A000)

GUISetOnEvent(-3, "_Exit")
Local $mpos, $d, $dx,$dy, $lx, $ly, $cl
Local Const $180_div_pi = 180 / ACos(-1)
Local Const $pi_div_180 = ACos(-1) / 180
Local $lmb = 0
Local $angle = 0
Local $ball_x = 0
Local $ball_y = 0
Local Const $ball_r = 16
Local Const $ball_r2 = $ball_r / 2
Local $fire = 0
Local $power = 2
Local $power_scale = 0
Local Const $max_power = 30
Local Const $gravity = 1
Local Const $max_dist = 350
Local Const $min_dist = 100

While Sleep(30)
    _GDIPlus_GraphicsClear($hBuffer, 0xE0F0F0F0)
    $mpos = MouseGetPos()
    If _IsPressed("01") And $fire = 0 And WinActive($hGUI) Then
        $lmb = 1
        If $power < $max_power Then $power += 1
        _GDIPlus_PenSetWidth($hPen, $power)
        $d = Pixel_Distance(0, $height, $mpos[0], $mpos[1])
        If $d > $max_dist Then
            $angle = ATan(($height - $mpos[1]) / $mpos[0]) * $180_div_pi
            $lx = Cos($angle * $pi_div_180) * $max_dist
            $ly = Sin($angle * $pi_div_180) * $max_dist
            _GDIPlus_GraphicsDrawLine($hBuffer, 0, $height, $lx, $height - $ly, $hPen)
            $ball_x = $lx
            $ball_y = $height - $ly
            $power_scale = 0.005 * $power
            $dx = -$lx * $power_scale
            $dy = $ly * $power_scale
        ElseIf $d < $min_dist Then
            $angle = ATan(($height - $mpos[1]) / $mpos[0]) * $180_div_pi
            $lx = Cos($angle * $pi_div_180) * $min_dist
            $ly = Sin($angle * $pi_div_180) * $min_dist
            _GDIPlus_GraphicsDrawLine($hBuffer, 0, $height, $lx, $height - $ly, $hPen)
            $ball_x = $lx
            $ball_y = $height - $ly
            $power_scale = 0.005 * $power
            $dx = -$lx * $power_scale
            $dy = $ly * $power_scale
        Else
            _GDIPlus_GraphicsDrawLine($hBuffer, 0, $height, $mpos[0], $mpos[1], $hPen)
            $ball_x = $mpos[0]
            $ball_y = $mpos[1]
            $dx = -$mpos[0] * 0.005 * $power
            $dy = ($height - $mpos[1]) * 0.005 * $power
        EndIf
    Else
        If $lmb = 1 Then
            $fire = 1
            $lmb = 0
        EndIf
        $power = 2
        _GDIPlus_PenSetWidth($hPen, $power)
        $d = Pixel_Distance(0, $height, $mpos[0], $mpos[1])
        $angle = ATan(($height - $mpos[1]) / $mpos[0]) * $180_div_pi
        If $d > $max_dist Then
            $lx = Cos($angle * $pi_div_180) * $max_dist
            $ly = Sin($angle * $pi_div_180) * $max_dist
            _GDIPlus_GraphicsDrawLine($hBuffer, 0, $height, $lx, $height - $ly, $hPen)
            $cl = Pixel_Distance(0, $height, $lx, $height - $ly)
        ElseIf $d < $min_dist Then
            $lx = Cos($angle * $pi_div_180) * $min_dist
            $ly = Sin($angle * $pi_div_180) * $min_dist
            _GDIPlus_GraphicsDrawLine($hBuffer, 0, $height, $lx, $height - $ly, $hPen)
            $cl = Pixel_Distance(0, $height, $lx, $height - $ly)
        Else
            _GDIPlus_GraphicsDrawLine($hBuffer, 0, $height, $mpos[0], $mpos[1], $hPen)
            $cl = Pixel_Distance(0, $height, $mpos[0], $mpos[1])
        EndIf
    EndIf
    ToolTip("Cannon Angle: " & Int($angle) & "°, Cannon Power: " & $power & ", Cannon Length: " & Int($cl) & ", Ball x: " & Int($ball_x) & ", Ball y: " & Int($ball_y))
    If $fire Then
        _GDIPlus_GraphicsFillEllipse($hBuffer, $ball_x - $ball_r2, $ball_y - $ball_r2, $ball_r, $ball_r, $hBrush)
        $ball_x -= $dx
        $ball_y -= $dy
        $dy -= $gravity
        If $ball_x > $width Or $ball_y > $height Or $ball_x < -$ball_r Then
            $fire = 0
            $ball_x = 0
            $ball_y = 0
        EndIf
    EndIf
    _GDIPlus_GraphicsDrawString($hBuffer, "Coded by UEZ 2010", $width - 105, $height - 15, "Arial", 8)
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0)
WEnd

Func _Exit()
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hBuffer)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
    Exit
EndFunc


Func Pixel_Distance($x1, $y1, $x2, $y2) ;Pythagoras theorem
    Local $a, $b, $c
    If $x2 = $x1 And $y2 = $y1 Then
        Return 0
    Else
        $a = $y2 - $y1
        $b = $x2 - $x1
        $c = Sqrt($a * $a + $b * $b)
        Return $c
    EndIf
EndFunc ;==>Pixel_Distance

Press left mouse button to shoot ball!

It is not perfect but a good beginning, isn't it? :mellow:

BR,

UEZ

GDI+ Ball Trajectory.au3

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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