Jump to content

Basic simple AABB swept collisions


Aily
 Share

Recommended Posts

Long time fighting fith charachter controller don't give good result for my Last Chapter platformer.

So Arbuz give me a good idea - make my own AABB collisions engine, because Last Chapter in fact have only box bodies.

 

Hahaha!!!! Now with this i can make movable platforms, accurate to metall attachement player and much more!

 

This is AABB collisions base, maybe someone will be make such platformer :P

 


Global bodies:TList=CreateList()

Type body

Field x#,y# ' aa
Field w#,h# ' bb

Field width#
Field height#

End Type

Function add_body:body(x#,y#,width#,height#)

Local b:body=New body

b.x=x-width/2
b.y=y-height/2
b.width=width
b.height=height
b.w=b.x+width
b.h=b.y+height

ListAddLast bodies,b

Return b

End Function

Function render_bodies()

For b:body=EachIn bodies
	SetColor 255,255,255
	DrawRect b.x,b.y,b.width,b.height
	SetColor 50,50,50
	DrawRect b.x+1,b.y+1,b.width-2,b.height-2
Next

End Function

Function ol(pv#,cv#,nv#)
Return cv>pv And cv<nv
End Function


Function move_body(b:body,x#,y#)

nx#=b.x+x
ny#=b.y+y
nw#=b.w+x
nh#=b.h+y
fx#=x
fy#=y

For bb:body=EachIn bodies
	If bb=b Continue
	If fx>0
		If ol(bb.x,nw,bb.w) Or ol(b.x,bb.x,nw)
			If ol(bb.y,ny,bb.h)	fx=bb.x-b.w
			If ol(bb.y,nh,bb.h)	fx=bb.x-b.w
			If ol(ny,bb.y,nh)	fx=bb.x-b.w
			If ol(ny,bb.h,nh)	fx=bb.x-b.w
			If bb.y=b.y And b.h=bb.h fx=bb.x-b.w
		End If
	End If
	If fx<0
		If ol(bb.x,nx,bb.w) Or ol(nx,bb.w,b.w)
			If ol(bb.y,ny,bb.h)	fx=bb.w-b.x
			If ol(bb.y,nh,bb.h)	fx=bb.w-b.x
			If ol(ny,bb.y,nh)	fx=bb.w-b.x
			If ol(ny,bb.h,nh)	fx=bb.w-b.x
			If bb.y=b.y And b.h=bb.h fx=bb.w-b.x
		End If
	End If
	If fy>0
		If ol(bb.y,nh,bb.h) Or ol(b.y,bb.y,nh)
			If ol(bb.x,nx,bb.w)	fy=bb.y-b.h
			If ol(bb.x,nw,bb.w)	fy=bb.y-b.h
			If ol(nx,bb.x,nw)	fy=bb.y-b.h
			If ol(nx,bb.w,nw)	fy=bb.y-b.h
			If bb.x=b.x And b.w=bb.w fy=bb.y-b.h
		End If
	End If
	If fy<0
		If ol(bb.y,ny,bb.h) Or ol(ny,bb.h,b.h)
			If ol(bb.x,nx,bb.w)	fy=bb.h-b.y
			If ol(bb.x,nw,bb.w)	fy=bb.h-b.y
			If ol(nx,bb.x,nw)	fy=bb.h-b.y
			If ol(nx,bb.w,nw)	fy=bb.h-b.y
			If bb.x=b.x And b.w=bb.w fy=bb.h-b.y
		End If
	End If
Next

b.x=b.x+fx
b.y=b.y+fy
b.w=b.w+fx
b.h=b.h+fy

End Function

Graphics 800,600

add_body(410,160,20,240)
add_body(50,70,20,140)
add_body(50,210,20,140)
add_body(230,290,340,20)
add_body(130,150,20,100)
add_body(280,170,120,20)

player=add_body(310,260,20,40)

Repeat

If KeyHit(KEY_escape) Or AppTerminate() End

shift=KeyDown(key_lshift)*200

If KeyDown(key_left) move_body player,-2-shift,0
If KeyDown(key_right) move_body player,2+shift,0
If KeyDown(key_up) move_body player,0,-2-shift
If KeyDown(key_down) move_body player,0,2+shift

Cls
render_bodies
Flip 1

Forever

 

Here's compiled EXE, arrows-for move, shift - some speedup movable body (check swept collisions)

aabb_collisions.rar

post-260-0-30616100-1324691141_thumb.jpg

"Better" is big enemy of "good"

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...