Using Swept AABB to detect and process collision
文章推薦指數: 80 %
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
延伸文章資訊
- 12D collision detection - Game development - MDN Web Docs
One of the simpler forms of collision detection is between two rectangles that are axis aligned —...
- 2A Primer on AABB Collision Resolution - Deen Games
AABB (Axis Aligned Bounding Boxes) means two non-rotated boxes, that are aligned on one axis. · C...
- 3Collision detection - LearnOpenGL
Because the ball is a circle-like object, an AABB is probably not the best choice for the ball's ...
- 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...