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(600,480,0)

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

Local OldX:Int,OldY:Int,XDif:Int,YDif:Int

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
	xdif=0
	ydif=0
	
	SetColor(255,255,0)
	DrawRect(x2,y2,w2,h2)
	
	xdif = MouseX()-oldX
	
	If MouseX() > oldx Then
		dirright=1
	ElseIf MouseX() < oldx
		dirleft=1
	End If
	
	ydif = MouseY()-oldy
	
	If MouseY() > oldy Then
		dirdn=1
	ElseIf MouseY() < oldy
		dirup=1
	End If
		
	CollideCheck = RectsOverlap(x1,y1,w1,h1,x2,y2,w2,h2,dirup,dirdn,dirleft,dirright,xdif,ydif)
	
	x1 = CollideCheck.x
	y1 = CollideCheck.y
	OldX = x1
	OldY = y1
	Flip;Cls;ResetCollisions
	
Wend

End



Function RectsOverlap:CollideResultType (x1%,y1%,w1%,h1%,x2%,y2%,w2%,h2%,up:Byte,down:Byte,leftt:Byte,rightt:Byte,xSpeed:Int,YSpeed:Int)
	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+xspeed >= 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+xspeed <= 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+yspeed >= 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+yspeed  <= y2+h2
				Collided.Y = y2+h2
				Collided.Collide = True
			End If
		End If
	End If

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

End Function



Something like that?