| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517 |
- // MonoGame - Copyright (C) MonoGame Foundation, Inc
- // This file is subject to the terms and conditions defined in
- // file 'LICENSE.txt', which is part of this source code package.
- using System;
- using Microsoft.Xna.Framework;
- namespace MonoGame.Extended.Tests
- {
- public sealed class Collision2DTest
- {
- #region Helper Methods
- #region IsValidPolygon Tests
- [Fact]
- public void IsValidPolygon_ValidPolygon_ReturnsTrue()
- {
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(1, 0),
- new Vector2(0, 1)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- Vector2.Normalize(new Vector2(1, 1)),
- new Vector2(-1, 0)
- };
- bool result = Collision2D.IsValidPolygon(vertices, normals);
- Assert.True(result);
- }
- [Fact]
- public void IsValidPolygon_NullVertices_ReturnsFalse()
- {
- var normals = new[] { Vector2.UnitX, Vector2.UnitY, -Vector2.UnitX };
- bool result = Collision2D.IsValidPolygon(null, normals);
- Assert.False(result);
- }
- [Fact]
- public void IsValidPolygon_NullNormals_ReturnsFalse()
- {
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(1, 0),
- new Vector2(0, 1)
- };
- bool result = Collision2D.IsValidPolygon(vertices, null);
- Assert.False(result);
- }
- [Fact]
- public void IsValidPolygon_MismatchedArrayLengths_ReturnsFalse()
- {
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(1, 0),
- new Vector2(0, 1)
- };
- var normals = new[]
- {
- Vector2.UnitX,
- Vector2.UnitY
- };
- bool result = Collision2D.IsValidPolygon(vertices, normals);
- Assert.False(result);
- }
- [Fact]
- public void IsValidPolygon_LessThanThreeVertices_ReturnsFalse()
- {
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(1, 0)
- };
- var normals = new[]
- {
- Vector2.UnitX,
- Vector2.UnitY
- };
- bool result = Collision2D.IsValidPolygon(vertices, normals);
- Assert.False(result);
- }
- #endregion
- #region ClipInterval Tests (ref overload)
- [Fact]
- public void ClipInterval_OverlappingIntervals_ReturnsTrue()
- {
- float tMin = 0.0f;
- float tMax = 10.0f;
- float clipMin = 3.0f;
- float clipMax = 8.0f;
- bool result = Collision2D.ClipInterval(ref tMin, ref tMax, clipMin, clipMax);
- Assert.True(result);
- Assert.Equal(3.0f, tMin);
- Assert.Equal(8.0f, tMax);
- }
- [Fact]
- public void ClipInterval_NonOverlappingIntervals_ReturnsFalse()
- {
- float tMin = 0.0f;
- float tMax = 5.0f;
- float clipMin = 10.0f;
- float clipMax = 15.0f;
- bool result = Collision2D.ClipInterval(ref tMin, ref tMax, clipMin, clipMax);
- Assert.False(result);
- }
- [Fact]
- public void ClipInterval_TouchingAtBoundary_ReturnsTrue()
- {
- float tMin = 0.0f;
- float tMax = 5.0f;
- float clipMin = 5.0f;
- float clipMax = 10.0f;
- bool result = Collision2D.ClipInterval(ref tMin, ref tMax, clipMin, clipMax);
- Assert.True(result);
- Assert.Equal(5.0f, tMin);
- Assert.Equal(5.0f, tMax);
- }
- [Fact]
- public void ClipInterval_ClippingWiderThanOriginal_KeepsOriginalBounds()
- {
- float tMin = 3.0f;
- float tMax = 7.0f;
- float clipMin = 0.0f;
- float clipMax = 10.0f;
- bool result = Collision2D.ClipInterval(ref tMin, ref tMax, clipMin, clipMax);
- Assert.True(result);
- Assert.Equal(3.0f, tMin);
- Assert.Equal(7.0f, tMax);
- }
- [Fact]
- public void ClipInterval_NegativeIntervals_WorksCorrectly()
- {
- float tMin = -10.0f;
- float tMax = -5.0f;
- float clipMin = -8.0f;
- float clipMax = -3.0f;
- bool result = Collision2D.ClipInterval(ref tMin, ref tMax, clipMin, clipMax);
- Assert.True(result);
- Assert.Equal(-8.0f, tMin);
- Assert.Equal(-5.0f, tMax);
- }
- #endregion
- #region ClipInterval Tests (out overload)
- [Fact]
- public void ClipIntervalOut_OverlappingIntervals_ReturnsClippedValues()
- {
- float tEnter = 0.0f;
- float tExit = 10.0f;
- float tLower = 3.0f;
- float tUpper = 8.0f;
- bool result = Collision2D.ClipInterval(tEnter, tExit, tLower, tUpper, out float clippedEnter, out float clippedExit);
- Assert.True(result);
- Assert.Equal(3.0f, clippedEnter);
- Assert.Equal(8.0f, clippedExit);
- }
- [Fact]
- public void ClipIntervalOut_NonOverlappingIntervals_ReturnsFalse()
- {
- float tEnter = 0.0f;
- float tExit = 5.0f;
- float tLower = 10.0f;
- float tUpper = 15.0f;
- bool result = Collision2D.ClipInterval(tEnter, tExit, tLower, tUpper, out float clippedEnter, out float clippedExit);
- Assert.False(result);
- }
- [Fact]
- public void ClipIntervalOut_TouchingAtBoundary_ReturnsTrue()
- {
- float tEnter = 0.0f;
- float tExit = 5.0f;
- float tLower = 5.0f;
- float tUpper = 10.0f;
- bool result = Collision2D.ClipInterval(tEnter, tExit, tLower, tUpper, out float clippedEnter, out float clippedExit);
- Assert.True(result);
- Assert.Equal(5.0f, clippedEnter);
- Assert.Equal(5.0f, clippedExit);
- }
- [Fact]
- public void ClipIntervalOut_ClippingWiderThanOriginal_ReturnsOriginal()
- {
- float tEnter = 3.0f;
- float tExit = 7.0f;
- float tLower = 0.0f;
- float tUpper = 10.0f;
- bool result = Collision2D.ClipInterval(tEnter, tExit, tLower, tUpper, out float clippedEnter, out float clippedExit);
- Assert.True(result);
- Assert.Equal(3.0f, clippedEnter);
- Assert.Equal(7.0f, clippedExit);
- }
- #endregion
- #region IntervalsOverlap Tests
- [Fact]
- public void IntervalsOverlap_Overlapping_ReturnsTrue()
- {
- float minA = 0.0f;
- float maxA = 10.0f;
- float minB = 5.0f;
- float maxB = 15.0f;
- bool result = Collision2D.IntervalsOverlap(minA, maxA, minB, maxB);
- Assert.True(result);
- }
- [Fact]
- public void IntervalsOverlap_Separated_ReturnsFalse()
- {
- float minA = 0.0f;
- float maxA = 5.0f;
- float minB = 10.0f;
- float maxB = 15.0f;
- bool result = Collision2D.IntervalsOverlap(minA, maxA, minB, maxB);
- Assert.False(result);
- }
- [Fact]
- public void IntervalsOverlap_TouchingAtPoint_ReturnsTrue()
- {
- float minA = 0.0f;
- float maxA = 5.0f;
- float minB = 5.0f;
- float maxB = 10.0f;
- bool result = Collision2D.IntervalsOverlap(minA, maxA, minB, maxB);
- Assert.True(result);
- }
- [Fact]
- public void IntervalsOverlap_NegativeIntervals_WorksCorrectly()
- {
- float minA = -10.0f;
- float maxA = -5.0f;
- float minB = -8.0f;
- float maxB = -3.0f;
- bool result = Collision2D.IntervalsOverlap(minA, maxA, minB, maxB);
- Assert.True(result);
- }
- [Fact]
- public void IntervalsOverlap_NearlyTouching_ReturnsFalse()
- {
- float minA = 0.0f;
- float maxA = 5.0f;
- float minB = 5.0f + Collision2D.Epsilon * 2.0f;
- float maxB = 10.0f;
- bool result = Collision2D.IntervalsOverlap(minA, maxA, minB, maxB);
- Assert.False(result);
- }
- #endregion
- #endregion
- #region Containment - AABB Methods
- #region ContainsAabbPoint Tests
- [Fact]
- public void ContainsAabbPoint_PointInside_ReturnsContains()
- {
- var point = new Vector2(5, 5);
- var min = new Vector2(0, 0);
- var max = new Vector2(10, 10);
- var result = Collision2D.ContainsAabbPoint(point, min, max);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsAabbPoint_PointOutside_ReturnsDisjoint()
- {
- var point = new Vector2(15, 5);
- var min = new Vector2(0, 0);
- var max = new Vector2(10, 10);
- var result = Collision2D.ContainsAabbPoint(point, min, max);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsAabbPoint_PointOnBoundary_ReturnsContains()
- {
- var point = new Vector2(10, 5);
- var min = new Vector2(0, 0);
- var max = new Vector2(10, 10);
- var result = Collision2D.ContainsAabbPoint(point, min, max);
- Assert.Equal(ContainmentType.Contains, result);
- }
- #endregion
- #region ContainsAabbAabb Tests
- [Fact]
- public void ContainsAabbAabb_CompletelyContains_ReturnsContains()
- {
- var aMin = new Vector2(0, 0);
- var aMax = new Vector2(10, 10);
- var bMin = new Vector2(2, 2);
- var bMax = new Vector2(8, 8);
- var result = Collision2D.ContainsAabbAabb(aMin, aMax, bMin, bMax);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsAabbAabb_NoOverlap_ReturnsDisjoint()
- {
- var aMin = new Vector2(0, 0);
- var aMax = new Vector2(10, 10);
- var bMin = new Vector2(15, 15);
- var bMax = new Vector2(20, 20);
- var result = Collision2D.ContainsAabbAabb(aMin, aMax, bMin, bMax);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsAabbAabb_PartialOverlap_ReturnsIntersects()
- {
- var aMin = new Vector2(0, 0);
- var aMax = new Vector2(10, 10);
- var bMin = new Vector2(5, 5);
- var bMax = new Vector2(15, 15);
- var result = Collision2D.ContainsAabbAabb(aMin, aMax, bMin, bMax);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsAabbCircle Tests
- [Fact]
- public void ContainsAabbCircle_CircleCompletelyInside_ReturnsContains()
- {
- var boxMin = new Vector2(0, 0);
- var boxMax = new Vector2(10, 10);
- var circleCenter = new Vector2(5, 5);
- var circleRadius = 2.0f;
- var result = Collision2D.ContainsAabbCircle(boxMin, boxMax, circleCenter, circleRadius);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsAabbCircle_CircleCompletelyOutside_ReturnsDisjoint()
- {
- var boxMin = new Vector2(0, 0);
- var boxMax = new Vector2(10, 10);
- var circleCenter = new Vector2(20, 20);
- var circleRadius = 3.0f;
- var result = Collision2D.ContainsAabbCircle(boxMin, boxMax, circleCenter, circleRadius);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsAabbCircle_CirclePartiallyInside_ReturnsIntersects()
- {
- var boxMin = new Vector2(0, 0);
- var boxMax = new Vector2(10, 10);
- var circleCenter = new Vector2(5, 5);
- var circleRadius = 7.0f;
- var result = Collision2D.ContainsAabbCircle(boxMin, boxMax, circleCenter, circleRadius);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsAabbObb Tests
- [Fact]
- public void ContainsAabbObb_ObbCompletelyInside_ReturnsContains()
- {
- var aabbMin = new Vector2(0, 0);
- var aabbMax = new Vector2(10, 10);
- var obbCenter = new Vector2(5, 5);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(2, 2);
- var result = Collision2D.ContainsAabbObb(aabbMin, aabbMax, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsAabbObb_ObbCompletelyOutside_ReturnsDisjoint()
- {
- var aabbMin = new Vector2(0, 0);
- var aabbMax = new Vector2(10, 10);
- var obbCenter = new Vector2(20, 20);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(2, 2);
- var result = Collision2D.ContainsAabbObb(aabbMin, aabbMax, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsAabbObb_ObbPartiallyInside_ReturnsIntersects()
- {
- var aabbMin = new Vector2(0, 0);
- var aabbMax = new Vector2(10, 10);
- var obbCenter = new Vector2(8, 8);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(5, 5);
- var result = Collision2D.ContainsAabbObb(aabbMin, aabbMax, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsAabbCapsule Tests
- [Fact]
- public void ContainsAabbCapsule_CapsuleCompletelyInside_ReturnsContains()
- {
- var boxMin = new Vector2(0, 0);
- var boxMax = new Vector2(10, 10);
- var capsuleA = new Vector2(3, 5);
- var capsuleB = new Vector2(7, 5);
- var capsuleRadius = 1.0f;
- var result = Collision2D.ContainsAabbCapsule(boxMin, boxMax, capsuleA, capsuleB, capsuleRadius);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsAabbCapsule_CapsuleCompletelyOutside_ReturnsDisjoint()
- {
- var boxMin = new Vector2(0, 0);
- var boxMax = new Vector2(10, 10);
- var capsuleA = new Vector2(15, 15);
- var capsuleB = new Vector2(20, 20);
- var capsuleRadius = 1.0f;
- var result = Collision2D.ContainsAabbCapsule(boxMin, boxMax, capsuleA, capsuleB, capsuleRadius);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsAabbCapsule_CapsulePartiallyInside_ReturnsIntersects()
- {
- var boxMin = new Vector2(0, 0);
- var boxMax = new Vector2(10, 10);
- var capsuleA = new Vector2(5, 5);
- var capsuleB = new Vector2(15, 5);
- var capsuleRadius = 1.0f;
- var result = Collision2D.ContainsAabbCapsule(boxMin, boxMax, capsuleA, capsuleB, capsuleRadius);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsAabbConvexPolygon Tests
- [Fact]
- public void ContainsAabbConvexPolygon_PolygonCompletelyInside_ReturnsContains()
- {
- var boxMin = new Vector2(0, 0);
- var boxMax = new Vector2(10, 10);
- var vertices = new[]
- {
- new Vector2(3, 3),
- new Vector2(7, 3),
- new Vector2(7, 7),
- new Vector2(3, 7)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsAabbConvexPolygon(boxMin, boxMax, vertices, normals);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsAabbConvexPolygon_PolygonCompletelyOutside_ReturnsDisjoint()
- {
- var boxMin = new Vector2(0, 0);
- var boxMax = new Vector2(10, 10);
- var vertices = new[]
- {
- new Vector2(15, 15),
- new Vector2(20, 15),
- new Vector2(20, 20),
- new Vector2(15, 20)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsAabbConvexPolygon(boxMin, boxMax, vertices, normals);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsAabbConvexPolygon_PolygonPartiallyInside_ReturnsIntersects()
- {
- var boxMin = new Vector2(0, 0);
- var boxMax = new Vector2(10, 10);
- var vertices = new[]
- {
- new Vector2(5, 5),
- new Vector2(15, 5),
- new Vector2(15, 15),
- new Vector2(5, 15)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsAabbConvexPolygon(boxMin, boxMax, vertices, normals);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #endregion
- #region Containment - Circle Methods
- #region ContainsCirclePoint Tests
- [Fact]
- public void ContainsCirclePoint_PointInside_ReturnsContains()
- {
- var point = new Vector2(3, 0);
- var center = new Vector2(0, 0);
- var radius = 5.0f;
- var result = Collision2D.ContainsCirclePoint(point, center, radius);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsCirclePoint_PointOutside_ReturnsDisjoint()
- {
- var point = new Vector2(10, 0);
- var center = new Vector2(0, 0);
- var radius = 5.0f;
- var result = Collision2D.ContainsCirclePoint(point, center, radius);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsCirclePoint_PointOnCircumference_ReturnsContains()
- {
- var point = new Vector2(5, 0);
- var center = new Vector2(0, 0);
- var radius = 5.0f;
- var result = Collision2D.ContainsCirclePoint(point, center, radius);
- Assert.Equal(ContainmentType.Contains, result);
- }
- #endregion
- #region ContainsCircleAabb Tests
- [Fact]
- public void ContainsCircleAabb_AabbCompletelyInside_ReturnsContains()
- {
- var circleCenter = new Vector2(10, 10);
- var circleRadius = 10.0f;
- var aabbMin = new Vector2(8, 8);
- var aabbMax = new Vector2(12, 12);
- var result = Collision2D.ContainsCircleAabb(circleCenter, circleRadius, aabbMin, aabbMax);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsCircleAabb_AabbCompletelyOutside_ReturnsDisjoint()
- {
- var circleCenter = new Vector2(0, 0);
- var circleRadius = 5.0f;
- var aabbMin = new Vector2(20, 20);
- var aabbMax = new Vector2(25, 25);
- var result = Collision2D.ContainsCircleAabb(circleCenter, circleRadius, aabbMin, aabbMax);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsCircleAabb_AabbPartiallyInside_ReturnsIntersects()
- {
- var circleCenter = new Vector2(0, 0);
- var circleRadius = 5.0f;
- var aabbMin = new Vector2(3, 3);
- var aabbMax = new Vector2(8, 8);
- var result = Collision2D.ContainsCircleAabb(circleCenter, circleRadius, aabbMin, aabbMax);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsCircleCircle Tests
- [Fact]
- public void ContainsCircleCircle_CompletelyContains_ReturnsContains()
- {
- var aCenter = new Vector2(0, 0);
- var aRadius = 10.0f;
- var bCenter = new Vector2(2, 0);
- var bRadius = 3.0f;
- var result = Collision2D.ContainsCircleCircle(aCenter, aRadius, bCenter, bRadius);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsCircleCircle_Separated_ReturnsDisjoint()
- {
- var aCenter = new Vector2(0, 0);
- var aRadius = 5.0f;
- var bCenter = new Vector2(20, 0);
- var bRadius = 3.0f;
- var result = Collision2D.ContainsCircleCircle(aCenter, aRadius, bCenter, bRadius);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsCircleCircle_PartialOverlap_ReturnsIntersects()
- {
- var aCenter = new Vector2(0, 0);
- var aRadius = 5.0f;
- var bCenter = new Vector2(7, 0);
- var bRadius = 4.0f;
- var result = Collision2D.ContainsCircleCircle(aCenter, aRadius, bCenter, bRadius);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsCircleObb Tests
- [Fact]
- public void ContainsCircleObb_ObbCompletelyInside_ReturnsContains()
- {
- var circleCenter = new Vector2(10, 10);
- var circleRadius = 10.0f;
- var obbCenter = new Vector2(10, 10);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(2, 2);
- var result = Collision2D.ContainsCircleObb(circleCenter, circleRadius, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsCircleObb_ObbCompletelyOutside_ReturnsDisjoint()
- {
- var circleCenter = new Vector2(0, 0);
- var circleRadius = 5.0f;
- var obbCenter = new Vector2(20, 20);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(2, 2);
- var result = Collision2D.ContainsCircleObb(circleCenter, circleRadius, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsCircleObb_ObbPartiallyInside_ReturnsIntersects()
- {
- var circleCenter = new Vector2(0, 0);
- var circleRadius = 5.0f;
- var obbCenter = new Vector2(4, 4);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(3, 3);
- var result = Collision2D.ContainsCircleObb(circleCenter, circleRadius, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsCircleCapsule Tests
- [Fact]
- public void ContainsCircleCapsule_CapsuleCompletelyInside_ReturnsContains()
- {
- var circleCenter = new Vector2(10, 10);
- var circleRadius = 10.0f;
- var capsuleA = new Vector2(8, 10);
- var capsuleB = new Vector2(12, 10);
- var capsuleRadius = 1.0f;
- var result = Collision2D.ContainsCircleCapsule(circleCenter, circleRadius, capsuleA, capsuleB, capsuleRadius);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsCircleCapsule_CapsuleCompletelyOutside_ReturnsDisjoint()
- {
- var circleCenter = new Vector2(0, 0);
- var circleRadius = 5.0f;
- var capsuleA = new Vector2(20, 20);
- var capsuleB = new Vector2(25, 25);
- var capsuleRadius = 1.0f;
- var result = Collision2D.ContainsCircleCapsule(circleCenter, circleRadius, capsuleA, capsuleB, capsuleRadius);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsCircleCapsule_CapsulePartiallyInside_ReturnsIntersects()
- {
- var circleCenter = new Vector2(0, 0);
- var circleRadius = 5.0f;
- var capsuleA = new Vector2(3, 0);
- var capsuleB = new Vector2(10, 0);
- var capsuleRadius = 1.0f;
- var result = Collision2D.ContainsCircleCapsule(circleCenter, circleRadius, capsuleA, capsuleB, capsuleRadius);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsCircleConvexPolygon Tests
- [Fact]
- public void ContainsCircleConvexPolygon_PolygonCompletelyInside_ReturnsContains()
- {
- var circleCenter = new Vector2(10, 10);
- var circleRadius = 10.0f;
- var vertices = new[]
- {
- new Vector2(9, 9),
- new Vector2(11, 9),
- new Vector2(11, 11),
- new Vector2(9, 11)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsCircleConvexPolygon(circleCenter, circleRadius, vertices, normals);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsCircleConvexPolygon_PolygonCompletelyOutside_ReturnsDisjoint()
- {
- var circleCenter = new Vector2(0, 0);
- var circleRadius = 5.0f;
- var vertices = new[]
- {
- new Vector2(20, 20),
- new Vector2(25, 20),
- new Vector2(25, 25),
- new Vector2(20, 25)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsCircleConvexPolygon(circleCenter, circleRadius, vertices, normals);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsCircleConvexPolygon_PolygonPartiallyInside_ReturnsIntersects()
- {
- var circleCenter = new Vector2(0, 0);
- var circleRadius = 5.0f;
- var vertices = new[]
- {
- new Vector2(3, 3),
- new Vector2(8, 3),
- new Vector2(8, 8),
- new Vector2(3, 8)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsCircleConvexPolygon(circleCenter, circleRadius, vertices, normals);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #endregion
- #region Containment - OBB Methods
- #region ContainsObbPoint Tests
- [Fact]
- public void ContainsObbPoint_PointInside_ReturnsContains()
- {
- var point = new Vector2(5, 5);
- var center = new Vector2(5, 5);
- var axisX = Vector2.UnitX;
- var axisY = Vector2.UnitY;
- var halfExtents = new Vector2(3, 3);
- var result = Collision2D.ContainsObbPoint(point, center, axisX, axisY, halfExtents);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsObbPoint_PointOutside_ReturnsDisjoint()
- {
- var point = new Vector2(15, 15);
- var center = new Vector2(5, 5);
- var axisX = Vector2.UnitX;
- var axisY = Vector2.UnitY;
- var halfExtents = new Vector2(3, 3);
- var result = Collision2D.ContainsObbPoint(point, center, axisX, axisY, halfExtents);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsObbPoint_PointOnEdge_ReturnsContains()
- {
- var point = new Vector2(8, 5);
- var center = new Vector2(5, 5);
- var axisX = Vector2.UnitX;
- var axisY = Vector2.UnitY;
- var halfExtents = new Vector2(3, 3);
- var result = Collision2D.ContainsObbPoint(point, center, axisX, axisY, halfExtents);
- Assert.Equal(ContainmentType.Contains, result);
- }
- #endregion
- #region ContainsObbObb Tests
- [Fact]
- public void ContainsObbObb_CompletelyContains_ReturnsContains()
- {
- var aCenter = new Vector2(5, 5);
- var aAxisX = Vector2.UnitX;
- var aAxisY = Vector2.UnitY;
- var aHalf = new Vector2(5, 5);
- var bCenter = new Vector2(5, 5);
- var bAxisX = Vector2.UnitX;
- var bAxisY = Vector2.UnitY;
- var bHalf = new Vector2(2, 2);
- var result = Collision2D.ContainsObbObb(aCenter, aAxisX, aAxisY, aHalf, bCenter, bAxisX, bAxisY, bHalf);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsObbObb_Separated_ReturnsDisjoint()
- {
- var aCenter = new Vector2(0, 0);
- var aAxisX = Vector2.UnitX;
- var aAxisY = Vector2.UnitY;
- var aHalf = new Vector2(2, 2);
- var bCenter = new Vector2(20, 20);
- var bAxisX = Vector2.UnitX;
- var bAxisY = Vector2.UnitY;
- var bHalf = new Vector2(2, 2);
- var result = Collision2D.ContainsObbObb(aCenter, aAxisX, aAxisY, aHalf, bCenter, bAxisX, bAxisY, bHalf);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsObbObb_PartialOverlap_ReturnsIntersects()
- {
- var aCenter = new Vector2(5, 5);
- var aAxisX = Vector2.UnitX;
- var aAxisY = Vector2.UnitY;
- var aHalf = new Vector2(5, 5);
- var bCenter = new Vector2(8, 8);
- var bAxisX = Vector2.UnitX;
- var bAxisY = Vector2.UnitY;
- var bHalf = new Vector2(5, 5);
- var result = Collision2D.ContainsObbObb(aCenter, aAxisX, aAxisY, aHalf, bCenter, bAxisX, bAxisY, bHalf);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsObbCapsule Tests
- [Fact]
- public void ContainsObbCapsule_CapsuleCompletelyInside_ReturnsContains()
- {
- var boxCenter = new Vector2(10, 10);
- var axisX = Vector2.UnitX;
- var axisY = Vector2.UnitY;
- var halfExtents = new Vector2(5, 5);
- var capsuleA = new Vector2(8, 10);
- var capsuleB = new Vector2(12, 10);
- var capsuleRadius = 1.0f;
- var result = Collision2D.ContainsObbCapsule(boxCenter, axisX, axisY, halfExtents, capsuleA, capsuleB, capsuleRadius);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsObbCapsule_CapsuleCompletelyOutside_ReturnsDisjoint()
- {
- var boxCenter = new Vector2(0, 0);
- var axisX = Vector2.UnitX;
- var axisY = Vector2.UnitY;
- var halfExtents = new Vector2(5, 5);
- var capsuleA = new Vector2(20, 20);
- var capsuleB = new Vector2(25, 25);
- var capsuleRadius = 1.0f;
- var result = Collision2D.ContainsObbCapsule(boxCenter, axisX, axisY, halfExtents, capsuleA, capsuleB, capsuleRadius);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsObbCapsule_CapsulePartiallyInside_ReturnsIntersects()
- {
- var boxCenter = new Vector2(5, 5);
- var axisX = Vector2.UnitX;
- var axisY = Vector2.UnitY;
- var halfExtents = new Vector2(5, 5);
- var capsuleA = new Vector2(5, 5);
- var capsuleB = new Vector2(15, 5);
- var capsuleRadius = 1.0f;
- var result = Collision2D.ContainsObbCapsule(boxCenter, axisX, axisY, halfExtents, capsuleA, capsuleB, capsuleRadius);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsObbConvexPolygon Tests
- [Fact]
- public void ContainsObbConvexPolygon_PolygonCompletelyInside_ReturnsContains()
- {
- var boxCenter = new Vector2(10, 10);
- var axisX = Vector2.UnitX;
- var axisY = Vector2.UnitY;
- var halfExtents = new Vector2(5, 5);
- var vertices = new[]
- {
- new Vector2(8, 8),
- new Vector2(12, 8),
- new Vector2(12, 12),
- new Vector2(8, 12)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsObbConvexPolygon(boxCenter, axisX, axisY, halfExtents, vertices, normals);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsObbConvexPolygon_PolygonCompletelyOutside_ReturnsDisjoint()
- {
- var boxCenter = new Vector2(0, 0);
- var axisX = Vector2.UnitX;
- var axisY = Vector2.UnitY;
- var halfExtents = new Vector2(5, 5);
- var vertices = new[]
- {
- new Vector2(20, 20),
- new Vector2(25, 20),
- new Vector2(25, 25),
- new Vector2(20, 25)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsObbConvexPolygon(boxCenter, axisX, axisY, halfExtents, vertices, normals);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsObbConvexPolygon_PolygonPartiallyInside_ReturnsIntersects()
- {
- var boxCenter = new Vector2(5, 5);
- var axisX = Vector2.UnitX;
- var axisY = Vector2.UnitY;
- var halfExtents = new Vector2(5, 5);
- var vertices = new[]
- {
- new Vector2(5, 5),
- new Vector2(15, 5),
- new Vector2(15, 15),
- new Vector2(5, 15)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsObbConvexPolygon(boxCenter, axisX, axisY, halfExtents, vertices, normals);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #endregion
- #region Containment - Capsule Methods
- #region ContainsCapsulePoint Tests
- [Fact]
- public void ContainsCapsulePoint_PointInside_ReturnsContains()
- {
- var point = new Vector2(5, 1);
- var a = new Vector2(0, 0);
- var b = new Vector2(10, 0);
- var radius = 2.0f;
- var result = Collision2D.ContainsCapsulePoint(point, a, b, radius);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsCapsulePoint_PointOutside_ReturnsDisjoint()
- {
- var point = new Vector2(5, 10);
- var a = new Vector2(0, 0);
- var b = new Vector2(10, 0);
- var radius = 2.0f;
- var result = Collision2D.ContainsCapsulePoint(point, a, b, radius);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsCapsulePoint_PointOnBoundary_ReturnsContains()
- {
- var point = new Vector2(5, 2);
- var a = new Vector2(0, 0);
- var b = new Vector2(10, 0);
- var radius = 2.0f;
- var result = Collision2D.ContainsCapsulePoint(point, a, b, radius);
- Assert.Equal(ContainmentType.Contains, result);
- }
- #endregion
- #region ContainsCapsuleObb Tests
- [Fact]
- public void ContainsCapsuleObb_ObbCompletelyInside_ReturnsContains()
- {
- var capsuleA = new Vector2(0, 5);
- var capsuleB = new Vector2(20, 5);
- var capsuleRadius = 5.0f;
- var obbCenter = new Vector2(10, 5);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalf = new Vector2(2, 2);
- var result = Collision2D.ContainsCapsuleObb(capsuleA, capsuleB, capsuleRadius, obbCenter, obbAxisX, obbAxisY, obbHalf);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsCapsuleObb_ObbCompletelyOutside_ReturnsDisjoint()
- {
- var capsuleA = new Vector2(0, 0);
- var capsuleB = new Vector2(10, 0);
- var capsuleRadius = 2.0f;
- var obbCenter = new Vector2(20, 20);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalf = new Vector2(2, 2);
- var result = Collision2D.ContainsCapsuleObb(capsuleA, capsuleB, capsuleRadius, obbCenter, obbAxisX, obbAxisY, obbHalf);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsCapsuleObb_ObbPartiallyInside_ReturnsIntersects()
- {
- var capsuleA = new Vector2(0, 5);
- var capsuleB = new Vector2(10, 5);
- var capsuleRadius = 3.0f;
- var obbCenter = new Vector2(8, 5);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalf = new Vector2(5, 5);
- var result = Collision2D.ContainsCapsuleObb(capsuleA, capsuleB, capsuleRadius, obbCenter, obbAxisX, obbAxisY, obbHalf);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsCapsuleCapsule Tests
- [Fact]
- public void ContainsCapsuleCapsule_CompletelyContains_ReturnsContains()
- {
- var a0 = new Vector2(0, 0);
- var a1 = new Vector2(20, 0);
- var aRadius = 5.0f;
- var b0 = new Vector2(8, 0);
- var b1 = new Vector2(12, 0);
- var bRadius = 2.0f;
- var result = Collision2D.ContainsCapsuleCapsule(a0, a1, aRadius, b0, b1, bRadius);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsCapsuleCapsule_Separated_ReturnsDisjoint()
- {
- var a0 = new Vector2(0, 0);
- var a1 = new Vector2(10, 0);
- var aRadius = 2.0f;
- var b0 = new Vector2(20, 20);
- var b1 = new Vector2(30, 20);
- var bRadius = 2.0f;
- var result = Collision2D.ContainsCapsuleCapsule(a0, a1, aRadius, b0, b1, bRadius);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsCapsuleCapsule_PartialOverlap_ReturnsIntersects()
- {
- var a0 = new Vector2(0, 0);
- var a1 = new Vector2(10, 0);
- var aRadius = 3.0f;
- var b0 = new Vector2(8, 0);
- var b1 = new Vector2(15, 0);
- var bRadius = 2.0f;
- var result = Collision2D.ContainsCapsuleCapsule(a0, a1, aRadius, b0, b1, bRadius);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsCapsuleConvexPolygon Tests
- [Fact]
- public void ContainsCapsuleConvexPolygon_PolygonCompletelyInside_ReturnsContains()
- {
- var capsuleA = new Vector2(0, 10);
- var capsuleB = new Vector2(20, 10);
- var capsuleRadius = 8.0f;
- var vertices = new[]
- {
- new Vector2(8, 8),
- new Vector2(12, 8),
- new Vector2(12, 12),
- new Vector2(8, 12)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsCapsuleConvexPolygon(capsuleA, capsuleB, capsuleRadius, vertices, normals);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsCapsuleConvexPolygon_PolygonCompletelyOutside_ReturnsDisjoint()
- {
- var capsuleA = new Vector2(0, 0);
- var capsuleB = new Vector2(10, 0);
- var capsuleRadius = 2.0f;
- var vertices = new[]
- {
- new Vector2(20, 20),
- new Vector2(25, 20),
- new Vector2(25, 25),
- new Vector2(20, 25)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsCapsuleConvexPolygon(capsuleA, capsuleB, capsuleRadius, vertices, normals);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsCapsuleConvexPolygon_PolygonPartiallyInside_ReturnsIntersects()
- {
- var capsuleA = new Vector2(0, 5);
- var capsuleB = new Vector2(10, 5);
- var capsuleRadius = 3.0f;
- var vertices = new[]
- {
- new Vector2(5, 5),
- new Vector2(15, 5),
- new Vector2(15, 15),
- new Vector2(5, 15)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsCapsuleConvexPolygon(capsuleA, capsuleB, capsuleRadius, vertices, normals);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #endregion
- #region Containment - ConvexPolygon Methods
- #region ContainsConvexPolygonPoint Tests
- [Fact]
- public void ContainsConvexPolygonPoint_PointInside_ReturnsContains()
- {
- var point = new Vector2(5, 5);
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsConvexPolygonPoint(point, vertices, normals);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsConvexPolygonPoint_PointOutside_ReturnsDisjoint()
- {
- var point = new Vector2(15, 15);
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsConvexPolygonPoint(point, vertices, normals);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsConvexPolygonPoint_PointOnEdge_ReturnsContains()
- {
- var point = new Vector2(10, 5);
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsConvexPolygonPoint(point, vertices, normals);
- Assert.Equal(ContainmentType.Contains, result);
- }
- #endregion
- #region ContainsConvexPolygonCapsule Tests
- [Fact]
- public void ContainsConvexPolygonCapsule_CapsuleCompletelyInside_ReturnsContains()
- {
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(20, 0),
- new Vector2(20, 20),
- new Vector2(0, 20)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var capsuleA = new Vector2(8, 10);
- var capsuleB = new Vector2(12, 10);
- var capsuleRadius = 2.0f;
- var result = Collision2D.ContainsConvexPolygonCapsule(vertices, normals, capsuleA, capsuleB, capsuleRadius);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsConvexPolygonCapsule_CapsuleCompletelyOutside_ReturnsDisjoint()
- {
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var capsuleA = new Vector2(20, 20);
- var capsuleB = new Vector2(25, 25);
- var capsuleRadius = 1.0f;
- var result = Collision2D.ContainsConvexPolygonCapsule(vertices, normals, capsuleA, capsuleB, capsuleRadius);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsConvexPolygonCapsule_CapsulePartiallyInside_ReturnsIntersects()
- {
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var capsuleA = new Vector2(5, 5);
- var capsuleB = new Vector2(15, 5);
- var capsuleRadius = 1.0f;
- var result = Collision2D.ContainsConvexPolygonCapsule(vertices, normals, capsuleA, capsuleB, capsuleRadius);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsConvexPolygonObb Tests
- [Fact]
- public void ContainsConvexPolygonObb_ObbCompletelyInside_ReturnsContains()
- {
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(20, 0),
- new Vector2(20, 20),
- new Vector2(0, 20)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var obbCenter = new Vector2(10, 10);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalf = new Vector2(3, 3);
- var result = Collision2D.ContainsConvexPolygonObb(vertices, normals, obbCenter, obbAxisX, obbAxisY, obbHalf);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsConvexPolygonObb_ObbCompletelyOutside_ReturnsDisjoint()
- {
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var obbCenter = new Vector2(20, 20);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalf = new Vector2(2, 2);
- var result = Collision2D.ContainsConvexPolygonObb(vertices, normals, obbCenter, obbAxisX, obbAxisY, obbHalf);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsConvexPolygonObb_ObbPartiallyInside_ReturnsIntersects()
- {
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var obbCenter = new Vector2(8, 8);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalf = new Vector2(5, 5);
- var result = Collision2D.ContainsConvexPolygonObb(vertices, normals, obbCenter, obbAxisX, obbAxisY, obbHalf);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #region ContainsConvexPolygonConvexPolygon Tests
- [Fact]
- public void ContainsConvexPolygonConvexPolygon_CompletelyContains_ReturnsContains()
- {
- var aVertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(20, 0),
- new Vector2(20, 20),
- new Vector2(0, 20)
- };
- var aNormals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var bVertices = new[]
- {
- new Vector2(5, 5),
- new Vector2(15, 5),
- new Vector2(15, 15),
- new Vector2(5, 15)
- };
- var bNormals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsConvexPolygonConvexPolygon(aVertices, aNormals, bVertices, bNormals);
- Assert.Equal(ContainmentType.Contains, result);
- }
- [Fact]
- public void ContainsConvexPolygonConvexPolygon_Separated_ReturnsDisjoint()
- {
- var aVertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var aNormals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var bVertices = new[]
- {
- new Vector2(20, 20),
- new Vector2(30, 20),
- new Vector2(30, 30),
- new Vector2(20, 30)
- };
- var bNormals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsConvexPolygonConvexPolygon(aVertices, aNormals, bVertices, bNormals);
- Assert.Equal(ContainmentType.Disjoint, result);
- }
- [Fact]
- public void ContainsConvexPolygonConvexPolygon_PartialOverlap_ReturnsIntersects()
- {
- var aVertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var aNormals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var bVertices = new[]
- {
- new Vector2(5, 5),
- new Vector2(15, 5),
- new Vector2(15, 15),
- new Vector2(5, 15)
- };
- var bNormals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var result = Collision2D.ContainsConvexPolygonConvexPolygon(aVertices, aNormals, bVertices, bNormals);
- Assert.Equal(ContainmentType.Intersects, result);
- }
- #endregion
- #endregion
- #region Projection Methods
- #region ProjectOntoAxis Tests
- [Fact]
- public void ProjectOntoAxis_AlignedWithAxis_ReturnsCorrectInterval()
- {
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var axis = Vector2.UnitX;
- Collision2D.ProjectOntoAxis(vertices, axis, out float min, out float max);
- Assert.Equal(0.0f, min, Collision2D.Epsilon);
- Assert.Equal(10.0f, max, Collision2D.Epsilon);
- }
- [Fact]
- public void ProjectOntoAxis_PerpendicularToAxis_ReturnsNarrowInterval()
- {
- var vertices = new[]
- {
- new Vector2(5, 0),
- new Vector2(5, 10)
- };
- var axis = Vector2.UnitX;
- Collision2D.ProjectOntoAxis(vertices, axis, out float min, out float max);
- Assert.Equal(5.0f, min, Collision2D.Epsilon);
- Assert.Equal(5.0f, max, Collision2D.Epsilon);
- }
- [Fact]
- public void ProjectOntoAxis_DiagonalAxis_ReturnsCorrectInterval()
- {
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var axis = Vector2.Normalize(new Vector2(1, 1));
- Collision2D.ProjectOntoAxis(vertices, axis, out float min, out float max);
- float expected = 10.0f / MathF.Sqrt(2);
- Assert.Equal(0.0f, min, Collision2D.Epsilon);
- Assert.Equal(expected * 2, max, Collision2D.Epsilon);
- }
- #endregion
- #region ProjectAabbOntoAxis Tests
- [Fact]
- public void ProjectAabbOntoAxis_AlignedAxis_ReturnsExtents()
- {
- var center = new Vector2(5, 5);
- var halfExtents = new Vector2(5, 5);
- var axis = Vector2.UnitX;
- Collision2D.ProjectAabbOntoAxis(center, halfExtents, axis, out float min, out float max);
- Assert.Equal(0.0f, min, Collision2D.Epsilon);
- Assert.Equal(10.0f, max, Collision2D.Epsilon);
- }
- [Fact]
- public void ProjectAabbOntoAxis_DiagonalAxis_ReturnsCorrectInterval()
- {
- var center = new Vector2(5, 5);
- var halfExtents = new Vector2(5, 5);
- var axis = Vector2.Normalize(new Vector2(1, 1));
- Collision2D.ProjectAabbOntoAxis(center, halfExtents, axis, out float min, out float max);
- float centerProj = 5.0f * MathF.Sqrt(2);
- float radius = 5.0f * MathF.Sqrt(2);
- Assert.Equal(centerProj - radius, min, Collision2D.Epsilon);
- Assert.Equal(centerProj + radius, max, Collision2D.Epsilon);
- }
- #endregion
- #region ProjectObbOntoAxis Tests
- [Fact]
- public void ProjectObbOntoAxis_AxisAlignedBox_MatchesAabbResult()
- {
- var center = new Vector2(5, 5);
- var axisX = Vector2.UnitX;
- var axisY = Vector2.UnitY;
- var halfExtents = new Vector2(5, 5);
- var projAxis = Vector2.UnitX;
- Collision2D.ProjectObbOntoAxis(center, axisX, axisY, halfExtents, projAxis, out float min, out float max);
- Assert.Equal(0.0f, min, Collision2D.Epsilon);
- Assert.Equal(10.0f, max, Collision2D.Epsilon);
- }
- [Fact]
- public void ProjectObbOntoAxis_RotatedBox_ReturnsCorrectInterval()
- {
- var center = new Vector2(0, 0);
- var axisX = Vector2.Normalize(new Vector2(1, 1));
- var axisY = Vector2.Normalize(new Vector2(-1, 1));
- var halfExtents = new Vector2(5, 5);
- var projAxis = Vector2.UnitX;
- Collision2D.ProjectObbOntoAxis(center, axisX, axisY, halfExtents, projAxis, out float min, out float max);
- Assert.Equal(min, -max, Collision2D.Epsilon);
- }
- #endregion
- #endregion
- #region Distance Calculations
- #region DistanceSquaredPointSegment Tests
- [Fact]
- public void DistanceSquaredPointSegment_PointOnSegment_ReturnsZero()
- {
- var point = new Vector2(5, 0);
- var a = new Vector2(0, 0);
- var b = new Vector2(10, 0);
- float distance = Collision2D.DistanceSquaredPointSegment(point, a, b, out float t, out Vector2 closestPoint);
- Assert.Equal(0.0f, distance, Collision2D.Epsilon);
- Assert.Equal(0.5f, t, Collision2D.Epsilon);
- Assert.Equal(point, closestPoint);
- }
- [Fact]
- public void DistanceSquaredPointSegment_ClosestAtEndpoint_ClampsParameter()
- {
- var point = new Vector2(15, 5);
- var a = new Vector2(0, 0);
- var b = new Vector2(10, 0);
- float distance = Collision2D.DistanceSquaredPointSegment(point, a, b, out float t, out Vector2 closestPoint);
- Assert.Equal(50.0f, distance, Collision2D.Epsilon);
- Assert.Equal(1.0f, t);
- Assert.Equal(b, closestPoint);
- }
- [Fact]
- public void DistanceSquaredPointSegment_PerpendicularDistance_ReturnsCorrectValue()
- {
- var point = new Vector2(5, 3);
- var a = new Vector2(0, 0);
- var b = new Vector2(10, 0);
- float distance = Collision2D.DistanceSquaredPointSegment(point, a, b, out float t, out Vector2 closestPoint);
- Assert.Equal(9.0f, distance, Collision2D.Epsilon);
- Assert.Equal(0.5f, t, Collision2D.Epsilon);
- Assert.Equal(new Vector2(5, 0), closestPoint);
- }
- [Fact]
- public void DistanceSquaredPointSegment_DegenerateSegment_TreatsAsPoint()
- {
- var point = new Vector2(5, 5);
- var a = new Vector2(0, 0);
- var b = new Vector2(0, 0);
- float distance = Collision2D.DistanceSquaredPointSegment(point, a, b, out float t, out Vector2 closestPoint);
- Assert.Equal(50.0f, distance, Collision2D.Epsilon);
- Assert.Equal(0.0f, t);
- Assert.Equal(a, closestPoint);
- }
- #endregion
- #region DistanceSquaredSegmentSegment Tests
- [Fact]
- public void DistanceSquaredSegmentSegment_IntersectingSegments_ReturnsZero()
- {
- var p1 = new Vector2(0, 5);
- var q1 = new Vector2(10, 5);
- var p2 = new Vector2(5, 0);
- var q2 = new Vector2(5, 10);
- float distance = Collision2D.DistanceSquaredSegmentSegment(p1, q1, p2, q2, out float s, out float t, out Vector2 c1, out Vector2 c2);
- Assert.Equal(0.0f, distance, Collision2D.Epsilon);
- Assert.Equal(new Vector2(5, 5), c1);
- Assert.Equal(new Vector2(5, 5), c2);
- }
- [Fact]
- public void DistanceSquaredSegmentSegment_ParallelSegments_ReturnsCorrectDistance()
- {
- var p1 = new Vector2(0, 0);
- var q1 = new Vector2(10, 0);
- var p2 = new Vector2(0, 5);
- var q2 = new Vector2(10, 5);
- float distance = Collision2D.DistanceSquaredSegmentSegment(p1, q1, p2, q2, out float s, out float t, out Vector2 c1, out Vector2 c2);
- Assert.Equal(25.0f, distance, Collision2D.Epsilon);
- }
- [Fact]
- public void DistanceSquaredSegmentSegment_SkewSegments_ReturnsCorrectDistance()
- {
- var p1 = new Vector2(0, 0);
- var q1 = new Vector2(5, 0);
- var p2 = new Vector2(0, 2);
- var q2 = new Vector2(0, 5);
- float distance = Collision2D.DistanceSquaredSegmentSegment(p1, q1, p2, q2, out float s, out float t, out Vector2 c1, out Vector2 c2);
- Assert.Equal(4.0f, distance, Collision2D.Epsilon);
- }
- [Fact]
- public void DistanceSquaredSegmentSegment_DegenerateSegments_HandlesCorrectly()
- {
- var p1 = new Vector2(0, 0);
- var q1 = new Vector2(0, 0);
- var p2 = new Vector2(3, 4);
- var q2 = new Vector2(3, 4);
- float distance = Collision2D.DistanceSquaredSegmentSegment(p1, q1, p2, q2, out float s, out float t, out Vector2 c1, out Vector2 c2);
- Assert.Equal(25.0f, distance, Collision2D.Epsilon);
- }
- #endregion
- #region DistanceSquaredPointAabb Tests
- [Fact]
- public void DistanceSquaredPointAabb_PointInside_ReturnsZero()
- {
- var point = new Vector2(5, 5);
- var min = new Vector2(0, 0);
- var max = new Vector2(10, 10);
- float distance = Collision2D.DistanceSquaredPointAabb(point, min, max);
- Assert.Equal(0.0f, distance, Collision2D.Epsilon);
- }
- [Fact]
- public void DistanceSquaredPointAabb_ClosestToCorner_ReturnsCorrectDistance()
- {
- var point = new Vector2(15, 15);
- var min = new Vector2(0, 0);
- var max = new Vector2(10, 10);
- float distance = Collision2D.DistanceSquaredPointAabb(point, min, max);
- Assert.Equal(50.0f, distance, Collision2D.Epsilon);
- }
- [Fact]
- public void DistanceSquaredPointAabb_ClosestToEdge_ReturnsCorrectDistance()
- {
- var point = new Vector2(5, 15);
- var min = new Vector2(0, 0);
- var max = new Vector2(10, 10);
- float distance = Collision2D.DistanceSquaredPointAabb(point, min, max);
- Assert.Equal(25.0f, distance, Collision2D.Epsilon);
- }
- #endregion
- #region DistanceSquaredPointObb Tests
- [Fact]
- public void DistanceSquaredPointObb_PointInside_ReturnsZero()
- {
- var point = new Vector2(5, 5);
- var center = new Vector2(5, 5);
- var axisX = Vector2.UnitX;
- var axisY = Vector2.UnitY;
- var halfExtents = new Vector2(3, 3);
- float distance = Collision2D.DistanceSquaredPointObb(point, center, axisX, axisY, halfExtents);
- Assert.Equal(0.0f, distance, Collision2D.Epsilon);
- }
- [Fact]
- public void DistanceSquaredPointObb_PointOutside_ReturnsCorrectDistance()
- {
- var point = new Vector2(10, 5);
- var center = new Vector2(5, 5);
- var axisX = Vector2.UnitX;
- var axisY = Vector2.UnitY;
- var halfExtents = new Vector2(2, 2);
- float distance = Collision2D.DistanceSquaredPointObb(point, center, axisX, axisY, halfExtents);
- Assert.Equal(9.0f, distance, Collision2D.Epsilon);
- }
- [Fact]
- public void DistanceSquaredPointObb_RotatedBox_ReturnsCorrectDistance()
- {
- var point = new Vector2(10, 0);
- var center = new Vector2(0, 0);
- var axisX = Vector2.Normalize(new Vector2(1, 1));
- var axisY = Vector2.Normalize(new Vector2(-1, 1));
- var halfExtents = new Vector2(2, 2);
- float distance = Collision2D.DistanceSquaredPointObb(point, center, axisX, axisY, halfExtents);
- Assert.True(distance > 0.0f);
- }
- #endregion
- #region DistanceSquaredPointConvexPolygon Tests
- [Fact]
- public void DistanceSquaredPointConvexPolygon_PointInside_ReturnsZero()
- {
- var point = new Vector2(5, 5);
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- float distance = Collision2D.DistanceSquaredPointConvexPolygon(point, vertices, normals);
- Assert.Equal(0.0f, distance, Collision2D.Epsilon);
- }
- [Fact]
- public void DistanceSquaredPointConvexPolygon_ClosestToEdge_ReturnsCorrectDistance()
- {
- var point = new Vector2(5, -3);
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- float distance = Collision2D.DistanceSquaredPointConvexPolygon(point, vertices, normals);
- Assert.Equal(9.0f, distance, Collision2D.Epsilon);
- }
- [Fact]
- public void DistanceSquaredPointConvexPolygon_ClosestToVertex_ReturnsCorrectDistance()
- {
- var point = new Vector2(-3, -3);
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- float distance = Collision2D.DistanceSquaredPointConvexPolygon(point, vertices, normals);
- Assert.Equal(18.0f, distance, Collision2D.Epsilon);
- }
- #endregion
- #region DistanceSquaredSegmentAabb Tests
- [Fact]
- public void DistanceSquaredSegmentAabb_SegmentInsideBox_ReturnsZero()
- {
- var a = new Vector2(3, 3);
- var b = new Vector2(7, 7);
- var min = new Vector2(0, 0);
- var max = new Vector2(10, 10);
- float distance = Collision2D.DistanceSquaredSegmentAabb(a, b, min, max);
- Assert.Equal(0.0f, distance, Collision2D.Epsilon);
- }
- [Fact]
- public void DistanceSquaredSegmentAabb_SegmentIntersectsBox_ReturnsZero()
- {
- var a = new Vector2(-5, 5);
- var b = new Vector2(15, 5);
- var min = new Vector2(0, 0);
- var max = new Vector2(10, 10);
- float distance = Collision2D.DistanceSquaredSegmentAabb(a, b, min, max);
- Assert.Equal(0.0f, distance, Collision2D.Epsilon);
- }
- [Fact]
- public void DistanceSquaredSegmentAabb_SegmentOutsideBox_ReturnsCorrectDistance()
- {
- var a = new Vector2(15, 5);
- var b = new Vector2(20, 5);
- var min = new Vector2(0, 0);
- var max = new Vector2(10, 10);
- float distance = Collision2D.DistanceSquaredSegmentAabb(a, b, min, max);
- Assert.Equal(25.0f, distance, Collision2D.Epsilon);
- }
- #endregion
- #region DistanceSquaredSegmentConvexPolygon Tests
- [Fact]
- public void DistanceSquaredSegmentConvexPolygon_SegmentInside_ReturnsZero()
- {
- var a = new Vector2(3, 3);
- var b = new Vector2(7, 7);
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- float distance = Collision2D.DistanceSquaredSegmentConvexPolygon(a, b, vertices, normals);
- Assert.Equal(0.0f, distance, Collision2D.Epsilon);
- }
- [Fact]
- public void DistanceSquaredSegmentConvexPolygon_SegmentIntersects_ReturnsZero()
- {
- var a = new Vector2(-5, 5);
- var b = new Vector2(15, 5);
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- float distance = Collision2D.DistanceSquaredSegmentConvexPolygon(a, b, vertices, normals);
- Assert.Equal(0.0f, distance, Collision2D.Epsilon);
- }
- [Fact]
- public void DistanceSquaredSegmentConvexPolygon_SegmentOutside_ReturnsCorrectDistance()
- {
- var a = new Vector2(15, 5);
- var b = new Vector2(20, 5);
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- float distance = Collision2D.DistanceSquaredSegmentConvexPolygon(a, b, vertices, normals);
- Assert.Equal(25.0f, distance, Collision2D.Epsilon);
- }
- #endregion
- #region ClosestPointRaySegment Tests
- [Fact]
- public void ClosestPointRaySegment_RayIntersectsSegment_ReturnsZeroDistance()
- {
- var rayOrigin = new Vector2(0, 5);
- var rayDirection = Vector2.UnitX;
- var segA = new Vector2(5, 0);
- var segB = new Vector2(5, 10);
- Collision2D.ClosestPointRaySegment(rayOrigin, rayDirection, segA, segB, out float sRay, out float tSeg, out float distanceSquared);
- Assert.Equal(0.0f, distanceSquared, Collision2D.Epsilon);
- Assert.Equal(5.0f, sRay, Collision2D.Epsilon);
- Assert.Equal(0.5f, tSeg, Collision2D.Epsilon);
- }
- [Fact]
- public void ClosestPointRaySegment_RayParallelToSegment_ReturnsCorrectDistance()
- {
- var rayOrigin = new Vector2(0, 0);
- var rayDirection = Vector2.UnitX;
- var segA = new Vector2(0, 5);
- var segB = new Vector2(10, 5);
- Collision2D.ClosestPointRaySegment(rayOrigin, rayDirection, segA, segB, out float sRay, out float tSeg, out float distanceSquared);
- Assert.Equal(25.0f, distanceSquared, Collision2D.Epsilon);
- }
- [Fact]
- public void ClosestPointRaySegment_RayMissesSegment_ReturnsCorrectDistance()
- {
- var rayOrigin = new Vector2(0, 0);
- var rayDirection = Vector2.UnitX;
- var segA = new Vector2(5, 5);
- var segB = new Vector2(10, 5);
- Collision2D.ClosestPointRaySegment(rayOrigin, rayDirection, segA, segB, out float sRay, out float tSeg, out float distanceSquared);
- Assert.True(distanceSquared > 0.0f);
- }
- #endregion
- #endregion
- #region Parametric Solvers
- #region SolveParametricIntersection2D Tests
- [Fact]
- public void SolveParametricIntersection2D_PerpendicularLines_FindsIntersection()
- {
- var origin1 = new Vector2(0, 5);
- var direction1 = Vector2.UnitX;
- var origin2 = new Vector2(5, 0);
- var direction2 = Vector2.UnitY;
- bool result = Collision2D.SolveParametricIntersection2D(origin1, direction1, origin2, direction2, out float t1, out float t2);
- Assert.True(result);
- Assert.Equal(5.0f, t1, Collision2D.Epsilon);
- Assert.Equal(5.0f, t2, Collision2D.Epsilon);
- }
- [Fact]
- public void SolveParametricIntersection2D_ParallelLines_ReturnsFalse()
- {
- var origin1 = new Vector2(0, 0);
- var direction1 = Vector2.UnitX;
- var origin2 = new Vector2(0, 5);
- var direction2 = Vector2.UnitX;
- bool result = Collision2D.SolveParametricIntersection2D(origin1, direction1, origin2, direction2, out float t1, out float t2);
- Assert.False(result);
- }
- [Fact]
- public void SolveParametricIntersection2D_CollinearLines_ReturnsFalse()
- {
- var origin1 = new Vector2(0, 0);
- var direction1 = Vector2.UnitX;
- var origin2 = new Vector2(5, 0);
- var direction2 = Vector2.UnitX;
- bool result = Collision2D.SolveParametricIntersection2D(origin1, direction1, origin2, direction2, out float t1, out float t2);
- Assert.False(result);
- }
- [Fact]
- public void SolveParametricIntersection2D_LinesAtAnAngle_FindsIntersection()
- {
- var origin1 = new Vector2(0, 0);
- var direction1 = new Vector2(1, 0);
- var origin2 = new Vector2(0, 0);
- var direction2 = new Vector2(0, 1);
- bool result = Collision2D.SolveParametricIntersection2D(origin1, direction1, origin2, direction2, out float t1, out float t2);
- Assert.True(result);
- Assert.Equal(0.0f, t1, Collision2D.Epsilon);
- Assert.Equal(0.0f, t2, Collision2D.Epsilon);
- }
- #endregion
- #region SolveParametricIntersectionWithImplicitLine Tests
- [Fact]
- public void SolveParametricIntersectionWithImplicitLine_Intersecting_FindsParameter()
- {
- var lineNormal = Vector2.UnitY;
- var lineDistance = 5.0f;
- var origin = new Vector2(0, 0);
- var direction = Vector2.UnitY;
- bool result = Collision2D.SolveParametricIntersectionWithImplicitLine(lineNormal, lineDistance, origin, direction, out float t);
- Assert.True(result);
- Assert.Equal(5.0f, t, Collision2D.Epsilon);
- }
- [Fact]
- public void SolveParametricIntersectionWithImplicitLine_Parallel_ReturnsFalse()
- {
- var lineNormal = Vector2.UnitY;
- var lineDistance = 5.0f;
- var origin = new Vector2(0, 0);
- var direction = Vector2.UnitX;
- bool result = Collision2D.SolveParametricIntersectionWithImplicitLine(lineNormal, lineDistance, origin, direction, out float t);
- Assert.False(result);
- }
- [Fact]
- public void SolveParametricIntersectionWithImplicitLine_Perpendicular_FindsParameter()
- {
- var lineNormal = Vector2.UnitX;
- var lineDistance = 10.0f;
- var origin = new Vector2(0, 0);
- var direction = Vector2.UnitX;
- bool result = Collision2D.SolveParametricIntersectionWithImplicitLine(lineNormal, lineDistance, origin, direction, out float t);
- Assert.True(result);
- Assert.Equal(10.0f, t, Collision2D.Epsilon);
- }
- #endregion
- #endregion
- #region Clipping Methods
- #region ClipLineToAabb Tests
- [Fact]
- public void ClipLineToAabb_LineCompletelyInside_ReturnsFullInterval()
- {
- var origin = new Vector2(5, 5);
- var direction = Vector2.UnitX;
- var min = new Vector2(0, 0);
- var max = new Vector2(20, 20);
- float tLower = 0.0f;
- float tUpper = 10.0f;
- bool result = Collision2D.ClipLineToAabb(origin, direction, min, max, tLower, tUpper, out float tEnter, out float tExit);
- Assert.True(result);
- Assert.Equal(0.0f, tEnter, Collision2D.Epsilon);
- Assert.Equal(10.0f, tExit, Collision2D.Epsilon);
- }
- [Fact]
- public void ClipLineToAabb_LinePartiallyInside_ReturnsClippedInterval()
- {
- var origin = new Vector2(-5, 5);
- var direction = Vector2.UnitX;
- var min = new Vector2(0, 0);
- var max = new Vector2(10, 10);
- float tLower = 0.0f;
- float tUpper = 20.0f;
- bool result = Collision2D.ClipLineToAabb(origin, direction, min, max, tLower, tUpper, out float tEnter, out float tExit);
- Assert.True(result);
- Assert.Equal(5.0f, tEnter, Collision2D.Epsilon);
- Assert.Equal(15.0f, tExit, Collision2D.Epsilon);
- }
- [Fact]
- public void ClipLineToAabb_LineMissesBox_ReturnsFalse()
- {
- var origin = new Vector2(0, 20);
- var direction = Vector2.UnitX;
- var min = new Vector2(0, 0);
- var max = new Vector2(10, 10);
- float tLower = 0.0f;
- float tUpper = 10.0f;
- bool result = Collision2D.ClipLineToAabb(origin, direction, min, max, tLower, tUpper, out float tEnter, out float tExit);
- Assert.False(result);
- }
- [Fact]
- public void ClipLineToAabb_LineThroughCorner_ReturnsInterval()
- {
- var origin = new Vector2(0, 0);
- var direction = Vector2.Normalize(new Vector2(1, 1));
- var min = new Vector2(5, 5);
- var max = new Vector2(15, 15);
- float tLower = 0.0f;
- float tUpper = 50.0f;
- bool result = Collision2D.ClipLineToAabb(origin, direction, min, max, tLower, tUpper, out float tEnter, out float tExit);
- Assert.True(result);
- }
- #endregion
- #region ClipLineToConvexPolygon Tests
- [Fact]
- public void ClipLineToConvexPolygon_LineCompletelyInside_ReturnsFullInterval()
- {
- var origin = new Vector2(5, 5);
- var direction = Vector2.UnitX;
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(20, 0),
- new Vector2(20, 20),
- new Vector2(0, 20)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- float tLower = 0.0f;
- float tUpper = 10.0f;
- bool result = Collision2D.ClipLineToConvexPolygon(origin, direction, vertices, normals, tLower, tUpper, out float tEnter, out float tExit);
- Assert.True(result);
- Assert.Equal(0.0f, tEnter, Collision2D.Epsilon);
- Assert.Equal(10.0f, tExit, Collision2D.Epsilon);
- }
- [Fact]
- public void ClipLineToConvexPolygon_LinePartiallyInside_ReturnsClippedInterval()
- {
- var origin = new Vector2(-5, 5);
- var direction = Vector2.UnitX;
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- float tLower = 0.0f;
- float tUpper = 20.0f;
- bool result = Collision2D.ClipLineToConvexPolygon(origin, direction, vertices, normals, tLower, tUpper, out float tEnter, out float tExit);
- Assert.True(result);
- Assert.Equal(5.0f, tEnter, Collision2D.Epsilon);
- Assert.Equal(15.0f, tExit, Collision2D.Epsilon);
- }
- [Fact]
- public void ClipLineToConvexPolygon_LineMissesPolygon_ReturnsFalse()
- {
- var origin = new Vector2(0, 20);
- var direction = Vector2.UnitX;
- var vertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- float tLower = 0.0f;
- float tUpper = 10.0f;
- bool result = Collision2D.ClipLineToConvexPolygon(origin, direction, vertices, normals, tLower, tUpper, out float tEnter, out float tExit);
- Assert.False(result);
- }
- #endregion
- #endregion
- #region Ray Interval Methods
- #region RayCircleIntersectionInterval Tests
- [Fact]
- public void RayCircleIntersectionInterval_TwoIntersections_ReturnsBothParameters()
- {
- var origin = new Vector2(-10, 0);
- var direction = Vector2.UnitX;
- var center = new Vector2(0, 0);
- var radius = 5.0f;
- bool result = Collision2D.RayCircleIntersectionInterval(origin, direction, center, radius, out float tMin, out float tMax);
- Assert.True(result);
- Assert.Equal(5.0f, tMin, Collision2D.Epsilon);
- Assert.Equal(15.0f, tMax, Collision2D.Epsilon);
- }
- [Fact]
- public void RayCircleIntersectionInterval_Tangent_ReturnsSinglePoint()
- {
- var origin = new Vector2(-10, 5);
- var direction = Vector2.UnitX;
- var center = new Vector2(0, 0);
- var radius = 5.0f;
- bool result = Collision2D.RayCircleIntersectionInterval(origin, direction, center, radius, out float tMin, out float tMax);
- Assert.True(result);
- Assert.Equal(tMin, tMax, Collision2D.Epsilon);
- }
- [Fact]
- public void RayCircleIntersectionInterval_Miss_ReturnsFalse()
- {
- var origin = new Vector2(-10, 10);
- var direction = Vector2.UnitX;
- var center = new Vector2(0, 0);
- var radius = 5.0f;
- bool result = Collision2D.RayCircleIntersectionInterval(origin, direction, center, radius, out float tMin, out float tMax);
- Assert.False(result);
- }
- [Fact]
- public void RayCircleIntersectionInterval_OriginInside_ReturnsIntervalFromZero()
- {
- var origin = new Vector2(0, 0);
- var direction = Vector2.UnitX;
- var center = new Vector2(0, 0);
- var radius = 5.0f;
- bool result = Collision2D.RayCircleIntersectionInterval(origin, direction, center, radius, out float tMin, out float tMax);
- Assert.True(result);
- Assert.Equal(0.0f, tMin, Collision2D.Epsilon);
- Assert.Equal(5.0f, tMax, Collision2D.Epsilon);
- }
- #endregion
- #region RayCapsuleIntersectionInterval Tests
- [Fact]
- public void RayCapsuleIntersectionInterval_HitsCylinder_ReturnsInterval()
- {
- var rayOrigin = new Vector2(-10, 0);
- var rayDirection = Vector2.UnitX;
- var segA = new Vector2(0, -5);
- var segB = new Vector2(0, 5);
- var radius = 3.0f;
- bool result = Collision2D.RayCapsuleIntersectionInterval(rayOrigin, rayDirection, segA, segB, radius, out float tMin, out float tMax);
- Assert.True(result);
- Assert.True(tMax > tMin);
- }
- [Fact]
- public void RayCapsuleIntersectionInterval_HitsEndCap_ReturnsInterval()
- {
- var rayOrigin = new Vector2(-10, 10);
- var rayDirection = Vector2.UnitX;
- var segA = new Vector2(0, 10);
- var segB = new Vector2(10, 10);
- var radius = 3.0f;
- bool result = Collision2D.RayCapsuleIntersectionInterval(rayOrigin, rayDirection, segA, segB, radius, out float tMin, out float tMax);
- Assert.True(result);
- }
- [Fact]
- public void RayCapsuleIntersectionInterval_Miss_ReturnsFalse()
- {
- var rayOrigin = new Vector2(-10, 20);
- var rayDirection = Vector2.UnitX;
- var segA = new Vector2(0, 0);
- var segB = new Vector2(10, 0);
- var radius = 3.0f;
- bool result = Collision2D.RayCapsuleIntersectionInterval(rayOrigin, rayDirection, segA, segB, radius, out float tMin, out float tMax);
- Assert.False(result);
- }
- [Fact]
- public void RayCapsuleIntersectionInterval_OriginInside_ReturnsIntervalFromZero()
- {
- var rayOrigin = new Vector2(5, 5);
- var rayDirection = Vector2.UnitX;
- var segA = new Vector2(0, 5);
- var segB = new Vector2(10, 5);
- var radius = 3.0f;
- bool result = Collision2D.RayCapsuleIntersectionInterval(rayOrigin, rayDirection, segA, segB, radius, out float tMin, out float tMax);
- Assert.True(result);
- Assert.Equal(0.0f, tMin, Collision2D.Epsilon);
- }
- [Fact]
- public void RayCapsuleIntersectionInterval_DegenerateCapsule_BehavesLikeCircle()
- {
- var rayOrigin = new Vector2(-10, 0);
- var rayDirection = Vector2.UnitX;
- var segA = new Vector2(0, 0);
- var segB = new Vector2(0, 0);
- var radius = 5.0f;
- bool result = Collision2D.RayCapsuleIntersectionInterval(rayOrigin, rayDirection, segA, segB, radius, out float tMin, out float tMax);
- Assert.True(result);
- Assert.Equal(5.0f, tMin, Collision2D.Epsilon);
- Assert.Equal(15.0f, tMax, Collision2D.Epsilon);
- }
- #endregion
- #endregion
- #region Overlap Methods
- #region OverlapOnAxis Tests
- [Fact]
- public void OverlapOnAxis_TwoPolygonsOverlapping_ReturnsTrue()
- {
- var aVerts = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var bVerts = new[]
- {
- new Vector2(5, 5),
- new Vector2(15, 5),
- new Vector2(15, 15),
- new Vector2(5, 15)
- };
- var axis = Vector2.UnitX;
- bool result = Collision2D.OverlapOnAxis(aVerts, bVerts, axis);
- Assert.True(result);
- }
- [Fact]
- public void OverlapOnAxis_TwoPolygonsSeparated_ReturnsFalse()
- {
- var aVerts = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var bVerts = new[]
- {
- new Vector2(20, 20),
- new Vector2(30, 20),
- new Vector2(30, 30),
- new Vector2(20, 30)
- };
- var axis = Vector2.UnitX;
- bool result = Collision2D.OverlapOnAxis(aVerts, bVerts, axis);
- Assert.False(result);
- }
- [Fact]
- public void OverlapOnAxis_PolygonsTouching_ReturnsTrue()
- {
- var aVerts = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var bVerts = new[]
- {
- new Vector2(10, 0),
- new Vector2(20, 0),
- new Vector2(20, 10),
- new Vector2(10, 10)
- };
- var axis = Vector2.UnitX;
- bool result = Collision2D.OverlapOnAxis(aVerts, bVerts, axis);
- Assert.True(result);
- }
- #endregion
- #region OverlapOnAxisAabbPolygon Tests
- [Fact]
- public void OverlapOnAxisAabbPolygon_Overlapping_ReturnsTrue()
- {
- var aabbCenter = new Vector2(5, 5);
- var aabbHalfExtents = new Vector2(5, 5);
- var polygonVertices = new[]
- {
- new Vector2(8, 8),
- new Vector2(15, 8),
- new Vector2(15, 15),
- new Vector2(8, 15)
- };
- var axis = Vector2.UnitX;
- bool result = Collision2D.OverlapOnAxis(aabbCenter, aabbHalfExtents, polygonVertices, axis);
- Assert.True(result);
- }
- [Fact]
- public void OverlapOnAxisAabbPolygon_Separated_ReturnsFalse()
- {
- var aabbCenter = new Vector2(5, 5);
- var aabbHalfExtents = new Vector2(5, 5);
- var polygonVertices = new[]
- {
- new Vector2(20, 20),
- new Vector2(30, 20),
- new Vector2(30, 30),
- new Vector2(20, 30)
- };
- var axis = Vector2.UnitX;
- bool result = Collision2D.OverlapOnAxis(aabbCenter, aabbHalfExtents, polygonVertices, axis);
- Assert.False(result);
- }
- #endregion
- #endregion
- #region Intersection Methods
- #region IntersectsAabbAabb Tests
- [Fact]
- public void IntersectsAabbAabb_Overlapping_ReturnsTrue()
- {
- var aMin = new Vector2(0, 0);
- var aMax = new Vector2(10, 10);
- var bMin = new Vector2(5, 5);
- var bMax = new Vector2(15, 15);
- bool result = Collision2D.IntersectsAabbAabb(aMin, aMax, bMin, bMax);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsAabbAabb_Separated_ReturnsFalse()
- {
- var aMin = new Vector2(0, 0);
- var aMax = new Vector2(10, 10);
- var bMin = new Vector2(20, 20);
- var bMax = new Vector2(30, 30);
- bool result = Collision2D.IntersectsAabbAabb(aMin, aMax, bMin, bMax);
- Assert.False(result);
- }
- #endregion
- #region IntersectsAabbCapsule Tests
- [Fact]
- public void IntersectsAabbCapsule_Overlapping_ReturnsTrue()
- {
- var boxMin = new Vector2(0, 0);
- var boxMax = new Vector2(10, 10);
- var capsuleA = new Vector2(5, 5);
- var capsuleB = new Vector2(15, 5);
- var capsuleRadius = 2.0f;
- bool result = Collision2D.IntersectsAabbCapsule(boxMin, boxMax, capsuleA, capsuleB, capsuleRadius);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsAabbCapsule_Separated_ReturnsFalse()
- {
- var boxMin = new Vector2(0, 0);
- var boxMax = new Vector2(10, 10);
- var capsuleA = new Vector2(20, 20);
- var capsuleB = new Vector2(30, 30);
- var capsuleRadius = 2.0f;
- bool result = Collision2D.IntersectsAabbCapsule(boxMin, boxMax, capsuleA, capsuleB, capsuleRadius);
- Assert.False(result);
- }
- #endregion
- #region IntersectsAabbConvexPolygon Tests
- [Fact]
- public void IntersectsAabbConvexPolygon_Overlapping_ReturnsTrue()
- {
- var aabbCenter = new Vector2(5, 5);
- var aabbHalfExtents = new Vector2(5, 5);
- var vertices = new[]
- {
- new Vector2(8, 8),
- new Vector2(15, 8),
- new Vector2(15, 15),
- new Vector2(8, 15)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- bool result = Collision2D.IntersectsAabbConvexPolygon(aabbCenter, aabbHalfExtents, vertices, normals);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsAabbConvexPolygon_Separated_ReturnsFalse()
- {
- var aabbCenter = new Vector2(5, 5);
- var aabbHalfExtents = new Vector2(5, 5);
- var vertices = new[]
- {
- new Vector2(20, 20),
- new Vector2(30, 20),
- new Vector2(30, 30),
- new Vector2(20, 30)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- bool result = Collision2D.IntersectsAabbConvexPolygon(aabbCenter, aabbHalfExtents, vertices, normals);
- Assert.False(result);
- }
- #endregion
- #region IntersectsAabbObb Tests
- [Fact]
- public void IntersectsAabbObb_Overlapping_ReturnsTrue()
- {
- var aabbCenter = new Vector2(5, 5);
- var aabbHalfExtents = new Vector2(5, 5);
- var obbCenter = new Vector2(8, 8);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(3, 3);
- bool result = Collision2D.IntersectsAabbObb(aabbCenter, aabbHalfExtents, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsAabbObb_Separated_ReturnsFalse()
- {
- var aabbCenter = new Vector2(5, 5);
- var aabbHalfExtents = new Vector2(5, 5);
- var obbCenter = new Vector2(20, 20);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(3, 3);
- bool result = Collision2D.IntersectsAabbObb(aabbCenter, aabbHalfExtents, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
- Assert.False(result);
- }
- #endregion
- #region IntersectsCircleCircle Tests
- [Fact]
- public void IntersectsCircleCircle_Overlapping_ReturnsTrue()
- {
- var aCenter = new Vector2(0, 0);
- var aRadius = 5.0f;
- var bCenter = new Vector2(8, 0);
- var bRadius = 5.0f;
- bool result = Collision2D.IntersectsCircleCircle(aCenter, aRadius, bCenter, bRadius);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsCircleCircle_Separated_ReturnsFalse()
- {
- var aCenter = new Vector2(0, 0);
- var aRadius = 5.0f;
- var bCenter = new Vector2(20, 0);
- var bRadius = 5.0f;
- bool result = Collision2D.IntersectsCircleCircle(aCenter, aRadius, bCenter, bRadius);
- Assert.False(result);
- }
- [Fact]
- public void IntersectsCircleCircle_Touching_ReturnsTrue()
- {
- var aCenter = new Vector2(0, 0);
- var aRadius = 5.0f;
- var bCenter = new Vector2(10, 0);
- var bRadius = 5.0f;
- bool result = Collision2D.IntersectsCircleCircle(aCenter, aRadius, bCenter, bRadius);
- Assert.True(result);
- }
- #endregion
- #region IntersectsCircleAabb Tests
- [Fact]
- public void IntersectsCircleAabb_Overlapping_ReturnsTrue()
- {
- var circleCenter = new Vector2(5, 5);
- var circleRadius = 5.0f;
- var boxMin = new Vector2(0, 0);
- var boxMax = new Vector2(10, 10);
- bool result = Collision2D.IntersectsCircleAabb(circleCenter, circleRadius, boxMin, boxMax);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsCircleAabb_Separated_ReturnsFalse()
- {
- var circleCenter = new Vector2(0, 0);
- var circleRadius = 5.0f;
- var boxMin = new Vector2(20, 20);
- var boxMax = new Vector2(30, 30);
- bool result = Collision2D.IntersectsCircleAabb(circleCenter, circleRadius, boxMin, boxMax);
- Assert.False(result);
- }
- #endregion
- #region IntersectsCircleObb Tests
- [Fact]
- public void IntersectsCircleObb_Overlapping_ReturnsTrue()
- {
- var circleCenter = new Vector2(5, 5);
- var circleRadius = 5.0f;
- var obbCenter = new Vector2(8, 8);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(3, 3);
- bool result = Collision2D.IntersectsCircleObb(circleCenter, circleRadius, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsCircleObb_Separated_ReturnsFalse()
- {
- var circleCenter = new Vector2(0, 0);
- var circleRadius = 5.0f;
- var obbCenter = new Vector2(20, 20);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(3, 3);
- bool result = Collision2D.IntersectsCircleObb(circleCenter, circleRadius, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
- Assert.False(result);
- }
- #endregion
- #region IntersectsCircleConvexPolygon Tests
- [Fact]
- public void IntersectsCircleConvexPolygon_Overlapping_ReturnsTrue()
- {
- var circleCenter = new Vector2(5, 5);
- var circleRadius = 5.0f;
- var vertices = new[]
- {
- new Vector2(8, 8),
- new Vector2(15, 8),
- new Vector2(15, 15),
- new Vector2(8, 15)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- bool result = Collision2D.IntersectsCircleConvexPolygon(circleCenter, circleRadius, vertices, normals);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsCircleConvexPolygon_Separated_ReturnsFalse()
- {
- var circleCenter = new Vector2(0, 0);
- var circleRadius = 5.0f;
- var vertices = new[]
- {
- new Vector2(20, 20),
- new Vector2(30, 20),
- new Vector2(30, 30),
- new Vector2(20, 30)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- bool result = Collision2D.IntersectsCircleConvexPolygon(circleCenter, circleRadius, vertices, normals);
- Assert.False(result);
- }
- #endregion
- #region IntersectsCircleCapsule Tests
- [Fact]
- public void IntersectsCircleCapsule_Overlapping_ReturnsTrue()
- {
- var circleCenter = new Vector2(5, 5);
- var circleRadius = 5.0f;
- var capsuleA = new Vector2(8, 8);
- var capsuleB = new Vector2(15, 8);
- var capsuleRadius = 2.0f;
- bool result = Collision2D.IntersectsCircleCapsule(circleCenter, circleRadius, capsuleA, capsuleB, capsuleRadius);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsCircleCapsule_Separated_ReturnsFalse()
- {
- var circleCenter = new Vector2(0, 0);
- var circleRadius = 5.0f;
- var capsuleA = new Vector2(20, 20);
- var capsuleB = new Vector2(30, 30);
- var capsuleRadius = 2.0f;
- bool result = Collision2D.IntersectsCircleCapsule(circleCenter, circleRadius, capsuleA, capsuleB, capsuleRadius);
- Assert.False(result);
- }
- #endregion
- #region IntersectsObbObb Tests
- [Fact]
- public void IntersectsObbObb_Overlapping_ReturnsTrue()
- {
- var aCenter = new Vector2(5, 5);
- var aAxisX = Vector2.UnitX;
- var aAxisY = Vector2.UnitY;
- var aHalf = new Vector2(5, 5);
- var bCenter = new Vector2(8, 8);
- var bAxisX = Vector2.UnitX;
- var bAxisY = Vector2.UnitY;
- var bHalf = new Vector2(3, 3);
- bool result = Collision2D.IntersectsObbObb(aCenter, aAxisX, aAxisY, aHalf, bCenter, bAxisX, bAxisY, bHalf);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsObbObb_Separated_ReturnsFalse()
- {
- var aCenter = new Vector2(0, 0);
- var aAxisX = Vector2.UnitX;
- var aAxisY = Vector2.UnitY;
- var aHalf = new Vector2(5, 5);
- var bCenter = new Vector2(20, 20);
- var bAxisX = Vector2.UnitX;
- var bAxisY = Vector2.UnitY;
- var bHalf = new Vector2(3, 3);
- bool result = Collision2D.IntersectsObbObb(aCenter, aAxisX, aAxisY, aHalf, bCenter, bAxisX, bAxisY, bHalf);
- Assert.False(result);
- }
- #endregion
- #region IntersectsObbCapsule Tests
- [Fact]
- public void IntersectsObbCapsule_Overlapping_ReturnsTrue()
- {
- var obbCenter = new Vector2(5, 5);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(5, 5);
- var capsuleA = new Vector2(8, 8);
- var capsuleB = new Vector2(15, 8);
- var capsuleRadius = 2.0f;
- bool result = Collision2D.IntersectsObbCapsule(obbCenter, obbAxisX, obbAxisY, obbHalfExtents, capsuleA, capsuleB, capsuleRadius);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsObbCapsule_Separated_ReturnsFalse()
- {
- var obbCenter = new Vector2(0, 0);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(5, 5);
- var capsuleA = new Vector2(20, 20);
- var capsuleB = new Vector2(30, 30);
- var capsuleRadius = 2.0f;
- bool result = Collision2D.IntersectsObbCapsule(obbCenter, obbAxisX, obbAxisY, obbHalfExtents, capsuleA, capsuleB, capsuleRadius);
- Assert.False(result);
- }
- #endregion
- #region IntersectsObbConvexPolygon Tests
- [Fact]
- public void IntersectsObbConvexPolygon_Overlapping_ReturnsTrue()
- {
- var obbCenter = new Vector2(5, 5);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(5, 5);
- var vertices = new[]
- {
- new Vector2(8, 8),
- new Vector2(15, 8),
- new Vector2(15, 15),
- new Vector2(8, 15)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- bool result = Collision2D.IntersectsObbConvexPolygon(obbCenter, obbAxisX, obbAxisY, obbHalfExtents, vertices, normals);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsObbConvexPolygon_Separated_ReturnsFalse()
- {
- var obbCenter = new Vector2(0, 0);
- var obbAxisX = Vector2.UnitX;
- var obbAxisY = Vector2.UnitY;
- var obbHalfExtents = new Vector2(5, 5);
- var vertices = new[]
- {
- new Vector2(20, 20),
- new Vector2(30, 20),
- new Vector2(30, 30),
- new Vector2(20, 30)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- bool result = Collision2D.IntersectsObbConvexPolygon(obbCenter, obbAxisX, obbAxisY, obbHalfExtents, vertices, normals);
- Assert.False(result);
- }
- #endregion
- #region IntersectsCapsuleCapsule Tests
- [Fact]
- public void IntersectsCapsuleCapsule_Overlapping_ReturnsTrue()
- {
- var a0 = new Vector2(0, 0);
- var a1 = new Vector2(10, 0);
- var aRadius = 3.0f;
- var b0 = new Vector2(8, 0);
- var b1 = new Vector2(15, 0);
- var bRadius = 3.0f;
- bool result = Collision2D.IntersectsCapsuleCapsule(a0, a1, aRadius, b0, b1, bRadius);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsCapsuleCapsule_Separated_ReturnsFalse()
- {
- var a0 = new Vector2(0, 0);
- var a1 = new Vector2(10, 0);
- var aRadius = 2.0f;
- var b0 = new Vector2(20, 20);
- var b1 = new Vector2(30, 30);
- var bRadius = 2.0f;
- bool result = Collision2D.IntersectsCapsuleCapsule(a0, a1, aRadius, b0, b1, bRadius);
- Assert.False(result);
- }
- #endregion
- #region IntersectsCapsuleConvexPolygon Tests
- [Fact]
- public void IntersectsCapsuleConvexPolygon_Overlapping_ReturnsTrue()
- {
- var capsuleA = new Vector2(5, 5);
- var capsuleB = new Vector2(15, 5);
- var capsuleRadius = 3.0f;
- var vertices = new[]
- {
- new Vector2(8, 8),
- new Vector2(15, 8),
- new Vector2(15, 15),
- new Vector2(8, 15)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- bool result = Collision2D.IntersectsCapsuleConvexPolygon(capsuleA, capsuleB, capsuleRadius, vertices, normals);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsCapsuleConvexPolygon_Separated_ReturnsFalse()
- {
- var capsuleA = new Vector2(0, 0);
- var capsuleB = new Vector2(10, 0);
- var capsuleRadius = 2.0f;
- var vertices = new[]
- {
- new Vector2(20, 20),
- new Vector2(30, 20),
- new Vector2(30, 30),
- new Vector2(20, 30)
- };
- var normals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- bool result = Collision2D.IntersectsCapsuleConvexPolygon(capsuleA, capsuleB, capsuleRadius, vertices, normals);
- Assert.False(result);
- }
- #endregion
- #region IntersectsConvexPolygonConvexPolygon Tests
- [Fact]
- public void IntersectsConvexPolygonConvexPolygon_Overlapping_ReturnsTrue()
- {
- var aVertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var aNormals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var bVertices = new[]
- {
- new Vector2(5, 5),
- new Vector2(15, 5),
- new Vector2(15, 15),
- new Vector2(5, 15)
- };
- var bNormals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- bool result = Collision2D.IntersectsConvexPolygonConvexPolygon(aVertices, aNormals, bVertices, bNormals);
- Assert.True(result);
- }
- [Fact]
- public void IntersectsConvexPolygonConvexPolygon_Separated_ReturnsFalse()
- {
- var aVertices = new[]
- {
- new Vector2(0, 0),
- new Vector2(10, 0),
- new Vector2(10, 10),
- new Vector2(0, 10)
- };
- var aNormals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- var bVertices = new[]
- {
- new Vector2(20, 20),
- new Vector2(30, 20),
- new Vector2(30, 30),
- new Vector2(20, 30)
- };
- var bNormals = new[]
- {
- new Vector2(0, -1),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(-1, 0)
- };
- bool result = Collision2D.IntersectsConvexPolygonConvexPolygon(aVertices, aNormals, bVertices, bNormals);
- Assert.False(result);
- }
- #endregion
- #endregion
- }
- }
|