Hallo Community,
ich habe bereits eine Kollisionserkennung für ein GDI+ Spiel, an dem ich noch eine ganze Weile dran sein werde, erstellt. Jedoch möchte ich meine eigene Erkennung (leider viel zu komplex und nicht gerade dynamisch) durch die Möglichkeiten mit _GDIPlus_PathIsVisiblePoint() ersetzen. Auf der Suche nach passenden Beispielen bin ich über einen Thread im englischen Forum von UEZ gestoßen, den ich nun (den Code daraus) für meine Bedürfnisse angepasst habe.
Leider tritt auf einer bestimmten Höhe (Y Pos) für die links --> rechts Bewegung oder auch für die rechts <-- links Bewegung keine Kollision auf. Warum?
Die Hintergründe der beiden Bilder sind nur zur Darstellung wo die Grenzen der Vierecke sind. Die eigentliche Kollision bezieht sich auf das rote Viereck. Der Player soll nicht in diesem Bereich eintreten können (Bewegung stop).
Danke für Vorschläge, Tipps und Hilfe - ich bin dankbar!
Sven
Das animierte GIF sollte hoffentlich mein Problem weiter erklären:
[Solved] Finale Lösung:
Danke der Community.
Code:
; includes ---------------------------------------------------------------------
#include-once
#include <GDIPlus.au3>
#include <Misc.au3>
; declaration ------------------------------------------------------------------
Global Const $iGuiWidth = 1000
Global Const $iGuiHeight = 690
Global Const $iGuiLeft = 10
Global Const $iGuiTop = 10
Global Const $GUI_EVENT_CLOSE = -3
Global $bExit = False
Global Const $iPlayerWidth = 32
Global Const $iPlayerHeight = 48
Global Const $iPlayerStep = 4
Global $iXPosPlayer = 16
Global $iYPosPlayer = 16
Global Const $iTreeWidth = 128
Global Const $iTreeHeight = 160
Global Const $iXPosTree = 100
Global Const $iYPosTree = 100
Global Const $iWidthCollisionBlock = 40
Global Const $iHeightCollisionBlock = 20
Global Const $iXCollisionBlock = 145
Global Const $iYCollisionBlock = 220
Global Const $sPlayerImg = 'player01_01.png'
Global Const $sTreeImg = 'tree01_01.png'
; gui --------------------------------------------------------------------------
Global $hMainGui = GUICreate( '', $iGuiWidth, $iGuiHeight, $iGuiLeft, $iGuiTop )
GUISetState( @SW_SHOW, $hMainGui )
; init -------------------------------------------------------------------------
Opt( 'GUIOnEventMode', 1 )
GUISetOnEvent( $GUI_EVENT_CLOSE, '_exitToggle' )
OnAutoItExitRegister( '_exit' )
_GDIPlus_Startup()
Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND( $hMainGui )
Global $hBitmap = _GDIPlus_BitmapCreateFromGraphics( $iGuiWidth, $iGuiHeight, $hGraphics )
Global $hBuffer = _GDIPlus_ImageGetGraphicsContext( $hBitmap )
Global $vRed = _GDIPlus_BrushCreateSolid( 0xFFFF0000 )
Global $hPlayerImg = _GDIPlus_ImageLoadFromFile( $sPlayerImg )
Global $hTreeImg = _GDIPlus_ImageLoadFromFile( $sTreeImg )
; functions --------------------------------------------------------------------
Func _isCollisionRect( $iX, $iY, $iWidth, $iHeight, $iXBlock, $iYBlock, $iWidthBlock, $iHeightBlock )
Return ( $iX + $iWidth > $iXBlock And $iY + $iHeight > $iYBlock And $iX < $iXBlock + $iWidthBlock And $iY < $iYBlock + $iHeightBlock )
EndFunc
Func _isCollisionPoint( $iX, $iY, $iXBlock, $iYBlock, $iWidthBlock, $iHeightBlock ) ; currently unused
Return ( $iX > $iXBlock And $iY > $iYBlock And $iX < $iXBlock + $iWidthBlock And $iY < $iYBlock + $iHeightBlock )
EndFunc
Func _exitToggle()
$bExit = True
EndFunc
Func _exit()
_GDIPlus_BrushDispose( $vRed )
_GDIPlus_GraphicsDispose( $hBuffer )
_GDIPlus_BitmapDispose( $hBitmap )
_GDIPlus_GraphicsDispose( $hGraphics )
_GDIPlus_Shutdown()
GUIDelete( $hMainGui )
EndFunc
; processing -------------------------------------------------------------------
While Not $bExit And Sleep( 25 )
_GDIPlus_GraphicsClear( $hBuffer )
_GDIPlus_GraphicsDrawImageRect( $hBuffer, $hPlayerImg, $iXPosPlayer, $iYPosPlayer, $iPlayerWidth, $iPlayerHeight ) ; draw player
_GDIPlus_GraphicsDrawImageRect( $hBuffer, $hTreeImg, $iXPosTree, $iYPosTree, $iTreeWidth, $iTreeHeight ) ; draw tree
If $iYPosPlayer >= $iYCollisionBlock Then _GDIPlus_GraphicsDrawImageRect( $hBuffer, $hPlayerImg, $iXPosPlayer, $iYPosPlayer, $iPlayerWidth, $iPlayerHeight ) ; draw player in the forground
_GDIPlus_GraphicsFillRect( $hBuffer, $iXCollisionBlock, $iYCollisionBlock, $iWidthCollisionBlock, $iHeightCollisionBlock, $vRed ) ; just for visualization purpose
If _IsPressed( '25' ) Then ; left
If Not _isCollisionRect( $iXPosPlayer - $iPlayerStep, $iYPosPlayer, $iPlayerWidth, $iPlayerHeight, $iXCollisionBlock, $iYCollisionBlock, $iWidthCollisionBlock, $iHeightCollisionBlock ) Then
$iXPosPlayer -= $iPlayerStep
EndIf
EndIf
If _IsPressed( '27' ) Then ; right
If Not _isCollisionRect( $iXPosPlayer + $iPlayerStep, $iYPosPlayer, $iPlayerWidth, $iPlayerHeight, $iXCollisionBlock, $iYCollisionBlock, $iWidthCollisionBlock, $iHeightCollisionBlock ) Then
$iXPosPlayer += $iPlayerStep
EndIf
EndIf
If _IsPressed( '26' ) Then ; up
If Not _isCollisionRect( $iXPosPlayer, $iYPosPlayer - $iPlayerStep, $iPlayerWidth, $iPlayerHeight, $iXCollisionBlock, $iYCollisionBlock, $iWidthCollisionBlock, $iHeightCollisionBlock ) Then
$iYPosPlayer -= $iPlayerStep
EndIf
EndIf
If _IsPressed( '28' ) Then ; down
If Not _isCollisionRect( $iXPosPlayer, $iYPosPlayer + $iPlayerStep, $iPlayerWidth, $iPlayerHeight, $iXCollisionBlock, $iYCollisionBlock, $iWidthCollisionBlock, $iHeightCollisionBlock ) Then
$iYPosPlayer += $iPlayerStep
EndIf
EndIf
_GDIPlus_GraphicsDrawImageRect( $hGraphics, $hBitmap, 0, 0, $iGuiWidth, $iGuiHeight )
WEnd
Alles anzeigen