PhysicsCommon.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Runtime.InteropServices;
  4. namespace BansheeEngine
  5. {
  6. /// <summary>
  7. /// Hit information from a physics query.
  8. /// </summary>
  9. public struct PhysicsQueryHit
  10. {
  11. /// <summary>
  12. /// Position of the hit in world space.
  13. /// </summary>
  14. public Vector3 point;
  15. /// <summary>
  16. /// Normal to the surface that was hit.
  17. /// </summary>
  18. public Vector3 normal;
  19. /// <summary>
  20. /// UV coordinates of the triangle that was hit (only applicable when triangle meshes are hit).
  21. /// </summary>
  22. public Vector2 uv;
  23. /// <summary>
  24. /// Distance from the query origin to the hit position.
  25. /// </summary>
  26. public float distance;
  27. /// <summary>
  28. /// Index of the triangle that was hit (only applicable when triangle meshes are hit).
  29. /// </summary>
  30. public int triangleIdx;
  31. /// <summary>
  32. /// Collider that was hit.
  33. /// </summary>
  34. public Collider collider;
  35. }
  36. /// <summary>
  37. /// Information about a single contact point during physics collision.
  38. /// </summary>
  39. [StructLayout(LayoutKind.Sequential)]
  40. public struct ContactPoint // Note: Must match C++ ContactPoint struct
  41. {
  42. /// <summary>
  43. /// Contact point in world space.
  44. /// </summary>
  45. public Vector3 position;
  46. /// <summary>
  47. /// Normal pointing from the second shape to the first shape.
  48. /// </summary>
  49. public Vector3 normal;
  50. /// <summary>
  51. /// Impulse applied to the objects to keep them from penetrating. Divide by simulation step to get the force.
  52. /// </summary>
  53. public float impulse;
  54. /// <summary>
  55. /// Determines how far are the objects. Negative value denotes penetration.
  56. /// </summary>
  57. public float separation;
  58. }
  59. /// <summary>
  60. /// Information about a collision between two physics objects.
  61. /// </summary>
  62. public struct CollisionData
  63. {
  64. internal CollisionData(ScriptCollisionData data)
  65. {
  66. if (data.colliderA != null)
  67. colliderA = data.colliderA.Component;
  68. else
  69. colliderA = null;
  70. if (data.colliderB != null)
  71. colliderB = data.colliderB.Component;
  72. else
  73. colliderB = null;
  74. contactPoints = data.contactPoints;
  75. }
  76. /// <summary>
  77. /// First of the colliders involved in the collision.
  78. /// </summary>
  79. public Collider colliderA;
  80. /// <summary>
  81. /// Second of the colliders involved in the collision.
  82. /// </summary>
  83. public Collider colliderB;
  84. /// <summary>
  85. /// Information about all the contact points for the hit.
  86. /// </summary>
  87. public ContactPoint[] contactPoints;
  88. }
  89. /// <summary>
  90. /// Interop class used for passing PhysicsQueryHit data from native to managed code. <see cref="PhysicsQueryHit"/>.
  91. /// </summary>
  92. [StructLayout(LayoutKind.Sequential)]
  93. internal struct ScriptPhysicsQueryHit // Note: Must match C++ struct ScriptPhysicsQueryHit
  94. {
  95. public Vector3 point;
  96. public Vector3 normal;
  97. public Vector2 uv;
  98. public float distance;
  99. public int triangleIdx;
  100. public NativeCollider collider;
  101. }
  102. /// <summary>
  103. /// Interop class used for passing CollisionData data from native to managed code. <see cref="CollisionData"/>.
  104. /// </summary>
  105. [StructLayout(LayoutKind.Sequential)]
  106. internal struct ScriptCollisionData // Note: Must match C++ CollisionData struct
  107. {
  108. public NativeCollider colliderA;
  109. public NativeCollider colliderB;
  110. public ContactPoint[] contactPoints;
  111. }
  112. /// <summary>
  113. /// Determines which collision events will be reported by physics objects.
  114. /// </summary>
  115. public enum CollisionReportMode
  116. {
  117. /// <summary>
  118. /// No collision events will be triggered.
  119. /// </summary>
  120. None,
  121. /// <summary>
  122. /// Collision events will be triggered when object enters and/or leaves collision.
  123. /// </summary>
  124. Report,
  125. /// <summary>
  126. /// Collision events will be triggered when object enters and/or leaves collision, but also every frame the object
  127. /// remains in collision.
  128. /// </summary>
  129. ReportPersistent,
  130. }
  131. }