2D Tilemap Kollision

Übersicht BlitzMax, BlitzMax NG Beginners-Corner

Neue Antwort erstellen

Progger93

Betreff: 2D Tilemap Kollision

BeitragSo, Nov 30, 2008 16:34
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich habe folgendes Problem:
Bei meiner 2D Tilemap Kollision werden alle 4 richtungen gleich abgefragt:

Code: [AUSKLAPPEN]

SuperStrict

Const width:Int = 1024
Const height:Int = 768


Graphics width , height
SetBlend(ALPHABLEND)

Global mapwidth:Int=1
Global mapheight:Int=1
Global map:Int[mapheight , mapwidth]

loadmap()

Type spieler
   Field x:Float
   Field y:Float
   Field ammo:Int
   Field weapon:Int
   Field bot:Byte
   Field camera:Byte
   Field speed:Float
End Type
Global spielerlist:TList = CreateList()

Local sp:spieler = New spieler
sp.x = 500
sp.y = 372
sp.ammo = 200
sp.weapon = 1
sp.camera = 1
sp.speed=2
ListAddLast(spielerlist,sp)


Global I:Int , J:Int

Global scroll_x:Int=500,scroll_y:Int=372


Local frame_timer:TTimer=CreateTimer(60)
Repeat
   WaitTimer(frame_timer)
   Cls
   
   
   drawmap()
   
   spielerupdate()

   
   Flip 0
Until KeyHit(KEY_ESCAPE)
End

Function loadmap()
   Local datei:TStream = ReadFile("map1.txt")
   mapwidth = ReadShort(datei:TStream)
   mapheight = ReadShort(datei:TStream)
   map=New Int[mapwidth+1,mapheight+1]
   For I = 0 To mapwidth
      For J = 0 To mapheight
         map[I , J] = ReadByte(datei:TStream) 
      Next
   Next
   CloseFile(datei:TStream)
End Function
   
Function drawmap()
   For I = 0 To mapwidth
      For J = 0 To mapheight
         Select map[I , J]
            Case 0
               SetColor 255,255,255
            Case 1
               SetColor 0,0,0
         End Select
         DrawRect I * 32-scroll_x , J * 32-scroll_y , 32 , 32
      Next
   Next
End Function

Function spielerupdate()
   For Local sp:spieler = EachIn spielerlist
      If sp.bot = 0 Then
         If KeyDown(KEY_W) Then
            sp.y=sp.y-sp.speed
         EndIf
         If KeyDown(KEY_S) Then
            sp.y=sp.y+sp.speed
         EndIf
         If KeyDown(KEY_A) Then
            sp.x=sp.x-sp.speed
         EndIf
         If KeyDown(KEY_D) Then
            sp.x=sp.x+sp.speed
         EndIf
      EndIf
      If sp.camera = 1 Then
         scroll_x = sp.x-500
         scroll_y = sp.y-372
      EndIf
      For I = 0 To mapwidth
         For J = 0 To mapheight
            If map[I,J]=1 Then
            If rectsoverlap(sp.x , sp.y , 24 , 24 , I * 32 , J * 32 , 32 , 1) Then sp.y = sp.y - sp.speed 'wird schneller
            If rectsoverlap(sp.x , sp.y , 24 , 24 , I * 32 , J * 32 , 1 , 32) Then sp.x = sp.x - sp.speed 'Bleibt hängen
            If rectsoverlap(sp.x , sp.y , 24 , 24 , I * 32 , J * 32 + 32 , 32 , 1) Then sp.y = sp.y + sp.speed 'Funktioniert
            If rectsoverlap(sp.x , sp.y , 24 , 24 , I * 32 + 32 , J * 32 , 32 , 1) Then sp.x = sp.x + sp.speed 'geht garnich
            EndIf
         Next
      Next
      SetColor 0,0,255
      DrawRect sp.x - scroll_x , sp.y - scroll_y , 24 , 24
      SetColor 255,0,0
      DrawText "X: "+Int(sp.x)+"  Y: "+Int(sp.y),0,0
   Next
End Function



Function rectsoverlap:Int(x1:Int , y1:Int , width1:Int , height1:Int , x2:Int , y2:Int , width2:Int , height2:Int)
   If x1 + width1 > x2 And y1 + height1 > y2 And x1 < x2 + width2 And y1 < y2 + height2 Then
      Return 1
   Else
      Return 0
   EndIf
End Function


Es gibt folgende Kollisions Auswirkungen:
-Oben: Nach Rechts wirds schneller, nach Links bleibt er hängen.
-Rechts: Man kann recht einfach durchgehen
-Unten: Geht perfekt bis auf 2 Pixel Abstand von der Wan
-Links: Nach Unten entlang laufen geht garnicht, beim Hoch laufen ruckelts


Hoffe ihr könnt mir helfen.

MFG Progger93
MfG Pascal
Win 7|T7250@2.0Ghz|3GB RAM|M8600GT

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Beginners-Corner

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group