Hi, On one of my many projects I'm having difficulties with collision detection. I have made it work great for vertical check but just can't seem to get further with the horizontal. Following is the current code. It's a scrolling platformer that moves from right to left. Any suggestions are very welcome.
[code]
Graphics 800,600,32,2
tileImg = LoadAnimImage("gfx\tiles.png",32,32,0,15)
playerImg = LoadImage("gfx\player.png")
playerX# = 200
playerY# = 32
playerZ# = -1
playerU# = 0
gravity# = .1
friction# = .98
offset = 0
scroll = 0
Dim playfield(100,2500,16)
Gosub loadlevel
SetBuffer BackBuffer()
While Not KeyHit(1)
;-----------------------------------------------------------------------------
; CHECK PLAYER CONTROLS
;-----------------------------------------------------------------------------
If chkkeys + 25 <= MilliSecs() Then
chkkeys = MilliSecs()
If KeyDown(203) Or JoyXDir()=-1 Then
playerU = playerU - .2 ;left
Left = 1
Else
Left = 0
End If
If KeyDown(205) Or JoyXDir()=1
playerU = playerU + .2 ;right
Right = 1
Else
Right = 0
End If
jy = Int(JoyY())
If jy = -1 Or KeyHit(57) Then playerZ = 5.5
End If
If mvscreen + 5 <= MilliSecs() Then
mvscreen = MilliSecs()
If offset > 0 And offset < 2475 Then
If Left = 1 Then scroll = scroll - 2
If Right = 1 Then scroll = scroll + 2
End If
If offset = 0 Then
If Right = 1 And scroll < 31 Then scroll = scroll + 2
End If
If offset = 2475 Then
If Left = 1 And scroll > 0 Then scroll = scroll - 2
End If
If offset < 0 Then offset = 0
If offset > 2475 Then offset = 2475
If scroll > 31 Then
scroll = scroll - 32
offset = offset + 1
End If
If scroll < 0 Then
scroll = scroll + 32
offset = offset - 1
End If
End If
playerU = playerU * friction
playerZ = playerZ - gravity
playerX = playerX + playerU
playerY = playerY - playerZ
;-----------------------------------------------------------------------------
; DRAW SCREEN AND CHECK FOR PLAYER COLLISION
;-----------------------------------------------------------------------------
nomove = 0
For x=0 To 25
For y=0 To 15
If playfield(level,offset+x,y) >0 Then
DrawImage tileImg,x*32-scroll,y*32,playfield(level,offset+x,y)
If ImagesCollide(tileImg,x*32-scroll,y*32,playfield(level,offset+x,y),playerImg,playerX,playerY,0) Then nomove = 1
End If
Next
Next
If nomove = 1 Then
; playerX = playerX - playerU
playerY = playerY + playerZ
; playerU = 0
playerZ = 0
End If
If playerY < 0 Then playerY = 0
If playerY > 480 Then playerY = 480
If playerX > 768 Then playerX = 768
If playerX < 0 Then playerX = 0
DrawImage playerImg,playerX,playerY
Flip
Cls
Wend
FreeImage titleImg
FreeImage playerImg
End
;-----------------------------------------------------------------------------
; LOAD LEVEL INTO ARRAY
;-----------------------------------------------------------------------------
.loadLevel
filein = ReadFile("gianasisters.lvl")
;For lv = 0 To 99
For x = 0 To 2499
For y = 0 To 15
playfield(lv,x,y) = ReadInt(filein)
Next
Next
;Next
CloseFile(filein)
Return
[\code]
[code]
Graphics 800,600,32,2
tileImg = LoadAnimImage("gfx\tiles.png",32,32,0,15)
playerImg = LoadImage("gfx\player.png")
playerX# = 200
playerY# = 32
playerZ# = -1
playerU# = 0
gravity# = .1
friction# = .98
offset = 0
scroll = 0
Dim playfield(100,2500,16)
Gosub loadlevel
SetBuffer BackBuffer()
While Not KeyHit(1)
;-----------------------------------------------------------------------------
; CHECK PLAYER CONTROLS
;-----------------------------------------------------------------------------
If chkkeys + 25 <= MilliSecs() Then
chkkeys = MilliSecs()
If KeyDown(203) Or JoyXDir()=-1 Then
playerU = playerU - .2 ;left
Left = 1
Else
Left = 0
End If
If KeyDown(205) Or JoyXDir()=1
playerU = playerU + .2 ;right
Right = 1
Else
Right = 0
End If
jy = Int(JoyY())
If jy = -1 Or KeyHit(57) Then playerZ = 5.5
End If
If mvscreen + 5 <= MilliSecs() Then
mvscreen = MilliSecs()
If offset > 0 And offset < 2475 Then
If Left = 1 Then scroll = scroll - 2
If Right = 1 Then scroll = scroll + 2
End If
If offset = 0 Then
If Right = 1 And scroll < 31 Then scroll = scroll + 2
End If
If offset = 2475 Then
If Left = 1 And scroll > 0 Then scroll = scroll - 2
End If
If offset < 0 Then offset = 0
If offset > 2475 Then offset = 2475
If scroll > 31 Then
scroll = scroll - 32
offset = offset + 1
End If
If scroll < 0 Then
scroll = scroll + 32
offset = offset - 1
End If
End If
playerU = playerU * friction
playerZ = playerZ - gravity
playerX = playerX + playerU
playerY = playerY - playerZ
;-----------------------------------------------------------------------------
; DRAW SCREEN AND CHECK FOR PLAYER COLLISION
;-----------------------------------------------------------------------------
nomove = 0
For x=0 To 25
For y=0 To 15
If playfield(level,offset+x,y) >0 Then
DrawImage tileImg,x*32-scroll,y*32,playfield(level,offset+x,y)
If ImagesCollide(tileImg,x*32-scroll,y*32,playfield(level,offset+x,y),playerImg,playerX,playerY,0) Then nomove = 1
End If
Next
Next
If nomove = 1 Then
; playerX = playerX - playerU
playerY = playerY + playerZ
; playerU = 0
playerZ = 0
End If
If playerY < 0 Then playerY = 0
If playerY > 480 Then playerY = 480
If playerX > 768 Then playerX = 768
If playerX < 0 Then playerX = 0
DrawImage playerImg,playerX,playerY
Flip
Cls
Wend
FreeImage titleImg
FreeImage playerImg
End
;-----------------------------------------------------------------------------
; LOAD LEVEL INTO ARRAY
;-----------------------------------------------------------------------------
.loadLevel
filein = ReadFile("gianasisters.lvl")
;For lv = 0 To 99
For x = 0 To 2499
For y = 0 To 15
playfield(lv,x,y) = ReadInt(filein)
Next
Next
;Next
CloseFile(filein)
Return
[\code]