Collision...

BlitzMax Forums/BlitzMax Beginners Area/Collision...

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.



SuperStrict

Graphics(400,300,0)

Local x1%=10,y1%=10,x2%=200,y2%=200
Local w1%=15,h1%=10,w2%=20,h2%=30
Local dirup%,dirdn%,dirleft%,dirright%
Local CollideCheck:CollideResultType = New CollideResultType

Const MoveSpeed:Int = 10

Type CollideResultType
	Field X:Int,Y:Int,Collide:Byte
End Type

While Not KeyHit(KEY_ESCAPE)

	SetColor(255,255,255)
	DrawRect(x1,y1,w1,h1)
	dirup=0
	dirdn=0
	dirleft=0
	dirright=0
	SetColor(255,255,0)
	DrawRect(x2,y2,w2,h2)
	If KeyDown(KEY_UP) Then 
		dirup=1
	End If
	If KeyDown(KEY_DOWN) Then 
		dirdn=1
	End If
	If KeyDown(KEY_LEFT) Then 
		dirleft=1
	End If
	If KeyDown(KEY_RIGHT) Then 
		dirright=1
	End If
	
	CollideCheck = RectsOverlap(x1,y1,w1,h1,x2,y2,w2,h2,movespeed,dirup,dirdn,dirleft,dirright)
	
	x1 = CollideCheck.x
	y1 = CollideCheck.y
	
	Flip;Cls;ResetCollisions
	
Wend

End



Function RectsOverlap:CollideResultType (x1%,y1%,w1%,h1%,x2%,y2%,w2%,h2%,move_speed%,up:Byte,down:Byte,leftt:Byte,rightt:Byte)
	Local Collided:CollideResultType = New CollideResultType
	collided.x=x1
	Collided.y=y1
	
	If Rightt
		If x1+w1<=x2 And y1+h1>=y2 And Y1 <=y2+h2 Then 
			If x1+w1+move_speed >= x2
				Collided.X = x2-w1 
				Collided.Collide = True
			End If
		End If
	End If
	
	If leftt
		If x1>=x2+w2 And y1+h1>=y2 And Y1 <=y2+h2 Then 
			If x1-move_speed <= x2+w2
				Collided.X = x2+w2
				Collided.Collide = True
			End If
		End If
	End If
	
	If down
		If y1+h1<=y2 And x1+w1>=x2 And x1 <=x2+w2 Then 
			If y1+h1+move_speed >= y2
				Collided.y = y2-h1 
				Collided.Collide = True
			End If
		End If
	End If
	
	If up
		If y1>=y2+h2 And x1+w1>=x2 And x1 <=x2+w2 Then 
			If y1-move_speed <= y2+h2
				Collided.Y = y2+h2
				Collided.Collide = True
			End If
		End If
	End If

	If Not Collided.Collide
		If rightt Then Collided.X = X1 + move_speed
		If leftt Then Collided.X = X1-move_speed
		If up Then collided.Y = y1-move_speed
		If down Then collided.Y = y1+move_speed
		Collided.Collide = False
	End If
		
	Return Collided

End Function



Fixed, sorry.