Physics.cs 38 KB

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