Using Swept AABB to detect and process collision

文章推薦指數: 80 %
投票人數:10人

AABB stands for Axis-Aligned Bounding Box, it is an algorithm to detect collision between a rectangle's edges, in this case, those edges are ...  Feb26,20192minreadUsingSweptAABBtodetectandprocesscollisionUpdated:Jul2,2021Level:VeryBeginnerWhatisAABB?AABBstandsforAxis-AlignedBoundingBox,itisanalgorithmtodetectcollisionbetweenarectangle’sedges,inthiscase,thoseedgesareparallelwithcoordinateaxes.Basically,wewillchecktworectanglesoverlapwitheachotherornot.WhatisSweptAABB?WewillhavesomeproblemsifweuseAABB,let’sseethisexample:Inthefirstframe,rectangle1doesn’tcollidewithabluerectangle.Andnextframe,therectangle1movetothenextposition,butvelocitytoofastandmakeitgothroughthebluerectangle.Ifwecheckcollisionatthisframe,rectangle1doesn’tcollidewiththeblueone.Let’sthinkifwecanpredictthenextpositioninthenextframe,wewillknowandpreventourobjectfromthisproblem.ThisiscalledSweptAABB.AABBCollisionTocheck2rectanglesareoverlappedeachotherornot,wewillcheckeachpairofcorrespondingedges.Wealwayshavethisatthesametime:other.left<=object.rightother.right>=object.leftother.top>=object.bottomother.bottom<=object.topIfoneoftheseconditionsiswrong,so2rectanglesaren’toverlapped.Inthisexample,wehave‘left’of‘other’greaterthan‘right’of‘object’.Noted:allexamplesinthisarticleusebottom-leftorigin.Thisisasimplefunctiontocheckcollision:Wecaninvertconditionstocheckfaster.UsingSweptFirst,wecalculatethedistancebetweenthecorrespondingedges.Inthis,dxEntry,dyEntry:thedistanceneedtomovetobegincontactdxExit,dyExit:thedistanceneedtomovetoexitcontactWealsoneedtocheckthesignofvelocityandcalculatethedistanceineachcaseNext,fromdistanceandvelocity,wecancalculatethetime||time=distance/velocityInordertocollide,bothaxesxandyneedtogetcollided.So,wewilltakethelongesttimetobegincollision.Andwhenendcollision,weonlyneedoneoftheaxesexitcollision,sowewilltakethequickesttimetoexitcollision.Wecancheckcollisionby:timetobegincollisionfrom0to1timetoendcollisionmustbebiggerthantimetobegincollision.Finally,wewillreturntimetobeginacollision.HandlecollisionWecanuseittocalculatethedistanceneedtomovetothenextframebasedonthereturnedtime.Broad-PhasingTooptimize,wecandoonemorestepbyusingarectanglemadefromthepositionatthecurrentframeandthenextframetocheckcollisionwithanotherobject.WeputthiscodeatthetopofSweptAABBfunction.DetectcollisiondirectionThat’sit!Thisalgorithmisverysimple,right?IhopethisbasicconceptcanhelpyouinyourprojectandmovetothemoredifficultalgorithmslikeSAT(SeparatingAxisTheorem).ByLuuTheVinhProductDeveloperofAmanotesGames0views0commentsPostnotmarkedaslikedCategoriesAllPosts(38)38postsGames(11)11postsMusic(9)9postsAmanotesPeople(7)7postsAboutUs(1)1postNews(12)12postsEvents(6)6postsMusicMobile-GamesAppspublishingArtistCollaborationblogmagictiles3PartnershipPrototypesAppStoreApp-Anniebeatblader3dbrandsCorporateCSRDeconstructionhaiphuthonIdeationiOSkpopLifeatAmanotesmarketingmilestonemobile-gameMonetizationMonstercatmusiceducationmusicgamingconOptimizationPeoplepháothefatrattopchartvirtualeventWebinarygTagsArchiveJune2022(1)1postMay2022(2)2postsApril2022(1)1postMarch2022(2)2postsJanuary2022(2)2postsDecember2021(1)1postNovember2021(2)2postsOctober2021(1)1postSeptember2021(1)1postAugust2021(1)1postMarch2021(1)1postFebruary2021(1)1postBlog HOMEPRODUCTSCAREERSBLOGFAQCONTACT 



請為這篇文章評分?