Physics.cs 37 KB

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