A Primer on AABB Collision Resolution - Deen Games
文章推薦指數: 80 %
AABB (Axis Aligned Bounding Boxes) means two non-rotated boxes, that are aligned on one axis. · Collsiion detection means detecting if there is a ...
Togglenavigation
Home
|
About
|
Games
Newsletter
Discord
Categories:
Devlog|
GameDesign|
GameDevelopment|
News|
Retrospective|
Technical|
WebDevelopment|
APrimeronAABBCollisionResolution
Technical
·
Physics
·
2020-01-30
·
nightblade
ThisblogpostincludesadiscussionaboutAABBcollisionresolution:whatitisandisn't,it'sstrengthsandweaknesses,somecommonpitfalls,andhowyoucan(hopefully)implementitinyourlow-levelgamingtoolofchoice,ifneeded.
Ilearnedallthis(thesecondtime)aspartofaddingfast/stablecollisionresolutiontoPuffin,afast,lightweight2DgameenginebuiltontopofMonoGame.
AABB,CollisionDetection,andCollisionResolution
Somequickdefinitionstostart:
AABB(AxisAlignedBoundingBoxes)meanstwonon-rotatedboxes,thatarealignedononeaxis.In2D,everythingonthesame"layer"or"z"isonthesameaxis;in3D,itmeansyourboxesareonthesameplane.
Collsiiondetectionmeansdetectingifthereisacollision(twoAABBsoverlapping)orwillbeacollision(eg.in0.3sthesetwowillstartcolliding).
Collisionresolutionmeansresolvingthecollision.Broadly,therearetwoapproachestothis:preventionorpre-collisionresolution(stopjustattheonsetofcollisionwhenthetwoAABBstouch),andpost-collisionresolution(oncetwoAABBsoverlap,movethembackwardintimeuntiltheynolongeroverlap).
MyapproachtoAABBusespre-collisionresolution,becauseittendstobelesscomplexandmorestable.
ProsandConsofAABB
WhyshouldyouuseAABBcollisionresolution?Therearemanyotheroptions,suchascollisionpoints,sphere/linecollsionalgorithms,polygons,etc.
ThestrengthsofAABBinclude:
Itworkswellinmostcases.Mostgamescandowellenoughwithjustboundingboxesontheirentities.
It'srelativelysimpletocode(math-wise),becauseit'sjustboxes.
It'squitecheapcomputationally(eg.doesn'thaveanexpensivesquare-rootcalculation,unlikesphericalchecks)
However,itincludessomedrawbacks:
Itdoesn'tworkwithrotatedboxes
Itdoesn'tworkwellwithnon-boxshapes
Itrequiresextraworkforittoworkwellwithmulti-entitycollisions
It'ssucceptibleto"tunnelling"(high-speedobjectsmovethroughsolidobjectsbecauseoftheirvelocity)
Ifyoucanlivewiththoselimitations,IrecommendAABB,primarilybecauseitiscomputationallycheap(workswellwithahighnumberofcollidingentities).
CollisionResolutionisComplex,likePhysics
WhileAABBcollisionresolutionisrelativelyeasiertocode,itdoesn'tmeanit'seasytocode.Manygameframeworksdon'tincludecollisionresolution,becausethisispartofthephysicsengine.
Readthatagain:it'softenpartofthephysicsengine.Physicsenginesarenotoriouslydifficulttogetright,andrequirelotsoffiddlingandcorner-caseevaluation.Evenhigh-qualityphysicsengineshavelimitations,suchastunneling.
Ittookmearound10hourstodiscoverallthecaveatsandgetthistoworkright.Anditworkswell,includingwithmulti-entitycollisions.Testthoroughly.
Thatsaid,myimplementationincludesafewbonusfeatures:
It'sresistanttocollisiontunneling(butnotimpervious)
Itworkswithmultipleobjectscollidingatthesametime
Itallowsanobjecttooptionally"collideandslide"alongtheobjectitcollideswith
Withthatoutoftheway,let'sdiveintotheactualtheoryofhowtomakeastableAABBresolution,andthensomecode.
High-LevelDescriptionofAABB
AABBcollisionresolutionworksbylookingattheXandYcomponentresolutionsofyourvelocity.Simplyput:
Considertheintendeddestinationofyourmovingentity(whereitwillbeafterupdatingitsposition,notwhereitisnow)
LookatthedistancedxtotravelbeforewecollideontheX-axisanddyfortheY-axis
DividethesebyyourcomponentX-velocityandY-velocityrespectively(vxandvy)tofigureouthowlongbeforeeachaxiscollisiontakesplace(txandty)
Resolvethecollisionontheaxisthatcollidesfirst
Thisexcellentdiagram(credit:LaroLaroonGameDev.SE)showsamovingobject(A)thatwillcollidewithasecondobject(B).Basedonthecomponentvelocities,youcanseefromtheprojectedAboxthatthefastercollisionwillbeontheY-axisfirst.
Becausecollisionresolutiontakesplaceonasingleaxisatatime,youmayenduphavingtoresolvethesamecollisionmultipletimestogetastableresolution.Ifindthatrunningthecollisionresolutiontwicesuffices.
AndNow,theCode
Below,Idiscusssomepseudocodethat'salmostthesameastheactual(C#)codefromPuffin.Thesamecodecanapplytoanyprogramminglanguageorframework.
OneunorthodoximplementationdetailIused:wheneachentitymoves,Imakeanoteoftheir"intended"destinationX/Y.Ifthatlocationwouldcauseittocollideinsideanobject,Iinsteadupdateitsoitstopsjustatthepointofcollision.Inmypseudocodebelow,you'llseethisasintendedXandintendedY.
Foreverycollidableentity,you'regoingtocompareittoeveryothercollidableentity.Sincewe'reusingAABBs,thisisprettysimple:justcomparethecoordinatesplusintendedmovementofthemovingentity,againsttheentitythatisn'tmoving:
privatestaticboolisAabbCollision(floatx1,floaty1,intw1,inth1,floatx2,floaty2,intw2,inth2)
{
//Adaptedfromhttps://tutorialedge.net/gamedev/aabb-collision-detection-tutorial/
returnx1
延伸文章資訊
- 12D collision detection - Game development - MDN Web Docs
One of the simpler forms of collision detection is between two rectangles that are axis aligned —...
- 2Collision detection - LearnOpenGL
Because the ball is a circle-like object, an AABB is probably not the best choice for the ball's ...
- 3Using Swept AABB to detect and process collision
AABB stands for Axis-Aligned Bounding Box, it is an algorithm to detect collision between a recta...
- 4AABB Collision Detection Tutorial - TutorialEdge.net
AABB Collision Detection or "Axis-Aligned Bounding Box" Collision detection as it stands for is t...
- 5What is AABB - Collision detection? - Stack Overflow
AABB stands for "Axis-Aligned Bounding Box." It is a fairly computationally- and memory-efficient...