Physics.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /// <summary>
  8. /// Provides global access to the physics system, including scene queries and global options.
  9. /// </summary>
  10. public static class Physics
  11. {
  12. /// <summary>
  13. /// Global gravity value for all objects in the scene.
  14. /// </summary>
  15. public static Vector3 Gravity
  16. {
  17. get { Vector3 gravity; Internal_GetGravity(out gravity); return gravity; }
  18. set { Internal_SetGravity(ref value); }
  19. }
  20. /// <summary>
  21. /// Casts a ray into the scene and returns the closest found hit, if any.
  22. /// </summary>
  23. /// <param name="ray">Ray to cast into the scene.</param>
  24. /// <param name="hit">Information recorded about a hit. Only valid if method returns true.</param>
  25. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  26. /// </param>
  27. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  28. /// </param>
  29. /// <returns>True if something was hit, false otherwise.</returns>
  30. public static bool RayCast(Ray ray, out PhysicsQueryHit hit, ulong layer = ulong.MaxValue, float max = float.MaxValue)
  31. {
  32. return RayCast(ray.origin, ray.direction, out hit, layer, max);
  33. }
  34. /// <summary>
  35. /// Casts a ray into the scene and returns the closest found hit, if any.
  36. /// </summary>
  37. /// <param name="origin">Origin of the ray to cast into the scene.</param>
  38. /// <param name="unitDir">Unit direction of the ray to cast into the scene.</param>
  39. /// <param name="hit">Information recorded about a hit. Only valid if method returns true.</param>
  40. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  41. /// </param>
  42. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  43. /// </param>
  44. /// <returns>True if something was hit, false otherwise.</returns>
  45. public static bool RayCast(Vector3 origin, Vector3 unitDir, out PhysicsQueryHit hit,
  46. ulong layer = ulong.MaxValue, float max = float.MaxValue)
  47. {
  48. ScriptPhysicsQueryHit scriptHit = new ScriptPhysicsQueryHit();
  49. if(Internal_RayCast(ref origin, ref unitDir, out scriptHit, layer, max))
  50. {
  51. ConvertPhysicsQueryHit(ref scriptHit, out hit);
  52. return true;
  53. }
  54. hit = new PhysicsQueryHit();
  55. return false;
  56. }
  57. /// <summary>
  58. /// Performs a sweep into the scene using a box and returns the closest found hit, if any.
  59. /// </summary>
  60. /// <param name="box">Box to sweep through the scene.</param>
  61. /// <param name="rotation">Orientation of the box.</param>
  62. /// <param name="unitDir">Unit direction towards which to perform the sweep.</param>
  63. /// <param name="hit">Information recorded about a hit. Only valid if method returns true.</param>
  64. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  65. /// </param>
  66. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  67. /// </param>
  68. /// <returns>True if something was hit, false otherwise.</returns>
  69. public static bool BoxCast(AABox box, Quaternion rotation, Vector3 unitDir, out PhysicsQueryHit hit,
  70. ulong layer = ulong.MaxValue, float max = float.MaxValue)
  71. {
  72. ScriptPhysicsQueryHit scriptHit = new ScriptPhysicsQueryHit();
  73. if(Internal_BoxCast(ref box, ref rotation, ref unitDir, out scriptHit, layer, max))
  74. {
  75. ConvertPhysicsQueryHit(ref scriptHit, out hit);
  76. return true;
  77. }
  78. hit = new PhysicsQueryHit();
  79. return false;
  80. }
  81. /// <summary>
  82. /// Performs a sweep into the scene using a sphere and returns the closest found hit, if any.
  83. /// </summary>
  84. /// <param name="sphere">Sphere to sweep through the scene.</param>
  85. /// <param name="unitDir">Unit direction towards which to perform the sweep.</param>
  86. /// <param name="hit">Information recorded about a hit. Only valid if method returns true.</param>
  87. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  88. /// </param>
  89. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  90. /// </param>
  91. /// <returns>True if something was hit, false otherwise.</returns>
  92. public static bool SphereCast(Sphere sphere, Vector3 unitDir, out PhysicsQueryHit hit,
  93. ulong layer = ulong.MaxValue, float max = float.MaxValue)
  94. {
  95. ScriptPhysicsQueryHit scriptHit = new ScriptPhysicsQueryHit();
  96. if(Internal_SphereCast(ref sphere, ref unitDir, out scriptHit, layer, max))
  97. {
  98. ConvertPhysicsQueryHit(ref scriptHit, out hit);
  99. return true;
  100. }
  101. hit = new PhysicsQueryHit();
  102. return false;
  103. }
  104. /// <summary>
  105. /// Performs a sweep into the scene using a capsule and returns the closest found hit, if any.
  106. /// </summary>
  107. /// <param name="capsule">Capsule to sweep through the scene.</param>
  108. /// <param name="rotation">Orientation of the capsule.</param>
  109. /// <param name="unitDir">Unit direction towards which to perform the sweep.</param>
  110. /// <param name="hit">Information recorded about a hit. Only valid if method returns true.</param>
  111. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  112. /// </param>
  113. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  114. /// </param>
  115. /// <returns>True if something was hit, false otherwise.</returns>
  116. public static bool CapsuleCast(Capsule capsule, Quaternion rotation, Vector3 unitDir,
  117. out PhysicsQueryHit hit, ulong layer = ulong.MaxValue, float max = float.MaxValue)
  118. {
  119. ScriptPhysicsQueryHit scriptHit = new ScriptPhysicsQueryHit();
  120. if(Internal_CapsuleCast(ref capsule, ref rotation, ref unitDir, out scriptHit, layer, max))
  121. {
  122. ConvertPhysicsQueryHit(ref scriptHit, out hit);
  123. return true;
  124. }
  125. hit = new PhysicsQueryHit();
  126. return false;
  127. }
  128. /// <summary>
  129. /// Performs a sweep into the scene using a convex mesh and returns the closest found hit, if any.
  130. /// </summary>
  131. /// <param name="mesh">Mesh to sweep through the scene. Must be convex.</param>
  132. /// <param name="position">Starting position of the mesh.</param>
  133. /// <param name="rotation">Orientation of the mesh.</param>
  134. /// <param name="unitDir">Unit direction towards which to perform the sweep.</param>
  135. /// <param name="hit">Information recorded about a hit. Only valid if method returns true.</param>
  136. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  137. /// </param>
  138. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  139. /// </param>
  140. /// <returns>True if something was hit, false otherwise.</returns>
  141. public static bool ConvexCast(PhysicsMesh mesh, Vector3 position, Quaternion rotation,
  142. Vector3 unitDir, out PhysicsQueryHit hit, ulong layer = ulong.MaxValue, float max = float.MaxValue)
  143. {
  144. IntPtr meshPtr = IntPtr.Zero;
  145. if (mesh != null)
  146. meshPtr = mesh.GetCachedPtr();
  147. ScriptPhysicsQueryHit scriptHit = new ScriptPhysicsQueryHit();
  148. if(Internal_ConvexCast(meshPtr, ref position, ref rotation, ref unitDir, out scriptHit, layer, max))
  149. {
  150. ConvertPhysicsQueryHit(ref scriptHit, out hit);
  151. return true;
  152. }
  153. hit = new PhysicsQueryHit();
  154. return false;
  155. }
  156. /// <summary>
  157. /// Casts a ray into the scene and returns all found hits.
  158. /// </summary>
  159. /// <param name="ray">Ray to cast into the scene.</param>
  160. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  161. /// </param>
  162. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  163. /// </param>
  164. /// <returns>List of all detected hits.</returns>
  165. public static PhysicsQueryHit[] RayCastAll(Ray ray, ulong layer = ulong.MaxValue, float max = float.MaxValue)
  166. {
  167. return RayCastAll(ray.origin, ray.direction, layer, max);
  168. }
  169. /// <summary>
  170. /// Casts a ray into the scene and returns all found hits.
  171. /// </summary>
  172. /// <param name="origin">Origin of the ray to cast into the scene.</param>
  173. /// <param name="unitDir">Unit direction of the ray to cast into the scene.</param>
  174. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  175. /// </param>
  176. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  177. /// </param>
  178. /// <returns>List of all detected hits.</returns>
  179. public static PhysicsQueryHit[] RayCastAll(Vector3 origin, Vector3 unitDir,
  180. ulong layer = ulong.MaxValue, float max = float.MaxValue)
  181. {
  182. return ConvertPhysicsQueryHits(Internal_RayCastAll(ref origin, ref unitDir, layer, max));
  183. }
  184. /// <summary>
  185. /// Performs a sweep into the scene using a box and returns all found hits.
  186. /// </summary>
  187. /// <param name="box">Box to sweep through the scene.</param>
  188. /// <param name="rotation">Orientation of the box.</param>
  189. /// <param name="unitDir">Unit direction towards which to perform the sweep.</param>
  190. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  191. /// </param>
  192. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  193. /// </param>
  194. /// <returns>List of all detected hits.</returns>
  195. public static PhysicsQueryHit[] BoxCastAll(AABox box, Quaternion rotation,
  196. Vector3 unitDir, ulong layer = ulong.MaxValue, float max = float.MaxValue)
  197. {
  198. return ConvertPhysicsQueryHits(Internal_BoxCastAll(ref box, ref rotation, ref unitDir, layer, max));
  199. }
  200. /// <summary>
  201. /// Performs a sweep into the scene using a sphere and returns all found hits.
  202. /// </summary>
  203. /// <param name="sphere">Sphere to sweep through the scene.</param>
  204. /// <param name="unitDir">Unit direction towards which to perform the sweep.</param>
  205. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  206. /// </param>
  207. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  208. /// </param>
  209. /// <returns>List of all detected hits.</returns>
  210. public static PhysicsQueryHit[] SphereCastAll(Sphere sphere, Vector3 unitDir,
  211. ulong layer = ulong.MaxValue, float max = float.MaxValue)
  212. {
  213. return ConvertPhysicsQueryHits(Internal_SphereCastAll(ref sphere, ref unitDir, layer, max));
  214. }
  215. /// <summary>
  216. /// Performs a sweep into the scene using a capsule and returns all found hits.
  217. /// </summary>
  218. /// <param name="capsule">Capsule to sweep through the scene.</param>
  219. /// <param name="rotation">Orientation of the capsule.</param>
  220. /// <param name="unitDir">Unit direction towards which to perform the sweep.</param>
  221. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  222. /// </param>
  223. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  224. /// </param>
  225. /// <returns>List of all detected hits.</returns>
  226. public static PhysicsQueryHit[] CapsuleCastAll(Capsule capsule, Quaternion rotation,
  227. Vector3 unitDir, ulong layer = ulong.MaxValue, float max = float.MaxValue)
  228. {
  229. return ConvertPhysicsQueryHits(Internal_CapsuleCastAll(ref capsule, ref rotation, ref unitDir, layer, max));
  230. }
  231. /// <summary>
  232. /// Performs a sweep into the scene using a convex mesh and returns all found hits.
  233. /// </summary>
  234. /// <param name="mesh">Mesh to sweep through the scene. Must be convex.</param>
  235. /// <param name="position">Starting position of the mesh.</param>
  236. /// <param name="rotation">Orientation of the mesh.</param>
  237. /// <param name="unitDir">Unit direction towards which to perform the sweep.</param>
  238. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  239. /// </param>
  240. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  241. /// </param>
  242. /// <returns>List of all detected hits.</returns>
  243. public static PhysicsQueryHit[] ConvexCastAll(PhysicsMesh mesh, Vector3 position,
  244. Quaternion rotation, Vector3 unitDir, ulong layer = ulong.MaxValue, float max = float.MaxValue)
  245. {
  246. IntPtr meshPtr = IntPtr.Zero;
  247. if (mesh != null)
  248. meshPtr = mesh.GetCachedPtr();
  249. return ConvertPhysicsQueryHits(Internal_ConvexCastAll(meshPtr, ref position, ref rotation, ref unitDir, layer, max));
  250. }
  251. /// <summary>
  252. /// Casts a ray into the scene and checks if it has hit anything. This can be significantly more efficient than
  253. /// other types of cast* calls.
  254. /// </summary>
  255. /// <param name="ray">Ray to cast into the scene.</param>
  256. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  257. /// </param>
  258. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  259. /// </param>
  260. /// <returns>True if something was hit, false otherwise.</returns>
  261. public static bool RayCastAny(Ray ray, ulong layer = ulong.MaxValue, float max = float.MaxValue)
  262. {
  263. return RayCastAny(ray.origin, ray.direction, layer, max);
  264. }
  265. /// <summary>
  266. /// Casts a ray into the scene and checks if it has hit anything. This can be significantly more efficient than
  267. /// other types of cast* calls.
  268. /// </summary>
  269. /// <param name="origin">Origin of the ray to cast into the scene.</param>
  270. /// <param name="unitDir">Unit direction of the ray to cast into the scene.</param>
  271. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  272. /// </param>
  273. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  274. /// </param>
  275. /// <returns>True if something was hit, false otherwise.</returns>
  276. public static bool RayCastAny(Vector3 origin, Vector3 unitDir, ulong layer = ulong.MaxValue,
  277. float max = float.MaxValue)
  278. {
  279. return Internal_RayCastAny(ref origin, ref unitDir, layer, max);
  280. }
  281. /// <summary>
  282. /// Performs a sweep into the scene using a box and checks if it has hit anything. This can be significantly more
  283. /// efficient than other types of cast* calls.
  284. /// </summary>
  285. /// <param name="box">Box to sweep through the scene.</param>
  286. /// <param name="rotation">Orientation of the box.</param>
  287. /// <param name="unitDir">Unit direction towards which to perform the sweep.</param>
  288. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  289. /// </param>
  290. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  291. /// </param>
  292. /// <returns>True if something was hit, false otherwise.</returns>
  293. public static bool BoxCastAny(AABox box, Quaternion rotation, Vector3 unitDir, ulong layer = ulong.MaxValue,
  294. float max = float.MaxValue)
  295. {
  296. return Internal_BoxCastAny(ref box, ref rotation, ref unitDir, layer, max);
  297. }
  298. /// <summary>
  299. /// Performs a sweep into the scene using a sphere and checks if it has hit anything. This can be significantly more
  300. /// efficient than other types of cast* calls.
  301. /// </summary>
  302. /// <param name="sphere">Sphere to sweep through the scene.</param>
  303. /// <param name="unitDir">Unit direction towards which to perform the sweep.</param>
  304. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  305. /// </param>
  306. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  307. /// </param>
  308. /// <returns>True if something was hit, false otherwise.</returns>
  309. public static bool SphereCastAny(Sphere sphere, Vector3 unitDir, ulong layer = ulong.MaxValue,
  310. float max = float.MaxValue)
  311. {
  312. return Internal_SphereCastAny(ref sphere, ref unitDir, layer, max);
  313. }
  314. /// <summary>
  315. /// Performs a sweep into the scene using a capsule and checks if it has hit anything. This can be significantly
  316. /// more efficient than other types of cast* calls.
  317. /// </summary>
  318. /// <param name="capsule">Capsule to sweep through the scene.</param>
  319. /// <param name="rotation">Orientation of the capsule.</param>
  320. /// <param name="unitDir">Unit direction towards which to perform the sweep.</param>
  321. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  322. /// </param>
  323. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  324. /// </param>
  325. /// <returns>True if something was hit, false otherwise.</returns>
  326. public static bool CapsuleCastAny(Capsule capsule, Quaternion rotation, Vector3 unitDir,
  327. ulong layer = ulong.MaxValue, float max = float.MaxValue)
  328. {
  329. return Internal_CapsuleCastAny(ref capsule, ref rotation, ref unitDir, layer, max);
  330. }
  331. /// <summary>
  332. /// Performs a sweep into the scene using a convex mesh and checks if it has hit anything. This can be significantly
  333. /// more efficient than other types of cast* calls.
  334. /// </summary>
  335. /// <param name="mesh">Mesh to sweep through the scene. Must be convex.</param>
  336. /// <param name="position">Starting position of the mesh.</param>
  337. /// <param name="rotation">Orientation of the mesh.</param>
  338. /// <param name="unitDir">Unit direction towards which to perform the sweep.</param>
  339. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  340. /// </param>
  341. /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected.
  342. /// </param>
  343. /// <returns>True if something was hit, false otherwise.</returns>
  344. public static bool ConvexCastAny(PhysicsMesh mesh, Vector3 position, Quaternion rotation,
  345. Vector3 unitDir, ulong layer = ulong.MaxValue, float max = float.MaxValue)
  346. {
  347. IntPtr meshPtr = IntPtr.Zero;
  348. if (mesh != null)
  349. meshPtr = mesh.GetCachedPtr();
  350. return Internal_ConvexCastAny(meshPtr, ref position, ref rotation, ref unitDir, layer, max);
  351. }
  352. /// <summary>
  353. /// Returns a list of all colliders in the scene that overlap the provided box.
  354. /// </summary>
  355. /// <param name="box">Box to check for overlap.</param>
  356. /// <param name="rotation">Orientation of the box.</param>
  357. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  358. /// </param>
  359. /// <returns>List of all colliders that overlap the box.</returns>
  360. public static Collider[] BoxOverlap(AABox box, Quaternion rotation, ulong layer = ulong.MaxValue)
  361. {
  362. return ConvertColliders(Internal_BoxOverlap(ref box, ref rotation, layer));
  363. }
  364. /// <summary>
  365. /// Returns a list of all colliders in the scene that overlap the provided sphere.
  366. /// </summary>
  367. /// <param name="sphere">Sphere to check for overlap.</param>
  368. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  369. /// </param>
  370. /// <returns>List of all colliders that overlap the sphere.</returns>
  371. public static Collider[] SphereOverlap(Sphere sphere, ulong layer = ulong.MaxValue)
  372. {
  373. return ConvertColliders(Internal_SphereOverlap(ref sphere, layer));
  374. }
  375. /// <summary>
  376. /// Returns a list of all colliders in the scene that overlap the provided capsule.
  377. /// </summary>
  378. /// <param name="capsule">Capsule to check for overlap.</param>
  379. /// <param name="rotation">Orientation of the capsule.</param>
  380. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  381. /// </param>
  382. /// <returns>List of all colliders that overlap the sphere.</returns>
  383. public static Collider[] CapsuleOverlap(Capsule capsule, Quaternion rotation, ulong layer = ulong.MaxValue)
  384. {
  385. return ConvertColliders(Internal_CapsuleOverlap(ref capsule, ref rotation, layer));
  386. }
  387. /// <summary>
  388. /// Returns a list of all colliders in the scene that overlap the provided convex mesh.
  389. /// </summary>
  390. /// <param name="mesh">Mesh to check for overlap. Must be convex.</param>
  391. /// <param name="position">Position of the mesh.</param>
  392. /// <param name="rotation">Orientation of the mesh.</param>
  393. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  394. /// </param>
  395. /// <returns>List of all colliders that overlap the mesh.</returns>
  396. public static Collider[] ConvexOverlap(PhysicsMesh mesh, Vector3 position, Quaternion rotation,
  397. ulong layer = ulong.MaxValue)
  398. {
  399. IntPtr meshPtr = IntPtr.Zero;
  400. if (mesh != null)
  401. meshPtr = mesh.GetCachedPtr();
  402. return ConvertColliders(Internal_ConvexOverlap(meshPtr, ref position, ref rotation, layer));
  403. }
  404. /// <summary>
  405. /// Checks if the provided box overlaps any other collider in the scene.
  406. /// </summary>
  407. /// <param name="box">Box to check for overlap.</param>
  408. /// <param name="rotation">Orientation of the box.</param>
  409. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  410. /// </param>
  411. /// <returns>True if there is overlap with another object, false otherwise.</returns>
  412. public static bool BoxOverlapAny(AABox box, Quaternion rotation, ulong layer = ulong.MaxValue)
  413. {
  414. return Internal_BoxOverlapAny(ref box, ref rotation, layer);
  415. }
  416. /// <summary>
  417. /// Checks if the provided sphere overlaps any other collider in the scene.
  418. /// </summary>
  419. /// <param name="sphere">Sphere to check for overlap.</param>
  420. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  421. /// </param>
  422. /// <returns>True if there is overlap with another object, false otherwise.</returns>
  423. public static bool SphereOverlapAny(Sphere sphere, ulong layer = ulong.MaxValue)
  424. {
  425. return Internal_SphereOverlapAny(ref sphere, layer);
  426. }
  427. /// <summary>
  428. /// Checks if the provided capsule overlaps any other collider in the scene.
  429. /// </summary>
  430. /// <param name="capsule">Capsule to check for overlap.</param>
  431. /// <param name="rotation">Orientation of the capsule.</param>
  432. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  433. /// </param>
  434. /// <returns>True if there is overlap with another object, false otherwise.</returns>
  435. public static bool CapsuleOverlapAny(Capsule capsule, Quaternion rotation, ulong layer = ulong.MaxValue)
  436. {
  437. return Internal_CapsuleOverlapAny(ref capsule, ref rotation, layer);
  438. }
  439. /// <summary>
  440. /// Checks if the provided convex mesh overlaps any other collider in the scene.
  441. /// </summary>
  442. /// <param name="mesh">Mesh to check for overlap. Must be convex.</param>
  443. /// <param name="position">Position of the mesh.</param>
  444. /// <param name="rotation">Orientation of the mesh.</param>
  445. /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects.
  446. /// </param>
  447. /// <returns>True if there is overlap with another object, false otherwise.</returns>
  448. public static bool ConvexOverlapAny(PhysicsMesh mesh, Vector3 position, Quaternion rotation,
  449. ulong layer = ulong.MaxValue)
  450. {
  451. IntPtr meshPtr = IntPtr.Zero;
  452. if (mesh != null)
  453. meshPtr = mesh.GetCachedPtr();
  454. return Internal_ConvexOverlapAny(meshPtr, ref position, ref rotation, layer);
  455. }
  456. /// <summary>
  457. /// Adds a new physics region. Certain physics options require you to set up regions in which physics objects are
  458. /// allowed to be in, and objects outside of these regions will not be handled by physics.You do not need to set
  459. /// up these regions by default.
  460. /// </summary>
  461. /// <param name="region">Region of space that physics objects are allowed to be in.</param>
  462. /// <returns>A unique handle to the region.</returns>
  463. public static int AddPhysicsRegion(AABox region)
  464. {
  465. return Internal_AddPhysicsRegion(ref region);
  466. }
  467. /// <summary>
  468. /// Removes a physics region.
  469. /// </summary>
  470. /// <param name="handle">Handle of the region returned by <see cref="AddPhysicsRegion"/></param>
  471. public static void RemovePhysicsRegion(int handle)
  472. {
  473. Internal_RemovePhysicsRegion(handle);
  474. }
  475. /// <summary>
  476. /// Removes all physics regions.
  477. /// </summary>
  478. public static void ClearPhysicsRegions()
  479. {
  480. Internal_ClearPhysicsRegions();
  481. }
  482. /// <summary>
  483. /// Enables or disables collision between two layers. Each physics object can be assigned a specific layer, and here
  484. /// you can determine which layers can interact with each other.
  485. /// </summary>
  486. /// <param name="layerA">First collision layer.</param>
  487. /// <param name="layerB">Second collision layer.</param>
  488. /// <param name="enabled">True if the layers are allowed to interact, false otherwise.</param>
  489. public static void ToggleCollision(ulong layerA, ulong layerB, bool enabled)
  490. {
  491. Internal_ToggleCollision(layerA, layerB, enabled);
  492. }
  493. /// <summary>
  494. /// Checks if two collision layers are allowed to interact.
  495. /// </summary>
  496. /// <param name="layerA">First collision layer.</param>
  497. /// <param name="layerB">Second collision layer.</param>
  498. /// <returns>True if the two provided layers are allowed to interact.</returns>
  499. public static bool IsCollisionEnabled(ulong layerA, ulong layerB)
  500. {
  501. return Internal_IsCollisionEnabled(layerA, layerB);
  502. }
  503. /// <summary>
  504. /// Checks is the physics simulation update currently in progress.
  505. /// </summary>
  506. internal static bool IsUpdateInProgress
  507. {
  508. get { return Internal_IsUpdateInProgress(); }
  509. }
  510. /// <summary>
  511. /// Converts a physics query hit info retrieved from native code into managed physics query hit.
  512. /// </summary>
  513. /// <param name="scriptHit">Native physics query hit info.</param>
  514. /// <param name="hit">Managed physics query hit info</param>
  515. private static void ConvertPhysicsQueryHit(ref ScriptPhysicsQueryHit scriptHit, out PhysicsQueryHit hit)
  516. {
  517. if (scriptHit.collider != null)
  518. hit.collider = scriptHit.collider.Component;
  519. else
  520. hit.collider = null;
  521. hit.distance = scriptHit.distance;
  522. hit.normal = scriptHit.normal;
  523. hit.point = scriptHit.point;
  524. hit.triangleIdx = scriptHit.triangleIdx;
  525. hit.uv = scriptHit.uv;
  526. }
  527. /// <summary>
  528. /// Converts all provided physics query hit infos retrieved from native code into managed physics query hits.
  529. /// </summary>
  530. /// <param name="scriptHits">Native physics query hits.</param>
  531. /// <returns>Converted managed physics query hits.</returns>
  532. private static PhysicsQueryHit[] ConvertPhysicsQueryHits(ScriptPhysicsQueryHit[] scriptHits)
  533. {
  534. PhysicsQueryHit[] output = new PhysicsQueryHit[scriptHits.Length];
  535. for (int i = 0; i < scriptHits.Length; i++)
  536. ConvertPhysicsQueryHit(ref scriptHits[i], out output[i]);
  537. return output;
  538. }
  539. /// <summary>
  540. /// Converts an array of native colliders to collider components.
  541. /// </summary>
  542. /// <param name="colliders">Native colliders.</param>
  543. /// <returns>Managed collider components.</returns>
  544. private static Collider[] ConvertColliders(NativeCollider[] colliders)
  545. {
  546. Collider[] output = new Collider[colliders.Length];
  547. for (int i = 0; i < colliders.Length; i++)
  548. output[i] = colliders[i].Component;
  549. return output;
  550. }
  551. [MethodImpl(MethodImplOptions.InternalCall)]
  552. private static extern void Internal_GetGravity(out Vector3 gravity);
  553. [MethodImpl(MethodImplOptions.InternalCall)]
  554. private static extern void Internal_SetGravity(ref Vector3 gravity);
  555. [MethodImpl(MethodImplOptions.InternalCall)]
  556. private static extern int Internal_AddPhysicsRegion(ref AABox region);
  557. [MethodImpl(MethodImplOptions.InternalCall)]
  558. private static extern void Internal_RemovePhysicsRegion(int handle);
  559. [MethodImpl(MethodImplOptions.InternalCall)]
  560. private static extern void Internal_ClearPhysicsRegions();
  561. [MethodImpl(MethodImplOptions.InternalCall)]
  562. private static extern void Internal_ToggleCollision(ulong layerA, ulong layerB, bool enabled);
  563. [MethodImpl(MethodImplOptions.InternalCall)]
  564. private static extern bool Internal_IsCollisionEnabled(ulong layerA, ulong layerB);
  565. [MethodImpl(MethodImplOptions.InternalCall)]
  566. private static extern bool Internal_IsUpdateInProgress();
  567. [MethodImpl(MethodImplOptions.InternalCall)]
  568. private static extern bool Internal_RayCast(ref Vector3 origin, ref Vector3 unitDir, out ScriptPhysicsQueryHit hit, ulong layer, float max);
  569. [MethodImpl(MethodImplOptions.InternalCall)]
  570. private static extern bool Internal_BoxCast(ref AABox box, ref Quaternion rotation, ref Vector3 unitDir, out ScriptPhysicsQueryHit hit, ulong layer, float max);
  571. [MethodImpl(MethodImplOptions.InternalCall)]
  572. private static extern bool Internal_SphereCast(ref Sphere sphere, ref Vector3 unitDir, out ScriptPhysicsQueryHit hit, ulong layer, float max);
  573. [MethodImpl(MethodImplOptions.InternalCall)]
  574. private static extern bool Internal_CapsuleCast(ref Capsule capsule, ref Quaternion rotation, ref Vector3 unitDir, out ScriptPhysicsQueryHit hit, ulong layer, float max);
  575. [MethodImpl(MethodImplOptions.InternalCall)]
  576. private static extern bool Internal_ConvexCast(IntPtr mesh, ref Vector3 position, ref Quaternion rotation, ref Vector3 unitDir, out ScriptPhysicsQueryHit hit, ulong layer, float max);
  577. [MethodImpl(MethodImplOptions.InternalCall)]
  578. private static extern ScriptPhysicsQueryHit[] Internal_RayCastAll(ref Vector3 origin, ref Vector3 unitDir, ulong layer, float max);
  579. [MethodImpl(MethodImplOptions.InternalCall)]
  580. private static extern ScriptPhysicsQueryHit[] Internal_BoxCastAll(ref AABox box, ref Quaternion rotation, ref Vector3 unitDir, ulong layer, float max);
  581. [MethodImpl(MethodImplOptions.InternalCall)]
  582. private static extern ScriptPhysicsQueryHit[] Internal_SphereCastAll(ref Sphere sphere, ref Vector3 unitDir, ulong layer, float max);
  583. [MethodImpl(MethodImplOptions.InternalCall)]
  584. private static extern ScriptPhysicsQueryHit[] Internal_CapsuleCastAll(ref Capsule capsule, ref Quaternion rotation, ref Vector3 unitDir, ulong layer, float max);
  585. [MethodImpl(MethodImplOptions.InternalCall)]
  586. private static extern ScriptPhysicsQueryHit[] Internal_ConvexCastAll(IntPtr mesh, ref Vector3 position, ref Quaternion rotation, ref Vector3 unitDir, ulong layer, float max);
  587. [MethodImpl(MethodImplOptions.InternalCall)]
  588. private static extern bool Internal_RayCastAny(ref Vector3 origin, ref Vector3 unitDir, ulong layer, float max);
  589. [MethodImpl(MethodImplOptions.InternalCall)]
  590. private static extern bool Internal_BoxCastAny(ref AABox box, ref Quaternion rotation, ref Vector3 unitDir, ulong layer, float max);
  591. [MethodImpl(MethodImplOptions.InternalCall)]
  592. private static extern bool Internal_SphereCastAny(ref Sphere sphere, ref Vector3 unitDir, ulong layer, float max);
  593. [MethodImpl(MethodImplOptions.InternalCall)]
  594. private static extern bool Internal_CapsuleCastAny(ref Capsule capsule, ref Quaternion rotation, ref Vector3 unitDir, ulong layer, float max);
  595. [MethodImpl(MethodImplOptions.InternalCall)]
  596. private static extern bool Internal_ConvexCastAny(IntPtr mesh, ref Vector3 position, ref Quaternion rotation, ref Vector3 unitDir, ulong layer, float max);
  597. [MethodImpl(MethodImplOptions.InternalCall)]
  598. private static extern NativeCollider[] Internal_BoxOverlap(ref AABox box, ref Quaternion rotation, ulong layer);
  599. [MethodImpl(MethodImplOptions.InternalCall)]
  600. private static extern NativeCollider[] Internal_SphereOverlap(ref Sphere sphere, ulong layer);
  601. [MethodImpl(MethodImplOptions.InternalCall)]
  602. private static extern NativeCollider[] Internal_CapsuleOverlap(ref Capsule capsule, ref Quaternion rotation, ulong layer);
  603. [MethodImpl(MethodImplOptions.InternalCall)]
  604. private static extern NativeCollider[] Internal_ConvexOverlap(IntPtr mesh, ref Vector3 position, ref Quaternion rotation, ulong layer);
  605. [MethodImpl(MethodImplOptions.InternalCall)]
  606. private static extern bool Internal_BoxOverlapAny(ref AABox box, ref Quaternion rotation, ulong layer);
  607. [MethodImpl(MethodImplOptions.InternalCall)]
  608. private static extern bool Internal_SphereOverlapAny(ref Sphere sphere, ulong layer);
  609. [MethodImpl(MethodImplOptions.InternalCall)]
  610. private static extern bool Internal_CapsuleOverlapAny(ref Capsule capsule, ref Quaternion rotation, ulong layer);
  611. [MethodImpl(MethodImplOptions.InternalCall)]
  612. private static extern bool Internal_ConvexOverlapAny(IntPtr mesh, ref Vector3 position, ref Quaternion rotation, ulong layer);
  613. }
  614. }