Collision2DTest.cs 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517
  1. // MonoGame - Copyright (C) MonoGame Foundation, Inc
  2. // This file is subject to the terms and conditions defined in
  3. // file 'LICENSE.txt', which is part of this source code package.
  4. using System;
  5. using Microsoft.Xna.Framework;
  6. namespace MonoGame.Extended.Tests
  7. {
  8. public sealed class Collision2DTest
  9. {
  10. #region Helper Methods
  11. #region IsValidPolygon Tests
  12. [Fact]
  13. public void IsValidPolygon_ValidPolygon_ReturnsTrue()
  14. {
  15. var vertices = new[]
  16. {
  17. new Vector2(0, 0),
  18. new Vector2(1, 0),
  19. new Vector2(0, 1)
  20. };
  21. var normals = new[]
  22. {
  23. new Vector2(0, -1),
  24. Vector2.Normalize(new Vector2(1, 1)),
  25. new Vector2(-1, 0)
  26. };
  27. bool result = Collision2D.IsValidPolygon(vertices, normals);
  28. Assert.True(result);
  29. }
  30. [Fact]
  31. public void IsValidPolygon_NullVertices_ReturnsFalse()
  32. {
  33. var normals = new[] { Vector2.UnitX, Vector2.UnitY, -Vector2.UnitX };
  34. bool result = Collision2D.IsValidPolygon(null, normals);
  35. Assert.False(result);
  36. }
  37. [Fact]
  38. public void IsValidPolygon_NullNormals_ReturnsFalse()
  39. {
  40. var vertices = new[]
  41. {
  42. new Vector2(0, 0),
  43. new Vector2(1, 0),
  44. new Vector2(0, 1)
  45. };
  46. bool result = Collision2D.IsValidPolygon(vertices, null);
  47. Assert.False(result);
  48. }
  49. [Fact]
  50. public void IsValidPolygon_MismatchedArrayLengths_ReturnsFalse()
  51. {
  52. var vertices = new[]
  53. {
  54. new Vector2(0, 0),
  55. new Vector2(1, 0),
  56. new Vector2(0, 1)
  57. };
  58. var normals = new[]
  59. {
  60. Vector2.UnitX,
  61. Vector2.UnitY
  62. };
  63. bool result = Collision2D.IsValidPolygon(vertices, normals);
  64. Assert.False(result);
  65. }
  66. [Fact]
  67. public void IsValidPolygon_LessThanThreeVertices_ReturnsFalse()
  68. {
  69. var vertices = new[]
  70. {
  71. new Vector2(0, 0),
  72. new Vector2(1, 0)
  73. };
  74. var normals = new[]
  75. {
  76. Vector2.UnitX,
  77. Vector2.UnitY
  78. };
  79. bool result = Collision2D.IsValidPolygon(vertices, normals);
  80. Assert.False(result);
  81. }
  82. #endregion
  83. #region ClipInterval Tests (ref overload)
  84. [Fact]
  85. public void ClipInterval_OverlappingIntervals_ReturnsTrue()
  86. {
  87. float tMin = 0.0f;
  88. float tMax = 10.0f;
  89. float clipMin = 3.0f;
  90. float clipMax = 8.0f;
  91. bool result = Collision2D.ClipInterval(ref tMin, ref tMax, clipMin, clipMax);
  92. Assert.True(result);
  93. Assert.Equal(3.0f, tMin);
  94. Assert.Equal(8.0f, tMax);
  95. }
  96. [Fact]
  97. public void ClipInterval_NonOverlappingIntervals_ReturnsFalse()
  98. {
  99. float tMin = 0.0f;
  100. float tMax = 5.0f;
  101. float clipMin = 10.0f;
  102. float clipMax = 15.0f;
  103. bool result = Collision2D.ClipInterval(ref tMin, ref tMax, clipMin, clipMax);
  104. Assert.False(result);
  105. }
  106. [Fact]
  107. public void ClipInterval_TouchingAtBoundary_ReturnsTrue()
  108. {
  109. float tMin = 0.0f;
  110. float tMax = 5.0f;
  111. float clipMin = 5.0f;
  112. float clipMax = 10.0f;
  113. bool result = Collision2D.ClipInterval(ref tMin, ref tMax, clipMin, clipMax);
  114. Assert.True(result);
  115. Assert.Equal(5.0f, tMin);
  116. Assert.Equal(5.0f, tMax);
  117. }
  118. [Fact]
  119. public void ClipInterval_ClippingWiderThanOriginal_KeepsOriginalBounds()
  120. {
  121. float tMin = 3.0f;
  122. float tMax = 7.0f;
  123. float clipMin = 0.0f;
  124. float clipMax = 10.0f;
  125. bool result = Collision2D.ClipInterval(ref tMin, ref tMax, clipMin, clipMax);
  126. Assert.True(result);
  127. Assert.Equal(3.0f, tMin);
  128. Assert.Equal(7.0f, tMax);
  129. }
  130. [Fact]
  131. public void ClipInterval_NegativeIntervals_WorksCorrectly()
  132. {
  133. float tMin = -10.0f;
  134. float tMax = -5.0f;
  135. float clipMin = -8.0f;
  136. float clipMax = -3.0f;
  137. bool result = Collision2D.ClipInterval(ref tMin, ref tMax, clipMin, clipMax);
  138. Assert.True(result);
  139. Assert.Equal(-8.0f, tMin);
  140. Assert.Equal(-5.0f, tMax);
  141. }
  142. #endregion
  143. #region ClipInterval Tests (out overload)
  144. [Fact]
  145. public void ClipIntervalOut_OverlappingIntervals_ReturnsClippedValues()
  146. {
  147. float tEnter = 0.0f;
  148. float tExit = 10.0f;
  149. float tLower = 3.0f;
  150. float tUpper = 8.0f;
  151. bool result = Collision2D.ClipInterval(tEnter, tExit, tLower, tUpper, out float clippedEnter, out float clippedExit);
  152. Assert.True(result);
  153. Assert.Equal(3.0f, clippedEnter);
  154. Assert.Equal(8.0f, clippedExit);
  155. }
  156. [Fact]
  157. public void ClipIntervalOut_NonOverlappingIntervals_ReturnsFalse()
  158. {
  159. float tEnter = 0.0f;
  160. float tExit = 5.0f;
  161. float tLower = 10.0f;
  162. float tUpper = 15.0f;
  163. bool result = Collision2D.ClipInterval(tEnter, tExit, tLower, tUpper, out float clippedEnter, out float clippedExit);
  164. Assert.False(result);
  165. }
  166. [Fact]
  167. public void ClipIntervalOut_TouchingAtBoundary_ReturnsTrue()
  168. {
  169. float tEnter = 0.0f;
  170. float tExit = 5.0f;
  171. float tLower = 5.0f;
  172. float tUpper = 10.0f;
  173. bool result = Collision2D.ClipInterval(tEnter, tExit, tLower, tUpper, out float clippedEnter, out float clippedExit);
  174. Assert.True(result);
  175. Assert.Equal(5.0f, clippedEnter);
  176. Assert.Equal(5.0f, clippedExit);
  177. }
  178. [Fact]
  179. public void ClipIntervalOut_ClippingWiderThanOriginal_ReturnsOriginal()
  180. {
  181. float tEnter = 3.0f;
  182. float tExit = 7.0f;
  183. float tLower = 0.0f;
  184. float tUpper = 10.0f;
  185. bool result = Collision2D.ClipInterval(tEnter, tExit, tLower, tUpper, out float clippedEnter, out float clippedExit);
  186. Assert.True(result);
  187. Assert.Equal(3.0f, clippedEnter);
  188. Assert.Equal(7.0f, clippedExit);
  189. }
  190. #endregion
  191. #region IntervalsOverlap Tests
  192. [Fact]
  193. public void IntervalsOverlap_Overlapping_ReturnsTrue()
  194. {
  195. float minA = 0.0f;
  196. float maxA = 10.0f;
  197. float minB = 5.0f;
  198. float maxB = 15.0f;
  199. bool result = Collision2D.IntervalsOverlap(minA, maxA, minB, maxB);
  200. Assert.True(result);
  201. }
  202. [Fact]
  203. public void IntervalsOverlap_Separated_ReturnsFalse()
  204. {
  205. float minA = 0.0f;
  206. float maxA = 5.0f;
  207. float minB = 10.0f;
  208. float maxB = 15.0f;
  209. bool result = Collision2D.IntervalsOverlap(minA, maxA, minB, maxB);
  210. Assert.False(result);
  211. }
  212. [Fact]
  213. public void IntervalsOverlap_TouchingAtPoint_ReturnsTrue()
  214. {
  215. float minA = 0.0f;
  216. float maxA = 5.0f;
  217. float minB = 5.0f;
  218. float maxB = 10.0f;
  219. bool result = Collision2D.IntervalsOverlap(minA, maxA, minB, maxB);
  220. Assert.True(result);
  221. }
  222. [Fact]
  223. public void IntervalsOverlap_NegativeIntervals_WorksCorrectly()
  224. {
  225. float minA = -10.0f;
  226. float maxA = -5.0f;
  227. float minB = -8.0f;
  228. float maxB = -3.0f;
  229. bool result = Collision2D.IntervalsOverlap(minA, maxA, minB, maxB);
  230. Assert.True(result);
  231. }
  232. [Fact]
  233. public void IntervalsOverlap_NearlyTouching_ReturnsFalse()
  234. {
  235. float minA = 0.0f;
  236. float maxA = 5.0f;
  237. float minB = 5.0f + Collision2D.Epsilon * 2.0f;
  238. float maxB = 10.0f;
  239. bool result = Collision2D.IntervalsOverlap(minA, maxA, minB, maxB);
  240. Assert.False(result);
  241. }
  242. #endregion
  243. #endregion
  244. #region Containment - AABB Methods
  245. #region ContainsAabbPoint Tests
  246. [Fact]
  247. public void ContainsAabbPoint_PointInside_ReturnsContains()
  248. {
  249. var point = new Vector2(5, 5);
  250. var min = new Vector2(0, 0);
  251. var max = new Vector2(10, 10);
  252. var result = Collision2D.ContainsAabbPoint(point, min, max);
  253. Assert.Equal(ContainmentType.Contains, result);
  254. }
  255. [Fact]
  256. public void ContainsAabbPoint_PointOutside_ReturnsDisjoint()
  257. {
  258. var point = new Vector2(15, 5);
  259. var min = new Vector2(0, 0);
  260. var max = new Vector2(10, 10);
  261. var result = Collision2D.ContainsAabbPoint(point, min, max);
  262. Assert.Equal(ContainmentType.Disjoint, result);
  263. }
  264. [Fact]
  265. public void ContainsAabbPoint_PointOnBoundary_ReturnsContains()
  266. {
  267. var point = new Vector2(10, 5);
  268. var min = new Vector2(0, 0);
  269. var max = new Vector2(10, 10);
  270. var result = Collision2D.ContainsAabbPoint(point, min, max);
  271. Assert.Equal(ContainmentType.Contains, result);
  272. }
  273. #endregion
  274. #region ContainsAabbAabb Tests
  275. [Fact]
  276. public void ContainsAabbAabb_CompletelyContains_ReturnsContains()
  277. {
  278. var aMin = new Vector2(0, 0);
  279. var aMax = new Vector2(10, 10);
  280. var bMin = new Vector2(2, 2);
  281. var bMax = new Vector2(8, 8);
  282. var result = Collision2D.ContainsAabbAabb(aMin, aMax, bMin, bMax);
  283. Assert.Equal(ContainmentType.Contains, result);
  284. }
  285. [Fact]
  286. public void ContainsAabbAabb_NoOverlap_ReturnsDisjoint()
  287. {
  288. var aMin = new Vector2(0, 0);
  289. var aMax = new Vector2(10, 10);
  290. var bMin = new Vector2(15, 15);
  291. var bMax = new Vector2(20, 20);
  292. var result = Collision2D.ContainsAabbAabb(aMin, aMax, bMin, bMax);
  293. Assert.Equal(ContainmentType.Disjoint, result);
  294. }
  295. [Fact]
  296. public void ContainsAabbAabb_PartialOverlap_ReturnsIntersects()
  297. {
  298. var aMin = new Vector2(0, 0);
  299. var aMax = new Vector2(10, 10);
  300. var bMin = new Vector2(5, 5);
  301. var bMax = new Vector2(15, 15);
  302. var result = Collision2D.ContainsAabbAabb(aMin, aMax, bMin, bMax);
  303. Assert.Equal(ContainmentType.Intersects, result);
  304. }
  305. #endregion
  306. #region ContainsAabbCircle Tests
  307. [Fact]
  308. public void ContainsAabbCircle_CircleCompletelyInside_ReturnsContains()
  309. {
  310. var boxMin = new Vector2(0, 0);
  311. var boxMax = new Vector2(10, 10);
  312. var circleCenter = new Vector2(5, 5);
  313. var circleRadius = 2.0f;
  314. var result = Collision2D.ContainsAabbCircle(boxMin, boxMax, circleCenter, circleRadius);
  315. Assert.Equal(ContainmentType.Contains, result);
  316. }
  317. [Fact]
  318. public void ContainsAabbCircle_CircleCompletelyOutside_ReturnsDisjoint()
  319. {
  320. var boxMin = new Vector2(0, 0);
  321. var boxMax = new Vector2(10, 10);
  322. var circleCenter = new Vector2(20, 20);
  323. var circleRadius = 3.0f;
  324. var result = Collision2D.ContainsAabbCircle(boxMin, boxMax, circleCenter, circleRadius);
  325. Assert.Equal(ContainmentType.Disjoint, result);
  326. }
  327. [Fact]
  328. public void ContainsAabbCircle_CirclePartiallyInside_ReturnsIntersects()
  329. {
  330. var boxMin = new Vector2(0, 0);
  331. var boxMax = new Vector2(10, 10);
  332. var circleCenter = new Vector2(5, 5);
  333. var circleRadius = 7.0f;
  334. var result = Collision2D.ContainsAabbCircle(boxMin, boxMax, circleCenter, circleRadius);
  335. Assert.Equal(ContainmentType.Intersects, result);
  336. }
  337. #endregion
  338. #region ContainsAabbObb Tests
  339. [Fact]
  340. public void ContainsAabbObb_ObbCompletelyInside_ReturnsContains()
  341. {
  342. var aabbMin = new Vector2(0, 0);
  343. var aabbMax = new Vector2(10, 10);
  344. var obbCenter = new Vector2(5, 5);
  345. var obbAxisX = Vector2.UnitX;
  346. var obbAxisY = Vector2.UnitY;
  347. var obbHalfExtents = new Vector2(2, 2);
  348. var result = Collision2D.ContainsAabbObb(aabbMin, aabbMax, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
  349. Assert.Equal(ContainmentType.Contains, result);
  350. }
  351. [Fact]
  352. public void ContainsAabbObb_ObbCompletelyOutside_ReturnsDisjoint()
  353. {
  354. var aabbMin = new Vector2(0, 0);
  355. var aabbMax = new Vector2(10, 10);
  356. var obbCenter = new Vector2(20, 20);
  357. var obbAxisX = Vector2.UnitX;
  358. var obbAxisY = Vector2.UnitY;
  359. var obbHalfExtents = new Vector2(2, 2);
  360. var result = Collision2D.ContainsAabbObb(aabbMin, aabbMax, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
  361. Assert.Equal(ContainmentType.Disjoint, result);
  362. }
  363. [Fact]
  364. public void ContainsAabbObb_ObbPartiallyInside_ReturnsIntersects()
  365. {
  366. var aabbMin = new Vector2(0, 0);
  367. var aabbMax = new Vector2(10, 10);
  368. var obbCenter = new Vector2(8, 8);
  369. var obbAxisX = Vector2.UnitX;
  370. var obbAxisY = Vector2.UnitY;
  371. var obbHalfExtents = new Vector2(5, 5);
  372. var result = Collision2D.ContainsAabbObb(aabbMin, aabbMax, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
  373. Assert.Equal(ContainmentType.Intersects, result);
  374. }
  375. #endregion
  376. #region ContainsAabbCapsule Tests
  377. [Fact]
  378. public void ContainsAabbCapsule_CapsuleCompletelyInside_ReturnsContains()
  379. {
  380. var boxMin = new Vector2(0, 0);
  381. var boxMax = new Vector2(10, 10);
  382. var capsuleA = new Vector2(3, 5);
  383. var capsuleB = new Vector2(7, 5);
  384. var capsuleRadius = 1.0f;
  385. var result = Collision2D.ContainsAabbCapsule(boxMin, boxMax, capsuleA, capsuleB, capsuleRadius);
  386. Assert.Equal(ContainmentType.Contains, result);
  387. }
  388. [Fact]
  389. public void ContainsAabbCapsule_CapsuleCompletelyOutside_ReturnsDisjoint()
  390. {
  391. var boxMin = new Vector2(0, 0);
  392. var boxMax = new Vector2(10, 10);
  393. var capsuleA = new Vector2(15, 15);
  394. var capsuleB = new Vector2(20, 20);
  395. var capsuleRadius = 1.0f;
  396. var result = Collision2D.ContainsAabbCapsule(boxMin, boxMax, capsuleA, capsuleB, capsuleRadius);
  397. Assert.Equal(ContainmentType.Disjoint, result);
  398. }
  399. [Fact]
  400. public void ContainsAabbCapsule_CapsulePartiallyInside_ReturnsIntersects()
  401. {
  402. var boxMin = new Vector2(0, 0);
  403. var boxMax = new Vector2(10, 10);
  404. var capsuleA = new Vector2(5, 5);
  405. var capsuleB = new Vector2(15, 5);
  406. var capsuleRadius = 1.0f;
  407. var result = Collision2D.ContainsAabbCapsule(boxMin, boxMax, capsuleA, capsuleB, capsuleRadius);
  408. Assert.Equal(ContainmentType.Intersects, result);
  409. }
  410. #endregion
  411. #region ContainsAabbConvexPolygon Tests
  412. [Fact]
  413. public void ContainsAabbConvexPolygon_PolygonCompletelyInside_ReturnsContains()
  414. {
  415. var boxMin = new Vector2(0, 0);
  416. var boxMax = new Vector2(10, 10);
  417. var vertices = new[]
  418. {
  419. new Vector2(3, 3),
  420. new Vector2(7, 3),
  421. new Vector2(7, 7),
  422. new Vector2(3, 7)
  423. };
  424. var normals = new[]
  425. {
  426. new Vector2(0, -1),
  427. new Vector2(1, 0),
  428. new Vector2(0, 1),
  429. new Vector2(-1, 0)
  430. };
  431. var result = Collision2D.ContainsAabbConvexPolygon(boxMin, boxMax, vertices, normals);
  432. Assert.Equal(ContainmentType.Contains, result);
  433. }
  434. [Fact]
  435. public void ContainsAabbConvexPolygon_PolygonCompletelyOutside_ReturnsDisjoint()
  436. {
  437. var boxMin = new Vector2(0, 0);
  438. var boxMax = new Vector2(10, 10);
  439. var vertices = new[]
  440. {
  441. new Vector2(15, 15),
  442. new Vector2(20, 15),
  443. new Vector2(20, 20),
  444. new Vector2(15, 20)
  445. };
  446. var normals = new[]
  447. {
  448. new Vector2(0, -1),
  449. new Vector2(1, 0),
  450. new Vector2(0, 1),
  451. new Vector2(-1, 0)
  452. };
  453. var result = Collision2D.ContainsAabbConvexPolygon(boxMin, boxMax, vertices, normals);
  454. Assert.Equal(ContainmentType.Disjoint, result);
  455. }
  456. [Fact]
  457. public void ContainsAabbConvexPolygon_PolygonPartiallyInside_ReturnsIntersects()
  458. {
  459. var boxMin = new Vector2(0, 0);
  460. var boxMax = new Vector2(10, 10);
  461. var vertices = new[]
  462. {
  463. new Vector2(5, 5),
  464. new Vector2(15, 5),
  465. new Vector2(15, 15),
  466. new Vector2(5, 15)
  467. };
  468. var normals = new[]
  469. {
  470. new Vector2(0, -1),
  471. new Vector2(1, 0),
  472. new Vector2(0, 1),
  473. new Vector2(-1, 0)
  474. };
  475. var result = Collision2D.ContainsAabbConvexPolygon(boxMin, boxMax, vertices, normals);
  476. Assert.Equal(ContainmentType.Intersects, result);
  477. }
  478. #endregion
  479. #endregion
  480. #region Containment - Circle Methods
  481. #region ContainsCirclePoint Tests
  482. [Fact]
  483. public void ContainsCirclePoint_PointInside_ReturnsContains()
  484. {
  485. var point = new Vector2(3, 0);
  486. var center = new Vector2(0, 0);
  487. var radius = 5.0f;
  488. var result = Collision2D.ContainsCirclePoint(point, center, radius);
  489. Assert.Equal(ContainmentType.Contains, result);
  490. }
  491. [Fact]
  492. public void ContainsCirclePoint_PointOutside_ReturnsDisjoint()
  493. {
  494. var point = new Vector2(10, 0);
  495. var center = new Vector2(0, 0);
  496. var radius = 5.0f;
  497. var result = Collision2D.ContainsCirclePoint(point, center, radius);
  498. Assert.Equal(ContainmentType.Disjoint, result);
  499. }
  500. [Fact]
  501. public void ContainsCirclePoint_PointOnCircumference_ReturnsContains()
  502. {
  503. var point = new Vector2(5, 0);
  504. var center = new Vector2(0, 0);
  505. var radius = 5.0f;
  506. var result = Collision2D.ContainsCirclePoint(point, center, radius);
  507. Assert.Equal(ContainmentType.Contains, result);
  508. }
  509. #endregion
  510. #region ContainsCircleAabb Tests
  511. [Fact]
  512. public void ContainsCircleAabb_AabbCompletelyInside_ReturnsContains()
  513. {
  514. var circleCenter = new Vector2(10, 10);
  515. var circleRadius = 10.0f;
  516. var aabbMin = new Vector2(8, 8);
  517. var aabbMax = new Vector2(12, 12);
  518. var result = Collision2D.ContainsCircleAabb(circleCenter, circleRadius, aabbMin, aabbMax);
  519. Assert.Equal(ContainmentType.Contains, result);
  520. }
  521. [Fact]
  522. public void ContainsCircleAabb_AabbCompletelyOutside_ReturnsDisjoint()
  523. {
  524. var circleCenter = new Vector2(0, 0);
  525. var circleRadius = 5.0f;
  526. var aabbMin = new Vector2(20, 20);
  527. var aabbMax = new Vector2(25, 25);
  528. var result = Collision2D.ContainsCircleAabb(circleCenter, circleRadius, aabbMin, aabbMax);
  529. Assert.Equal(ContainmentType.Disjoint, result);
  530. }
  531. [Fact]
  532. public void ContainsCircleAabb_AabbPartiallyInside_ReturnsIntersects()
  533. {
  534. var circleCenter = new Vector2(0, 0);
  535. var circleRadius = 5.0f;
  536. var aabbMin = new Vector2(3, 3);
  537. var aabbMax = new Vector2(8, 8);
  538. var result = Collision2D.ContainsCircleAabb(circleCenter, circleRadius, aabbMin, aabbMax);
  539. Assert.Equal(ContainmentType.Intersects, result);
  540. }
  541. #endregion
  542. #region ContainsCircleCircle Tests
  543. [Fact]
  544. public void ContainsCircleCircle_CompletelyContains_ReturnsContains()
  545. {
  546. var aCenter = new Vector2(0, 0);
  547. var aRadius = 10.0f;
  548. var bCenter = new Vector2(2, 0);
  549. var bRadius = 3.0f;
  550. var result = Collision2D.ContainsCircleCircle(aCenter, aRadius, bCenter, bRadius);
  551. Assert.Equal(ContainmentType.Contains, result);
  552. }
  553. [Fact]
  554. public void ContainsCircleCircle_Separated_ReturnsDisjoint()
  555. {
  556. var aCenter = new Vector2(0, 0);
  557. var aRadius = 5.0f;
  558. var bCenter = new Vector2(20, 0);
  559. var bRadius = 3.0f;
  560. var result = Collision2D.ContainsCircleCircle(aCenter, aRadius, bCenter, bRadius);
  561. Assert.Equal(ContainmentType.Disjoint, result);
  562. }
  563. [Fact]
  564. public void ContainsCircleCircle_PartialOverlap_ReturnsIntersects()
  565. {
  566. var aCenter = new Vector2(0, 0);
  567. var aRadius = 5.0f;
  568. var bCenter = new Vector2(7, 0);
  569. var bRadius = 4.0f;
  570. var result = Collision2D.ContainsCircleCircle(aCenter, aRadius, bCenter, bRadius);
  571. Assert.Equal(ContainmentType.Intersects, result);
  572. }
  573. #endregion
  574. #region ContainsCircleObb Tests
  575. [Fact]
  576. public void ContainsCircleObb_ObbCompletelyInside_ReturnsContains()
  577. {
  578. var circleCenter = new Vector2(10, 10);
  579. var circleRadius = 10.0f;
  580. var obbCenter = new Vector2(10, 10);
  581. var obbAxisX = Vector2.UnitX;
  582. var obbAxisY = Vector2.UnitY;
  583. var obbHalfExtents = new Vector2(2, 2);
  584. var result = Collision2D.ContainsCircleObb(circleCenter, circleRadius, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
  585. Assert.Equal(ContainmentType.Contains, result);
  586. }
  587. [Fact]
  588. public void ContainsCircleObb_ObbCompletelyOutside_ReturnsDisjoint()
  589. {
  590. var circleCenter = new Vector2(0, 0);
  591. var circleRadius = 5.0f;
  592. var obbCenter = new Vector2(20, 20);
  593. var obbAxisX = Vector2.UnitX;
  594. var obbAxisY = Vector2.UnitY;
  595. var obbHalfExtents = new Vector2(2, 2);
  596. var result = Collision2D.ContainsCircleObb(circleCenter, circleRadius, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
  597. Assert.Equal(ContainmentType.Disjoint, result);
  598. }
  599. [Fact]
  600. public void ContainsCircleObb_ObbPartiallyInside_ReturnsIntersects()
  601. {
  602. var circleCenter = new Vector2(0, 0);
  603. var circleRadius = 5.0f;
  604. var obbCenter = new Vector2(4, 4);
  605. var obbAxisX = Vector2.UnitX;
  606. var obbAxisY = Vector2.UnitY;
  607. var obbHalfExtents = new Vector2(3, 3);
  608. var result = Collision2D.ContainsCircleObb(circleCenter, circleRadius, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
  609. Assert.Equal(ContainmentType.Intersects, result);
  610. }
  611. #endregion
  612. #region ContainsCircleCapsule Tests
  613. [Fact]
  614. public void ContainsCircleCapsule_CapsuleCompletelyInside_ReturnsContains()
  615. {
  616. var circleCenter = new Vector2(10, 10);
  617. var circleRadius = 10.0f;
  618. var capsuleA = new Vector2(8, 10);
  619. var capsuleB = new Vector2(12, 10);
  620. var capsuleRadius = 1.0f;
  621. var result = Collision2D.ContainsCircleCapsule(circleCenter, circleRadius, capsuleA, capsuleB, capsuleRadius);
  622. Assert.Equal(ContainmentType.Contains, result);
  623. }
  624. [Fact]
  625. public void ContainsCircleCapsule_CapsuleCompletelyOutside_ReturnsDisjoint()
  626. {
  627. var circleCenter = new Vector2(0, 0);
  628. var circleRadius = 5.0f;
  629. var capsuleA = new Vector2(20, 20);
  630. var capsuleB = new Vector2(25, 25);
  631. var capsuleRadius = 1.0f;
  632. var result = Collision2D.ContainsCircleCapsule(circleCenter, circleRadius, capsuleA, capsuleB, capsuleRadius);
  633. Assert.Equal(ContainmentType.Disjoint, result);
  634. }
  635. [Fact]
  636. public void ContainsCircleCapsule_CapsulePartiallyInside_ReturnsIntersects()
  637. {
  638. var circleCenter = new Vector2(0, 0);
  639. var circleRadius = 5.0f;
  640. var capsuleA = new Vector2(3, 0);
  641. var capsuleB = new Vector2(10, 0);
  642. var capsuleRadius = 1.0f;
  643. var result = Collision2D.ContainsCircleCapsule(circleCenter, circleRadius, capsuleA, capsuleB, capsuleRadius);
  644. Assert.Equal(ContainmentType.Intersects, result);
  645. }
  646. #endregion
  647. #region ContainsCircleConvexPolygon Tests
  648. [Fact]
  649. public void ContainsCircleConvexPolygon_PolygonCompletelyInside_ReturnsContains()
  650. {
  651. var circleCenter = new Vector2(10, 10);
  652. var circleRadius = 10.0f;
  653. var vertices = new[]
  654. {
  655. new Vector2(9, 9),
  656. new Vector2(11, 9),
  657. new Vector2(11, 11),
  658. new Vector2(9, 11)
  659. };
  660. var normals = new[]
  661. {
  662. new Vector2(0, -1),
  663. new Vector2(1, 0),
  664. new Vector2(0, 1),
  665. new Vector2(-1, 0)
  666. };
  667. var result = Collision2D.ContainsCircleConvexPolygon(circleCenter, circleRadius, vertices, normals);
  668. Assert.Equal(ContainmentType.Contains, result);
  669. }
  670. [Fact]
  671. public void ContainsCircleConvexPolygon_PolygonCompletelyOutside_ReturnsDisjoint()
  672. {
  673. var circleCenter = new Vector2(0, 0);
  674. var circleRadius = 5.0f;
  675. var vertices = new[]
  676. {
  677. new Vector2(20, 20),
  678. new Vector2(25, 20),
  679. new Vector2(25, 25),
  680. new Vector2(20, 25)
  681. };
  682. var normals = new[]
  683. {
  684. new Vector2(0, -1),
  685. new Vector2(1, 0),
  686. new Vector2(0, 1),
  687. new Vector2(-1, 0)
  688. };
  689. var result = Collision2D.ContainsCircleConvexPolygon(circleCenter, circleRadius, vertices, normals);
  690. Assert.Equal(ContainmentType.Disjoint, result);
  691. }
  692. [Fact]
  693. public void ContainsCircleConvexPolygon_PolygonPartiallyInside_ReturnsIntersects()
  694. {
  695. var circleCenter = new Vector2(0, 0);
  696. var circleRadius = 5.0f;
  697. var vertices = new[]
  698. {
  699. new Vector2(3, 3),
  700. new Vector2(8, 3),
  701. new Vector2(8, 8),
  702. new Vector2(3, 8)
  703. };
  704. var normals = new[]
  705. {
  706. new Vector2(0, -1),
  707. new Vector2(1, 0),
  708. new Vector2(0, 1),
  709. new Vector2(-1, 0)
  710. };
  711. var result = Collision2D.ContainsCircleConvexPolygon(circleCenter, circleRadius, vertices, normals);
  712. Assert.Equal(ContainmentType.Intersects, result);
  713. }
  714. #endregion
  715. #endregion
  716. #region Containment - OBB Methods
  717. #region ContainsObbPoint Tests
  718. [Fact]
  719. public void ContainsObbPoint_PointInside_ReturnsContains()
  720. {
  721. var point = new Vector2(5, 5);
  722. var center = new Vector2(5, 5);
  723. var axisX = Vector2.UnitX;
  724. var axisY = Vector2.UnitY;
  725. var halfExtents = new Vector2(3, 3);
  726. var result = Collision2D.ContainsObbPoint(point, center, axisX, axisY, halfExtents);
  727. Assert.Equal(ContainmentType.Contains, result);
  728. }
  729. [Fact]
  730. public void ContainsObbPoint_PointOutside_ReturnsDisjoint()
  731. {
  732. var point = new Vector2(15, 15);
  733. var center = new Vector2(5, 5);
  734. var axisX = Vector2.UnitX;
  735. var axisY = Vector2.UnitY;
  736. var halfExtents = new Vector2(3, 3);
  737. var result = Collision2D.ContainsObbPoint(point, center, axisX, axisY, halfExtents);
  738. Assert.Equal(ContainmentType.Disjoint, result);
  739. }
  740. [Fact]
  741. public void ContainsObbPoint_PointOnEdge_ReturnsContains()
  742. {
  743. var point = new Vector2(8, 5);
  744. var center = new Vector2(5, 5);
  745. var axisX = Vector2.UnitX;
  746. var axisY = Vector2.UnitY;
  747. var halfExtents = new Vector2(3, 3);
  748. var result = Collision2D.ContainsObbPoint(point, center, axisX, axisY, halfExtents);
  749. Assert.Equal(ContainmentType.Contains, result);
  750. }
  751. #endregion
  752. #region ContainsObbObb Tests
  753. [Fact]
  754. public void ContainsObbObb_CompletelyContains_ReturnsContains()
  755. {
  756. var aCenter = new Vector2(5, 5);
  757. var aAxisX = Vector2.UnitX;
  758. var aAxisY = Vector2.UnitY;
  759. var aHalf = new Vector2(5, 5);
  760. var bCenter = new Vector2(5, 5);
  761. var bAxisX = Vector2.UnitX;
  762. var bAxisY = Vector2.UnitY;
  763. var bHalf = new Vector2(2, 2);
  764. var result = Collision2D.ContainsObbObb(aCenter, aAxisX, aAxisY, aHalf, bCenter, bAxisX, bAxisY, bHalf);
  765. Assert.Equal(ContainmentType.Contains, result);
  766. }
  767. [Fact]
  768. public void ContainsObbObb_Separated_ReturnsDisjoint()
  769. {
  770. var aCenter = new Vector2(0, 0);
  771. var aAxisX = Vector2.UnitX;
  772. var aAxisY = Vector2.UnitY;
  773. var aHalf = new Vector2(2, 2);
  774. var bCenter = new Vector2(20, 20);
  775. var bAxisX = Vector2.UnitX;
  776. var bAxisY = Vector2.UnitY;
  777. var bHalf = new Vector2(2, 2);
  778. var result = Collision2D.ContainsObbObb(aCenter, aAxisX, aAxisY, aHalf, bCenter, bAxisX, bAxisY, bHalf);
  779. Assert.Equal(ContainmentType.Disjoint, result);
  780. }
  781. [Fact]
  782. public void ContainsObbObb_PartialOverlap_ReturnsIntersects()
  783. {
  784. var aCenter = new Vector2(5, 5);
  785. var aAxisX = Vector2.UnitX;
  786. var aAxisY = Vector2.UnitY;
  787. var aHalf = new Vector2(5, 5);
  788. var bCenter = new Vector2(8, 8);
  789. var bAxisX = Vector2.UnitX;
  790. var bAxisY = Vector2.UnitY;
  791. var bHalf = new Vector2(5, 5);
  792. var result = Collision2D.ContainsObbObb(aCenter, aAxisX, aAxisY, aHalf, bCenter, bAxisX, bAxisY, bHalf);
  793. Assert.Equal(ContainmentType.Intersects, result);
  794. }
  795. #endregion
  796. #region ContainsObbCapsule Tests
  797. [Fact]
  798. public void ContainsObbCapsule_CapsuleCompletelyInside_ReturnsContains()
  799. {
  800. var boxCenter = new Vector2(10, 10);
  801. var axisX = Vector2.UnitX;
  802. var axisY = Vector2.UnitY;
  803. var halfExtents = new Vector2(5, 5);
  804. var capsuleA = new Vector2(8, 10);
  805. var capsuleB = new Vector2(12, 10);
  806. var capsuleRadius = 1.0f;
  807. var result = Collision2D.ContainsObbCapsule(boxCenter, axisX, axisY, halfExtents, capsuleA, capsuleB, capsuleRadius);
  808. Assert.Equal(ContainmentType.Contains, result);
  809. }
  810. [Fact]
  811. public void ContainsObbCapsule_CapsuleCompletelyOutside_ReturnsDisjoint()
  812. {
  813. var boxCenter = new Vector2(0, 0);
  814. var axisX = Vector2.UnitX;
  815. var axisY = Vector2.UnitY;
  816. var halfExtents = new Vector2(5, 5);
  817. var capsuleA = new Vector2(20, 20);
  818. var capsuleB = new Vector2(25, 25);
  819. var capsuleRadius = 1.0f;
  820. var result = Collision2D.ContainsObbCapsule(boxCenter, axisX, axisY, halfExtents, capsuleA, capsuleB, capsuleRadius);
  821. Assert.Equal(ContainmentType.Disjoint, result);
  822. }
  823. [Fact]
  824. public void ContainsObbCapsule_CapsulePartiallyInside_ReturnsIntersects()
  825. {
  826. var boxCenter = new Vector2(5, 5);
  827. var axisX = Vector2.UnitX;
  828. var axisY = Vector2.UnitY;
  829. var halfExtents = new Vector2(5, 5);
  830. var capsuleA = new Vector2(5, 5);
  831. var capsuleB = new Vector2(15, 5);
  832. var capsuleRadius = 1.0f;
  833. var result = Collision2D.ContainsObbCapsule(boxCenter, axisX, axisY, halfExtents, capsuleA, capsuleB, capsuleRadius);
  834. Assert.Equal(ContainmentType.Intersects, result);
  835. }
  836. #endregion
  837. #region ContainsObbConvexPolygon Tests
  838. [Fact]
  839. public void ContainsObbConvexPolygon_PolygonCompletelyInside_ReturnsContains()
  840. {
  841. var boxCenter = new Vector2(10, 10);
  842. var axisX = Vector2.UnitX;
  843. var axisY = Vector2.UnitY;
  844. var halfExtents = new Vector2(5, 5);
  845. var vertices = new[]
  846. {
  847. new Vector2(8, 8),
  848. new Vector2(12, 8),
  849. new Vector2(12, 12),
  850. new Vector2(8, 12)
  851. };
  852. var normals = new[]
  853. {
  854. new Vector2(0, -1),
  855. new Vector2(1, 0),
  856. new Vector2(0, 1),
  857. new Vector2(-1, 0)
  858. };
  859. var result = Collision2D.ContainsObbConvexPolygon(boxCenter, axisX, axisY, halfExtents, vertices, normals);
  860. Assert.Equal(ContainmentType.Contains, result);
  861. }
  862. [Fact]
  863. public void ContainsObbConvexPolygon_PolygonCompletelyOutside_ReturnsDisjoint()
  864. {
  865. var boxCenter = new Vector2(0, 0);
  866. var axisX = Vector2.UnitX;
  867. var axisY = Vector2.UnitY;
  868. var halfExtents = new Vector2(5, 5);
  869. var vertices = new[]
  870. {
  871. new Vector2(20, 20),
  872. new Vector2(25, 20),
  873. new Vector2(25, 25),
  874. new Vector2(20, 25)
  875. };
  876. var normals = new[]
  877. {
  878. new Vector2(0, -1),
  879. new Vector2(1, 0),
  880. new Vector2(0, 1),
  881. new Vector2(-1, 0)
  882. };
  883. var result = Collision2D.ContainsObbConvexPolygon(boxCenter, axisX, axisY, halfExtents, vertices, normals);
  884. Assert.Equal(ContainmentType.Disjoint, result);
  885. }
  886. [Fact]
  887. public void ContainsObbConvexPolygon_PolygonPartiallyInside_ReturnsIntersects()
  888. {
  889. var boxCenter = new Vector2(5, 5);
  890. var axisX = Vector2.UnitX;
  891. var axisY = Vector2.UnitY;
  892. var halfExtents = new Vector2(5, 5);
  893. var vertices = new[]
  894. {
  895. new Vector2(5, 5),
  896. new Vector2(15, 5),
  897. new Vector2(15, 15),
  898. new Vector2(5, 15)
  899. };
  900. var normals = new[]
  901. {
  902. new Vector2(0, -1),
  903. new Vector2(1, 0),
  904. new Vector2(0, 1),
  905. new Vector2(-1, 0)
  906. };
  907. var result = Collision2D.ContainsObbConvexPolygon(boxCenter, axisX, axisY, halfExtents, vertices, normals);
  908. Assert.Equal(ContainmentType.Intersects, result);
  909. }
  910. #endregion
  911. #endregion
  912. #region Containment - Capsule Methods
  913. #region ContainsCapsulePoint Tests
  914. [Fact]
  915. public void ContainsCapsulePoint_PointInside_ReturnsContains()
  916. {
  917. var point = new Vector2(5, 1);
  918. var a = new Vector2(0, 0);
  919. var b = new Vector2(10, 0);
  920. var radius = 2.0f;
  921. var result = Collision2D.ContainsCapsulePoint(point, a, b, radius);
  922. Assert.Equal(ContainmentType.Contains, result);
  923. }
  924. [Fact]
  925. public void ContainsCapsulePoint_PointOutside_ReturnsDisjoint()
  926. {
  927. var point = new Vector2(5, 10);
  928. var a = new Vector2(0, 0);
  929. var b = new Vector2(10, 0);
  930. var radius = 2.0f;
  931. var result = Collision2D.ContainsCapsulePoint(point, a, b, radius);
  932. Assert.Equal(ContainmentType.Disjoint, result);
  933. }
  934. [Fact]
  935. public void ContainsCapsulePoint_PointOnBoundary_ReturnsContains()
  936. {
  937. var point = new Vector2(5, 2);
  938. var a = new Vector2(0, 0);
  939. var b = new Vector2(10, 0);
  940. var radius = 2.0f;
  941. var result = Collision2D.ContainsCapsulePoint(point, a, b, radius);
  942. Assert.Equal(ContainmentType.Contains, result);
  943. }
  944. #endregion
  945. #region ContainsCapsuleObb Tests
  946. [Fact]
  947. public void ContainsCapsuleObb_ObbCompletelyInside_ReturnsContains()
  948. {
  949. var capsuleA = new Vector2(0, 5);
  950. var capsuleB = new Vector2(20, 5);
  951. var capsuleRadius = 5.0f;
  952. var obbCenter = new Vector2(10, 5);
  953. var obbAxisX = Vector2.UnitX;
  954. var obbAxisY = Vector2.UnitY;
  955. var obbHalf = new Vector2(2, 2);
  956. var result = Collision2D.ContainsCapsuleObb(capsuleA, capsuleB, capsuleRadius, obbCenter, obbAxisX, obbAxisY, obbHalf);
  957. Assert.Equal(ContainmentType.Contains, result);
  958. }
  959. [Fact]
  960. public void ContainsCapsuleObb_ObbCompletelyOutside_ReturnsDisjoint()
  961. {
  962. var capsuleA = new Vector2(0, 0);
  963. var capsuleB = new Vector2(10, 0);
  964. var capsuleRadius = 2.0f;
  965. var obbCenter = new Vector2(20, 20);
  966. var obbAxisX = Vector2.UnitX;
  967. var obbAxisY = Vector2.UnitY;
  968. var obbHalf = new Vector2(2, 2);
  969. var result = Collision2D.ContainsCapsuleObb(capsuleA, capsuleB, capsuleRadius, obbCenter, obbAxisX, obbAxisY, obbHalf);
  970. Assert.Equal(ContainmentType.Disjoint, result);
  971. }
  972. [Fact]
  973. public void ContainsCapsuleObb_ObbPartiallyInside_ReturnsIntersects()
  974. {
  975. var capsuleA = new Vector2(0, 5);
  976. var capsuleB = new Vector2(10, 5);
  977. var capsuleRadius = 3.0f;
  978. var obbCenter = new Vector2(8, 5);
  979. var obbAxisX = Vector2.UnitX;
  980. var obbAxisY = Vector2.UnitY;
  981. var obbHalf = new Vector2(5, 5);
  982. var result = Collision2D.ContainsCapsuleObb(capsuleA, capsuleB, capsuleRadius, obbCenter, obbAxisX, obbAxisY, obbHalf);
  983. Assert.Equal(ContainmentType.Intersects, result);
  984. }
  985. #endregion
  986. #region ContainsCapsuleCapsule Tests
  987. [Fact]
  988. public void ContainsCapsuleCapsule_CompletelyContains_ReturnsContains()
  989. {
  990. var a0 = new Vector2(0, 0);
  991. var a1 = new Vector2(20, 0);
  992. var aRadius = 5.0f;
  993. var b0 = new Vector2(8, 0);
  994. var b1 = new Vector2(12, 0);
  995. var bRadius = 2.0f;
  996. var result = Collision2D.ContainsCapsuleCapsule(a0, a1, aRadius, b0, b1, bRadius);
  997. Assert.Equal(ContainmentType.Contains, result);
  998. }
  999. [Fact]
  1000. public void ContainsCapsuleCapsule_Separated_ReturnsDisjoint()
  1001. {
  1002. var a0 = new Vector2(0, 0);
  1003. var a1 = new Vector2(10, 0);
  1004. var aRadius = 2.0f;
  1005. var b0 = new Vector2(20, 20);
  1006. var b1 = new Vector2(30, 20);
  1007. var bRadius = 2.0f;
  1008. var result = Collision2D.ContainsCapsuleCapsule(a0, a1, aRadius, b0, b1, bRadius);
  1009. Assert.Equal(ContainmentType.Disjoint, result);
  1010. }
  1011. [Fact]
  1012. public void ContainsCapsuleCapsule_PartialOverlap_ReturnsIntersects()
  1013. {
  1014. var a0 = new Vector2(0, 0);
  1015. var a1 = new Vector2(10, 0);
  1016. var aRadius = 3.0f;
  1017. var b0 = new Vector2(8, 0);
  1018. var b1 = new Vector2(15, 0);
  1019. var bRadius = 2.0f;
  1020. var result = Collision2D.ContainsCapsuleCapsule(a0, a1, aRadius, b0, b1, bRadius);
  1021. Assert.Equal(ContainmentType.Intersects, result);
  1022. }
  1023. #endregion
  1024. #region ContainsCapsuleConvexPolygon Tests
  1025. [Fact]
  1026. public void ContainsCapsuleConvexPolygon_PolygonCompletelyInside_ReturnsContains()
  1027. {
  1028. var capsuleA = new Vector2(0, 10);
  1029. var capsuleB = new Vector2(20, 10);
  1030. var capsuleRadius = 8.0f;
  1031. var vertices = new[]
  1032. {
  1033. new Vector2(8, 8),
  1034. new Vector2(12, 8),
  1035. new Vector2(12, 12),
  1036. new Vector2(8, 12)
  1037. };
  1038. var normals = new[]
  1039. {
  1040. new Vector2(0, -1),
  1041. new Vector2(1, 0),
  1042. new Vector2(0, 1),
  1043. new Vector2(-1, 0)
  1044. };
  1045. var result = Collision2D.ContainsCapsuleConvexPolygon(capsuleA, capsuleB, capsuleRadius, vertices, normals);
  1046. Assert.Equal(ContainmentType.Contains, result);
  1047. }
  1048. [Fact]
  1049. public void ContainsCapsuleConvexPolygon_PolygonCompletelyOutside_ReturnsDisjoint()
  1050. {
  1051. var capsuleA = new Vector2(0, 0);
  1052. var capsuleB = new Vector2(10, 0);
  1053. var capsuleRadius = 2.0f;
  1054. var vertices = new[]
  1055. {
  1056. new Vector2(20, 20),
  1057. new Vector2(25, 20),
  1058. new Vector2(25, 25),
  1059. new Vector2(20, 25)
  1060. };
  1061. var normals = new[]
  1062. {
  1063. new Vector2(0, -1),
  1064. new Vector2(1, 0),
  1065. new Vector2(0, 1),
  1066. new Vector2(-1, 0)
  1067. };
  1068. var result = Collision2D.ContainsCapsuleConvexPolygon(capsuleA, capsuleB, capsuleRadius, vertices, normals);
  1069. Assert.Equal(ContainmentType.Disjoint, result);
  1070. }
  1071. [Fact]
  1072. public void ContainsCapsuleConvexPolygon_PolygonPartiallyInside_ReturnsIntersects()
  1073. {
  1074. var capsuleA = new Vector2(0, 5);
  1075. var capsuleB = new Vector2(10, 5);
  1076. var capsuleRadius = 3.0f;
  1077. var vertices = new[]
  1078. {
  1079. new Vector2(5, 5),
  1080. new Vector2(15, 5),
  1081. new Vector2(15, 15),
  1082. new Vector2(5, 15)
  1083. };
  1084. var normals = new[]
  1085. {
  1086. new Vector2(0, -1),
  1087. new Vector2(1, 0),
  1088. new Vector2(0, 1),
  1089. new Vector2(-1, 0)
  1090. };
  1091. var result = Collision2D.ContainsCapsuleConvexPolygon(capsuleA, capsuleB, capsuleRadius, vertices, normals);
  1092. Assert.Equal(ContainmentType.Intersects, result);
  1093. }
  1094. #endregion
  1095. #endregion
  1096. #region Containment - ConvexPolygon Methods
  1097. #region ContainsConvexPolygonPoint Tests
  1098. [Fact]
  1099. public void ContainsConvexPolygonPoint_PointInside_ReturnsContains()
  1100. {
  1101. var point = new Vector2(5, 5);
  1102. var vertices = new[]
  1103. {
  1104. new Vector2(0, 0),
  1105. new Vector2(10, 0),
  1106. new Vector2(10, 10),
  1107. new Vector2(0, 10)
  1108. };
  1109. var normals = new[]
  1110. {
  1111. new Vector2(0, -1),
  1112. new Vector2(1, 0),
  1113. new Vector2(0, 1),
  1114. new Vector2(-1, 0)
  1115. };
  1116. var result = Collision2D.ContainsConvexPolygonPoint(point, vertices, normals);
  1117. Assert.Equal(ContainmentType.Contains, result);
  1118. }
  1119. [Fact]
  1120. public void ContainsConvexPolygonPoint_PointOutside_ReturnsDisjoint()
  1121. {
  1122. var point = new Vector2(15, 15);
  1123. var vertices = new[]
  1124. {
  1125. new Vector2(0, 0),
  1126. new Vector2(10, 0),
  1127. new Vector2(10, 10),
  1128. new Vector2(0, 10)
  1129. };
  1130. var normals = new[]
  1131. {
  1132. new Vector2(0, -1),
  1133. new Vector2(1, 0),
  1134. new Vector2(0, 1),
  1135. new Vector2(-1, 0)
  1136. };
  1137. var result = Collision2D.ContainsConvexPolygonPoint(point, vertices, normals);
  1138. Assert.Equal(ContainmentType.Disjoint, result);
  1139. }
  1140. [Fact]
  1141. public void ContainsConvexPolygonPoint_PointOnEdge_ReturnsContains()
  1142. {
  1143. var point = new Vector2(10, 5);
  1144. var vertices = new[]
  1145. {
  1146. new Vector2(0, 0),
  1147. new Vector2(10, 0),
  1148. new Vector2(10, 10),
  1149. new Vector2(0, 10)
  1150. };
  1151. var normals = new[]
  1152. {
  1153. new Vector2(0, -1),
  1154. new Vector2(1, 0),
  1155. new Vector2(0, 1),
  1156. new Vector2(-1, 0)
  1157. };
  1158. var result = Collision2D.ContainsConvexPolygonPoint(point, vertices, normals);
  1159. Assert.Equal(ContainmentType.Contains, result);
  1160. }
  1161. #endregion
  1162. #region ContainsConvexPolygonCapsule Tests
  1163. [Fact]
  1164. public void ContainsConvexPolygonCapsule_CapsuleCompletelyInside_ReturnsContains()
  1165. {
  1166. var vertices = new[]
  1167. {
  1168. new Vector2(0, 0),
  1169. new Vector2(20, 0),
  1170. new Vector2(20, 20),
  1171. new Vector2(0, 20)
  1172. };
  1173. var normals = new[]
  1174. {
  1175. new Vector2(0, -1),
  1176. new Vector2(1, 0),
  1177. new Vector2(0, 1),
  1178. new Vector2(-1, 0)
  1179. };
  1180. var capsuleA = new Vector2(8, 10);
  1181. var capsuleB = new Vector2(12, 10);
  1182. var capsuleRadius = 2.0f;
  1183. var result = Collision2D.ContainsConvexPolygonCapsule(vertices, normals, capsuleA, capsuleB, capsuleRadius);
  1184. Assert.Equal(ContainmentType.Contains, result);
  1185. }
  1186. [Fact]
  1187. public void ContainsConvexPolygonCapsule_CapsuleCompletelyOutside_ReturnsDisjoint()
  1188. {
  1189. var vertices = new[]
  1190. {
  1191. new Vector2(0, 0),
  1192. new Vector2(10, 0),
  1193. new Vector2(10, 10),
  1194. new Vector2(0, 10)
  1195. };
  1196. var normals = new[]
  1197. {
  1198. new Vector2(0, -1),
  1199. new Vector2(1, 0),
  1200. new Vector2(0, 1),
  1201. new Vector2(-1, 0)
  1202. };
  1203. var capsuleA = new Vector2(20, 20);
  1204. var capsuleB = new Vector2(25, 25);
  1205. var capsuleRadius = 1.0f;
  1206. var result = Collision2D.ContainsConvexPolygonCapsule(vertices, normals, capsuleA, capsuleB, capsuleRadius);
  1207. Assert.Equal(ContainmentType.Disjoint, result);
  1208. }
  1209. [Fact]
  1210. public void ContainsConvexPolygonCapsule_CapsulePartiallyInside_ReturnsIntersects()
  1211. {
  1212. var vertices = new[]
  1213. {
  1214. new Vector2(0, 0),
  1215. new Vector2(10, 0),
  1216. new Vector2(10, 10),
  1217. new Vector2(0, 10)
  1218. };
  1219. var normals = new[]
  1220. {
  1221. new Vector2(0, -1),
  1222. new Vector2(1, 0),
  1223. new Vector2(0, 1),
  1224. new Vector2(-1, 0)
  1225. };
  1226. var capsuleA = new Vector2(5, 5);
  1227. var capsuleB = new Vector2(15, 5);
  1228. var capsuleRadius = 1.0f;
  1229. var result = Collision2D.ContainsConvexPolygonCapsule(vertices, normals, capsuleA, capsuleB, capsuleRadius);
  1230. Assert.Equal(ContainmentType.Intersects, result);
  1231. }
  1232. #endregion
  1233. #region ContainsConvexPolygonObb Tests
  1234. [Fact]
  1235. public void ContainsConvexPolygonObb_ObbCompletelyInside_ReturnsContains()
  1236. {
  1237. var vertices = new[]
  1238. {
  1239. new Vector2(0, 0),
  1240. new Vector2(20, 0),
  1241. new Vector2(20, 20),
  1242. new Vector2(0, 20)
  1243. };
  1244. var normals = new[]
  1245. {
  1246. new Vector2(0, -1),
  1247. new Vector2(1, 0),
  1248. new Vector2(0, 1),
  1249. new Vector2(-1, 0)
  1250. };
  1251. var obbCenter = new Vector2(10, 10);
  1252. var obbAxisX = Vector2.UnitX;
  1253. var obbAxisY = Vector2.UnitY;
  1254. var obbHalf = new Vector2(3, 3);
  1255. var result = Collision2D.ContainsConvexPolygonObb(vertices, normals, obbCenter, obbAxisX, obbAxisY, obbHalf);
  1256. Assert.Equal(ContainmentType.Contains, result);
  1257. }
  1258. [Fact]
  1259. public void ContainsConvexPolygonObb_ObbCompletelyOutside_ReturnsDisjoint()
  1260. {
  1261. var vertices = new[]
  1262. {
  1263. new Vector2(0, 0),
  1264. new Vector2(10, 0),
  1265. new Vector2(10, 10),
  1266. new Vector2(0, 10)
  1267. };
  1268. var normals = new[]
  1269. {
  1270. new Vector2(0, -1),
  1271. new Vector2(1, 0),
  1272. new Vector2(0, 1),
  1273. new Vector2(-1, 0)
  1274. };
  1275. var obbCenter = new Vector2(20, 20);
  1276. var obbAxisX = Vector2.UnitX;
  1277. var obbAxisY = Vector2.UnitY;
  1278. var obbHalf = new Vector2(2, 2);
  1279. var result = Collision2D.ContainsConvexPolygonObb(vertices, normals, obbCenter, obbAxisX, obbAxisY, obbHalf);
  1280. Assert.Equal(ContainmentType.Disjoint, result);
  1281. }
  1282. [Fact]
  1283. public void ContainsConvexPolygonObb_ObbPartiallyInside_ReturnsIntersects()
  1284. {
  1285. var vertices = new[]
  1286. {
  1287. new Vector2(0, 0),
  1288. new Vector2(10, 0),
  1289. new Vector2(10, 10),
  1290. new Vector2(0, 10)
  1291. };
  1292. var normals = new[]
  1293. {
  1294. new Vector2(0, -1),
  1295. new Vector2(1, 0),
  1296. new Vector2(0, 1),
  1297. new Vector2(-1, 0)
  1298. };
  1299. var obbCenter = new Vector2(8, 8);
  1300. var obbAxisX = Vector2.UnitX;
  1301. var obbAxisY = Vector2.UnitY;
  1302. var obbHalf = new Vector2(5, 5);
  1303. var result = Collision2D.ContainsConvexPolygonObb(vertices, normals, obbCenter, obbAxisX, obbAxisY, obbHalf);
  1304. Assert.Equal(ContainmentType.Intersects, result);
  1305. }
  1306. #endregion
  1307. #region ContainsConvexPolygonConvexPolygon Tests
  1308. [Fact]
  1309. public void ContainsConvexPolygonConvexPolygon_CompletelyContains_ReturnsContains()
  1310. {
  1311. var aVertices = new[]
  1312. {
  1313. new Vector2(0, 0),
  1314. new Vector2(20, 0),
  1315. new Vector2(20, 20),
  1316. new Vector2(0, 20)
  1317. };
  1318. var aNormals = new[]
  1319. {
  1320. new Vector2(0, -1),
  1321. new Vector2(1, 0),
  1322. new Vector2(0, 1),
  1323. new Vector2(-1, 0)
  1324. };
  1325. var bVertices = new[]
  1326. {
  1327. new Vector2(5, 5),
  1328. new Vector2(15, 5),
  1329. new Vector2(15, 15),
  1330. new Vector2(5, 15)
  1331. };
  1332. var bNormals = new[]
  1333. {
  1334. new Vector2(0, -1),
  1335. new Vector2(1, 0),
  1336. new Vector2(0, 1),
  1337. new Vector2(-1, 0)
  1338. };
  1339. var result = Collision2D.ContainsConvexPolygonConvexPolygon(aVertices, aNormals, bVertices, bNormals);
  1340. Assert.Equal(ContainmentType.Contains, result);
  1341. }
  1342. [Fact]
  1343. public void ContainsConvexPolygonConvexPolygon_Separated_ReturnsDisjoint()
  1344. {
  1345. var aVertices = new[]
  1346. {
  1347. new Vector2(0, 0),
  1348. new Vector2(10, 0),
  1349. new Vector2(10, 10),
  1350. new Vector2(0, 10)
  1351. };
  1352. var aNormals = new[]
  1353. {
  1354. new Vector2(0, -1),
  1355. new Vector2(1, 0),
  1356. new Vector2(0, 1),
  1357. new Vector2(-1, 0)
  1358. };
  1359. var bVertices = new[]
  1360. {
  1361. new Vector2(20, 20),
  1362. new Vector2(30, 20),
  1363. new Vector2(30, 30),
  1364. new Vector2(20, 30)
  1365. };
  1366. var bNormals = new[]
  1367. {
  1368. new Vector2(0, -1),
  1369. new Vector2(1, 0),
  1370. new Vector2(0, 1),
  1371. new Vector2(-1, 0)
  1372. };
  1373. var result = Collision2D.ContainsConvexPolygonConvexPolygon(aVertices, aNormals, bVertices, bNormals);
  1374. Assert.Equal(ContainmentType.Disjoint, result);
  1375. }
  1376. [Fact]
  1377. public void ContainsConvexPolygonConvexPolygon_PartialOverlap_ReturnsIntersects()
  1378. {
  1379. var aVertices = new[]
  1380. {
  1381. new Vector2(0, 0),
  1382. new Vector2(10, 0),
  1383. new Vector2(10, 10),
  1384. new Vector2(0, 10)
  1385. };
  1386. var aNormals = new[]
  1387. {
  1388. new Vector2(0, -1),
  1389. new Vector2(1, 0),
  1390. new Vector2(0, 1),
  1391. new Vector2(-1, 0)
  1392. };
  1393. var bVertices = new[]
  1394. {
  1395. new Vector2(5, 5),
  1396. new Vector2(15, 5),
  1397. new Vector2(15, 15),
  1398. new Vector2(5, 15)
  1399. };
  1400. var bNormals = new[]
  1401. {
  1402. new Vector2(0, -1),
  1403. new Vector2(1, 0),
  1404. new Vector2(0, 1),
  1405. new Vector2(-1, 0)
  1406. };
  1407. var result = Collision2D.ContainsConvexPolygonConvexPolygon(aVertices, aNormals, bVertices, bNormals);
  1408. Assert.Equal(ContainmentType.Intersects, result);
  1409. }
  1410. #endregion
  1411. #endregion
  1412. #region Projection Methods
  1413. #region ProjectOntoAxis Tests
  1414. [Fact]
  1415. public void ProjectOntoAxis_AlignedWithAxis_ReturnsCorrectInterval()
  1416. {
  1417. var vertices = new[]
  1418. {
  1419. new Vector2(0, 0),
  1420. new Vector2(10, 0),
  1421. new Vector2(10, 10),
  1422. new Vector2(0, 10)
  1423. };
  1424. var axis = Vector2.UnitX;
  1425. Collision2D.ProjectOntoAxis(vertices, axis, out float min, out float max);
  1426. Assert.Equal(0.0f, min, Collision2D.Epsilon);
  1427. Assert.Equal(10.0f, max, Collision2D.Epsilon);
  1428. }
  1429. [Fact]
  1430. public void ProjectOntoAxis_PerpendicularToAxis_ReturnsNarrowInterval()
  1431. {
  1432. var vertices = new[]
  1433. {
  1434. new Vector2(5, 0),
  1435. new Vector2(5, 10)
  1436. };
  1437. var axis = Vector2.UnitX;
  1438. Collision2D.ProjectOntoAxis(vertices, axis, out float min, out float max);
  1439. Assert.Equal(5.0f, min, Collision2D.Epsilon);
  1440. Assert.Equal(5.0f, max, Collision2D.Epsilon);
  1441. }
  1442. [Fact]
  1443. public void ProjectOntoAxis_DiagonalAxis_ReturnsCorrectInterval()
  1444. {
  1445. var vertices = new[]
  1446. {
  1447. new Vector2(0, 0),
  1448. new Vector2(10, 0),
  1449. new Vector2(10, 10),
  1450. new Vector2(0, 10)
  1451. };
  1452. var axis = Vector2.Normalize(new Vector2(1, 1));
  1453. Collision2D.ProjectOntoAxis(vertices, axis, out float min, out float max);
  1454. float expected = 10.0f / MathF.Sqrt(2);
  1455. Assert.Equal(0.0f, min, Collision2D.Epsilon);
  1456. Assert.Equal(expected * 2, max, Collision2D.Epsilon);
  1457. }
  1458. #endregion
  1459. #region ProjectAabbOntoAxis Tests
  1460. [Fact]
  1461. public void ProjectAabbOntoAxis_AlignedAxis_ReturnsExtents()
  1462. {
  1463. var center = new Vector2(5, 5);
  1464. var halfExtents = new Vector2(5, 5);
  1465. var axis = Vector2.UnitX;
  1466. Collision2D.ProjectAabbOntoAxis(center, halfExtents, axis, out float min, out float max);
  1467. Assert.Equal(0.0f, min, Collision2D.Epsilon);
  1468. Assert.Equal(10.0f, max, Collision2D.Epsilon);
  1469. }
  1470. [Fact]
  1471. public void ProjectAabbOntoAxis_DiagonalAxis_ReturnsCorrectInterval()
  1472. {
  1473. var center = new Vector2(5, 5);
  1474. var halfExtents = new Vector2(5, 5);
  1475. var axis = Vector2.Normalize(new Vector2(1, 1));
  1476. Collision2D.ProjectAabbOntoAxis(center, halfExtents, axis, out float min, out float max);
  1477. float centerProj = 5.0f * MathF.Sqrt(2);
  1478. float radius = 5.0f * MathF.Sqrt(2);
  1479. Assert.Equal(centerProj - radius, min, Collision2D.Epsilon);
  1480. Assert.Equal(centerProj + radius, max, Collision2D.Epsilon);
  1481. }
  1482. #endregion
  1483. #region ProjectObbOntoAxis Tests
  1484. [Fact]
  1485. public void ProjectObbOntoAxis_AxisAlignedBox_MatchesAabbResult()
  1486. {
  1487. var center = new Vector2(5, 5);
  1488. var axisX = Vector2.UnitX;
  1489. var axisY = Vector2.UnitY;
  1490. var halfExtents = new Vector2(5, 5);
  1491. var projAxis = Vector2.UnitX;
  1492. Collision2D.ProjectObbOntoAxis(center, axisX, axisY, halfExtents, projAxis, out float min, out float max);
  1493. Assert.Equal(0.0f, min, Collision2D.Epsilon);
  1494. Assert.Equal(10.0f, max, Collision2D.Epsilon);
  1495. }
  1496. [Fact]
  1497. public void ProjectObbOntoAxis_RotatedBox_ReturnsCorrectInterval()
  1498. {
  1499. var center = new Vector2(0, 0);
  1500. var axisX = Vector2.Normalize(new Vector2(1, 1));
  1501. var axisY = Vector2.Normalize(new Vector2(-1, 1));
  1502. var halfExtents = new Vector2(5, 5);
  1503. var projAxis = Vector2.UnitX;
  1504. Collision2D.ProjectObbOntoAxis(center, axisX, axisY, halfExtents, projAxis, out float min, out float max);
  1505. Assert.Equal(min, -max, Collision2D.Epsilon);
  1506. }
  1507. #endregion
  1508. #endregion
  1509. #region Distance Calculations
  1510. #region DistanceSquaredPointSegment Tests
  1511. [Fact]
  1512. public void DistanceSquaredPointSegment_PointOnSegment_ReturnsZero()
  1513. {
  1514. var point = new Vector2(5, 0);
  1515. var a = new Vector2(0, 0);
  1516. var b = new Vector2(10, 0);
  1517. float distance = Collision2D.DistanceSquaredPointSegment(point, a, b, out float t, out Vector2 closestPoint);
  1518. Assert.Equal(0.0f, distance, Collision2D.Epsilon);
  1519. Assert.Equal(0.5f, t, Collision2D.Epsilon);
  1520. Assert.Equal(point, closestPoint);
  1521. }
  1522. [Fact]
  1523. public void DistanceSquaredPointSegment_ClosestAtEndpoint_ClampsParameter()
  1524. {
  1525. var point = new Vector2(15, 5);
  1526. var a = new Vector2(0, 0);
  1527. var b = new Vector2(10, 0);
  1528. float distance = Collision2D.DistanceSquaredPointSegment(point, a, b, out float t, out Vector2 closestPoint);
  1529. Assert.Equal(50.0f, distance, Collision2D.Epsilon);
  1530. Assert.Equal(1.0f, t);
  1531. Assert.Equal(b, closestPoint);
  1532. }
  1533. [Fact]
  1534. public void DistanceSquaredPointSegment_PerpendicularDistance_ReturnsCorrectValue()
  1535. {
  1536. var point = new Vector2(5, 3);
  1537. var a = new Vector2(0, 0);
  1538. var b = new Vector2(10, 0);
  1539. float distance = Collision2D.DistanceSquaredPointSegment(point, a, b, out float t, out Vector2 closestPoint);
  1540. Assert.Equal(9.0f, distance, Collision2D.Epsilon);
  1541. Assert.Equal(0.5f, t, Collision2D.Epsilon);
  1542. Assert.Equal(new Vector2(5, 0), closestPoint);
  1543. }
  1544. [Fact]
  1545. public void DistanceSquaredPointSegment_DegenerateSegment_TreatsAsPoint()
  1546. {
  1547. var point = new Vector2(5, 5);
  1548. var a = new Vector2(0, 0);
  1549. var b = new Vector2(0, 0);
  1550. float distance = Collision2D.DistanceSquaredPointSegment(point, a, b, out float t, out Vector2 closestPoint);
  1551. Assert.Equal(50.0f, distance, Collision2D.Epsilon);
  1552. Assert.Equal(0.0f, t);
  1553. Assert.Equal(a, closestPoint);
  1554. }
  1555. #endregion
  1556. #region DistanceSquaredSegmentSegment Tests
  1557. [Fact]
  1558. public void DistanceSquaredSegmentSegment_IntersectingSegments_ReturnsZero()
  1559. {
  1560. var p1 = new Vector2(0, 5);
  1561. var q1 = new Vector2(10, 5);
  1562. var p2 = new Vector2(5, 0);
  1563. var q2 = new Vector2(5, 10);
  1564. float distance = Collision2D.DistanceSquaredSegmentSegment(p1, q1, p2, q2, out float s, out float t, out Vector2 c1, out Vector2 c2);
  1565. Assert.Equal(0.0f, distance, Collision2D.Epsilon);
  1566. Assert.Equal(new Vector2(5, 5), c1);
  1567. Assert.Equal(new Vector2(5, 5), c2);
  1568. }
  1569. [Fact]
  1570. public void DistanceSquaredSegmentSegment_ParallelSegments_ReturnsCorrectDistance()
  1571. {
  1572. var p1 = new Vector2(0, 0);
  1573. var q1 = new Vector2(10, 0);
  1574. var p2 = new Vector2(0, 5);
  1575. var q2 = new Vector2(10, 5);
  1576. float distance = Collision2D.DistanceSquaredSegmentSegment(p1, q1, p2, q2, out float s, out float t, out Vector2 c1, out Vector2 c2);
  1577. Assert.Equal(25.0f, distance, Collision2D.Epsilon);
  1578. }
  1579. [Fact]
  1580. public void DistanceSquaredSegmentSegment_SkewSegments_ReturnsCorrectDistance()
  1581. {
  1582. var p1 = new Vector2(0, 0);
  1583. var q1 = new Vector2(5, 0);
  1584. var p2 = new Vector2(0, 2);
  1585. var q2 = new Vector2(0, 5);
  1586. float distance = Collision2D.DistanceSquaredSegmentSegment(p1, q1, p2, q2, out float s, out float t, out Vector2 c1, out Vector2 c2);
  1587. Assert.Equal(4.0f, distance, Collision2D.Epsilon);
  1588. }
  1589. [Fact]
  1590. public void DistanceSquaredSegmentSegment_DegenerateSegments_HandlesCorrectly()
  1591. {
  1592. var p1 = new Vector2(0, 0);
  1593. var q1 = new Vector2(0, 0);
  1594. var p2 = new Vector2(3, 4);
  1595. var q2 = new Vector2(3, 4);
  1596. float distance = Collision2D.DistanceSquaredSegmentSegment(p1, q1, p2, q2, out float s, out float t, out Vector2 c1, out Vector2 c2);
  1597. Assert.Equal(25.0f, distance, Collision2D.Epsilon);
  1598. }
  1599. #endregion
  1600. #region DistanceSquaredPointAabb Tests
  1601. [Fact]
  1602. public void DistanceSquaredPointAabb_PointInside_ReturnsZero()
  1603. {
  1604. var point = new Vector2(5, 5);
  1605. var min = new Vector2(0, 0);
  1606. var max = new Vector2(10, 10);
  1607. float distance = Collision2D.DistanceSquaredPointAabb(point, min, max);
  1608. Assert.Equal(0.0f, distance, Collision2D.Epsilon);
  1609. }
  1610. [Fact]
  1611. public void DistanceSquaredPointAabb_ClosestToCorner_ReturnsCorrectDistance()
  1612. {
  1613. var point = new Vector2(15, 15);
  1614. var min = new Vector2(0, 0);
  1615. var max = new Vector2(10, 10);
  1616. float distance = Collision2D.DistanceSquaredPointAabb(point, min, max);
  1617. Assert.Equal(50.0f, distance, Collision2D.Epsilon);
  1618. }
  1619. [Fact]
  1620. public void DistanceSquaredPointAabb_ClosestToEdge_ReturnsCorrectDistance()
  1621. {
  1622. var point = new Vector2(5, 15);
  1623. var min = new Vector2(0, 0);
  1624. var max = new Vector2(10, 10);
  1625. float distance = Collision2D.DistanceSquaredPointAabb(point, min, max);
  1626. Assert.Equal(25.0f, distance, Collision2D.Epsilon);
  1627. }
  1628. #endregion
  1629. #region DistanceSquaredPointObb Tests
  1630. [Fact]
  1631. public void DistanceSquaredPointObb_PointInside_ReturnsZero()
  1632. {
  1633. var point = new Vector2(5, 5);
  1634. var center = new Vector2(5, 5);
  1635. var axisX = Vector2.UnitX;
  1636. var axisY = Vector2.UnitY;
  1637. var halfExtents = new Vector2(3, 3);
  1638. float distance = Collision2D.DistanceSquaredPointObb(point, center, axisX, axisY, halfExtents);
  1639. Assert.Equal(0.0f, distance, Collision2D.Epsilon);
  1640. }
  1641. [Fact]
  1642. public void DistanceSquaredPointObb_PointOutside_ReturnsCorrectDistance()
  1643. {
  1644. var point = new Vector2(10, 5);
  1645. var center = new Vector2(5, 5);
  1646. var axisX = Vector2.UnitX;
  1647. var axisY = Vector2.UnitY;
  1648. var halfExtents = new Vector2(2, 2);
  1649. float distance = Collision2D.DistanceSquaredPointObb(point, center, axisX, axisY, halfExtents);
  1650. Assert.Equal(9.0f, distance, Collision2D.Epsilon);
  1651. }
  1652. [Fact]
  1653. public void DistanceSquaredPointObb_RotatedBox_ReturnsCorrectDistance()
  1654. {
  1655. var point = new Vector2(10, 0);
  1656. var center = new Vector2(0, 0);
  1657. var axisX = Vector2.Normalize(new Vector2(1, 1));
  1658. var axisY = Vector2.Normalize(new Vector2(-1, 1));
  1659. var halfExtents = new Vector2(2, 2);
  1660. float distance = Collision2D.DistanceSquaredPointObb(point, center, axisX, axisY, halfExtents);
  1661. Assert.True(distance > 0.0f);
  1662. }
  1663. #endregion
  1664. #region DistanceSquaredPointConvexPolygon Tests
  1665. [Fact]
  1666. public void DistanceSquaredPointConvexPolygon_PointInside_ReturnsZero()
  1667. {
  1668. var point = new Vector2(5, 5);
  1669. var vertices = new[]
  1670. {
  1671. new Vector2(0, 0),
  1672. new Vector2(10, 0),
  1673. new Vector2(10, 10),
  1674. new Vector2(0, 10)
  1675. };
  1676. var normals = new[]
  1677. {
  1678. new Vector2(0, -1),
  1679. new Vector2(1, 0),
  1680. new Vector2(0, 1),
  1681. new Vector2(-1, 0)
  1682. };
  1683. float distance = Collision2D.DistanceSquaredPointConvexPolygon(point, vertices, normals);
  1684. Assert.Equal(0.0f, distance, Collision2D.Epsilon);
  1685. }
  1686. [Fact]
  1687. public void DistanceSquaredPointConvexPolygon_ClosestToEdge_ReturnsCorrectDistance()
  1688. {
  1689. var point = new Vector2(5, -3);
  1690. var vertices = new[]
  1691. {
  1692. new Vector2(0, 0),
  1693. new Vector2(10, 0),
  1694. new Vector2(10, 10),
  1695. new Vector2(0, 10)
  1696. };
  1697. var normals = new[]
  1698. {
  1699. new Vector2(0, -1),
  1700. new Vector2(1, 0),
  1701. new Vector2(0, 1),
  1702. new Vector2(-1, 0)
  1703. };
  1704. float distance = Collision2D.DistanceSquaredPointConvexPolygon(point, vertices, normals);
  1705. Assert.Equal(9.0f, distance, Collision2D.Epsilon);
  1706. }
  1707. [Fact]
  1708. public void DistanceSquaredPointConvexPolygon_ClosestToVertex_ReturnsCorrectDistance()
  1709. {
  1710. var point = new Vector2(-3, -3);
  1711. var vertices = new[]
  1712. {
  1713. new Vector2(0, 0),
  1714. new Vector2(10, 0),
  1715. new Vector2(10, 10),
  1716. new Vector2(0, 10)
  1717. };
  1718. var normals = new[]
  1719. {
  1720. new Vector2(0, -1),
  1721. new Vector2(1, 0),
  1722. new Vector2(0, 1),
  1723. new Vector2(-1, 0)
  1724. };
  1725. float distance = Collision2D.DistanceSquaredPointConvexPolygon(point, vertices, normals);
  1726. Assert.Equal(18.0f, distance, Collision2D.Epsilon);
  1727. }
  1728. #endregion
  1729. #region DistanceSquaredSegmentAabb Tests
  1730. [Fact]
  1731. public void DistanceSquaredSegmentAabb_SegmentInsideBox_ReturnsZero()
  1732. {
  1733. var a = new Vector2(3, 3);
  1734. var b = new Vector2(7, 7);
  1735. var min = new Vector2(0, 0);
  1736. var max = new Vector2(10, 10);
  1737. float distance = Collision2D.DistanceSquaredSegmentAabb(a, b, min, max);
  1738. Assert.Equal(0.0f, distance, Collision2D.Epsilon);
  1739. }
  1740. [Fact]
  1741. public void DistanceSquaredSegmentAabb_SegmentIntersectsBox_ReturnsZero()
  1742. {
  1743. var a = new Vector2(-5, 5);
  1744. var b = new Vector2(15, 5);
  1745. var min = new Vector2(0, 0);
  1746. var max = new Vector2(10, 10);
  1747. float distance = Collision2D.DistanceSquaredSegmentAabb(a, b, min, max);
  1748. Assert.Equal(0.0f, distance, Collision2D.Epsilon);
  1749. }
  1750. [Fact]
  1751. public void DistanceSquaredSegmentAabb_SegmentOutsideBox_ReturnsCorrectDistance()
  1752. {
  1753. var a = new Vector2(15, 5);
  1754. var b = new Vector2(20, 5);
  1755. var min = new Vector2(0, 0);
  1756. var max = new Vector2(10, 10);
  1757. float distance = Collision2D.DistanceSquaredSegmentAabb(a, b, min, max);
  1758. Assert.Equal(25.0f, distance, Collision2D.Epsilon);
  1759. }
  1760. #endregion
  1761. #region DistanceSquaredSegmentConvexPolygon Tests
  1762. [Fact]
  1763. public void DistanceSquaredSegmentConvexPolygon_SegmentInside_ReturnsZero()
  1764. {
  1765. var a = new Vector2(3, 3);
  1766. var b = new Vector2(7, 7);
  1767. var vertices = new[]
  1768. {
  1769. new Vector2(0, 0),
  1770. new Vector2(10, 0),
  1771. new Vector2(10, 10),
  1772. new Vector2(0, 10)
  1773. };
  1774. var normals = new[]
  1775. {
  1776. new Vector2(0, -1),
  1777. new Vector2(1, 0),
  1778. new Vector2(0, 1),
  1779. new Vector2(-1, 0)
  1780. };
  1781. float distance = Collision2D.DistanceSquaredSegmentConvexPolygon(a, b, vertices, normals);
  1782. Assert.Equal(0.0f, distance, Collision2D.Epsilon);
  1783. }
  1784. [Fact]
  1785. public void DistanceSquaredSegmentConvexPolygon_SegmentIntersects_ReturnsZero()
  1786. {
  1787. var a = new Vector2(-5, 5);
  1788. var b = new Vector2(15, 5);
  1789. var vertices = new[]
  1790. {
  1791. new Vector2(0, 0),
  1792. new Vector2(10, 0),
  1793. new Vector2(10, 10),
  1794. new Vector2(0, 10)
  1795. };
  1796. var normals = new[]
  1797. {
  1798. new Vector2(0, -1),
  1799. new Vector2(1, 0),
  1800. new Vector2(0, 1),
  1801. new Vector2(-1, 0)
  1802. };
  1803. float distance = Collision2D.DistanceSquaredSegmentConvexPolygon(a, b, vertices, normals);
  1804. Assert.Equal(0.0f, distance, Collision2D.Epsilon);
  1805. }
  1806. [Fact]
  1807. public void DistanceSquaredSegmentConvexPolygon_SegmentOutside_ReturnsCorrectDistance()
  1808. {
  1809. var a = new Vector2(15, 5);
  1810. var b = new Vector2(20, 5);
  1811. var vertices = new[]
  1812. {
  1813. new Vector2(0, 0),
  1814. new Vector2(10, 0),
  1815. new Vector2(10, 10),
  1816. new Vector2(0, 10)
  1817. };
  1818. var normals = new[]
  1819. {
  1820. new Vector2(0, -1),
  1821. new Vector2(1, 0),
  1822. new Vector2(0, 1),
  1823. new Vector2(-1, 0)
  1824. };
  1825. float distance = Collision2D.DistanceSquaredSegmentConvexPolygon(a, b, vertices, normals);
  1826. Assert.Equal(25.0f, distance, Collision2D.Epsilon);
  1827. }
  1828. #endregion
  1829. #region ClosestPointRaySegment Tests
  1830. [Fact]
  1831. public void ClosestPointRaySegment_RayIntersectsSegment_ReturnsZeroDistance()
  1832. {
  1833. var rayOrigin = new Vector2(0, 5);
  1834. var rayDirection = Vector2.UnitX;
  1835. var segA = new Vector2(5, 0);
  1836. var segB = new Vector2(5, 10);
  1837. Collision2D.ClosestPointRaySegment(rayOrigin, rayDirection, segA, segB, out float sRay, out float tSeg, out float distanceSquared);
  1838. Assert.Equal(0.0f, distanceSquared, Collision2D.Epsilon);
  1839. Assert.Equal(5.0f, sRay, Collision2D.Epsilon);
  1840. Assert.Equal(0.5f, tSeg, Collision2D.Epsilon);
  1841. }
  1842. [Fact]
  1843. public void ClosestPointRaySegment_RayParallelToSegment_ReturnsCorrectDistance()
  1844. {
  1845. var rayOrigin = new Vector2(0, 0);
  1846. var rayDirection = Vector2.UnitX;
  1847. var segA = new Vector2(0, 5);
  1848. var segB = new Vector2(10, 5);
  1849. Collision2D.ClosestPointRaySegment(rayOrigin, rayDirection, segA, segB, out float sRay, out float tSeg, out float distanceSquared);
  1850. Assert.Equal(25.0f, distanceSquared, Collision2D.Epsilon);
  1851. }
  1852. [Fact]
  1853. public void ClosestPointRaySegment_RayMissesSegment_ReturnsCorrectDistance()
  1854. {
  1855. var rayOrigin = new Vector2(0, 0);
  1856. var rayDirection = Vector2.UnitX;
  1857. var segA = new Vector2(5, 5);
  1858. var segB = new Vector2(10, 5);
  1859. Collision2D.ClosestPointRaySegment(rayOrigin, rayDirection, segA, segB, out float sRay, out float tSeg, out float distanceSquared);
  1860. Assert.True(distanceSquared > 0.0f);
  1861. }
  1862. #endregion
  1863. #endregion
  1864. #region Parametric Solvers
  1865. #region SolveParametricIntersection2D Tests
  1866. [Fact]
  1867. public void SolveParametricIntersection2D_PerpendicularLines_FindsIntersection()
  1868. {
  1869. var origin1 = new Vector2(0, 5);
  1870. var direction1 = Vector2.UnitX;
  1871. var origin2 = new Vector2(5, 0);
  1872. var direction2 = Vector2.UnitY;
  1873. bool result = Collision2D.SolveParametricIntersection2D(origin1, direction1, origin2, direction2, out float t1, out float t2);
  1874. Assert.True(result);
  1875. Assert.Equal(5.0f, t1, Collision2D.Epsilon);
  1876. Assert.Equal(5.0f, t2, Collision2D.Epsilon);
  1877. }
  1878. [Fact]
  1879. public void SolveParametricIntersection2D_ParallelLines_ReturnsFalse()
  1880. {
  1881. var origin1 = new Vector2(0, 0);
  1882. var direction1 = Vector2.UnitX;
  1883. var origin2 = new Vector2(0, 5);
  1884. var direction2 = Vector2.UnitX;
  1885. bool result = Collision2D.SolveParametricIntersection2D(origin1, direction1, origin2, direction2, out float t1, out float t2);
  1886. Assert.False(result);
  1887. }
  1888. [Fact]
  1889. public void SolveParametricIntersection2D_CollinearLines_ReturnsFalse()
  1890. {
  1891. var origin1 = new Vector2(0, 0);
  1892. var direction1 = Vector2.UnitX;
  1893. var origin2 = new Vector2(5, 0);
  1894. var direction2 = Vector2.UnitX;
  1895. bool result = Collision2D.SolveParametricIntersection2D(origin1, direction1, origin2, direction2, out float t1, out float t2);
  1896. Assert.False(result);
  1897. }
  1898. [Fact]
  1899. public void SolveParametricIntersection2D_LinesAtAnAngle_FindsIntersection()
  1900. {
  1901. var origin1 = new Vector2(0, 0);
  1902. var direction1 = new Vector2(1, 0);
  1903. var origin2 = new Vector2(0, 0);
  1904. var direction2 = new Vector2(0, 1);
  1905. bool result = Collision2D.SolveParametricIntersection2D(origin1, direction1, origin2, direction2, out float t1, out float t2);
  1906. Assert.True(result);
  1907. Assert.Equal(0.0f, t1, Collision2D.Epsilon);
  1908. Assert.Equal(0.0f, t2, Collision2D.Epsilon);
  1909. }
  1910. #endregion
  1911. #region SolveParametricIntersectionWithImplicitLine Tests
  1912. [Fact]
  1913. public void SolveParametricIntersectionWithImplicitLine_Intersecting_FindsParameter()
  1914. {
  1915. var lineNormal = Vector2.UnitY;
  1916. var lineDistance = 5.0f;
  1917. var origin = new Vector2(0, 0);
  1918. var direction = Vector2.UnitY;
  1919. bool result = Collision2D.SolveParametricIntersectionWithImplicitLine(lineNormal, lineDistance, origin, direction, out float t);
  1920. Assert.True(result);
  1921. Assert.Equal(5.0f, t, Collision2D.Epsilon);
  1922. }
  1923. [Fact]
  1924. public void SolveParametricIntersectionWithImplicitLine_Parallel_ReturnsFalse()
  1925. {
  1926. var lineNormal = Vector2.UnitY;
  1927. var lineDistance = 5.0f;
  1928. var origin = new Vector2(0, 0);
  1929. var direction = Vector2.UnitX;
  1930. bool result = Collision2D.SolveParametricIntersectionWithImplicitLine(lineNormal, lineDistance, origin, direction, out float t);
  1931. Assert.False(result);
  1932. }
  1933. [Fact]
  1934. public void SolveParametricIntersectionWithImplicitLine_Perpendicular_FindsParameter()
  1935. {
  1936. var lineNormal = Vector2.UnitX;
  1937. var lineDistance = 10.0f;
  1938. var origin = new Vector2(0, 0);
  1939. var direction = Vector2.UnitX;
  1940. bool result = Collision2D.SolveParametricIntersectionWithImplicitLine(lineNormal, lineDistance, origin, direction, out float t);
  1941. Assert.True(result);
  1942. Assert.Equal(10.0f, t, Collision2D.Epsilon);
  1943. }
  1944. #endregion
  1945. #endregion
  1946. #region Clipping Methods
  1947. #region ClipLineToAabb Tests
  1948. [Fact]
  1949. public void ClipLineToAabb_LineCompletelyInside_ReturnsFullInterval()
  1950. {
  1951. var origin = new Vector2(5, 5);
  1952. var direction = Vector2.UnitX;
  1953. var min = new Vector2(0, 0);
  1954. var max = new Vector2(20, 20);
  1955. float tLower = 0.0f;
  1956. float tUpper = 10.0f;
  1957. bool result = Collision2D.ClipLineToAabb(origin, direction, min, max, tLower, tUpper, out float tEnter, out float tExit);
  1958. Assert.True(result);
  1959. Assert.Equal(0.0f, tEnter, Collision2D.Epsilon);
  1960. Assert.Equal(10.0f, tExit, Collision2D.Epsilon);
  1961. }
  1962. [Fact]
  1963. public void ClipLineToAabb_LinePartiallyInside_ReturnsClippedInterval()
  1964. {
  1965. var origin = new Vector2(-5, 5);
  1966. var direction = Vector2.UnitX;
  1967. var min = new Vector2(0, 0);
  1968. var max = new Vector2(10, 10);
  1969. float tLower = 0.0f;
  1970. float tUpper = 20.0f;
  1971. bool result = Collision2D.ClipLineToAabb(origin, direction, min, max, tLower, tUpper, out float tEnter, out float tExit);
  1972. Assert.True(result);
  1973. Assert.Equal(5.0f, tEnter, Collision2D.Epsilon);
  1974. Assert.Equal(15.0f, tExit, Collision2D.Epsilon);
  1975. }
  1976. [Fact]
  1977. public void ClipLineToAabb_LineMissesBox_ReturnsFalse()
  1978. {
  1979. var origin = new Vector2(0, 20);
  1980. var direction = Vector2.UnitX;
  1981. var min = new Vector2(0, 0);
  1982. var max = new Vector2(10, 10);
  1983. float tLower = 0.0f;
  1984. float tUpper = 10.0f;
  1985. bool result = Collision2D.ClipLineToAabb(origin, direction, min, max, tLower, tUpper, out float tEnter, out float tExit);
  1986. Assert.False(result);
  1987. }
  1988. [Fact]
  1989. public void ClipLineToAabb_LineThroughCorner_ReturnsInterval()
  1990. {
  1991. var origin = new Vector2(0, 0);
  1992. var direction = Vector2.Normalize(new Vector2(1, 1));
  1993. var min = new Vector2(5, 5);
  1994. var max = new Vector2(15, 15);
  1995. float tLower = 0.0f;
  1996. float tUpper = 50.0f;
  1997. bool result = Collision2D.ClipLineToAabb(origin, direction, min, max, tLower, tUpper, out float tEnter, out float tExit);
  1998. Assert.True(result);
  1999. }
  2000. #endregion
  2001. #region ClipLineToConvexPolygon Tests
  2002. [Fact]
  2003. public void ClipLineToConvexPolygon_LineCompletelyInside_ReturnsFullInterval()
  2004. {
  2005. var origin = new Vector2(5, 5);
  2006. var direction = Vector2.UnitX;
  2007. var vertices = new[]
  2008. {
  2009. new Vector2(0, 0),
  2010. new Vector2(20, 0),
  2011. new Vector2(20, 20),
  2012. new Vector2(0, 20)
  2013. };
  2014. var normals = new[]
  2015. {
  2016. new Vector2(0, -1),
  2017. new Vector2(1, 0),
  2018. new Vector2(0, 1),
  2019. new Vector2(-1, 0)
  2020. };
  2021. float tLower = 0.0f;
  2022. float tUpper = 10.0f;
  2023. bool result = Collision2D.ClipLineToConvexPolygon(origin, direction, vertices, normals, tLower, tUpper, out float tEnter, out float tExit);
  2024. Assert.True(result);
  2025. Assert.Equal(0.0f, tEnter, Collision2D.Epsilon);
  2026. Assert.Equal(10.0f, tExit, Collision2D.Epsilon);
  2027. }
  2028. [Fact]
  2029. public void ClipLineToConvexPolygon_LinePartiallyInside_ReturnsClippedInterval()
  2030. {
  2031. var origin = new Vector2(-5, 5);
  2032. var direction = Vector2.UnitX;
  2033. var vertices = new[]
  2034. {
  2035. new Vector2(0, 0),
  2036. new Vector2(10, 0),
  2037. new Vector2(10, 10),
  2038. new Vector2(0, 10)
  2039. };
  2040. var normals = new[]
  2041. {
  2042. new Vector2(0, -1),
  2043. new Vector2(1, 0),
  2044. new Vector2(0, 1),
  2045. new Vector2(-1, 0)
  2046. };
  2047. float tLower = 0.0f;
  2048. float tUpper = 20.0f;
  2049. bool result = Collision2D.ClipLineToConvexPolygon(origin, direction, vertices, normals, tLower, tUpper, out float tEnter, out float tExit);
  2050. Assert.True(result);
  2051. Assert.Equal(5.0f, tEnter, Collision2D.Epsilon);
  2052. Assert.Equal(15.0f, tExit, Collision2D.Epsilon);
  2053. }
  2054. [Fact]
  2055. public void ClipLineToConvexPolygon_LineMissesPolygon_ReturnsFalse()
  2056. {
  2057. var origin = new Vector2(0, 20);
  2058. var direction = Vector2.UnitX;
  2059. var vertices = new[]
  2060. {
  2061. new Vector2(0, 0),
  2062. new Vector2(10, 0),
  2063. new Vector2(10, 10),
  2064. new Vector2(0, 10)
  2065. };
  2066. var normals = new[]
  2067. {
  2068. new Vector2(0, -1),
  2069. new Vector2(1, 0),
  2070. new Vector2(0, 1),
  2071. new Vector2(-1, 0)
  2072. };
  2073. float tLower = 0.0f;
  2074. float tUpper = 10.0f;
  2075. bool result = Collision2D.ClipLineToConvexPolygon(origin, direction, vertices, normals, tLower, tUpper, out float tEnter, out float tExit);
  2076. Assert.False(result);
  2077. }
  2078. #endregion
  2079. #endregion
  2080. #region Ray Interval Methods
  2081. #region RayCircleIntersectionInterval Tests
  2082. [Fact]
  2083. public void RayCircleIntersectionInterval_TwoIntersections_ReturnsBothParameters()
  2084. {
  2085. var origin = new Vector2(-10, 0);
  2086. var direction = Vector2.UnitX;
  2087. var center = new Vector2(0, 0);
  2088. var radius = 5.0f;
  2089. bool result = Collision2D.RayCircleIntersectionInterval(origin, direction, center, radius, out float tMin, out float tMax);
  2090. Assert.True(result);
  2091. Assert.Equal(5.0f, tMin, Collision2D.Epsilon);
  2092. Assert.Equal(15.0f, tMax, Collision2D.Epsilon);
  2093. }
  2094. [Fact]
  2095. public void RayCircleIntersectionInterval_Tangent_ReturnsSinglePoint()
  2096. {
  2097. var origin = new Vector2(-10, 5);
  2098. var direction = Vector2.UnitX;
  2099. var center = new Vector2(0, 0);
  2100. var radius = 5.0f;
  2101. bool result = Collision2D.RayCircleIntersectionInterval(origin, direction, center, radius, out float tMin, out float tMax);
  2102. Assert.True(result);
  2103. Assert.Equal(tMin, tMax, Collision2D.Epsilon);
  2104. }
  2105. [Fact]
  2106. public void RayCircleIntersectionInterval_Miss_ReturnsFalse()
  2107. {
  2108. var origin = new Vector2(-10, 10);
  2109. var direction = Vector2.UnitX;
  2110. var center = new Vector2(0, 0);
  2111. var radius = 5.0f;
  2112. bool result = Collision2D.RayCircleIntersectionInterval(origin, direction, center, radius, out float tMin, out float tMax);
  2113. Assert.False(result);
  2114. }
  2115. [Fact]
  2116. public void RayCircleIntersectionInterval_OriginInside_ReturnsIntervalFromZero()
  2117. {
  2118. var origin = new Vector2(0, 0);
  2119. var direction = Vector2.UnitX;
  2120. var center = new Vector2(0, 0);
  2121. var radius = 5.0f;
  2122. bool result = Collision2D.RayCircleIntersectionInterval(origin, direction, center, radius, out float tMin, out float tMax);
  2123. Assert.True(result);
  2124. Assert.Equal(0.0f, tMin, Collision2D.Epsilon);
  2125. Assert.Equal(5.0f, tMax, Collision2D.Epsilon);
  2126. }
  2127. #endregion
  2128. #region RayCapsuleIntersectionInterval Tests
  2129. [Fact]
  2130. public void RayCapsuleIntersectionInterval_HitsCylinder_ReturnsInterval()
  2131. {
  2132. var rayOrigin = new Vector2(-10, 0);
  2133. var rayDirection = Vector2.UnitX;
  2134. var segA = new Vector2(0, -5);
  2135. var segB = new Vector2(0, 5);
  2136. var radius = 3.0f;
  2137. bool result = Collision2D.RayCapsuleIntersectionInterval(rayOrigin, rayDirection, segA, segB, radius, out float tMin, out float tMax);
  2138. Assert.True(result);
  2139. Assert.True(tMax > tMin);
  2140. }
  2141. [Fact]
  2142. public void RayCapsuleIntersectionInterval_HitsEndCap_ReturnsInterval()
  2143. {
  2144. var rayOrigin = new Vector2(-10, 10);
  2145. var rayDirection = Vector2.UnitX;
  2146. var segA = new Vector2(0, 10);
  2147. var segB = new Vector2(10, 10);
  2148. var radius = 3.0f;
  2149. bool result = Collision2D.RayCapsuleIntersectionInterval(rayOrigin, rayDirection, segA, segB, radius, out float tMin, out float tMax);
  2150. Assert.True(result);
  2151. }
  2152. [Fact]
  2153. public void RayCapsuleIntersectionInterval_Miss_ReturnsFalse()
  2154. {
  2155. var rayOrigin = new Vector2(-10, 20);
  2156. var rayDirection = Vector2.UnitX;
  2157. var segA = new Vector2(0, 0);
  2158. var segB = new Vector2(10, 0);
  2159. var radius = 3.0f;
  2160. bool result = Collision2D.RayCapsuleIntersectionInterval(rayOrigin, rayDirection, segA, segB, radius, out float tMin, out float tMax);
  2161. Assert.False(result);
  2162. }
  2163. [Fact]
  2164. public void RayCapsuleIntersectionInterval_OriginInside_ReturnsIntervalFromZero()
  2165. {
  2166. var rayOrigin = new Vector2(5, 5);
  2167. var rayDirection = Vector2.UnitX;
  2168. var segA = new Vector2(0, 5);
  2169. var segB = new Vector2(10, 5);
  2170. var radius = 3.0f;
  2171. bool result = Collision2D.RayCapsuleIntersectionInterval(rayOrigin, rayDirection, segA, segB, radius, out float tMin, out float tMax);
  2172. Assert.True(result);
  2173. Assert.Equal(0.0f, tMin, Collision2D.Epsilon);
  2174. }
  2175. [Fact]
  2176. public void RayCapsuleIntersectionInterval_DegenerateCapsule_BehavesLikeCircle()
  2177. {
  2178. var rayOrigin = new Vector2(-10, 0);
  2179. var rayDirection = Vector2.UnitX;
  2180. var segA = new Vector2(0, 0);
  2181. var segB = new Vector2(0, 0);
  2182. var radius = 5.0f;
  2183. bool result = Collision2D.RayCapsuleIntersectionInterval(rayOrigin, rayDirection, segA, segB, radius, out float tMin, out float tMax);
  2184. Assert.True(result);
  2185. Assert.Equal(5.0f, tMin, Collision2D.Epsilon);
  2186. Assert.Equal(15.0f, tMax, Collision2D.Epsilon);
  2187. }
  2188. #endregion
  2189. #endregion
  2190. #region Overlap Methods
  2191. #region OverlapOnAxis Tests
  2192. [Fact]
  2193. public void OverlapOnAxis_TwoPolygonsOverlapping_ReturnsTrue()
  2194. {
  2195. var aVerts = new[]
  2196. {
  2197. new Vector2(0, 0),
  2198. new Vector2(10, 0),
  2199. new Vector2(10, 10),
  2200. new Vector2(0, 10)
  2201. };
  2202. var bVerts = new[]
  2203. {
  2204. new Vector2(5, 5),
  2205. new Vector2(15, 5),
  2206. new Vector2(15, 15),
  2207. new Vector2(5, 15)
  2208. };
  2209. var axis = Vector2.UnitX;
  2210. bool result = Collision2D.OverlapOnAxis(aVerts, bVerts, axis);
  2211. Assert.True(result);
  2212. }
  2213. [Fact]
  2214. public void OverlapOnAxis_TwoPolygonsSeparated_ReturnsFalse()
  2215. {
  2216. var aVerts = new[]
  2217. {
  2218. new Vector2(0, 0),
  2219. new Vector2(10, 0),
  2220. new Vector2(10, 10),
  2221. new Vector2(0, 10)
  2222. };
  2223. var bVerts = new[]
  2224. {
  2225. new Vector2(20, 20),
  2226. new Vector2(30, 20),
  2227. new Vector2(30, 30),
  2228. new Vector2(20, 30)
  2229. };
  2230. var axis = Vector2.UnitX;
  2231. bool result = Collision2D.OverlapOnAxis(aVerts, bVerts, axis);
  2232. Assert.False(result);
  2233. }
  2234. [Fact]
  2235. public void OverlapOnAxis_PolygonsTouching_ReturnsTrue()
  2236. {
  2237. var aVerts = new[]
  2238. {
  2239. new Vector2(0, 0),
  2240. new Vector2(10, 0),
  2241. new Vector2(10, 10),
  2242. new Vector2(0, 10)
  2243. };
  2244. var bVerts = new[]
  2245. {
  2246. new Vector2(10, 0),
  2247. new Vector2(20, 0),
  2248. new Vector2(20, 10),
  2249. new Vector2(10, 10)
  2250. };
  2251. var axis = Vector2.UnitX;
  2252. bool result = Collision2D.OverlapOnAxis(aVerts, bVerts, axis);
  2253. Assert.True(result);
  2254. }
  2255. #endregion
  2256. #region OverlapOnAxisAabbPolygon Tests
  2257. [Fact]
  2258. public void OverlapOnAxisAabbPolygon_Overlapping_ReturnsTrue()
  2259. {
  2260. var aabbCenter = new Vector2(5, 5);
  2261. var aabbHalfExtents = new Vector2(5, 5);
  2262. var polygonVertices = new[]
  2263. {
  2264. new Vector2(8, 8),
  2265. new Vector2(15, 8),
  2266. new Vector2(15, 15),
  2267. new Vector2(8, 15)
  2268. };
  2269. var axis = Vector2.UnitX;
  2270. bool result = Collision2D.OverlapOnAxis(aabbCenter, aabbHalfExtents, polygonVertices, axis);
  2271. Assert.True(result);
  2272. }
  2273. [Fact]
  2274. public void OverlapOnAxisAabbPolygon_Separated_ReturnsFalse()
  2275. {
  2276. var aabbCenter = new Vector2(5, 5);
  2277. var aabbHalfExtents = new Vector2(5, 5);
  2278. var polygonVertices = new[]
  2279. {
  2280. new Vector2(20, 20),
  2281. new Vector2(30, 20),
  2282. new Vector2(30, 30),
  2283. new Vector2(20, 30)
  2284. };
  2285. var axis = Vector2.UnitX;
  2286. bool result = Collision2D.OverlapOnAxis(aabbCenter, aabbHalfExtents, polygonVertices, axis);
  2287. Assert.False(result);
  2288. }
  2289. #endregion
  2290. #endregion
  2291. #region Intersection Methods
  2292. #region IntersectsAabbAabb Tests
  2293. [Fact]
  2294. public void IntersectsAabbAabb_Overlapping_ReturnsTrue()
  2295. {
  2296. var aMin = new Vector2(0, 0);
  2297. var aMax = new Vector2(10, 10);
  2298. var bMin = new Vector2(5, 5);
  2299. var bMax = new Vector2(15, 15);
  2300. bool result = Collision2D.IntersectsAabbAabb(aMin, aMax, bMin, bMax);
  2301. Assert.True(result);
  2302. }
  2303. [Fact]
  2304. public void IntersectsAabbAabb_Separated_ReturnsFalse()
  2305. {
  2306. var aMin = new Vector2(0, 0);
  2307. var aMax = new Vector2(10, 10);
  2308. var bMin = new Vector2(20, 20);
  2309. var bMax = new Vector2(30, 30);
  2310. bool result = Collision2D.IntersectsAabbAabb(aMin, aMax, bMin, bMax);
  2311. Assert.False(result);
  2312. }
  2313. #endregion
  2314. #region IntersectsAabbCapsule Tests
  2315. [Fact]
  2316. public void IntersectsAabbCapsule_Overlapping_ReturnsTrue()
  2317. {
  2318. var boxMin = new Vector2(0, 0);
  2319. var boxMax = new Vector2(10, 10);
  2320. var capsuleA = new Vector2(5, 5);
  2321. var capsuleB = new Vector2(15, 5);
  2322. var capsuleRadius = 2.0f;
  2323. bool result = Collision2D.IntersectsAabbCapsule(boxMin, boxMax, capsuleA, capsuleB, capsuleRadius);
  2324. Assert.True(result);
  2325. }
  2326. [Fact]
  2327. public void IntersectsAabbCapsule_Separated_ReturnsFalse()
  2328. {
  2329. var boxMin = new Vector2(0, 0);
  2330. var boxMax = new Vector2(10, 10);
  2331. var capsuleA = new Vector2(20, 20);
  2332. var capsuleB = new Vector2(30, 30);
  2333. var capsuleRadius = 2.0f;
  2334. bool result = Collision2D.IntersectsAabbCapsule(boxMin, boxMax, capsuleA, capsuleB, capsuleRadius);
  2335. Assert.False(result);
  2336. }
  2337. #endregion
  2338. #region IntersectsAabbConvexPolygon Tests
  2339. [Fact]
  2340. public void IntersectsAabbConvexPolygon_Overlapping_ReturnsTrue()
  2341. {
  2342. var aabbCenter = new Vector2(5, 5);
  2343. var aabbHalfExtents = new Vector2(5, 5);
  2344. var vertices = new[]
  2345. {
  2346. new Vector2(8, 8),
  2347. new Vector2(15, 8),
  2348. new Vector2(15, 15),
  2349. new Vector2(8, 15)
  2350. };
  2351. var normals = new[]
  2352. {
  2353. new Vector2(0, -1),
  2354. new Vector2(1, 0),
  2355. new Vector2(0, 1),
  2356. new Vector2(-1, 0)
  2357. };
  2358. bool result = Collision2D.IntersectsAabbConvexPolygon(aabbCenter, aabbHalfExtents, vertices, normals);
  2359. Assert.True(result);
  2360. }
  2361. [Fact]
  2362. public void IntersectsAabbConvexPolygon_Separated_ReturnsFalse()
  2363. {
  2364. var aabbCenter = new Vector2(5, 5);
  2365. var aabbHalfExtents = new Vector2(5, 5);
  2366. var vertices = new[]
  2367. {
  2368. new Vector2(20, 20),
  2369. new Vector2(30, 20),
  2370. new Vector2(30, 30),
  2371. new Vector2(20, 30)
  2372. };
  2373. var normals = new[]
  2374. {
  2375. new Vector2(0, -1),
  2376. new Vector2(1, 0),
  2377. new Vector2(0, 1),
  2378. new Vector2(-1, 0)
  2379. };
  2380. bool result = Collision2D.IntersectsAabbConvexPolygon(aabbCenter, aabbHalfExtents, vertices, normals);
  2381. Assert.False(result);
  2382. }
  2383. #endregion
  2384. #region IntersectsAabbObb Tests
  2385. [Fact]
  2386. public void IntersectsAabbObb_Overlapping_ReturnsTrue()
  2387. {
  2388. var aabbCenter = new Vector2(5, 5);
  2389. var aabbHalfExtents = new Vector2(5, 5);
  2390. var obbCenter = new Vector2(8, 8);
  2391. var obbAxisX = Vector2.UnitX;
  2392. var obbAxisY = Vector2.UnitY;
  2393. var obbHalfExtents = new Vector2(3, 3);
  2394. bool result = Collision2D.IntersectsAabbObb(aabbCenter, aabbHalfExtents, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
  2395. Assert.True(result);
  2396. }
  2397. [Fact]
  2398. public void IntersectsAabbObb_Separated_ReturnsFalse()
  2399. {
  2400. var aabbCenter = new Vector2(5, 5);
  2401. var aabbHalfExtents = new Vector2(5, 5);
  2402. var obbCenter = new Vector2(20, 20);
  2403. var obbAxisX = Vector2.UnitX;
  2404. var obbAxisY = Vector2.UnitY;
  2405. var obbHalfExtents = new Vector2(3, 3);
  2406. bool result = Collision2D.IntersectsAabbObb(aabbCenter, aabbHalfExtents, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
  2407. Assert.False(result);
  2408. }
  2409. #endregion
  2410. #region IntersectsCircleCircle Tests
  2411. [Fact]
  2412. public void IntersectsCircleCircle_Overlapping_ReturnsTrue()
  2413. {
  2414. var aCenter = new Vector2(0, 0);
  2415. var aRadius = 5.0f;
  2416. var bCenter = new Vector2(8, 0);
  2417. var bRadius = 5.0f;
  2418. bool result = Collision2D.IntersectsCircleCircle(aCenter, aRadius, bCenter, bRadius);
  2419. Assert.True(result);
  2420. }
  2421. [Fact]
  2422. public void IntersectsCircleCircle_Separated_ReturnsFalse()
  2423. {
  2424. var aCenter = new Vector2(0, 0);
  2425. var aRadius = 5.0f;
  2426. var bCenter = new Vector2(20, 0);
  2427. var bRadius = 5.0f;
  2428. bool result = Collision2D.IntersectsCircleCircle(aCenter, aRadius, bCenter, bRadius);
  2429. Assert.False(result);
  2430. }
  2431. [Fact]
  2432. public void IntersectsCircleCircle_Touching_ReturnsTrue()
  2433. {
  2434. var aCenter = new Vector2(0, 0);
  2435. var aRadius = 5.0f;
  2436. var bCenter = new Vector2(10, 0);
  2437. var bRadius = 5.0f;
  2438. bool result = Collision2D.IntersectsCircleCircle(aCenter, aRadius, bCenter, bRadius);
  2439. Assert.True(result);
  2440. }
  2441. #endregion
  2442. #region IntersectsCircleAabb Tests
  2443. [Fact]
  2444. public void IntersectsCircleAabb_Overlapping_ReturnsTrue()
  2445. {
  2446. var circleCenter = new Vector2(5, 5);
  2447. var circleRadius = 5.0f;
  2448. var boxMin = new Vector2(0, 0);
  2449. var boxMax = new Vector2(10, 10);
  2450. bool result = Collision2D.IntersectsCircleAabb(circleCenter, circleRadius, boxMin, boxMax);
  2451. Assert.True(result);
  2452. }
  2453. [Fact]
  2454. public void IntersectsCircleAabb_Separated_ReturnsFalse()
  2455. {
  2456. var circleCenter = new Vector2(0, 0);
  2457. var circleRadius = 5.0f;
  2458. var boxMin = new Vector2(20, 20);
  2459. var boxMax = new Vector2(30, 30);
  2460. bool result = Collision2D.IntersectsCircleAabb(circleCenter, circleRadius, boxMin, boxMax);
  2461. Assert.False(result);
  2462. }
  2463. #endregion
  2464. #region IntersectsCircleObb Tests
  2465. [Fact]
  2466. public void IntersectsCircleObb_Overlapping_ReturnsTrue()
  2467. {
  2468. var circleCenter = new Vector2(5, 5);
  2469. var circleRadius = 5.0f;
  2470. var obbCenter = new Vector2(8, 8);
  2471. var obbAxisX = Vector2.UnitX;
  2472. var obbAxisY = Vector2.UnitY;
  2473. var obbHalfExtents = new Vector2(3, 3);
  2474. bool result = Collision2D.IntersectsCircleObb(circleCenter, circleRadius, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
  2475. Assert.True(result);
  2476. }
  2477. [Fact]
  2478. public void IntersectsCircleObb_Separated_ReturnsFalse()
  2479. {
  2480. var circleCenter = new Vector2(0, 0);
  2481. var circleRadius = 5.0f;
  2482. var obbCenter = new Vector2(20, 20);
  2483. var obbAxisX = Vector2.UnitX;
  2484. var obbAxisY = Vector2.UnitY;
  2485. var obbHalfExtents = new Vector2(3, 3);
  2486. bool result = Collision2D.IntersectsCircleObb(circleCenter, circleRadius, obbCenter, obbAxisX, obbAxisY, obbHalfExtents);
  2487. Assert.False(result);
  2488. }
  2489. #endregion
  2490. #region IntersectsCircleConvexPolygon Tests
  2491. [Fact]
  2492. public void IntersectsCircleConvexPolygon_Overlapping_ReturnsTrue()
  2493. {
  2494. var circleCenter = new Vector2(5, 5);
  2495. var circleRadius = 5.0f;
  2496. var vertices = new[]
  2497. {
  2498. new Vector2(8, 8),
  2499. new Vector2(15, 8),
  2500. new Vector2(15, 15),
  2501. new Vector2(8, 15)
  2502. };
  2503. var normals = new[]
  2504. {
  2505. new Vector2(0, -1),
  2506. new Vector2(1, 0),
  2507. new Vector2(0, 1),
  2508. new Vector2(-1, 0)
  2509. };
  2510. bool result = Collision2D.IntersectsCircleConvexPolygon(circleCenter, circleRadius, vertices, normals);
  2511. Assert.True(result);
  2512. }
  2513. [Fact]
  2514. public void IntersectsCircleConvexPolygon_Separated_ReturnsFalse()
  2515. {
  2516. var circleCenter = new Vector2(0, 0);
  2517. var circleRadius = 5.0f;
  2518. var vertices = new[]
  2519. {
  2520. new Vector2(20, 20),
  2521. new Vector2(30, 20),
  2522. new Vector2(30, 30),
  2523. new Vector2(20, 30)
  2524. };
  2525. var normals = new[]
  2526. {
  2527. new Vector2(0, -1),
  2528. new Vector2(1, 0),
  2529. new Vector2(0, 1),
  2530. new Vector2(-1, 0)
  2531. };
  2532. bool result = Collision2D.IntersectsCircleConvexPolygon(circleCenter, circleRadius, vertices, normals);
  2533. Assert.False(result);
  2534. }
  2535. #endregion
  2536. #region IntersectsCircleCapsule Tests
  2537. [Fact]
  2538. public void IntersectsCircleCapsule_Overlapping_ReturnsTrue()
  2539. {
  2540. var circleCenter = new Vector2(5, 5);
  2541. var circleRadius = 5.0f;
  2542. var capsuleA = new Vector2(8, 8);
  2543. var capsuleB = new Vector2(15, 8);
  2544. var capsuleRadius = 2.0f;
  2545. bool result = Collision2D.IntersectsCircleCapsule(circleCenter, circleRadius, capsuleA, capsuleB, capsuleRadius);
  2546. Assert.True(result);
  2547. }
  2548. [Fact]
  2549. public void IntersectsCircleCapsule_Separated_ReturnsFalse()
  2550. {
  2551. var circleCenter = new Vector2(0, 0);
  2552. var circleRadius = 5.0f;
  2553. var capsuleA = new Vector2(20, 20);
  2554. var capsuleB = new Vector2(30, 30);
  2555. var capsuleRadius = 2.0f;
  2556. bool result = Collision2D.IntersectsCircleCapsule(circleCenter, circleRadius, capsuleA, capsuleB, capsuleRadius);
  2557. Assert.False(result);
  2558. }
  2559. #endregion
  2560. #region IntersectsObbObb Tests
  2561. [Fact]
  2562. public void IntersectsObbObb_Overlapping_ReturnsTrue()
  2563. {
  2564. var aCenter = new Vector2(5, 5);
  2565. var aAxisX = Vector2.UnitX;
  2566. var aAxisY = Vector2.UnitY;
  2567. var aHalf = new Vector2(5, 5);
  2568. var bCenter = new Vector2(8, 8);
  2569. var bAxisX = Vector2.UnitX;
  2570. var bAxisY = Vector2.UnitY;
  2571. var bHalf = new Vector2(3, 3);
  2572. bool result = Collision2D.IntersectsObbObb(aCenter, aAxisX, aAxisY, aHalf, bCenter, bAxisX, bAxisY, bHalf);
  2573. Assert.True(result);
  2574. }
  2575. [Fact]
  2576. public void IntersectsObbObb_Separated_ReturnsFalse()
  2577. {
  2578. var aCenter = new Vector2(0, 0);
  2579. var aAxisX = Vector2.UnitX;
  2580. var aAxisY = Vector2.UnitY;
  2581. var aHalf = new Vector2(5, 5);
  2582. var bCenter = new Vector2(20, 20);
  2583. var bAxisX = Vector2.UnitX;
  2584. var bAxisY = Vector2.UnitY;
  2585. var bHalf = new Vector2(3, 3);
  2586. bool result = Collision2D.IntersectsObbObb(aCenter, aAxisX, aAxisY, aHalf, bCenter, bAxisX, bAxisY, bHalf);
  2587. Assert.False(result);
  2588. }
  2589. #endregion
  2590. #region IntersectsObbCapsule Tests
  2591. [Fact]
  2592. public void IntersectsObbCapsule_Overlapping_ReturnsTrue()
  2593. {
  2594. var obbCenter = new Vector2(5, 5);
  2595. var obbAxisX = Vector2.UnitX;
  2596. var obbAxisY = Vector2.UnitY;
  2597. var obbHalfExtents = new Vector2(5, 5);
  2598. var capsuleA = new Vector2(8, 8);
  2599. var capsuleB = new Vector2(15, 8);
  2600. var capsuleRadius = 2.0f;
  2601. bool result = Collision2D.IntersectsObbCapsule(obbCenter, obbAxisX, obbAxisY, obbHalfExtents, capsuleA, capsuleB, capsuleRadius);
  2602. Assert.True(result);
  2603. }
  2604. [Fact]
  2605. public void IntersectsObbCapsule_Separated_ReturnsFalse()
  2606. {
  2607. var obbCenter = new Vector2(0, 0);
  2608. var obbAxisX = Vector2.UnitX;
  2609. var obbAxisY = Vector2.UnitY;
  2610. var obbHalfExtents = new Vector2(5, 5);
  2611. var capsuleA = new Vector2(20, 20);
  2612. var capsuleB = new Vector2(30, 30);
  2613. var capsuleRadius = 2.0f;
  2614. bool result = Collision2D.IntersectsObbCapsule(obbCenter, obbAxisX, obbAxisY, obbHalfExtents, capsuleA, capsuleB, capsuleRadius);
  2615. Assert.False(result);
  2616. }
  2617. #endregion
  2618. #region IntersectsObbConvexPolygon Tests
  2619. [Fact]
  2620. public void IntersectsObbConvexPolygon_Overlapping_ReturnsTrue()
  2621. {
  2622. var obbCenter = new Vector2(5, 5);
  2623. var obbAxisX = Vector2.UnitX;
  2624. var obbAxisY = Vector2.UnitY;
  2625. var obbHalfExtents = new Vector2(5, 5);
  2626. var vertices = new[]
  2627. {
  2628. new Vector2(8, 8),
  2629. new Vector2(15, 8),
  2630. new Vector2(15, 15),
  2631. new Vector2(8, 15)
  2632. };
  2633. var normals = new[]
  2634. {
  2635. new Vector2(0, -1),
  2636. new Vector2(1, 0),
  2637. new Vector2(0, 1),
  2638. new Vector2(-1, 0)
  2639. };
  2640. bool result = Collision2D.IntersectsObbConvexPolygon(obbCenter, obbAxisX, obbAxisY, obbHalfExtents, vertices, normals);
  2641. Assert.True(result);
  2642. }
  2643. [Fact]
  2644. public void IntersectsObbConvexPolygon_Separated_ReturnsFalse()
  2645. {
  2646. var obbCenter = new Vector2(0, 0);
  2647. var obbAxisX = Vector2.UnitX;
  2648. var obbAxisY = Vector2.UnitY;
  2649. var obbHalfExtents = new Vector2(5, 5);
  2650. var vertices = new[]
  2651. {
  2652. new Vector2(20, 20),
  2653. new Vector2(30, 20),
  2654. new Vector2(30, 30),
  2655. new Vector2(20, 30)
  2656. };
  2657. var normals = new[]
  2658. {
  2659. new Vector2(0, -1),
  2660. new Vector2(1, 0),
  2661. new Vector2(0, 1),
  2662. new Vector2(-1, 0)
  2663. };
  2664. bool result = Collision2D.IntersectsObbConvexPolygon(obbCenter, obbAxisX, obbAxisY, obbHalfExtents, vertices, normals);
  2665. Assert.False(result);
  2666. }
  2667. #endregion
  2668. #region IntersectsCapsuleCapsule Tests
  2669. [Fact]
  2670. public void IntersectsCapsuleCapsule_Overlapping_ReturnsTrue()
  2671. {
  2672. var a0 = new Vector2(0, 0);
  2673. var a1 = new Vector2(10, 0);
  2674. var aRadius = 3.0f;
  2675. var b0 = new Vector2(8, 0);
  2676. var b1 = new Vector2(15, 0);
  2677. var bRadius = 3.0f;
  2678. bool result = Collision2D.IntersectsCapsuleCapsule(a0, a1, aRadius, b0, b1, bRadius);
  2679. Assert.True(result);
  2680. }
  2681. [Fact]
  2682. public void IntersectsCapsuleCapsule_Separated_ReturnsFalse()
  2683. {
  2684. var a0 = new Vector2(0, 0);
  2685. var a1 = new Vector2(10, 0);
  2686. var aRadius = 2.0f;
  2687. var b0 = new Vector2(20, 20);
  2688. var b1 = new Vector2(30, 30);
  2689. var bRadius = 2.0f;
  2690. bool result = Collision2D.IntersectsCapsuleCapsule(a0, a1, aRadius, b0, b1, bRadius);
  2691. Assert.False(result);
  2692. }
  2693. #endregion
  2694. #region IntersectsCapsuleConvexPolygon Tests
  2695. [Fact]
  2696. public void IntersectsCapsuleConvexPolygon_Overlapping_ReturnsTrue()
  2697. {
  2698. var capsuleA = new Vector2(5, 5);
  2699. var capsuleB = new Vector2(15, 5);
  2700. var capsuleRadius = 3.0f;
  2701. var vertices = new[]
  2702. {
  2703. new Vector2(8, 8),
  2704. new Vector2(15, 8),
  2705. new Vector2(15, 15),
  2706. new Vector2(8, 15)
  2707. };
  2708. var normals = new[]
  2709. {
  2710. new Vector2(0, -1),
  2711. new Vector2(1, 0),
  2712. new Vector2(0, 1),
  2713. new Vector2(-1, 0)
  2714. };
  2715. bool result = Collision2D.IntersectsCapsuleConvexPolygon(capsuleA, capsuleB, capsuleRadius, vertices, normals);
  2716. Assert.True(result);
  2717. }
  2718. [Fact]
  2719. public void IntersectsCapsuleConvexPolygon_Separated_ReturnsFalse()
  2720. {
  2721. var capsuleA = new Vector2(0, 0);
  2722. var capsuleB = new Vector2(10, 0);
  2723. var capsuleRadius = 2.0f;
  2724. var vertices = new[]
  2725. {
  2726. new Vector2(20, 20),
  2727. new Vector2(30, 20),
  2728. new Vector2(30, 30),
  2729. new Vector2(20, 30)
  2730. };
  2731. var normals = new[]
  2732. {
  2733. new Vector2(0, -1),
  2734. new Vector2(1, 0),
  2735. new Vector2(0, 1),
  2736. new Vector2(-1, 0)
  2737. };
  2738. bool result = Collision2D.IntersectsCapsuleConvexPolygon(capsuleA, capsuleB, capsuleRadius, vertices, normals);
  2739. Assert.False(result);
  2740. }
  2741. #endregion
  2742. #region IntersectsConvexPolygonConvexPolygon Tests
  2743. [Fact]
  2744. public void IntersectsConvexPolygonConvexPolygon_Overlapping_ReturnsTrue()
  2745. {
  2746. var aVertices = new[]
  2747. {
  2748. new Vector2(0, 0),
  2749. new Vector2(10, 0),
  2750. new Vector2(10, 10),
  2751. new Vector2(0, 10)
  2752. };
  2753. var aNormals = new[]
  2754. {
  2755. new Vector2(0, -1),
  2756. new Vector2(1, 0),
  2757. new Vector2(0, 1),
  2758. new Vector2(-1, 0)
  2759. };
  2760. var bVertices = new[]
  2761. {
  2762. new Vector2(5, 5),
  2763. new Vector2(15, 5),
  2764. new Vector2(15, 15),
  2765. new Vector2(5, 15)
  2766. };
  2767. var bNormals = new[]
  2768. {
  2769. new Vector2(0, -1),
  2770. new Vector2(1, 0),
  2771. new Vector2(0, 1),
  2772. new Vector2(-1, 0)
  2773. };
  2774. bool result = Collision2D.IntersectsConvexPolygonConvexPolygon(aVertices, aNormals, bVertices, bNormals);
  2775. Assert.True(result);
  2776. }
  2777. [Fact]
  2778. public void IntersectsConvexPolygonConvexPolygon_Separated_ReturnsFalse()
  2779. {
  2780. var aVertices = new[]
  2781. {
  2782. new Vector2(0, 0),
  2783. new Vector2(10, 0),
  2784. new Vector2(10, 10),
  2785. new Vector2(0, 10)
  2786. };
  2787. var aNormals = new[]
  2788. {
  2789. new Vector2(0, -1),
  2790. new Vector2(1, 0),
  2791. new Vector2(0, 1),
  2792. new Vector2(-1, 0)
  2793. };
  2794. var bVertices = new[]
  2795. {
  2796. new Vector2(20, 20),
  2797. new Vector2(30, 20),
  2798. new Vector2(30, 30),
  2799. new Vector2(20, 30)
  2800. };
  2801. var bNormals = new[]
  2802. {
  2803. new Vector2(0, -1),
  2804. new Vector2(1, 0),
  2805. new Vector2(0, 1),
  2806. new Vector2(-1, 0)
  2807. };
  2808. bool result = Collision2D.IntersectsConvexPolygonConvexPolygon(aVertices, aNormals, bVertices, bNormals);
  2809. Assert.False(result);
  2810. }
  2811. #endregion
  2812. #endregion
  2813. }
  2814. }