2D Platformer collisions

BlitzMax Forums/BlitzMax Beginners Area/2D Platformer collisions

Uber Lieutenant(Posted 2005) [#1]
I'm working on a side-view 2D platformer and I'm having trouble getting collisions between the player and the level to work. Reading through both BlitzBasic and BlitzCoder, there are plenty of examples of 2D platform code that can easily be ported but don't help. The player is a PNG file and so is the level, and I can't get them to collide properly. Here's the source I'm working with (that actually works to an extent):

While Not KeyDown(KEY_ESCAPE)
	
	Cls
	ResetCollisions
	Update()

	DrawImage(lyr3,lx,ly,0)

	DrawImage(lyr2,lx,ly,0)
	DrawImage(plyr1,lx+X,ly+Y)

	DrawImage(lyr1,lx,ly)
	
	Flip
	FlushMem

Wend

EndGraphics()
End


Function Update()

	If KeyDown(KEY_D)
		Xvel = 2		
	ElseIf KeyDown(KEY_A)
		Xvel = -2
	Else
		Xvel = 0
	EndIf

	If KeyHit(KEY_F) Then Falling = True
	
	If ImagesCollide(plyr1,X,Y+1,0,lyr2,lx,ly,0) = False
		Falling = True
		FallSpeed:+accel
		Yvel:+FallSpeed
	Else	
		Yvel = 0
		Falling = False
		FallSpeed = grav
	EndIf
	
	If KeyHit(KEY_SPACE) And Yvel = 0
		Yvel = -18	
	EndIf
	
	X:+Xvel
	Y:+Yvel

End Function
The first layer is the foreground drawn on top of everything, the third is the background drawn first. The second layer is a simple image comprised only of pure red pixels and some heavily dark yellow pixels (for ground), simple so there's no messy problems occuring in collision checks. The player is an alpha-blended 32-bit PNG.

How can I do the player collisions properly? (btw this is all temporary code, reason why I don't have object methods and functions)


Curtastic(Posted 2005) [#2]
this would be the code you are looking for :)
It will run by itself, but it is blitzbasic code
;Use the arrow keys

Graphics 640, 480, 0, 2
SetBuffer BackBuffer()

;how fast things fall
Const gravity# = .4


;make the level
Global level_image=CreateImage(500,400)
SetBuffer ImageBuffer(level_image);draw to level_image
Line 200,200,230,300
Rect 300,300,40,20
Rect 0,0,500-1,400-1,0
Oval 30,350,20,50
Line 420,340,500,340
Rect 333,122,30,40
Rect 100,200,40,5
Rect 40,150,10,40
Rect 470,280,30,30
SetBuffer BackBuffer()




;setup you

;where you are drawn
you_draw_x = GraphicsWidth()/2
you_draw_y = GraphicsHeight()/2
;how fast you are currently moving
you_speed_x# = 0
you_speed_y# = 0
;where the topleft of the screen is
screen_x# = 50
screen_y# = 50
;where you are in the level
you_x# = 0
you_y# = 0
;size of your collision rectangle
you_size_x = 20
you_size_y = 41




;##################################################
;MAIN LOOP
;##################################################

Repeat

	If KeyHit(1) Then End

	;move
	you_speed_x = 0
	If KeyDown(203) Then you_speed_x = -3
	If KeyDown(205) Then you_speed_x = +3

	;fall
	you_speed_y = you_speed_y + gravity

	;jump
	If KeyHit(200) Then
		;you must be on the ground to jump
		If ImageRectCollide(level_image,0,0,0, you_x, you_y + 1, you_size_x, you_size_y) Then
			you_speed_y =  - 8
		EndIf
	EndIf

	;stop speed if going to collide
	If ImageRectCollide(level_image,0,0,0, you_x + you_speed_x, you_y, you_size_x, you_size_y) Then
		you_speed_x = 0
	EndIf
	;move
	screen_x = screen_x + you_speed_x


	;stop speed if going to collide
	If ImageRectCollide(level_image,0,0,0, you_x, you_y + you_speed_y, you_size_x, you_size_y) Then
		you_speed_y = 0
	EndIf
	;move
	screen_y = screen_y + you_speed_y

	
	;screen_pos is at the top-left of the screen.
	;so add your_draw_pos to it and that is where you are in the level
	you_x = screen_x + you_draw_x
	you_y = screen_y + you_draw_y


	;DRAW LEVEL
	DrawImage level_image,-screen_x,-screen_y


	;draw you
	Color 0, 255, 0
	Rect you_draw_x, you_draw_y, you_size_x + 1, you_size_y + 1

	Flip
	Cls
Forever



Uber Lieutenant(Posted 2005) [#3]
This helps, thanks!