Collision von Kugeln berechnen

  • Hallo wie der name schon sagt geht es um Collisonen von Kugeln. Ich möchte für ein spiel das eine Kugel, die man frei bewegen kann einen ball anstößt und das dieser dann physikalisch richtig abprallt. Jedoch bekomme ich die rechung dafür nicht hin.

    Spoiler anzeigen

    #include <GuiConstants.au3>
    #include <GDIPlus.au3>
    #include <Misc.au3>

    HotKeySet("{ESC}", "_beenden")

    Opt('MustDeclareVars', 1)
    Opt('GuiOnEventMode', 1)
    Opt ('MouseCoordMode', 0 )


    Global $hGUI, $hWnd, $hGraphic, $Bitmap, $Buffer, $hPen, $msg
    Global $width = 640
    Global $height = 400
    Global $titel = "Titel"
    Global $version = " v1"
    Global $rotate, $x = 288, $y = 168, $schwung, $auto_radius = 32
    Global $ball_x = 400, $ball_y = 250, $ball_radius = 16, $ball_rotate, $ball_schwung
    Global $pi = 3.14159265358979
    Global $f,$w,$wi

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


    $hGUI = GUICreate($Titel & $version, $width, $height,-1,-1)
    $hWnd = WinGetHandle($Titel & $version)

    GUISetState(@SW_SHOW)

    ;GUISetCursor(16)invisible

    _GDIPlus_Startup ()

    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd)
    $Bitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphic)
    $Buffer = _GDIPlus_ImageGetGraphicsContext($Bitmap)
    _GDIPlus_GraphicsSetSmoothingMode($Buffer, 4)
    _GDIPlus_GraphicsClear($Buffer,0xFF000000)
    $hPen = _GDIPlus_PenCreate (0xFF00FF00,4)

    While 1

    If $Rotate > 360 Then
    $Rotate -=360
    ElseIf $Rotate < 0 Then
    $Rotate +=360
    EndIf

    If _IsPressed ("27") Then
    $Rotate -= 5
    ElseIf _IsPressed ("25") Then
    $Rotate += 5
    EndIf
    If _IsPressed ("26") Then
    $schwung -= 0.1
    ElseIf _IsPressed ("28") Then
    $schwung += 0.1
    EndIf

    If $schwung >= 10 Then $schwung = 10
    If $schwung <= -10 Then $schwung = -10

    If $schwung < 0 Then $schwung += 0.01
    If $schwung > 0 Then $schwung -= 0.01

    ;##### Collision Seiten ################################################

    If $x <= 0 Then
    $rotate = -$rotate
    $x += 1
    EndIf
    If $x >= 576 Then
    $rotate = -$rotate
    $x -= 1
    EndIf

    If $y <= 0 Then
    $rotate += 90
    $rotate = -$rotate
    $rotate -= 90
    $y += 1
    EndIf
    If $y >= 336 Then
    $rotate += 90
    $rotate = -$rotate
    $rotate -= 90
    $y -= 1
    EndIf

    ;##### /Collision Seiten ###############################################

    ;#### Collision zwischen Auto und Kugel ###############################

    If (sqrt(($x-$ball_x)*($x-$ball_x)+($y-$ball_y)*($y-$ball_y))< 32) Then

    _collision ()

    EndIf

    ;#### /Collision zwischen Auto und Kugel ##############################

    $x += Sin (($rotate)*$Pi/180)*$schwung ;errechnet x
    $y += Cos (($rotate)*$Pi/180)*$schwung ;errechnet y

    $ball_x += Sin (($ball_rotate)*$Pi/180)*$ball_schwung ;errechnet x
    $ball_y += Cos (($ball_rotate)*$Pi/180)*$ball_schwung ;errechnet y

    $ball_schwung -= 0.01
    If $ball_schwung < 0 Then $ball_schwung = 0

    _zeichnen ()

    Sleep(10)
    WEnd

    Func _collision ()

    $ball_schwung = 1

    Local $x1 ,$y1, $var1, $var2, $radToDeg = 180 / $pi

    $x1 = $ball_x - $x
    $y1 = $ball_y - $y

    $var1 = $x1 / 64
    $var2 = ACos ($var1) * $radToDeg
    $ball_rotate = $var2
    ToolTip ($x1 & " ; " & $y1 & " ; " & $var1 & " ; " & $var2)

    EndFunc

    Func _zeichnen ()
    _GDIPlus_GraphicsClear($Buffer,0xFFFFFFFF)
    _GDIPlus_GraphicsDrawPie ($Buffer,$x,$y,64,64,0 - $Rotate - 90,360,$hPen)
    _GDIPlus_GraphicsDrawPie ($Buffer,$ball_x,$ball_y,32,32,0 - $ball_Rotate - 90,360,$hPen)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $Bitmap,0,0, $width, $height)
    EndFunc

    Func _beenden ()

    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_BitmapDispose($Bitmap)
    _GDIPlus_GraphicsDispose($Buffer)
    _GDIPlus_PenDispose ($hPen)

    _GDIPlus_Shutdown ()

    ; DllClose ($dll)

    Exit

    EndFunc

  • Hi
    Tut mir leid, ich kann dir leider nicht helfen. Wollte aber trotzdem mal meinen Respekt ausdrücken. Ist schon jetzt ganz lustig. Was man alles machen kann!

    PS: man kann die kleine Kugel aus dem Fenster schieben, dann ist sie weg.

    Werde den Thread weiter beobachten.
    Lustig.
    Gruß Autoirrer

  • ja das geht sogar im richtigen winkel

    so ich habs jetzt geschaft das es einigermaßen geht aber noch ncitg richtig

    Spoiler anzeigen

    #include <GuiConstants.au3>
    #include <GDIPlus.au3>
    #include <Misc.au3>

    HotKeySet("{ESC}", "_beenden")

    Opt('MustDeclareVars', 1)
    Opt('GuiOnEventMode', 1)
    Opt ('MouseCoordMode', 0 )


    Global $hGUI, $hWnd, $hGraphic, $Bitmap, $Buffer, $hPen, $msg
    Global $width = 640
    Global $height = 400
    Global $titel = "Titel"
    Global $version = " v1"
    Global $rotate, $x = 288, $y = 168, $schwung, $auto_radius = 32
    Global $ball_x = 400, $ball_y = 250, $ball_radius = 16, $ball_rotate, $ball_schwung
    Global $pi = 3.14159265358979
    Global $f,$w,$wi

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


    $hGUI = GUICreate($Titel & $version, $width, $height,-1,-1)
    $hWnd = WinGetHandle($Titel & $version)

    GUISetState(@SW_SHOW)

    ;GUISetCursor(16)invisible

    _GDIPlus_Startup ()

    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd)
    $Bitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphic)
    $Buffer = _GDIPlus_ImageGetGraphicsContext($Bitmap)
    _GDIPlus_GraphicsSetSmoothingMode($Buffer, 4)
    _GDIPlus_GraphicsClear($Buffer,0xFF000000)
    $hPen = _GDIPlus_PenCreate (0xFF00FF00,4)

    While 1

    If $Rotate > 360 Then
    $Rotate -=360
    ElseIf $Rotate < 0 Then
    $Rotate +=360
    EndIf

    If _IsPressed ("27") Then
    $Rotate -= 5
    ElseIf _IsPressed ("25") Then
    $Rotate += 5
    EndIf
    If _IsPressed ("26") Then
    $schwung -= 0.1
    ElseIf _IsPressed ("28") Then
    $schwung += 0.1
    EndIf

    If $schwung >= 10 Then $schwung = 10
    If $schwung <= -10 Then $schwung = -10

    If $schwung < 0 Then $schwung += 0.01
    If $schwung > 0 Then $schwung -= 0.01

    ;##### Collision Seiten ################################################

    If $x <= 0 Then
    $rotate = -$rotate
    $x += 1
    EndIf
    If $x >= 576 Then
    $rotate = -$rotate
    $x -= 1
    EndIf

    If $y <= 0 Then
    $rotate += 90
    $rotate = -$rotate
    $rotate -= 90
    $y += 1
    EndIf
    If $y >= 336 Then
    $rotate += 90
    $rotate = -$rotate
    $rotate -= 90
    $y -= 1
    EndIf

    ;##### /Collision Seiten ###############################################

    ;#### Collision zwischen Auto und Kugel ###############################

    If (sqrt(($x -$ball_x)*($x-$ball_x)+($y-$ball_y)*($y-$ball_y))< 32) Then

    _collision ()

    EndIf

    ;#### /Collision zwischen Auto und Kugel ##############################

    $x += Sin (($rotate)*$Pi/180)*$schwung ;errechnet x
    $y += Cos (($rotate)*$Pi/180)*$schwung ;errechnet y

    $ball_x += Sin (($ball_rotate)*$Pi/180)*$ball_schwung ;errechnet x
    $ball_y += Cos (($ball_rotate)*$Pi/180)*$ball_schwung ;errechnet y

    $ball_schwung -= 0.01
    If $ball_schwung < 0 Then $ball_schwung = 0

    _zeichnen ()

    Sleep(10)
    WEnd

    Func _collision ()

    $ball_schwung = 1

    Local $x1 ,$y1, $var1, $var2, $radToDeg = 180 / $pi

    $x1 = $ball_x - $x
    $y1 = $ball_y - $y

    $var1 = $x1 / 64
    $var2 = ACos ($var1) * $radToDeg
    $ball_rotate = $var2
    ToolTip ($x1 & " ; " & $y1 & " ; " & $var1 & " ; " & $var2 & " ; " & $rotate)

    If $rotate >= 0 And $rotate <= 180 Then $ball_rotate = -$ball_rotate

    EndFunc

    Func _zeichnen ()
    _GDIPlus_GraphicsClear($Buffer,0xFFFFFFFF)
    _GDIPlus_GraphicsDrawPie ($Buffer,$x,$y,64,64,0 - $Rotate - 90,360,$hPen)
    _GDIPlus_GraphicsDrawPie ($Buffer,$ball_x,$ball_y,32,32,0 - $ball_Rotate - 90,360,$hPen)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $Bitmap,0,0, $width, $height)
    EndFunc


    Func _beenden ()

    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_BitmapDispose($Bitmap)
    _GDIPlus_GraphicsDispose($Buffer)
    _GDIPlus_PenDispose ($hPen)

    _GDIPlus_Shutdown ()

    ; DllClose ($dll)

    Exit

    EndFunc

  • Zwei Fragen
    1) Das sind nicht zufällig ein Parr zeilen von mir oder^^
    2)Warum hattest du mir das in dem Terad nicht gepostet, hier? Hast du es nicht gesehen oder was?

    Naja, hab es ja inzwischen auch hinbekommen, ist nur ein Bischen länger^^

    Zu deiner Frage,
    Weiß ich nicht :(
    Aber hiermit kann man errechen, ob sich etwas über einem Runden Objekt befindet

    Spoiler anzeigen
    [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _PointInEllipse($xPt, $yPt, $xTL, $yTL, $w, $h)
    Local $bInside = False, $a = $w / 2, $b = $h / 2
    Local $c1X, $c2X, $dist, $xc = $xTL + $a, $yc = $yTL + $b
    $c1X = $xc - ($a ^ 2 - $b ^ 2) ^ (1 / 2); 1st focal point x position
    $c2X = $xc + ($a ^ 2 - $b ^ 2) ^ (1 / 2); 2nd focal point x position
    $dist = (($xPt - $c1X) ^ 2 + ($yPt - $yc) ^ 2) ^ 0.5 + (($xPt - $c2X) ^ 2 + ($yPt - $yc) ^ 2) ^ 0.5
    If $dist <= $w Then $bInside = Not $bInside
    Return $bInside
    EndFunc ;==>_PointInEllipse

    [/autoit]

    mfg. Jam00

  • was meinst du den welche funktion? (ist von dir)

    die funktion von dir ist zwar gut jedoch habe ich breits errechnet ob die kugek übereinander liegen (If (sqrt(($x -$ball_x)*($x-$ball_x)+($y-$ball_y)*($y-$ball_y))< 32) Then) ich bruach nur die winkel in den die kugel voneinander abprallen aber trotzdem danke für deinen rat

  • Hier ist noch ne version

    Spoiler anzeigen

    #include <GuiConstants.au3>
    #include <GDIPlus.au3>
    #include <Misc.au3>

    HotKeySet("{ESC}", "_beenden")

    Opt('MustDeclareVars', 1)
    Opt('GuiOnEventMode', 1)
    Opt ('MouseCoordMode', 0 )


    Global $hGUI, $hWnd, $hGraphic, $Bitmap, $Buffer, $hPen, $msg
    Global $width = 640
    Global $height = 400
    Global $titel = "Titel"
    Global $version = " v1"
    Global $rotate, $x = 288, $y = 168, $schwung, $auto_radius = 32
    Global $ball_x = 400, $ball_y = 250, $ball_radius = 16, $ball_rotate, $ball_schwung
    Global $pi = 3.14159265358979
    Global $f,$w,$wi

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


    $hGUI = GUICreate($Titel & $version, $width, $height,-1,-1)
    $hWnd = WinGetHandle($Titel & $version)

    GUISetState(@SW_SHOW)

    ;GUISetCursor(16)invisible

    _GDIPlus_Startup ()

    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd)
    $Bitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphic)
    $Buffer = _GDIPlus_ImageGetGraphicsContext($Bitmap)
    _GDIPlus_GraphicsSetSmoothingMode($Buffer, 4)
    _GDIPlus_GraphicsClear($Buffer,0xFF000000)
    $hPen = _GDIPlus_PenCreate (0xFF00FF00,4)

    While 1

    If $Rotate > 360 Then
    $Rotate -=360
    ElseIf $Rotate < 0 Then
    $Rotate +=360
    EndIf

    If _IsPressed ("27") Then
    $Rotate -= 5
    ElseIf _IsPressed ("25") Then
    $Rotate += 5
    EndIf
    If _IsPressed ("26") Then
    $schwung -= 0.1
    ElseIf _IsPressed ("28") Then
    $schwung += 0.1
    EndIf

    If $schwung >= 10 Then $schwung = 10
    If $schwung <= -10 Then $schwung = -10

    If $schwung < 0 Then $schwung += 0.01
    If $schwung > 0 Then $schwung -= 0.01

    ;##### Collision Seiten ################################################

    If $x <= 0 Then
    $rotate = -$rotate
    $x += 1
    EndIf
    If $x >= 576 Then
    $rotate = -$rotate
    $x -= 1
    EndIf

    If $y <= 0 Then
    $rotate += 90
    $rotate = -$rotate
    $rotate -= 90
    $y += 1
    EndIf
    If $y >= 336 Then
    $rotate += 90
    $rotate = -$rotate
    $rotate -= 90
    $y -= 1
    EndIf


    If $ball_x <= 0 Then
    $ball_rotate = -$ball_rotate
    $ball_x += 1
    EndIf
    If $ball_x >= 576 Then
    $ball_rotate = -$ball_rotate
    $ball_x -= 1
    EndIf

    If $ball_y <= 0 Then
    $ball_rotate += 90
    $ball_rotate = -$ball_rotate
    $ball_rotate -= 90
    $ball_y += 1
    EndIf
    If $ball_y >= 336 Then
    $ball_rotate += 90
    $ball_rotate = -$ball_rotate
    $ball_rotate -= 90
    $ball_y -= 1
    EndIf

    ;##### /Collision Seiten ###############################################

    ;#### Collision zwischen Auto und Kugel ###############################

    If (sqrt(($x -$ball_x)*($x-$ball_x)+($y-$ball_y)*($y-$ball_y))< 72) Then

    _collision ()

    EndIf

    ;#### /Collision zwischen Auto und Kugel ##############################

    $x += Sin (($rotate)*$Pi/180)*$schwung ;errechnet x
    $y += Cos (($rotate)*$Pi/180)*$schwung ;errechnet y

    $ball_x += Sin (($ball_rotate)*$Pi/180)*$ball_schwung ;errechnet x
    $ball_y += Cos (($ball_rotate)*$Pi/180)*$ball_schwung ;errechnet y

    $ball_schwung -= 0.01
    If $ball_schwung < 0 Then $ball_schwung = 0

    _zeichnen ()

    Sleep(10)
    WEnd

    Func _collision ()

    $ball_schwung = -$schwung

    Local $x1 ,$y1, $var1, $var2, $radToDeg = 180 / $pi

    $x1 = $ball_x - $x
    $y1 = $ball_y - $y

    $var1 = $x1 / 64
    $var2 = ACos ($var1) * $radToDeg
    $ball_rotate = $var2
    ToolTip ($x1 & " ; " & $y1 & " ; " & $var1 & " ; " & $var2 & " ; " & $rotate)

    If $rotate >= 0 And $rotate <= 180 Then $ball_rotate = -$ball_rotate

    EndFunc

    Func _zeichnen ()
    _GDIPlus_GraphicsClear($Buffer,0xFFFFFFFF)
    _GDIPlus_GraphicsDrawPie ($Buffer,$x,$y,64,64,0 - $Rotate - 90,360,$hPen)
    _GDIPlus_GraphicsDrawPie ($Buffer,$ball_x,$ball_y,32,32,0 - $ball_Rotate - 90,360,$hPen)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $Bitmap,0,0, $width, $height)
    EndFunc

    Func _PointInEllipse($xPt, $yPt, $xTL, $yTL, $w, $h)
    Local $bInside = False, $a = $w / 2, $b = $h / 2
    Local $c1X, $c2X, $dist, $xc = $xTL + $a, $yc = $yTL + $b
    $c1X = $xc - ($a ^ 2 - $b ^ 2) ^ (1 / 2); 1st focal point x position
    $c2X = $xc + ($a ^ 2 - $b ^ 2) ^ (1 / 2); 2nd focal point x position
    $dist = (($xPt - $c1X) ^ 2 + ($yPt - $yc) ^ 2) ^ 0.5 + (($xPt - $c2X) ^ 2 + ($yPt - $yc) ^ 2) ^ 0.5
    If $dist <= $w Then $bInside = Not $bInside
    Return $bInside
    EndFunc ;==>_PointInEllipse


    Func _beenden ()

    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_BitmapDispose($Bitmap)
    _GDIPlus_GraphicsDispose($Buffer)
    _GDIPlus_PenDispose ($hPen)

    _GDIPlus_Shutdown ()

    ; DllClose ($dll)

    Exit

    EndFunc